فهرست منبع

Add JSAlertView for showing simple alerts (temporary)

Marino Faggiana 8 سال پیش
والد
کامیت
07efbcef06

+ 28 - 0
Libraries external/JSAlertView/JSAlertView.h

@@ -0,0 +1,28 @@
+//
+//  JSAlertView.h
+//  JSAlertView
+//
+//  Created by Jitendra Singh on 10/12/16.
+//  Copyright © 2016 Jitendra Singh. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+#define ALERT(x) [JSAlertView alert:x]
+
+#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
+@interface JSAlertView : UIAlertController
+#else
+@interface JSAlertView : UIAlertView
+#endif
+// Displays simple alert message (no title) with 'Ok' button
++ (instancetype)alert:(NSString*)message;
+
+// Displays message (no title) with 'Yes' and 'No' button. It can be used to display confirmirmation alert.
++ (instancetype)confirm:(NSString*)message withCompletionHandler:(void(^)(BOOL accepted))completionHandler;
+
+// Same as previous one with addition option to show title
++ (instancetype)confirm:(NSString*)message withTitle:(NSString*)title  withCompletionHandler:(void(^)(BOOL accepted))completionHandler;
+
+// Standard menthod for displaying alert, fully customizable.
++ (instancetype)alert:(NSString*)message withTitle:(NSString*)title buttons:(NSArray*)buttonTitles withCompletionHandler:(void(^)(NSInteger buttonIndex, NSString *buttonTitle))completionHandler;
+@end

+ 181 - 0
Libraries external/JSAlertView/JSAlertView.m

