为视图控制器提供简单且现成的状态。
项目尚处于开发的早期阶段!


Platform License CodeFactor CocoaPods
Swift5 Swift Package Manager


简介

您是否厌倦了在所有视图控制器中编写相同的代码?现在您可以轻松简单地调用所需的显示状态,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
    }
}
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
    }
}


基本用法

状态

要在视图中设置状态,您需要使用所需的状态调用 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
}

配置

其余内容将在不久的将来添加😉!


安装

CocoaPods

WaterStates 可通过 CocoaPods 获得。要安装它,只需将以下行添加到您的 Podfile 中

pod 'WaterStates'

Swift 包管理器

要使用 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 文件。