Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A small refactoring #50

Merged
merged 8 commits into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions Sources/Errors/UIAlertController+Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,41 @@ import UIKit

public extension UIAlertController {
convenience init(error: Error, preferredStyle: UIAlertController.Style = .alert) {
switch error {
case let error as ResolvableError:
self.init(title: error.errorDescription ?? NSLocalizedString("Error", comment: "Error"), message: error.failureReason ?? error.localizedDescription, preferredStyle: preferredStyle)
self.init(title: UIAlertController.alertTitle(error: error),
message: UIAlertController.alertMessage(error: error),
preferredStyle: preferredStyle)
if let error = error as? ResolvableError {
error.actions.map { $0.alertAction() }.forEach(self.addAction)
if error.actions.isEmpty {
self.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "OK"), style: .default))
if !error.actions.isEmpty {
return
}
}
self.addAction(UIAlertAction(title: UIAlertController.okButtonText, style: .default))
}

private static func alertTitle(error: Error) -> String {
switch error {
case let error as LocalizedError:
self.init(title: error.errorDescription ?? NSLocalizedString("Error", comment: "Error"), message: error.failureReason ?? error.localizedDescription, preferredStyle: preferredStyle)
self.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "OK"), style: .default))
return error.errorDescription ?? defaultErrorTitle
default:
self.init(title: NSLocalizedString("Error", comment: "Error"), message: error.localizedDescription, preferredStyle: preferredStyle)
self.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "OK"), style: .default))
return defaultErrorTitle
}
}

private static func alertMessage(error: Error) -> String {
switch error {
case let error as LocalizedError:
return error.failureReason ?? error.localizedDescription
default:
return error.localizedDescription
}
}

private static var defaultErrorTitle: String {
return NSLocalizedString("Error", comment: "Error")
}

private static var okButtonText: String {
return NSLocalizedString("OK", comment: "OK")
}
}
45 changes: 31 additions & 14 deletions Sources/Extensions/UIViewController+Deselection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,43 @@ public extension UIViewController {
let selectedIndexPaths = deselectable?.indexPathsForSelectedItems ?? []

if let coordinator = transitionCoordinator {
let success = coordinator.animate(alongsideTransition: { context in
for indexPath in selectedIndexPaths {
deselectable?.deselectItem(at: indexPath, animated: context.isAnimated)
}
}, completion: { context in
let success = coordinator.animate(alongsideTransition: { [weak self] context in
self?.switchSelectedItemsState(on: deselectable,
selectedIndexPaths: selectedIndexPaths,
shouldBeSelected: false,
animated: context.isAnimated)
}, completion: { [weak self] context in
if context.isCancelled {
for indexPath in selectedIndexPaths {
deselectable?.selectItem(at: indexPath, animated: false)
}
self?.switchSelectedItemsState(on: deselectable,
selectedIndexPaths: selectedIndexPaths,
shouldBeSelected: true,
animated: false)
}
})
if !success {
for indexPath in selectedIndexPaths {
deselectable?.deselectItem(at: indexPath, animated: false)
}
switchSelectedItemsState(on: deselectable,
selectedIndexPaths: selectedIndexPaths,
shouldBeSelected: false,
animated: false)
}
} else {
for indexPath in selectedIndexPaths {
deselectable?.deselectItem(at: indexPath, animated: false)
}
switchSelectedItemsState(on: deselectable,
selectedIndexPaths: selectedIndexPaths,
shouldBeSelected: false,
animated: false)
}
}

private func switchSelectedItemsState(on deselectable: Deselectable?,
selectedIndexPaths: [IndexPath],
shouldBeSelected: Bool,
animated: Bool) {
guard let deselectable = deselectable else {
return
}
selectedIndexPaths.forEach {
let switchSelectionFunc = shouldBeSelected ? deselectable.selectItem : deselectable.deselectItem
switchSelectionFunc($0, animated)
}
}
}
10 changes: 3 additions & 7 deletions Sources/Protocols/Nibable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,9 @@ public extension Nibable where Self: UIView {

init(owner: AnyObject? = nil) {
let views = Self.nib.instantiate(withOwner: owner, options: [:])
for view in views {
if let view = view as? Self {
self = view
return
}
}
fatalError("Nib for class \(Self.nibName) cound not be loaded!")
self = views.lazy.compactMap { $0 as? Self }.first ?? { () -> Self in
mkj-is marked this conversation as resolved.
Show resolved Hide resolved
fatalError("Nib for class \(Self.nibName) cound not be loaded!")
}()
}

}