WaterStates 0.4.1

WaterStates 0.4.1

BarredEwe维护。



  • BarredEwe

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


Platform License CodeFactor CocoaPods
Swift5 Swift Package Manager


介绍

是否厌倦了在所有视图中编写相同的代码?现在您可以轻松简单地调用所需的显示状态,WaterStates将完成其余的工作。

内部使用一个状态机(受MasterWatcher启发),它确定延迟并决定何时显示、隐藏或跳过状态显示。

如果您喜欢此项目,请别忘了给star ⭐,并在GitHub上关注我。


要求

  • Xcode 11.0+

  • Swift 5.0+

  • 适用于iOS 9及以上系统使用


快速开始

在视图控制器中使用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
}

配置

其他功能将在不久的将来添加😉!


安装

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"作为你希望使用WaterStates的Target的依赖项。


作者

BarredEwe, [email protected]


许可证

WaterStates遵循MIT许可证。更多信息请参阅LICENSE文件。