{"id":32109228,"url":"https://github.com/dgrzeszczak/mvvm","last_synced_at":"2025-10-20T12:59:03.988Z","repository":{"id":64060154,"uuid":"134058563","full_name":"dgrzeszczak/MVVM","owner":"dgrzeszczak","description":"MVVM framework is swift library inspired by ViewModel from Android Architecture components.","archived":false,"fork":false,"pushed_at":"2019-12-25T14:09:42.000Z","size":27,"stargazers_count":13,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-20T12:58:52.559Z","etag":null,"topics":["ios-architecture","ios-swift","model-view-viewmodel","mvvm","mvvm-architecture"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dgrzeszczak.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-19T12:09:54.000Z","updated_at":"2023-10-05T17:44:06.000Z","dependencies_parsed_at":"2023-01-14T20:30:43.422Z","dependency_job_id":null,"html_url":"https://github.com/dgrzeszczak/MVVM","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/dgrzeszczak/MVVM","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgrzeszczak%2FMVVM","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgrzeszczak%2FMVVM/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgrzeszczak%2FMVVM/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgrzeszczak%2FMVVM/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dgrzeszczak","download_url":"https://codeload.github.com/dgrzeszczak/MVVM/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgrzeszczak%2FMVVM/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280094904,"owners_count":26271003,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-20T02:00:06.978Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ios-architecture","ios-swift","model-view-viewmodel","mvvm","mvvm-architecture"],"created_at":"2025-10-20T12:59:02.533Z","updated_at":"2025-10-20T12:59:03.983Z","avatar_url":"https://github.com/dgrzeszczak.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n*MVVM* framework is swift library inspired by *ViewModel* from Android Architecture components. I strongly recommend to look at [Android docs](https://developer.android.com/topic/libraries/architecture/viewmodel).\n\nWorking on *Model-View-ViewModel* architecture you can ask the question where you should instantiate *View Model* and how to pass it to the *View* (ie. ```UIViewController```). With *MVVM* framework you will use factories to create View Models. You can prepare your custom parametrized factory or use the default one ```InitializableViewModelFactory``` which works on ```Initializable``` ViewModels. \n```swift\nclass MyViewModel: Initializable { \n    required init() { }\n}\n```\nIn your ViewController you can instantiate it by calling\n```swift\nclass MyViewController: UIViewController {\n    override func viewDidLoad() {\n        super.viewDidLoad() \n        let vm: MyViewModel? = ViewModelProviders.provider(for: self).get()\n    }\n}\n```\n\n## Lifecycle\n\n*ViewModel* is created for specific contexts (*ViewControllers*) and live as long as the context (when context is deallocated, *ViewModel* will be  deallocated automatically). \n\n*View Model* is created by the factory when you call ```provider.get()``` method first time. You have to keep in mind all *View Models* are \"cached\" for specific context. It means that when you get the same type of View Model for the same context again it will return cached View Model, not a new one (eg. it can be useful for sharing data between views). \n\nFrom time to time you may need to re-create *Viewm Model(s)* for the context. You can clear the context calling  ```.clear()``` method from ```ViewModelStore``` but you have to know that all *View Models* will be removed for that context.\n\n## Custom factories\n\nWhen you need custom factory (eg. parameterized init for *ViewModel*) you can prepare it by implementing ```ViewModelFactory``` protocol.\n```swift\n\nstruct MyViewModel { }\n\nstruct MySecondViewModel { }\n\nstruct ParamViewModel {\n    let intParam: Int\n}\n\nstruct MyViewModelFactory: ViewModelFactory {\n    func create\u003cVM\u003e() -\u003e VM? {\n        switch VM.self {\n        case is MyViewModel.Type: return MyViewModel() as? VM\n        case is MySecondViewModel.Type: return MySecondViewModel() as? VM\n        case is ParamViewModel.Type: return ParamViewModel(intParam: 3) as? VM\n        default: return nil\n        }\n    }\n}\n```\n\nand then you can use it like that:\n```swift\nvar provider = ViewModelProviders.provider(for: controller, with: MyViewModelFactory())\nguard let viewModel: MyViewModel = provider.get() else { return }\n```\n\nAnother way to make custom factory is to use helpers classes that use closures to create *View Model* ```SingleViewModelFactory``` or ```CompositeViewModelFactory``` eg:\n\n```swift\nlet singleFactory = SingleViewModelFactory { ParamViewModel(intParam: 3) }\n\nvar compositeFactory = CompositeViewModelFactory()\ncompositeFactory.add { MyViewModel() }\ncompositeFactory.add { MySecondViewModel() }\ncompositeFactory.add { ParamViewModel(intParam: 3) }\n```\n\n## More \"complicated\" factory example with RX ;)\n```swift\nfinal class FindViewModelFactory: ViewModelFactory {\n\n    private let disposeBag = DisposeBag()\n    private var factory = CompositeViewModelFactory()\n\n    private lazy var findViewModel: FindViewModel = {\n        return FindViewModel()\n    }()\n\n    init() {\n        factory.add { [unowned self] in self.findViewModel }\n        factory.add { [unowned self] in self.createSearchGlobalViewModel() }\n        factory.add { BrandListViewModel(fetcher: ProductsWireframe.brandsFetcher) }\n        factory.add { ProductCategoryListViewModel() }\n        factory.add { ServiceCategoryListViewModel() }\n\n    }\n\n    private func createSearchGlobalViewModel() -\u003e SearchGlobalViewModel {\n        let searchGlobalViewModel = SearchGlobalViewModel()\n        findViewModel.searchPredicate.asObservable().bind(to: searchGlobalViewModel.searchPredicate).disposed(by: disposeBag)\n        return searchGlobalViewModel\n    }\n\n    func create\u003cVM\u003e() -\u003e VM? {\n        return factory.create()\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdgrzeszczak%2Fmvvm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdgrzeszczak%2Fmvvm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdgrzeszczak%2Fmvvm/lists"}