Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/heroesofcode/viewstate
ViewState is a library written in Swift for iOS, tvOS & macOS. It returns the results for each state
https://github.com/heroesofcode/viewstate
ios macos swift swift-package-manager tvos viewstate
Last synced: 1 day ago
JSON representation
ViewState is a library written in Swift for iOS, tvOS & macOS. It returns the results for each state
- Host: GitHub
- URL: https://github.com/heroesofcode/viewstate
- Owner: heroesofcode
- License: mit
- Created: 2020-05-21T04:14:29.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-05-21T02:58:53.000Z (6 months ago)
- Last Synced: 2024-05-21T03:53:25.256Z (6 months ago)
- Topics: ios, macos, swift, swift-package-manager, tvos, viewstate
- Language: Swift
- Homepage:
- Size: 672 KB
- Stars: 3
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT
Awesome Lists containing this project
README
[![CI](https://github.com/heroesofcode/ViewState/actions/workflows/CI.yml/badge.svg)](https://github.com/heroesofcode/ViewState/actions/workflows/CI.yml)
[![SPM compatible](https://img.shields.io/badge/SPM-compatible-brightgreen)](https://swift.org/package-manager/)
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fheroesofcode%2FViewState%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/heroesofcode/ViewState)
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fheroesofcode%2FViewState%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/heroesofcode/ViewState)
[![License](https://img.shields.io/github/license/joaolfp/ViewState.svg)](https://github.com/joaolfp/ViewState/blob/master/LICENSE)## Overview
A View State library to return the results for each state
## Usage
In ViewModel calls the states that will return to ViewController
```swift
import ViewStatefinal class ViewModel {
private var viewState = ViewState()
private let service = Service()
func fetchData() -> ViewState {
viewState.fetchSource {
self.service.getData { [weak self] result in
switch result {
case .success(let response):
self?.viewState.success(data: response)
case .failure(let error):
self?.viewState.error(error: error)
}
}
}return viewState
}
}
```
In the ViewController it calls the ViewModel method and places the states of each one.``` swift
import UIKit
import ViewStatefinal class ViewController: UIViewController {
private let viewModel = ViewModel()
override func viewDidLoad() {
super.viewDidLoad()loadData()
}
private func loadData() {
viewModel.fetchData()
.loadingObserver(onLoading)
.successObserver(onSuccess)
.errorObserver(onFailure)
}
private func onLoading() {
// Event loading
}
private func onSuccess(response: Model) {
// Event success
}
private func onFailure(error: APIError) {
// Event error
}
}
```loadingObserver is optional, you can just use success and error
``` swift
private func loadData() {
viewModel.fetchData()
.successObserver(onSuccess)
.errorObserver(onFailure)
}
```See a demo below. You can see this demo in our [example](https://github.com/heroesofcode/ViewState/tree/master/Example) :smiley:.
## Installation
### [Swift Package Manager (SPM)](https://swift.org/package-manager)
```swift
import PackageDescription
let package = Package(
name: "",
dependencies: [
.package(url: "https://github.com/heroesofcode/ViewState", exact: "2.0.1")
],
targets: [
.target(
name: "",
dependencies: ["ViewState"]),
]
)
```## Contributing
To 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.
## License
ViewState is released under the MIT license. See [LICENSE](https://github.com/heroesofcode/ViewState/blob/master/LICENSE) for details.