该项目处于早期开发阶段!
为视图控制器提供的简单和现成的状态。介绍
是否厌倦了在所有视图中编写相同的代码?现在您可以轻松简单地调用所需的显示状态,WaterStates将完成其余的工作。
内部使用一个状态机
(受MasterWatcher启发),它确定延迟并决定何时显示、隐藏或跳过状态显示。
如果您喜欢此项目,请别忘了给star ⭐
,并在GitHub上关注我。
要求
快速开始
在视图控制器中使用WaterStates
协议,并使用showState
方法调用所需的视图状态。
import UIKit
import WaterStates
class ExampleViewController: UIViewController, WaterStates {
override func viewDidLoad() {
super.viewDidLoad()
showState(.loading)
}
}
对于状态的动作,需要对应于某个特定状态的委托,例如:ErrorStateDelegate
。
extension ExampleViewController: WaterStatesDelegate {
func errorActionTapped(with type: StateActionType) {
// do something
}
}
VIPER 快速开始
您需要在ViewInput
协议中设置showState
方法
import WaterStates
protocol ExampleViewInput: class {
func showState(_ state: DefaultState)
}
在视图控制器中使用WaterStates
协议
import UIKit
import WaterStates
class ExampleViewController: UIViewController, ExampleViewInput, WaterStates { }
在Presenter
中,我们使用showState
方法设置视图状态
import WaterStates
class ExamplePresenter: ExampleViewOutput {
weak var view: ViewControllerInput?
func someMethodd() {
view?.showState(.loading)
}
}
对于状态的动作,ViewOutput
必须与特定的状态委托相匹配,例如:ErrorStateDelegate
protocol ExampleViewOutput: WaterStatesDelegate { }
class ExamplePresenter: ExampleViewOutput {
...
func errorActionTapped(with type: StateActionType) {
// do something
}
}
基本用法
状态
要在视图中设置state
,需要使用带有所需状态的showState
方法调用。
public enum State<T> {
case loading(StateInfo)
case content(T)
case error(StateInfo)
case empty(StateInfo)
}
// state for example: .loading
showState(.loading)
空状态
showState(.empty)
错误状态
showState(.error)
加载状态
showState(.loading)
内容状态
showState(.content(/* your content */))
要使用内容状态,需要实现带有您需要类型的showContent
方法
// Content type must be your view model, for example - String
func showContent(_ content: String) {
// do something with your content
}
如果您不需要内容状态的数据,则无法实现showContent
方法,或者您可以在showContent
方法中指定内容类型,如:DefaultState
- Any
。
func showContent(_ content: Any) {
// do something
}
配置
其他功能将在不久的将来添加
安装
Swift包管理器
要使用Apple的Swift包管理器进行集成,请在Package.swift
中将以下内容添加为依赖项
.package(url: "https://github.com/BarredEwe/WaterStates.git", .upToNextMajor(from: "0.2.0"))
然后指定"WaterStates"
作为你希望使用WaterStates的Target的依赖项。
作者
BarredEwe, [email protected]
许可证
WaterStates
遵循MIT许可证。更多信息请参阅LICENSE文件。