@@ -0,0 +1,181 @@
+//
+//  JSAlertView.m
+//  JSAlertView
+//
+//  Created by Jitendra Singh on 10/12/16.
+//  Copyright © 2016 Jitendra Singh. All rights reserved.
+//
+
+#import "JSAlertView.h"
+
+@interface JSAlertView ()
+
+@property (nonatomic, copy) void(^completionBlock)(NSInteger buttonIndex, NSString *buttonTitle);
+@property (nonatomic, copy) void(^confirmationBlock)(BOOL accepted);
+@property (nonatomic, strong) UIWindow *thisAlertWindow;
+@property (nonatomic, strong) NSMutableArray *allAlertWindows;
+
+@end
+
+@implementation JSAlertView
+
++ (instancetype)sharedInstance {
+    static dispatch_once_t predicate;
+    static JSAlertView *instance = nil;
+    dispatch_once(&predicate, ^{
+        instance = [[self alloc] init];
+        [instance initalization];
+    });
+    return instance;
+}
+
+- (void)initalization
+{
+    // do write all initalization code here
+    self.allAlertWindows = [NSMutableArray arrayWithCapacity:0];
+    
+}
+
++ (instancetype)alert:(NSString*)message
+{
+    return [self alert:message withTitle:nil buttons:@[@"Ok"] withCompletionHandler:nil];
+}
++ (instancetype)confirm:(NSString*)message withCompletionHandler:(void(^)(BOOL accepted))completionHandler
+{
+    return [self confirm:message withTitle:nil withCompletionHandler:completionHandler];
+}
+
++ (instancetype)confirm:(NSString*)message withTitle:(NSString*)title  withCompletionHandler:(void(^)(BOOL accepted))completionHandler
+{
+#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
+    
+    JSAlertView *alert = [JSAlertView alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert];
+    UIAlertAction* noButton = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
+        [alert dismissViewControllerAnimated:YES completion:nil];
+        if (completionHandler) {
+            completionHandler(NO);
+        }
+    }];
+    [alert addAction:noButton];
+    
+    UIAlertAction* yesButton = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
+        [alert dismissViewControllerAnimated:YES completion:nil];
+        if (completionHandler) {
+            completionHandler(YES);
+        }
+    }];
+    [alert addAction:yesButton];
+    
+    [alert show];
+    
+    return alert;
+#else
+    JSAlertView *alert = [[JSAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
+    __weak __typeof__(alert) alert_weak_ = (alert);
+    [alert setDelgate:alert_weak_];
+    
+    [alert addButtonWithTitle:@"No"];
+    [alert addButtonWithTitle:@"Yes"];
+    
+    alert.confirmationBlock = completionHandler;
+    [alert show];
+    return alert;
+#endif
+}
+
++ (instancetype)alert:(NSString*)message withTitle:(NSString*)title buttons:(NSArray*)buttonTitles withCompletionHandler:(void(^)(NSInteger buttonIndex, NSString *buttonTitle))completionHandler
+{
+#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
+    
+    JSAlertView *alert = [JSAlertView alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert];
+    for (NSString *btnTitle in buttonTitles) {
+        UIAlertAction* button = [UIAlertAction actionWithTitle:btnTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
+            [alert dismissViewControllerAnimated:YES completion:nil];
+            if (completionHandler) {
+                completionHandler([buttonTitles indexOfObject:btnTitle], btnTitle);
+            }
+        }];
+        [alert addAction:button];
+    }
+    [alert show];
+    
+    return alert;
+#else
+    JSAlertView *alert = [[JSAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
+    __weak __typeof__(alert) alert_weak_ = (alert);
+    [alert setDelgate:alert_weak_];
+    __block NSInteger index = 0;
+    for (NSString *btnTitle in buttonTitles) {
+        [alert addButtonWithTitle:btnTitle];
+    }
+    alert.completionBlock = completionHandler;
+    [alert show];
+    return alert;
+#endif
+}
+
+- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
+{
+    if (self.completionBlock) {
+        self.completionBlock(buttonIndex, [alertView buttonTitleAtIndex:buttonIndex]);
+    }
+    else if (self.confirmationBlock) {
+        self.confirmationBlock(!(buttonIndex == 0));
+    }
+}
+
+
+
+/***********************************************************************
+ *
+ *  Following methods will be used for iOS 8 or later for UIAlertController
+ *
+ ***********************************************************************/
+
+#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
+
+- (void)show {
+    [self show:YES];
+}
+
+- (void)show:(BOOL)animated {
+    
+    //create a new window for the alert
+    self.thisAlertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
+    self.thisAlertWindow.rootViewController = [[UIViewController alloc] init];
+    
+    // set this window on top in stack
+    UIWindow *topWindow = [UIApplication sharedApplication].windows.lastObject;
+    self.thisAlertWindow.windowLevel = topWindow.windowLevel + 1;
+    
+    // make it visible and show alert
+    [self.thisAlertWindow makeKeyAndVisible];
+    [self.thisAlertWindow.rootViewController presentViewController:self animated:animated completion:nil];
+    
+    // set alpha 0.0 for last alert to make it transparent, this will give feel of single alert displayed on screen
+    [[JSAlertView sharedInstance].allAlertWindows.lastObject setAlpha:0.0];
+    [[JSAlertView sharedInstance].allAlertWindows addObject:self.thisAlertWindow];
+}
+
+- (void)viewWillDisappear:(BOOL)animated {
+    [super viewWillDisappear:animated];
+    
+    // remove this window from stack
+    [[JSAlertView sharedInstance].allAlertWindows removeObject:self.thisAlertWindow];
+    // set alpha 1.0 for last alert to make it appear
+    [UIView animateWithDuration:0.3 animations:^{
+        [[JSAlertView sharedInstance].allAlertWindows.lastObject setAlpha:1.0];
+    }];
+}
+
+- (void)viewDidDisappear:(BOOL)animated {
+    [super viewDidDisappear:animated];
+    
+    // once the alert is disappeared set window property to nil, else it will create retain cycle
+    self.thisAlertWindow.hidden = YES;
+    self.thisAlertWindow = nil;
+}
+
+
+#endif
+@end

+ 14 - 0
Nextcloud.xcodeproj/project.pbxproj

@@ -80,6 +80,7 @@
 		F7145A241D12E3B700CAFEEC /* CCCellShareExt.xib in Resources */ = {isa = PBXBuildFile; fileRef = F7296A621C8880C9001A7809 /* CCCellShareExt.xib */; };
 		F71E68001DC1F792003BA52B /* libownCloudiOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = F71E67FC1DC1F76F003BA52B /* libownCloudiOS.a */; };
 		F71E68021DC1F79D003BA52B /* libownCloudiOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = F71E67FC1DC1F76F003BA52B /* libownCloudiOS.a */; };
+		F7253FCA1E38BAF20084135B /* JSAlertView.m in Sources */ = {isa = PBXBuildFile; fileRef = F7253FC91E38BAF20084135B /* JSAlertView.m */; };
 		F725437C1E12A44A009BF4C2 /* CCSection.m in Sources */ = {isa = PBXBuildFile; fileRef = F78F6FAF1CC8CCB700F4EA25 /* CCSection.m */; };
 		F72C63891DC14B0400FA5ED5 /* libMagicalRecord.a in Frameworks */ = {isa = PBXBuildFile; fileRef = F7B61E9B1DC13C20009E938F /* libMagicalRecord.a */; };
 		F732BA061D76CE1500E9878B /* CCNetworking.m in Sources */ = {isa = PBXBuildFile; fileRef = F732BA041D76CE1500E9878B /* CCNetworking.m */; };
