Created
May 11, 2016 18:30
-
-
Save yas375/eb37c74fe7f5ee7133a619dff9523681 to your computer and use it in GitHub Desktop.
Migrating from SVProgressHUD to MBProgressHUD. We needed to expose `hasFinished` from MBProgressHUD's implementation and have added tests to make sure we remember about it when we upgrade MBProgressHUD. We didn't use Swift's default values and instead have added multiple `showStatus...` because we need to use it from Objective-C.
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
// | |
// MBProgressHUD+Additions.h | |
// Caremobile | |
// | |
// Created by Victor Ilyukevich on 4/13/16. | |
// Copyright © 2016 Care Zone Inc. All rights reserved. | |
// | |
@interface MBProgressHUD (Additions) | |
- (BOOL)hasFinished; | |
@end |
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
// | |
// MBProgressHUD+Additions.swift | |
// Caremobile | |
// | |
// Created by Victor Ilyukevich on 4/11/16. | |
// Copyright © 2016 Care Zone Inc. All rights reserved. | |
// | |
import Foundation | |
import BlocksKit | |
extension MBProgressHUD { | |
static func isVisible() -> Bool { | |
if let _ = MBProgressHUD(forView: firstWindow) { | |
return true | |
} | |
return false | |
} | |
static func show() -> MBProgressHUD { | |
return showStatus(nil) | |
} | |
static func showStatus(text: String?) -> MBProgressHUD { | |
return showStatus(text, dimBackground: false, cancelBlock: nil) | |
} | |
static func showStatus(text: String?, cancelBlock: (UIButton -> ())?) -> MBProgressHUD { | |
return showStatus(text, dimBackground: false, cancelBlock: cancelBlock) | |
} | |
static func showStatus(text: String?, dimBackground: Bool) -> MBProgressHUD { | |
return showStatus(text, dimBackground: dimBackground, cancelBlock: nil) | |
} | |
static func showStatus(text: String?, dimBackground: Bool, cancelBlock: (UIButton -> ())?) -> MBProgressHUD { | |
let hud = findOrCreateHUD() | |
hud.mode = .Indeterminate | |
hud.label.text = text | |
hud.userInteractionEnabled = true | |
if dimBackground { | |
hud.backgroundView.color = UIColor(white: 0, alpha: 0.5) | |
} | |
if let handler = cancelBlock { | |
hud.button.setTitle(t("Cancel"), forState: .Normal) | |
hud.button.bk_addEventHandler({ sender in | |
MBProgressHUD.dismiss() | |
handler(sender as! UIButton) | |
}, forControlEvents: .TouchUpInside) | |
} | |
return hud | |
} | |
static func showSuccessAndDismiss(text: String?) -> MBProgressHUD { | |
return showAwesomeIconAndDismiss("\u{f00c}", text: text) | |
} | |
static func showErrorAndDismiss(text: String?) -> MBProgressHUD { | |
return showAwesomeIconAndDismiss("\u{f057}", text: text) | |
} | |
static func dismiss() { | |
hideHUDForView(firstWindow, animated: true) | |
} | |
static func setProgress(progress: Float) { | |
if let hud = MBProgressHUD(forView: firstWindow) { | |
hud.progress = progress | |
} | |
} | |
// MARK: Internal | |
private static func findOrCreateHUD() -> MBProgressHUD { | |
if let hud = MBProgressHUD(forView: firstWindow) where hud.hasFinished() == false { | |
hud.showAnimated(true) // cancel pending timer to hide, if any | |
return hud | |
} | |
return showHUDAddedTo(firstWindow, animated: true) | |
} | |
private static var firstWindow: UIView { | |
// There is also `UIApplication#keyWindow` but it might be wrong window. | |
// For example if and alert is presented, then `keyWindow` will have a reference to it. | |
return UIApplication.sharedApplication().windows.first! | |
} | |
private static func showAwesomeIconAndDismiss(icon: String, text: String?) -> MBProgressHUD { | |
let hud = findOrCreateHUD() | |
hud.label.text = text | |
hud.hideButton() | |
hud.customView = makeAwesomeLabel(icon) | |
hud.mode = .CustomView | |
hud.userInteractionEnabled = false | |
hud.hideAnimated(true, afterDelay: 1) | |
return hud | |
} | |
private func hideButton() { | |
button.removeTarget(nil, action: nil, forControlEvents: .AllEvents) | |
button.setTitle(nil, forState: .Normal) | |
setNeedsUpdateConstraints() | |
} | |
} | |
private func makeAwesomeLabel(text: String) -> UILabel { | |
let view = UILabel() | |
view.font = UIFont(name: "FontAwesome", size: 30) | |
view.text = text | |
return view | |
} |
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
// | |
// MBProgressHUDAdditionsSpec.m | |
// Caremobile | |
// | |
// Created by Victor Ilyukevich on 4/13/16. | |
// Copyright 2016 Care Zone Inc. All rights reserved. | |
// | |
@import Kiwi; | |
#import "MBProgressHUD+Additions.h" | |
SPEC_BEGIN(MBProgressHUDAdditionsSpec) | |
describe(@"MBProgressHUD+Additions", ^{ | |
__block MBProgressHUD *hud; | |
beforeEach(^{ | |
hud = [MBProgressHUD showHUDAddedTo:[UIView new] animated:NO]; | |
}); | |
it(@"responds to `hasFinished`", ^{ | |
[[hud should] respondToSelector:@selector(hasFinished)]; | |
}); | |
it(@"is finished after hide call", ^{ | |
[[theValue(hud.hasFinished) should] beFalse]; | |
[hud hideAnimated:NO]; | |
[[theValue(hud.hasFinished) should] beTrue]; | |
}); | |
}); | |
SPEC_END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment