Skip to content

Instantly share code, notes, and snippets.

View bill350's full-sized avatar

Jean-Charles SORIN bill350

View GitHub Profile
// MARK: Form Validation
@discardableResult
func isValid() -> (Bool, String?) {
var isValid = true
for item in self.formItems {
item.checkValidity()
if !item.isValid {
isValid = false
}
@bill350
bill350 / HomeViewController+Reloadable.swift
Last active March 13, 2018 15:25
HomeViewController+Reloadable.swift
// MARK: - Reloadable
extension HomeViewController: Reloadable {
var hasData: Bool {
//return if you have data to be currently displayed (from Realm or memory for instance) or not
}
func reloadData() {
// Display progress
// Network call
@bill350
bill350 / Reloadable.swift
Created March 13, 2018 15:26
Reloadable.swift
import UIKit
protocol Reloadable {
func reloadData()
var hasData: Bool { get }
}
extension Reloadable where Self: UIViewController {
/// Errors prompting protocol
protocol Failable {
/// Show an error
func showError(_ error: Swift.Error?)
}
/// Errors prompting protocol at its core
protocol FailableCore {
/// Show an error
func showError(_ error: Swift.Error?)
}
/// Failable protocol to display error
protocol Failable: FailableCore {
var errorScreenType: ErrorScreen? {get}
/// Specify the error display context when hasData is true : use the error message, or use data oriented priority
///
/// - dataOriented: For this type, the error message will show that data inconsistency is possible
/// - errorOriented: The error message always use the Error type localized message if available, default message if needed
enum ErrorContext {
case dataOriented
case errorOriented
}
// Type of screen and its error view to display
enum ErrorScreen {
case home
case details
var errorView: ErrorView.Type? {
switch self {
default:
return ErrorViewRetry.self
}
protocol ErrorDisplayable {
static func show<T: ErrorView>(
fromViewController viewController: UIViewController,
animated: Bool,
completion: ((Bool) -> Swift.Void)?) -> T
static func show<T: ErrorView>(
fromView view: UIView,
insets: UIEdgeInsets, animated: Bool,
completion: ((Bool) -> Swift.Void)?) -> T
protocol ErrorViewDelegate: class {
func errorView(_ errorView: ErrorView, didRetry sender: UIButton)
}
protocol ErrorAnimatable {
func playAnimation()
func stopAnimation()
}