Giter Club home page Giter Club logo

viewstates's Introduction

ViewStates

CI Status Version License Platform

ViewStates makes it easier to create a view with loading, success, no data and error states. It also has an action button, so that we can do some action such as navigate to other view, or retry an async task. The UI can be customized easily.

Preview

Requirements

  • iOS 9.0+
  • Xcode 9.0+
  • Swift 4.0+

Installation

Cocoapods

ViewStates is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'ViewStates'

Manual

You just need to copy 2 files ViewState.swift and UIImage+Gif.swift in the ViewStates/Classes/ directory to your project. For the UIImage+Gif.swift, I took it from this repo: https://github.com/swiftgif/SwiftGif . The purpose of this library is to animate a GIF image for the loading state. I would like to give a thank to the authors.

How to use

Basic usage

  • Init the ViewState, and set the view you want to display the ViewState as the parentView of the ViewState
class ViewController: UIViewController {
    let viewState = ViewState()

    override func viewDidLoad() {
        super.viewDidLoad()
        viewState.parentView = self.view
    }
}
  • Display the loading state:
viewState.showLoadingState(loadingMessage: "Loading...")
  • When an async task complete successfully, we hide the ViewState:
viewState.hideViewState()
  • When an async task complete with an error, we can show the error state:
self.viewState.showErrorState("Oops! Something went wrong...")
  • When there is nothing to display after geting from async task, we can display the NoData state:
self.viewState.showNoDataState("There is nothing to display")

Advance

  • Display NoData state with an image, and an action button:
self.viewState.showNoDataState("There is nothing to display", noDataImage: UIImage(named: "no_data",
actionButtonTitle: "CREATE ONE", actionHandler: {
    // The code to open the view to create one
}
  • Dispay error state with an image and a retry button:
self.viewState.showErrorState("Oops! Something went wrong...", errorImage: UIImage(named: "error", 
actionButtonTitle: "RETRY", actionHandler: {
    self.loadData()
})

Styling

You can set the custom theme for the ViewState once for all views in the app. You can put it in AppDelegate or somewhere you want.

let theme = ViewStateTheming()
theme.messageTextColor = .red
theme.actionButtonBackgroundColor = .green
theme.actionButtonTitleColor = .white
....
ViewState.useCustomeTheme(theme)

These are all the properties that we can change:

class ViewStateTheming {
    // Background color of the ViewState view
    var backgroundColor = UIColor.white

    // The message label style
    var messageTextColor = UIColor.darkText
    var messageTextAlignment = NSTextAlignment.center
    var messageTextFont = UIFont.systemFont(ofSize: 15)

    // The action button style
    var actionButtonBackgroundColor = UIColor.white
    var actionButtonTitleColor = UIColor.blue
    var actionButtonFont = UIFont.boldSystemFont(ofSize: 15)
    var actionButtonInset = UIEdgeInsets(top: 5, left: 20, bottom: 5, right: 20)
    var actionButtonCornerRadius: CGFloat = 5

    // If there is no GIF image, the iOS UIActivityIndicatorView will be used
    var defaultLoadingSpinnerColor = UIColor.gray

    // Default image and loading text. This can be set when changing state for a special view
    var defaultLoadingMessage = ""
    var defaultLoadingImageName: String?
    var defaultErrorImage: UIImage?
    var defaultNoDataImage: UIImage?
}

Author

Tanh Pham ([email protected])

License

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

viewstates's People

Contributors

thanhtanh avatar

Stargazers

Tanh Pham avatar Phạm Quân avatar Ashwin Vavaliya avatar Dzhunet Hasan avatar Carabineiro avatar Adrian Tineo avatar MohsinAli avatar Bùi Đức Hoàn avatar Nam Nguyen avatar  avatar Bryan Reymonenq avatar  avatar Tri Tran avatar Dong Nguyen avatar

Watchers

James Cloos avatar MohsinAli avatar Carabineiro avatar Dzhunet Hasan avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.