{"id":18308511,"url":"https://github.com/heroesofcode/viewstate","last_synced_at":"2025-04-05T17:32:13.871Z","repository":{"id":41959420,"uuid":"265752460","full_name":"heroesofcode/ViewState","owner":"heroesofcode","description":"ViewState is a library written in Swift for iOS, tvOS \u0026 macOS. It returns the results for each state","archived":false,"fork":false,"pushed_at":"2024-05-21T02:58:53.000Z","size":688,"stargazers_count":3,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-05-21T03:53:25.256Z","etag":null,"topics":["ios","macos","swift","swift-package-manager","tvos","viewstate"],"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/heroesofcode.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-21T04:14:29.000Z","updated_at":"2024-05-30T06:53:52.881Z","dependencies_parsed_at":"2023-12-18T04:20:51.026Z","dependency_job_id":"5010d06a-6bdb-4556-8bfc-d9387330feb4","html_url":"https://github.com/heroesofcode/ViewState","commit_stats":{"total_commits":103,"total_committers":4,"mean_commits":25.75,"dds":0.2912621359223301,"last_synced_commit":"59bd6bd4298f15e1f4bc64a191138923013ea22e"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heroesofcode%2FViewState","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heroesofcode%2FViewState/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heroesofcode%2FViewState/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heroesofcode%2FViewState/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/heroesofcode","download_url":"https://codeload.github.com/heroesofcode/ViewState/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247375035,"owners_count":20928937,"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","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","macos","swift","swift-package-manager","tvos","viewstate"],"created_at":"2024-11-05T16:08:16.108Z","updated_at":"2025-04-05T17:32:11.633Z","avatar_url":"https://github.com/heroesofcode.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://github.com/heroesofcode/ViewState/blob/master/assets/logo.png?raw=true\"\u003e\n\u003c/p\u003e\n\n[![CI](https://github.com/heroesofcode/ViewState/actions/workflows/CI.yml/badge.svg)](https://github.com/heroesofcode/ViewState/actions/workflows/CI.yml)\n[![SPM compatible](https://img.shields.io/badge/SPM-compatible-brightgreen)](https://swift.org/package-manager/)\n[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fheroesofcode%2FViewState%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/heroesofcode/ViewState)\n[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fheroesofcode%2FViewState%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/heroesofcode/ViewState)\n[![License](https://img.shields.io/github/license/joaolfp/ViewState.svg)](https://github.com/joaolfp/ViewState/blob/master/LICENSE)\n\n## Overview\n\nA View State library to return the results for each state\n\n## Usage\n\nIn ViewModel calls the states that will return to ViewController\n\n```swift\nimport ViewState\n\nfinal class ViewModel {\n    \n    private var viewState = ViewState\u003cModel, APIError\u003e()\n    private let service = Service()\n    \n    func fetchData() -\u003e ViewState\u003cModel, APIError\u003e {\n        viewState.fetchSource {\n            self.service.getData { [weak self] result in\n                switch result {\n                case .success(let response):\n                    self?.viewState.success(data: response)\n                case .failure(let error):\n                    self?.viewState.error(error: error)\n               }\n           }\n        }\n\n        return viewState\n    }\n}\n```\nIn the ViewController it calls the ViewModel method and places the states of each one.\n\n``` swift\nimport UIKit\nimport ViewState\n\nfinal class ViewController: UIViewController {\n\n    private let viewModel = ViewModel()\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n\n        loadData()\n    }\n    \n    private func loadData() {\n        viewModel.fetchData()\n            .loadingObserver(onLoading)\n            .successObserver(onSuccess)\n            .errorObserver(onFailure)\n    }\n    \n    private func onLoading() {\n        // Event loading\n    }\n    \n    private func onSuccess(response: Model) {\n        // Event success\n    }\n    \n    private func onFailure(error: APIError) {\n        // Event error\n    }\n}\n```\n\nloadingObserver is optional, you can just use success and error\n\n``` swift\nprivate func loadData() {\n     viewModel.fetchData()\n         .successObserver(onSuccess)\n         .errorObserver(onFailure)\n}\n```\n\nSee a demo below. You can see this demo in our [example](https://github.com/heroesofcode/ViewState/tree/master/Example) :smiley:.\u003cbr\u003e\n\u003cimg src=\"https://github.com/heroesofcode/ViewState/blob/master/assets/ImageExample.gif?raw=true\" width=\"310\" height=\"640\" /\u003e\n\n## Installation\n\n### [Swift Package Manager (SPM)](https://swift.org/package-manager)\n\n```swift\nimport PackageDescription\nlet package = Package(\n    name: \"\u003cYour Product Name\u003e\",\n    dependencies: [\n       .package(url: \"https://github.com/heroesofcode/ViewState\", exact: \"2.0.1\")\n    ],\n    targets: [\n        .target(\n            name: \"\u003cYour Target Name\u003e\",\n            dependencies: [\"ViewState\"]),\n    ]\n)\n```\n\n## Contributing\n\nTo contribute, just fork this project and then open a pull request, feel free to contribute, bring ideas and raise any problem in the issue tab.\n\n## License\n\nViewState is released under the MIT license. See [LICENSE](https://github.com/heroesofcode/ViewState/blob/master/LICENSE) for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fheroesofcode%2Fviewstate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fheroesofcode%2Fviewstate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fheroesofcode%2Fviewstate/lists"}