您是否厌倦了在所有视图控制器中编写相同的代码?现在您可以轻松简单地调用所需的显示状态,WaterStates 将完成剩下的工作。
内部使用了一个状态机
(灵感来自 MasterWatcher),它确定延迟并决定何时显示、隐藏或跳过状态显示。
如果您喜欢这个项目,请不要忘记点亮星星 ⭐
并在 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
}
}
您需要在 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
}
}
要在视图中设置状态
,您需要使用所需的状态调用 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
}
其余内容将在不久的将来添加😉!
要使用 Apple 的 Swift 包管理器进行集成,请将以下内容作为依赖项添加到您的 Package.swift
中
.package(url: "https://github.com/BarredEwe/WaterStates.git", .upToNextMajor(from: "0.2.0"))
然后,在您希望使用 WaterStates 的 Target 中指定 "WaterStates"
作为依赖项。
BarredEwe, barredewe@gmail.com
WaterStates
在 MIT 许可证下可用。有关更多信息,请参见 LICENSE 文件。