This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol ViewStateProtocol: class { | |
var stateManager: StateManager? { get } | |
var loadingView: UIView? { get } | |
var errorView: UIView? { get } | |
var emptyView: UIView? { get } | |
var errorMessage: String? { get set } | |
func addView(withState state: StatesType) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension ViewStateProtocol where Self: UIViewController { | |
// State manager class to remove/add views | |
var stateManager: StateManager? { | |
return StateManager.sharedInstance | |
} | |
// Loading view | |
var loadingView: UIView? { | |
return LoadingView(frame: UIScreen.main.bounds) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension ViewStateProtocol where Self: UIViewController { | |
.......... | |
// Manages and adds different views on the basis of the state | |
func addView(withState state: StatesType) { | |
// error state, empty state & loading state | |
switch state { | |
case .loading: | |
// calls state manager to add a laoding view | |
stateManager?.addView(loadingView!, forState: StatesType.loading.rawValue, superview: view) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum StatesType: String { | |
case error = "error" | |
case empty = "empty" | |
case loading = "loading" | |
case none = "none" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StateManager { | |
static let sharedInstance = StateManager() | |
var viewStore: [String: UIView] = [:] | |
// Associates a view for the given state | |
public func addView(_ view: UIView, forState state: String, superview: UIView) { | |
viewStore[state] = view | |
superview.addSubview(view) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StateViewController: UIViewController {} | |
extension StateViewController: ViewStateProtocol { | |
@objc func handleTap(_ sender: UIView) { | |
// for showing the loader | |
addView(withState: .loading) | |
// for showing the error message | |
addView(withState: .error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
precedencegroup UnionOperatorPrecedence { | |
associativity: left | |
} | |
infix operator ∪: UnionOperatorPrecedence | |
func ∪ <T:Equatable>(lhs: [T], rhs:[T])->[T] { | |
var result = lhs | |
for element in rhs { | |
if !lhs.contains(element) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
precedencegroup IntersectionOperatorPrecedence { | |
associativity: left | |
} | |
infix operator ∩: IntersectionOperatorPrecedence | |
func ∩<T: Equatable> (left: [T], right: [T]) -> [T] { | |
var intersection: [T] = [] | |
for value in left { | |
if right.contains(value) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
precedencegroup UnionOperatorPrecedence { | |
associativity: left | |
higherThan: IntersectionOperatorPrecedence | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Protocol | |
protocol Fruit { | |
func set(price: String?) | |
func clone() -> Fruit | |
} | |
// Class Apple which implements the protocol | |
class Apple: Fruit { | |
var count: Int |
OlderNewer