Created
May 19, 2016 16:31
-
-
Save lukeredpath/0b9ce86de65bab7f3382c8fbbdac911d to your computer and use it in GitHub Desktop.
Encapsulating displaying a HUD (and its implementation)
This file contains hidden or 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 HUDAppearanceMode { | |
case Indeterminate | |
case IndeterminateWithStatus(status: String) | |
} | |
enum HUDDismissalMode { | |
case Immediately | |
case WithDelay(delay: NSTimeInterval) | |
case WithStatus(successStatus: String, failureStatus: String) | |
} | |
class HUDOperationObserver: OperationDidStartObserver, OperationDidCancelObserver, OperationDidFinishObserver { | |
var appearanceMode: HUDAppearanceMode = .Indeterminate | |
var dismissalMode: HUDDismissalMode = .Immediately | |
init(appearanceMode: HUDAppearanceMode, dismissalMode: HUDDismissalMode) { | |
self.appearanceMode = appearanceMode | |
self.dismissalMode = dismissalMode | |
} | |
convenience init(appearanceMode: HUDAppearanceMode) { | |
self.init(appearanceMode: appearanceMode, dismissalMode: .Immediately) | |
} | |
func didStartOperation(operation: Operation) { | |
switch appearanceMode { | |
case .Indeterminate: | |
SVProgressHUD.show() | |
case .IndeterminateWithStatus(let status): | |
SVProgressHUD.showWithStatus(status) | |
} | |
} | |
func didCancelOperation(operation: Operation) { | |
switch dismissalMode { | |
case .Immediately: | |
SVProgressHUD.dismiss() | |
case .WithDelay(let delay): | |
SVProgressHUD.dismissWithDelay(delay) | |
default: | |
SVProgressHUD.dismiss() | |
} | |
} | |
func didFinishOperation(operation: Operation, errors: [ErrorType]) { | |
switch dismissalMode { | |
case .Immediately: | |
SVProgressHUD.dismiss() | |
case .WithDelay(let delay): | |
SVProgressHUD.dismissWithDelay(delay) | |
case .WithStatus(let successStatus, let failureStatus): | |
if errors.count > 0 { | |
SVProgressHUD.showErrorWithStatus(failureStatus) | |
} | |
else { | |
SVProgressHUD.showSuccessWithStatus(successStatus) | |
} | |
} | |
} | |
} |
This file contains hidden or 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
// I like this for several reasons: | |
// 1. Showing/Hiding a HUD during an operation is 1-2 lines of code | |
// 2. Simple API, builds on top of the observer pattern | |
// 3. HUD implementation (or library, SVProgressHUD in my case) is encapsulated and easily changed | |
let uploadOperation = UploadOperation(networkController: AlamoNetworkController.sharedInstance!, image: image) | |
imageUploadOperation.addObserver(HUDOperationObserver( | |
appearanceMode: .IndeterminateWithStatus(status: "Uploading..."), | |
dismissalMode: .WithStatus(successStatus: "Upload Complete", failureStatus: "Upload Failed") | |
)) | |
imageUploadOperation.addObserver(DidFinishObserver() { _, _ in | |
self.dismissViewControllerAnimated(true, completion: nil) | |
}) | |
operationQueue.addOperation(uploadOperation) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment