Skip to content

Commit 4747a20

Browse files
authored
Merge pull request #1602 from dwildr/CATTY-523
CATTY-523 Convert Util methods to Swift 2/3
2 parents 7e86133 + 3a5503f commit 4747a20

File tree

13 files changed

+116
-114
lines changed

13 files changed

+116
-114
lines changed

src/Catty/Extension&Delegate&Protocol/Extensions/UIViewController/CatrobatTableViewControllerExtension.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import Foundation
4343

4444
@nonobjc func openURL(url: URL, storeProjectDownloader: StoreProjectDownloaderProtocol) {
4545
guard let projectId = CatrobatTableViewController.catrobatProjectIdFromURL(url: url) else {
46-
Util.alert(withText: kLocalizedInvalidURLGiven)
46+
Util.alert(text: kLocalizedInvalidURLGiven)
4747
return
4848
}
4949
self.showLoadingView()
@@ -52,11 +52,11 @@ import Foundation
5252
self.hideLoadingView()
5353

5454
guard error == nil else {
55-
Util.alert(withText: kLocalizedUnableToLoadProject)
55+
Util.alert(text: kLocalizedUnableToLoadProject)
5656
return
5757
}
5858
guard let storeProject = project else {
59-
Util.alert(withText: kLocalizedInvalidZip)
59+
Util.alert(text: kLocalizedInvalidZip)
6060
return
6161
}
6262
let catrobatProject = storeProject.toCatrobatProject()

src/Catty/Extension&Delegate&Protocol/Extensions/UIViewController/ProjectDetailStoreViewControllerDownloadExtension.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ extension ProjectDetailStoreViewController {
3636
case .unexpectedError, .timeout:
3737
Util.defaultAlertForNetworkError()
3838
case .parse(error: _), .request(error: _, statusCode: _):
39-
Util.alert(withText: kLocalizedInvalidZip)
39+
Util.alert(text: kLocalizedInvalidZip)
4040
}
4141

4242
self.resetDownloadStatus()

src/Catty/Extension&Delegate&Protocol/Extensions/UIViewController/ProjectDetailStoreViewControllerReportExtension.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import Foundation
2626
extension ProjectDetailStoreViewController {
2727
func reportProject() {
2828
guard UserDefaults.standard.bool(forKey: NetworkDefines.kUserIsLoggedIn) else {
29-
Util.alert(withText: kLocalizedLoginToReport)
29+
Util.alert(text: kLocalizedLoginToReport)
3030
return
3131
}
3232

@@ -35,7 +35,7 @@ extension ProjectDetailStoreViewController {
3535
.addDefaultActionWithTitle(kLocalizedOK, handler: {report in
3636
let isValidInput = self.validateInput(input: report)
3737
guard isValidInput.valid else {
38-
Util.alert(withText: isValidInput.localizedMessage)
38+
Util.alert(text: isValidInput.localizedMessage!)
3939
return
4040
}
4141
self.sendReport(message: report)
@@ -53,13 +53,13 @@ extension ProjectDetailStoreViewController {
5353
Util.defaultAlertForNetworkError()
5454
self.hideLoadingView()
5555
} else {
56-
Util.alert(withText: kLocalizedProjectNotReported)
56+
Util.alert(text: kLocalizedProjectNotReported)
5757
}
5858
return
5959
}
6060

6161
DispatchQueue.main.async(execute: {
62-
Util.alert(withText: kLocalizedReportedProject)
62+
Util.alert(text: kLocalizedReportedProject)
6363
})
6464
})
6565
}

src/Catty/Extension&Delegate&Protocol/Extensions/UIViewController/UIViewControllerExtension.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,18 @@ extension UIViewController {
4747

4848
@nonobjc func openProjectDetails(url: URL, storeProjectDownloader: StoreProjectDownloaderProtocol) {
4949
guard let projectId = url.catrobatProjectId() else {
50-
Util.alert(withText: kLocalizedInvalidURLGiven)
50+
Util.alert(text: kLocalizedInvalidURLGiven)
5151
return
5252
}
5353

5454
storeProjectDownloader.fetchProjectDetails(for: projectId, completion: {project, error in
5555

5656
guard error == nil else {
57-
Util.alert(withText: kLocalizedUnableToLoadProject)
57+
Util.alert(text: kLocalizedUnableToLoadProject)
5858
return
5959
}
6060
guard let storeProject = project else {
61-
Util.alert(withText: kLocalizedInvalidZip)
61+
Util.alert(text: kLocalizedInvalidZip)
6262
return
6363
}
6464
let catrobatProject = storeProject.toCatrobatProject()

src/Catty/Helpers/Bluetooth/BluetoothService.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,21 +262,21 @@ open class BluetoothService: NSObject {
262262
if let manager = self.selectionManager {
263263
manager.deviceNotResponding()
264264
}
265-
Util.alert(withTitle: klocalizedBluetoothConnectionFailed, andText: klocalizedBluetoothNotResponding)
265+
Util.alert(title: klocalizedBluetoothConnectionFailed, text: klocalizedBluetoothNotResponding)
266266
}
267267

268268
func giveUpFailure() {
269269
if let manager = self.selectionManager {
270270
manager.giveUpConnectionToDevice()
271271
}
272-
Util.alert(withTitle: klocalizedBluetoothConnectionLost, andText: klocalizedBluetoothDisconnected)
272+
Util.alert(title: klocalizedBluetoothConnectionLost, text: klocalizedBluetoothDisconnected)
273273
}
274274

275275
func connectionFailure() {
276276
if let manager = self.selectionManager {
277277
manager.deviceFailedConnection()
278278
}
279-
Util.alert(withTitle: klocalizedBluetoothConnectionFailed, andText: klocalizedBluetoothCannotConnect)
279+
Util.alert(title: klocalizedBluetoothConnectionFailed, text: klocalizedBluetoothCannotConnect)
280280
}
281281

282282
// func setPhiroDevice(peripheral:Peripheral){

src/Catty/Helpers/Util/Util.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ if (__functor) __functor(__VA_ARGS__); \
5555

5656
+ (UIViewController* _Nonnull)topmostViewController;
5757

58-
+ (void)alertWithText:(NSString* _Nullable)text;
59-
60-
+ (void)alertWithTitle:(NSString* _Nullable)title andText:(NSString* _Nullable)text;
61-
6258
+ (void)askUserForVariableNameAndPerformAction:(SEL _Nullable)action
6359
target:(id _Nullable)target
6460
promptTitle:(NSString* _Nullable)title
@@ -115,10 +111,6 @@ if (__functor) __functor(__VA_ARGS__); \
115111

116112
+ (NSString* _Nullable)uniqueName:(NSString* _Nullable)nameToCheck existingNames:(NSArray* _Nullable)existingNames;
117113

118-
+ (void)showNotificationWithMessage:(NSString* _Nullable)message;
119-
120-
+ (void)showNotificationForSaveAction;
121-
122114
+ (CGFloat)detectCBLanguageVersionFromXMLWithPath:(NSString* _Nullable)xmlPath;
123115

124116
+ (double)radiansToDegree:(double)rad;
@@ -139,10 +131,6 @@ if (__functor) __functor(__VA_ARGS__); \
139131

140132
+ (BOOL)isNetworkError:(NSError* _Nullable)error;
141133

142-
+ (void)defaultAlertForNetworkError;
143-
144-
+ (void)defaultAlertForUnknownError;
145-
146134
+ (NSDictionary* _Nullable)getBrickInsertionDictionaryFromUserDefaults;
147135

148136
+ (void)setBrickInsertionDictionaryToUserDefaults:(NSDictionary* _Nullable) statistics;

src/Catty/Helpers/Util/Util.m

Lines changed: 12 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,6 @@ + (UIViewController *)topmostViewController {
7373
return [self topViewControllerInViewController:ROOTVIEW];
7474
}
7575

76-
+ (void)alertWithText:(NSString*)text
77-
{
78-
[Util alertWithTitle:kLocalizedPocketCode andText:text];
79-
}
80-
81-
+ (void)alertWithTitle:(NSString *)title andText:(NSString *)text
82-
{
83-
[[[[AlertControllerBuilder alertWithTitle:title message:text]
84-
addCancelActionWithTitle:kLocalizedOK handler:nil]
85-
build]
86-
showWithController:[Util topmostViewController]];
87-
}
88-
8976
+ (CATransition*)getPushCATransition
9077
{
9178
CATransition *transition = [CATransition animation];
@@ -135,7 +122,7 @@ + (InputValidationResult*)validationResultWithName:(NSString *)name minLength:(N
135122
} else {
136123
return [InputValidationResult validInput];
137124
}
138-
125+
139126
NSAssert(invalidNameMessage != nil, @"This case should already be handled");
140127
return [InputValidationResult invalidInputWithLocalizedMessage:invalidNameMessage];
141128
}
@@ -206,7 +193,7 @@ + (void)askUserForUniqueNameAndPerformAction:(SEL)action
206193
}]
207194
valueValidator:^InputValidationResult *(NSString *name) {
208195
InputValidationResult *result = [self validationResultWithName:name minLength:minInputLength maxlength:maxInputLength];
209-
196+
210197
if (!result.valid) {
211198
return result;
212199
}
@@ -278,7 +265,7 @@ + (void)askUserForVariableNameAndPerformAction:(SEL)action
278265
} else {
279266
return [InputValidationResult validInput];
280267
}
281-
268+
282269
NSAssert(invalidNameMessage != nil, @"This case should already be handled");
283270
return [InputValidationResult invalidInputWithLocalizedMessage:invalidNameMessage];
284271
}] build]
@@ -427,38 +414,38 @@ + (NSDictionary*)propertiesOfInstance:(id)instance
427414
{
428415
unsigned count;
429416
objc_property_t *properties = class_copyPropertyList([instance class], &count);
430-
417+
431418
NSMutableDictionary *propertiesDictionary = [NSMutableDictionary new];
432-
419+
433420
unsigned i;
434421
for (i = 0; i < count; i++)
435422
{
436423
objc_property_t property = properties[i];
437-
424+
438425
NSString *name = [NSString stringWithUTF8String:property_getName(property)];
439-
426+
440427
// TODO use introspection
441428
if ([name isEqualToString:@"hash"] || [name isEqualToString:@"superclass"]
442429
|| [name isEqualToString:@"description"] || [name isEqualToString:@"debugDescription"]
443430
|| [name isEqualToString:@"brickCategoryType"] || [name isEqualToString:@"brickType"]) {
444431
continue;
445432
}
446-
433+
447434
NSObject *currentProperty = [instance valueForKey:name];
448435
if(currentProperty != nil)
449436
[propertiesDictionary setValue:currentProperty forKey:name];
450437
}
451-
438+
452439
free(properties);
453-
440+
454441
return propertiesDictionary;
455442
}
456443

457444
+ (NSString*)defaultSceneNameForSceneNumber:(NSUInteger)sceneNumber
458445
{
459446
NSString *sceneNumberAsString = [NSString stringWithFormat:@" %@", @(sceneNumber)];
460447
NSString *sceneNameForSceneNumber = [kLocalizedScene stringByAppendingString:sceneNumberAsString];
461-
448+
462449
return sceneNameForSceneNumber;
463450
}
464451

@@ -547,28 +534,6 @@ + (BOOL)isNetworkError:(NSError*)error
547534
return error && error.code != kCFURLErrorCancelled;
548535
}
549536

550-
+ (void)defaultAlertForNetworkError
551-
{
552-
if ([NSThread isMainThread]) {
553-
[[self class] alertWithText:kLocalizedErrorInternetConnection];
554-
} else {
555-
dispatch_async(dispatch_get_main_queue(), ^{
556-
[Util defaultAlertForNetworkError];
557-
});
558-
}
559-
}
560-
561-
+ (void)defaultAlertForUnknownError
562-
{
563-
if ([NSThread isMainThread]) {
564-
[[self class] alertWithText:kLocalizedErrorUnknown];
565-
} else {
566-
dispatch_async(dispatch_get_main_queue(), ^{
567-
[Util defaultAlertForUnknownError];
568-
});
569-
}
570-
}
571-
572537
#pragma mark - brick statistics
573538

574539
+ (NSDictionary*)getBrickInsertionDictionaryFromUserDefaults
@@ -627,7 +592,7 @@ + (NSArray*) getSubsetOfTheMost:(NSUInteger)N usedBricksInDictionary:(NSDictiona
627592
return NSOrderedAscending;
628593
}
629594
}];
630-
595+
631596
NSUInteger count = ([sortedBricks count] >= N) ? N : [sortedBricks count];
632597
NSRange range;
633598
range.location = 0;
@@ -677,36 +642,6 @@ + (NSString*)enableBlockedCharactersForString:(NSString*)string
677642
return string;
678643
}
679644

