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
import Foundation | |
import SwiftyBeaver | |
// direct access to the logger across the app | |
let logger = SwiftyBeaver.self | |
extension SwiftyBeaver { | |
static func setupConsole() { | |
let console = ConsoleDestination() | |
console.levelColor.verbose = "⚪️ " |
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
final class HomeViewController: ViewController { | |
// Perhaps a UICollectionView here | |
// Data structures | |
fileprivate let dataFetcher = HomeDataFetcher() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
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
// MARK: - Reloadable | |
extension HomeViewController: Reloadable { | |
var hasData: Bool { | |
return !self.dataFetcher.data.isEmpty | |
} | |
func reloadData() { | |
self.progressView?.hide() | |
// Simple factory which choose and init a ProgressView subclass according to a type, here "home" |
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
// MARK: - Failable | |
extension HomeViewController: Failable { | |
var errorScreenType: ErrorScreen? { | |
return .home | |
} | |
var errorContext: ErrorContext { | |
return .dataOriented | |
} | |
} |
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 ViewController: UIViewController { | |
var errorView: ErrorView? | |
var progressView: ProgressView? | |
} |
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
// MARK: - ViewController Default implementation | |
extension Failable where Self: ViewController { | |
func showError<T: ErrorDisplayable>(_ error: Swift.Error?, errorView: T.Type, _ reloadable: Reloadable) { | |
self.errorView = errorView.show(fromView: self.view, insets: .zero, animated: true, completion: nil) | |
if var errorViewRetryable = self.errorView as? ErrorRetryable { | |
errorViewRetryable.retry = { reloadable.reloadData() } | |
errorViewRetryable.message = error?.localizedMessage | |
} |
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
// MARK: - UIViewController Default implementation | |
extension FailableCore where Self: UIViewController { | |
func showError(_ error: Swift.Error?) { | |
let errorMessage = error?.localizedMessage ?? L10n.commonErrorMessage | |
SwiftMessages.showAlert(title: errorMessage) | |
} |
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
struct SignIn { | |
enum Error: Swift.Error, Localizable, Underlying { | |
case unknown(underlying: Swift.Error?) | |
case invalidCredentials(underlying: Swift.Error?) | |
case mailEmpty | |
case mailIncorrect | |
case passwordIncorrect | |
case serviceInconsistency |
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 Error { | |
var localizedMessage: String { | |
if let error = self as? SignIn.Error { return error.underlying?.localizedMessage ?? error.localizedMessage } | |
if let error = self as? Network.Error { return error.localizedMessage } | |
if let error = self as? Store.Error { return error.localizedMessage } | |
return L10n.commonErrorMessage // Fallback error message | |
} | |
var underlying: Swift.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
protocol Localizable { | |
var localizedMessage: String { get } | |
} | |
protocol Underlying { | |
var underlying: Swift.Error? { get } | |
} |