@@ -1098,6 +1099,8 @@
 		F722CEEB1A40361A00C40606 /* Raleway-Regular.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "Raleway-Regular.ttf"; sourceTree = "<group>"; };
 		F722CEEC1A40361A00C40606 /* Raleway-SemiBold.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "Raleway-SemiBold.ttf"; sourceTree = "<group>"; };
 		F722CEED1A40361A00C40606 /* Raleway-Thin.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "Raleway-Thin.ttf"; sourceTree = "<group>"; };
+		F7253FC81E38BAF20084135B /* JSAlertView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSAlertView.h; sourceTree = "<group>"; };
+		F7253FC91E38BAF20084135B /* JSAlertView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSAlertView.m; sourceTree = "<group>"; };
 		F7296A601C8880C9001A7809 /* CCCellShareExt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCCellShareExt.h; sourceTree = "<group>"; };
 		F7296A611C8880C9001A7809 /* CCCellShareExt.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCCellShareExt.m; sourceTree = "<group>"; };
 		F7296A621C8880C9001A7809 /* CCCellShareExt.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = CCCellShareExt.xib; sourceTree = "<group>"; };
@@ -1674,6 +1677,7 @@
 				F73CCE221DC13788007E38D8 /* DZNEmptyDataSet */,
 				F7659A211DC0B726004860C4 /* EAIntroView */,
 				F7659A2A1DC0B72F004860C4 /* EARestrictedScrollView */,
+				F7253FC71E38BAF20084135B /* JSAlertView */,
 				F7659A2F1DC0B737004860C4 /* iRate */,
 				F70F0F251C889339008DAB36 /* LMMediaPlayer */,
 				F70F04821C889183008DAB36 /* MBProgressHUD */,
@@ -2362,6 +2366,15 @@
 			path = Raleway;
 			sourceTree = "<group>";
 		};
+		F7253FC71E38BAF20084135B /* JSAlertView */ = {
+			isa = PBXGroup;
+			children = (
+				F7253FC81E38BAF20084135B /* JSAlertView.h */,
+				F7253FC91E38BAF20084135B /* JSAlertView.m */,
+			);
+			path = JSAlertView;
+			sourceTree = "<group>";
+		};
 		F728CE741BF6322C00E69702 /* Share */ = {
 			isa = PBXGroup;
 			children = (
@@ -3926,6 +3939,7 @@
 				F77B0E241D118A16002130FE /* HRColorCursor.m in Sources */,
 				F77B0E251D118A16002130FE /* BKPasscodeInputView.m in Sources */,
 				F77B0E261D118A16002130FE /* ZSSTextView.m in Sources */,
+				F7253FCA1E38BAF20084135B /* JSAlertView.m in Sources */,
 				F77B0E271D118A16002130FE /* CYRLayoutManager.m in Sources */,
 				F77B0E291D118A16002130FE /* NSArray+LMMediaPlayerShuffle.m in Sources */,
 				F77B0E2A1D118A16002130FE /* REMenu.m in Sources */,

+ 1 - 1
iOSClient/AppDelegate.m

@@ -458,7 +458,7 @@
     NSLog(@"Error register remote notification %@", error);
 }
 
-- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
+- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
     
     UIApplicationState state = [application applicationState];
     

+ 1 - 0
iOSClient/Main/CCMain.h

@@ -60,6 +60,7 @@
 #import "CCNote.h"
 #import "CCPassaporto.h"
 #import "CCPatenteGuida.h"
+#import "JSAlertView.h"
 
 @interface CCMain : UITableViewController <UITableViewDataSource, UITableViewDelegate, UIActionSheetDelegate, UIGestureRecognizerDelegate, UIDocumentInteractionControllerDelegate, UIViewControllerPreviewingDelegate, CCMoveDelegate, CTAssetsPickerControllerDelegate, BKPasscodeViewControllerDelegate, UISplitViewControllerDelegate, UIPopoverControllerDelegate, CCNetworkingDelegate, CCShareOCDelegate, CCAccountWebDelegate, CCBancomatDelegate, CCCartaDiCreditoDelegate, CCCartaIdentitaDelegate, CCContoCorrenteDelegate, CCNoteDelegate, CCPassaportoDelegate, CCPatenteGuidaDelegate, CCDetailViewDelegate, CCPeekPopDelegate, UIDocumentMenuDelegate, UIDocumentPickerDelegate>
 

+ 10 - 1
iOSClient/Main/CCMain.m

@@ -1017,10 +1017,19 @@
     
     for (OCNotifications *notification in listOfNotifications) {
                 
-        [app messageNotification:@"Notication" description:notification.subject visible:YES delay:dismissAfterSecond type:TWMessageBarMessageTypeInfo];
+        //[app messageNotification:@"Notication" description:notification.subject visible:YES delay:dismissAfterSecond type:TWMessageBarMessageTypeInfo];
+        
+        [JSAlertView alert:notification.subject withTitle:@"Server Notification" buttons:@[@"OK",@"NO"] withCompletionHandler:^(NSInteger buttonIndex, NSString *buttonTitle) {
+            NSLog(@"Pressed %@ at index %ld", buttonTitle, (long)buttonIndex);
+        }];
     }
 }
 
+- (void)showNotification
+{
+    
+}
+
 - (void)getNotificationsOfServerFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
 {
     NSLog(@"Error Notification");