680-
+ (void)showNotificationWithMessage:(NSString *)message
681-
{
682-
BDKNotifyHUD *notficicationHud = [BDKNotifyHUD notifyHUDWithImage:nil text:message];
683-
UIViewController *vc = [Util topmostViewController];
684-
685-
notficicationHud.destinationOpacity = kBDKNotifyHUDDestinationOpacity;
686-
notficicationHud.center = CGPointMake(vc.view.center.x, vc.view.center.y);
687-
688-
[vc.view addSubview:notficicationHud];
689-
[notficicationHud presentWithDuration:kBDKNotifyHUDPresentationDuration
690-
speed:kBDKNotifyHUDPresentationSpeed
691-
inView:vc.view
692-
completion:^{ [notficicationHud removeFromSuperview]; }];
693-
}
694-
695-
+ (void)showNotificationForSaveAction {
696-
BDKNotifyHUD *hud = [BDKNotifyHUD notifyHUDWithImage:[UIImage imageNamed:kBDKNotifyHUDCheckmarkImageName] text:kLocalizedSaved];
697-
UIViewController *vc = [Util topmostViewController];
698-
699-
hud.destinationOpacity = kBDKNotifyHUDDestinationOpacity;
700-
hud.center = CGPointMake(vc.view.center.x, vc.view.center.y + kBDKNotifyHUDCenterOffsetY);
701-
hud.tag = kSavedViewTag;
702-
703-
[vc.view addSubview:hud];
704-
[hud presentWithDuration:kBDKNotifyHUDPresentationDuration
705-
speed:kBDKNotifyHUDPresentationSpeed
706-
inView:vc.view
707-
completion:^{ [hud removeFromSuperview]; }];
708-
}
709-
710645
+ (void)openUrlExternal:(NSURL*)url
711646
{
712647
[[UIApplication sharedApplication] openURL:url options:[NSDictionary dictionary] completionHandler:nil];

0 commit comments

Comments
 (0)