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 5 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
31 changes: 22 additions & 9 deletions Sources/Errors/UIAlertController+Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,32 @@ 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)
let (title, message) = UIAlertController.alertTitleMessage(error: error)
self.init(title: title, message: message, 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 alertTitleMessage(error: Error) -> (String, String) {
RomanPodymov marked this conversation as resolved.
Show resolved Hide resolved
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 ?? defaultErrorText,
error.failureReason ?? error.localizedDescription)
default:
self.init(title: NSLocalizedString("Error", comment: "Error"), message: error.localizedDescription, preferredStyle: preferredStyle)
self.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "OK"), style: .default))
return (defaultErrorText, error.localizedDescription)
}
}

private static var defaultErrorText: String {
RomanPodymov marked this conversation as resolved.
Show resolved Hide resolved
return NSLocalizedString("Error", comment: "Error")
}

private static var okButtonText: String {
return NSLocalizedString("OK", comment: "OK")
}
}
41 changes: 29 additions & 12 deletions Sources/Extensions/UIViewController+Deselection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,42 @@ public extension UIViewController {

if let coordinator = transitionCoordinator {
let success = coordinator.animate(alongsideTransition: { context in
for indexPath in selectedIndexPaths {
deselectable?.deselectItem(at: indexPath, animated: context.isAnimated)
}
UIViewController.switchSelectedItemsState(on: deselectable,
RomanPodymov marked this conversation as resolved.
Show resolved Hide resolved
selectedIndexPaths: selectedIndexPaths,
shouldBeSelected: false,
animated: context.isAnimated)
}, completion: { context in
if context.isCancelled {
for indexPath in selectedIndexPaths {
deselectable?.selectItem(at: indexPath, animated: false)
}
UIViewController.switchSelectedItemsState(on: deselectable,
selectedIndexPaths: selectedIndexPaths,
shouldBeSelected: true,
animated: false)
}
})
if !success {
for indexPath in selectedIndexPaths {
deselectable?.deselectItem(at: indexPath, animated: false)
}
UIViewController.switchSelectedItemsState(on: deselectable,
selectedIndexPaths: selectedIndexPaths,
shouldBeSelected: false,
animated: false)
}
} else {
for indexPath in selectedIndexPaths {
deselectable?.deselectItem(at: indexPath, animated: false)
}
UIViewController.switchSelectedItemsState(on: deselectable,
selectedIndexPaths: selectedIndexPaths,
shouldBeSelected: false,
animated: false)
}
}

private static 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!")
}()
}

}