GithubHelp home page GithubHelp logo

eneskaraosman / swiftemptystate Goto Github PK

View Code? Open in Web Editor NEW
11.0 3.0 1.0 3.71 MB

Displaying Empty state of a page. (UIKit)

License: MIT License

Ruby 5.87% Swift 94.13%
ios swift cocoapods swiftpackagemanager empty-state

swiftemptystate's Introduction

SwiftEmptyState

CI Status Version License Platform

Demo

Why do I need?

Common usage case;

TLDR;
Image you're getting data from a backend, your request succeded but it is just an empty list. Don't left user with a blank screen, or boring alerts.

Display Empty State - Medium Blog

Usage

For better understanding, please view Example project.

To run the example project, clone the repo, and run pod install from the Example directory first.

But here below the main concept.

Usable in any UITableViewController, UICollectionViewController and UIViewController

class TableViewController: UITableViewController {
    
    lazy var emptyStateManager: EmptyStateManager = {
        
        let esv = EmptyStateView(
            messageText: "This is label belongs to empty state view that sits in UITableViewController's UITableView",
            titleText: "EmptyState Title",
            image: UIImage(named: "empty_state_image"),
            buttonText: "Button",
            centerYOffset: -100 // In case you want to move it to top, by default it is centered (offset = 0)
        )
        esv.buttonAction = { _ in
            esv.messageText = "Button action works ๐Ÿ‘๐Ÿป"
        }
        
        let manager = EmptyStateManager(
            containerView: self.tableView,
            emptyView: esv,
            animationConfiguration: .init(animationType: .spring)
        )
        return manager
    }()
    
    var dataSource = (1...50).map { _ in UIColor.random } {
        didSet {
            self.tableView.reloadData()
            self.emptyStateManager.reloadState()
        }
    }
    
    // dataSource is the dataSource of tableView
}

Same example valid for UICollectionView contentView,
If your content view is subclass of ICollectionView/UITableView
manager knows whether it hasContent, but in case you use pure UIView as contentView
then make sure you set hasContent manually.

manager.hasContent = {
    !self.dataSource.isEmpty
}

Animation

EmptyStateManager has animationConfiguration parameter with default values.

struct AnimationConfiguration {
    let animationType: AnimationType     // .spring, .fromBottom, .fromLeft, .fromTop, .fromRight
    let animationDuration: TimeInterval
    let subItemDelayConstant: Double     // image, titleLabel, messageLabel, button. Except .spring animation
    let springDamping: CGFloat           // .spring animation case
    let initialVelocity: CGFloat
    let options: UIView.AnimationOptions
}

let manager = EmptyStateManager(
    containerView: ***,
    emptyView: ***,
    animationConfiguration: .init(animationType: .spring)
)

Customization

If you want to make your custom EmptyStateView
Just implement IEmptyStateView protocol in your custom UIView class.

Requirements

iOS 10+ Swift 5+

Installation

Pod

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

pod 'SwiftEmptyState'

Package Manager

Add new package from repository link: https://github.com/EnesKaraosman/SwiftEmptyState.git

Author

eneskaraosman, [email protected]

License

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

swiftemptystate's People

Contributors

eneskaraosman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

yuzhiyou1990

swiftemptystate's Issues

button in emptyStateView

action is not performing on button which is declared in emptyStateView, it's also not working in sample project

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.