Skip to content

Instantly share code, notes, and snippets.

@abury
Last active December 7, 2016 22:22
Show Gist options
  • Save abury/83a77f7de4a882e56b5ccc553bd8dc7b to your computer and use it in GitHub Desktop.
Save abury/83a77f7de4a882e56b5ccc553bd8dc7b to your computer and use it in GitHub Desktop.
An abstract wrapper class for DZNEmptyDataSet that makes showing simple empty states easy. Untested in Swift 3 yet
//
// AbstractEmptyDataSource.swift
// ABSocialMediaManager
//
// Created by Aron Bury on 14/09/2015.
// Copyright © 2015 Aron Bury. All rights reserved.
//
import Foundation
import DZNEmptyDataSet
enum EmptyDataSourceState {
case ReadyToSearch
case NoResults
case NetworkError
}
protocol EmptyDataSourceResponder: class {
func dataSourceShouldRefresh()
}
class AbstractEmptyDataSetSource: NSObject, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
let dataView: UIScrollView
private(set) var state: EmptyDataSourceState
weak var responder: EmptyDataSourceResponder?
init(dataView: UIScrollView) {
self.dataView = dataView
state = EmptyDataSourceState.ReadyToSearch
super.init()
dataView.emptyDataSetSource = self
dataView.emptyDataSetDelegate = self
}
convenience init(dataView: UIScrollView, responder: EmptyDataSourceResponder?, state: EmptyDataSourceState) {
self.init(dataView: dataView)
self.responder = responder
self.state = state
}
func updateState(state: EmptyDataSourceState) {
self.state = state
dataView.reloadEmptyDataSet()
}
//MARK: Empty DataSet Source
func titleForEmptyDataSet(scrollView: UIScrollView!) -> NSAttributedString! {
return NSAttributedString(string: "Find your clients")
}
func descriptionForEmptyDataSet(scrollView: UIScrollView!) -> NSAttributedString! {
return NSAttributedString(string: "Enter a search term to search for Instagram Users")
}
func imageForEmptyDataSet(scrollView: UIScrollView!) -> UIImage! {
return nil
}
func buttonTitleForEmptyDataSet(scrollView: UIScrollView!, forState state: UIControlState) -> NSAttributedString! {
switch self.state {
case .NetworkError:
return NSAttributedString(string: "Try Again")
default:
return nil
}
}
//MARK: Empty DataSet Delegate
func emptyDataSetDidTapButton(scrollView: UIScrollView!) {
if let _responder = responder {
_responder.dataSourceShouldRefresh()
}
}
//MARK: Subclass overides
internal func iconSize() -> CGSize {
let screenHeight = UIScreen.mainScreen().bounds.height
var iconSize: CGFloat = 80
if screenHeight > 480 {
iconSize = 150
}
return CGSizeMake(iconSize, iconSize)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment