BarredEwe/WaterStates
States for the ViewControllers
Introduction
Are you sure tired of writing the same code in all view controllers? Now you can easily and simply call up the necessary states for display, WaterStates will do the rest.
Inside, a state machine(Inspired by MasterWatcher) is used to that determines the delay and decides when to show, hide, or skip the state display.
If you like the project, do not forget to put star ⭐ and follow me on GitHub.
Requirements
- <img align="left" src="https://developer.apple.com/assets/elements/icons/xcode/xcode-64x64_2x.png" width="26">Xcode
11.0+
- <img align="left" src="https://developer.apple.com/assets/elements/icons/swift/swift-64x64_2x.png" width="26">Swift
5.0+
- <img align="left" src="https://developer.apple.com/assets/elements/icons/app-store/app-store-64x64.png" width="26">Ready for use on
iOS 9+
Quick Start
Use the WaterStates protocol on the view controller and invoke the state you need using the showState method.
import UIKit
import WaterStates
class ExampleViewController: UIViewController, WaterStates {
override func viewDidLoad() {
super.viewDidLoad()
showState(.loading)
}
}For the action of the states, you need to correspond to a delegate of a certain state, for example: ErrorStateDelegate.
extension ExampleViewController: WaterStatesDelegate {
func errorActionTapped(with type: StateActionType) {
// do something
}
}<details><summary>VIPER Quick Start</summary><p>
You need to set the showState method in the ViewInput protocol:
import WaterStates
protocol ExampleViewInput: class {
func showState(_ state: DefaultState)
}Use the WaterStates protocol on the view controller:
import UIKit
import WaterStates
class ExampleViewController: UIViewController, ExampleViewInput, WaterStates { }In the Presenter, we set the view state using the showState method:
import WaterStates
class ExamplePresenter: ExampleViewOutput {
weak var view: ViewControllerInput?
func someMethodd() {
view?.showState(.loading)
}
}For the action of the states, ViewOutput must correspond to a specific state delegate, for example: ErrorStateDelegate:
protocol ExampleViewOutput: WaterStatesDelegate { }
class ExamplePresenter: ExampleViewOutput {
...
func errorActionTapped(with type: StateActionType) {
// do something
}
}</p></details>
Basic Usage
States
To set the state in view, you need to call the showState method with the desired state:
public enum State<T> {
case loading(StateInfo)
case content(T)
case error(StateInfo)
case empty(StateInfo)
}
// state for example: .loading
showState(.loading)<img align="left" src="https://github.com/BarredEwe/WaterStates/blob/master/Resources/Empty.png" width="288"> <br><br><br>
Empty state
showState(.empty)<br><br><br><br><br><br> <img align="left" src="https://github.com/BarredEwe/WaterStates/blob/master/Resources/Error.png" width="288"> <br><br><br>
Error state
showState(.error)<br><br><br><br><br><br> <img align="left" src="https://github.com/BarredEwe/WaterStates/blob/master/Resources/Loading.png" width="288"> <br><br><br>
Loading state
showState(.loading)<br><br><br><br>
Content state
showState(.content(/* your content */))To use the content state, it is necessary to implement showContent method with a type that you need:
// Content type must be your view model, for example - String
func showContent(_ content: String) {
// do something with your content
}If you do not need data for the content state, you cannot implement the showContent method, or you can specify the type of content in the showContent method, as in DefaultState - Any:
func showContent(_ content: Any) {
// do something
}Configarations
The rest will be added in the near future 😉!
Installation
<details><summary>CocoaPods</summary><p>
WaterStates is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'WaterStates'</p> </details>
<details><summary>Swift package manager</summary><p>
To integrate using Apple's Swift package manager, add the following as a dependency to your Package.swift:
.package(url: "https://github.com/BarredEwe/WaterStates.git", .upToNextMajor(from: "0.2.0"))and then specify "WaterStates" as a dependency of the Target in which you wish to use WaterStates.
</p></details>
License
WaterStates is available under the MIT license. See the LICENSE file for more info.
Package Metadata
Repository: BarredEwe/WaterStates
Stars: 5
Forks: 0
Open issues: 1
Default branch: master
Primary language: swift
License: MIT
Topics: swift
README: README.md