An open API service indexing awesome lists of open source software.

https://github.com/barredewe/waterstates

States for the ViewControllers
https://github.com/barredewe/waterstates

swift

Last synced: 27 days ago
JSON representation

States for the ViewControllers

Awesome Lists containing this project

README

        




Simple and ready-made states for the view controller.


The project is at an early stage of development!




Platform
License
CodeFactor
CocoaPods

Swift5

Swift Package Manager

---

## 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](https://github.com/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

- Xcode `11.0`+

- Swift `5.0`+

- 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.

```swift
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`.

```swift
extension ExampleViewController: WaterStatesDelegate {
func errorActionTapped(with type: StateActionType) {
// do something
}
}
```

VIPER Quick Start

You need to set the `showState` method in the `ViewInput` protocol:

```swift
import WaterStates

protocol ExampleViewInput: class {
func showState(_ state: DefaultState)
}
```

Use the `WaterStates` protocol on the view controller:

```swift
import UIKit
import WaterStates

class ExampleViewController: UIViewController, ExampleViewInput, WaterStates { }
```

In the `Presenter`, we set the view state using the `showState` method:

```swift
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`:

```swift
protocol ExampleViewOutput: WaterStatesDelegate { }

class ExamplePresenter: ExampleViewOutput {

...

func errorActionTapped(with type: StateActionType) {
// do something
}
}
```

---

## Basic Usage

### States

To set the `state` in view, you need to call the `showState` method with the desired state:

```swift
public enum State {
case loading(StateInfo)
case content(T)
case error(StateInfo)
case empty(StateInfo)
}

// state for example: .loading
showState(.loading)
```





**Empty state**

```swift
showState(.empty)
```











**Error state**

```swift
showState(.error)
```












**Loading state**

```swift
showState(.loading)
```





**Content state**

```swift
showState(.content(/* your content */))
```

To use the content state, it is necessary to implement `showContent` method with a type that you need:

```swift
// 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`:

```swift
func showContent(_ content: Any) {
// do something
}
```

### Configarations

The rest will be added in the near future 😉!

---

## Installation

CocoaPods

WaterStates is available through [CocoaPods](https://cocoapods.org). To install
it, simply add the following line to your Podfile:

```ruby
pod 'WaterStates'
```

Swift package manager



To integrate using Apple's Swift package manager, add the following as a dependency to your `Package.swift`:

```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.

---

## Author

BarredEwe, [email protected]

---

## License

`WaterStates` is available under the MIT license. See the LICENSE file for more info.