Browse Source

new login

Marino Faggiana 8 years ago
parent
commit
cdf1fd6db2

+ 15 - 7
iOSClient/Login/CCLogin.h

@@ -27,12 +27,22 @@
 #import "CCCertificate.h"
 #import "OCNetworking.h"
 
+@protocol CCLoginDelegate <NSObject>
+
+- (void) loginSuccess:(NSInteger)loginType;
+@optional - (void) loginCancel:(NSInteger)loginType;
+
+@end
+
 @interface CCLogin : UIViewController <UITextFieldDelegate, NSURLSessionTaskDelegate, NSURLSessionDelegate, CCCertificateDelegate, OCNetworkingDelegate>
 
-{
-    UIAlertView *alertView;
-    UIView *rootView;
-}
+typedef enum {
+    loginAdd = 0,
+    loginAddForced = 1,
+    loginModifyPasswordUser = 2
+} enumLoginType;
+
+@property (nonatomic, weak) id <CCLoginDelegate> delegate;
 
 @property (nonatomic, weak) IBOutlet UITextField *user;
 @property (nonatomic, weak) IBOutlet UITextField *password;
@@ -43,8 +53,6 @@
 @property (nonatomic, weak) IBOutlet UIButton *annulla;
 @property (nonatomic, weak) IBOutlet UIButton *toggleVisiblePassword;
 
-@property BOOL modifyOnlyPassword;
-@property (nonatomic, strong) NSString *typeCloud;
-@property (nonatomic, strong) UIViewController *viewController;
+@property enumLoginType loginType;
 
 @end

+ 35 - 20
iOSClient/Login/CCLogin.m

@@ -26,6 +26,13 @@
 #import "CCUtility.h"
 #import "CCCoreData.h"
 
+@interface CCLogin ()
+{
+    UIAlertView *alertView;
+    UIView *rootView;
+}
+@end
+
 @implementation CCLogin
 
 - (void)viewDidLoad
@@ -48,23 +55,31 @@
     self.loadingBaseUrl.image = [UIImage animatedImageWithAnimatedGIFURL:[[NSBundle mainBundle] URLForResource: @"loading" withExtension:@"gif"]];
     self.loadingBaseUrl.hidden = YES;
     
-    if (_modifyOnlyPassword) {
+    if (_loginType == loginAdd) {
+        
+    }
+    
+    if (_loginType == loginAddForced) {
+        _annulla.hidden = YES;
+    }
+    
+    if (_loginType == loginModifyPasswordUser) {
         _baseUrl.userInteractionEnabled = NO;
         _baseUrl.textColor = [UIColor lightGrayColor];
         _user.userInteractionEnabled = NO;
         _user.textColor = [UIColor lightGrayColor];
     }
+    
+    [self.annulla setTitle:NSLocalizedString(@"_cancel_", nil) forState:UIControlStateNormal];
+    [self.login setTitle:NSLocalizedString(@"_login_", nil) forState:UIControlStateNormal];
 }
 
 - (void)viewWillAppear:(BOOL)animated
 {
     [super viewWillAppear:animated];
 
-    [self.annulla setTitle:NSLocalizedString(@"_cancel_", nil) forState:UIControlStateNormal];
-    [self.login setTitle:NSLocalizedString(@"_login_", nil) forState:UIControlStateNormal];
-    
     // verify URL
-    if (_modifyOnlyPassword && [self.baseUrl.text length] > 0)
+    if (_loginType == loginModifyPasswordUser && [self.baseUrl.text length] > 0)
         [self testUrl];
 }
 
@@ -74,12 +89,6 @@
     return YES;
 }
 
-- (void)didReceiveMemoryWarning
-{
-    [super didReceiveMemoryWarning];
-    // Dispose of any resources that can be recreated.
-}
-
 #pragma --------------------------------------------------------------------------------------------
 #pragma mark == Chech Server URL ==
 #pragma --------------------------------------------------------------------------------------------
@@ -90,7 +99,7 @@
     self.loadingBaseUrl.hidden = NO;
     
     NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.baseUrl.text] cachePolicy:0 timeoutInterval:20.0];
-    [request addValue:[CCUtility getUserAgent:_typeCloud] forHTTPHeaderField:@"User-Agent"];
+    [request addValue:[CCUtility getUserAgent:typeCloudNextcloud] forHTTPHeaderField:@"User-Agent"];
     
     NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
     NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
@@ -122,7 +131,7 @@
                     alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_connection_error_",nil) message:[error localizedDescription] delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
                     [alertView show];
                     
-                    if (!_modifyOnlyPassword)
+                    if (_loginType != loginModifyPasswordUser)
                         self.baseUrl.text = @"";
                 });
             }
@@ -140,7 +149,7 @@
 
 - (void)trustedCerticateDenied
 {
-    if (_modifyOnlyPassword)
+    if (_loginType == loginModifyPasswordUser)
         [self handleAnnulla:self];
 }
 
@@ -168,7 +177,7 @@
     if ([[self.baseUrl.text substringFromIndex:[self.baseUrl.text length] - 1] isEqualToString:@"/"])
         self.baseUrl.text = [self.baseUrl.text substringToIndex:[self.baseUrl.text length] - 1];
     
-    OCnetworking *ocNet = [[OCnetworking alloc] initWithDelegate:self metadataNet:nil withUser:self.user.text withPassword:self.password.text withUrl:nil withTypeCloud:_typeCloud activityIndicator:NO isCryptoCloudMode:NO];
+    OCnetworking *ocNet = [[OCnetworking alloc] initWithDelegate:self metadataNet:nil withUser:self.user.text withPassword:self.password.text withUrl:nil withTypeCloud:typeCloudNextcloud activityIndicator:NO isCryptoCloudMode:NO];
     NSError *error = [ocNet readFileSync:[NSString stringWithFormat:@"%@%@", self.baseUrl.text, webDAV]];
     
     if (!error) {
@@ -176,7 +185,7 @@
         // account
         NSString *account = [NSString stringWithFormat:@"%@ %@", self.user.text, self.baseUrl.text];
         
-        if (_modifyOnlyPassword) {
+        if (_loginType == loginModifyPasswordUser) {
             
             [CCCoreData updateAccount:account withPassword:self.password.text];
             
@@ -185,7 +194,7 @@
             [CCCoreData deleteAccount:account];
         
             // Add default account
-            [CCCoreData addAccount:account url:self.baseUrl.text user:self.user.text password:self.password.text uid:nil typeCloud:_typeCloud];
+            [CCCoreData addAccount:account url:self.baseUrl.text user:self.user.text password:self.password.text uid:nil typeCloud:typeCloudNextcloud];
         }
         
         TableAccount *tableAccount = [CCCoreData setActiveAccount:account];
@@ -195,11 +204,16 @@
             
             [app settingActiveAccount:tableAccount.account activeUrl:tableAccount.url activeUser:tableAccount.user activePassword:tableAccount.password activeUID:nil activeAccessToken:nil typeCloud:tableAccount.typeCloud];
             
-            [self dismissViewControllerAnimated:YES completion:nil];
+            [self.delegate loginSuccess:_loginType];
+            
+            // close
+            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
+                [self dismissViewControllerAnimated:YES completion:nil];
+            });
             
         } else {
             
-            if (_modifyOnlyPassword == NO)
+            if (_loginType != loginModifyPasswordUser)
                 [CCCoreData deleteAccount:account];
             
             alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_error_", nil) message:@"Fatal error writing database" delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"_ok_", nil), nil];
@@ -260,7 +274,8 @@
 
 - (IBAction)handleAnnulla:(id)sender
 {
-    [[NSNotificationCenter defaultCenter] postNotificationName:@"messageLoginIncorrect" object:nil];
+    if ([self.delegate respondsToSelector:@selector(loginCancel:)])
+        [self.delegate loginCancel:_loginType];
     
     [self dismissViewControllerAnimated:YES completion:nil];
 }

+ 7 - 7
iOSClient/Main/CCMain.m

@@ -1003,15 +1003,15 @@
 #pragma mark ===== Change Password =====
 #pragma --------------------------------------------------------------------------------------------
 
-- (void)changePasswordAccount
+- (void) loginSuccess:(NSInteger)loginType
 {
-    CCLogin *loginVC;
-
-    if ([app.typeCloud isEqualToString:typeCloudNextcloud])
-        loginVC = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"CCLoginNextcloud"];
     
-    [loginVC setModifyOnlyPassword:YES];
-    [loginVC setTypeCloud:app.typeCloud];
+}
+
+- (void)changePasswordAccount
+{
+    CCLogin *loginVC = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"CCLoginNextcloud"];
+    loginVC.loginType = loginModifyPasswordUser;
     
     [self presentViewController:loginVC animated:YES completion:nil];
 }

+ 1 - 1
iOSClient/Main/CCSplit.h

@@ -27,7 +27,7 @@
 #import "CCLogin.h"
 #import "CCDetail.h"
 
-@interface CCSplit : UISplitViewController <UISplitViewControllerDelegate, CCIntroDelegate>
+@interface CCSplit : UISplitViewController <UISplitViewControllerDelegate, CCIntroDelegate, CCLoginDelegate>
 
 @property (nonatomic, strong) CCIntro *intro;
 @property (nonatomic, strong) NSString *version;

+ 9 - 3
iOSClient/Main/CCSplit.m

@@ -22,8 +22,8 @@
 //
 
 #import "CCSplit.h"
-
 #import "AppDelegate.h"
+#import "CCLogin.h"
 
 @interface CCSplit ()
 {
@@ -134,13 +134,19 @@
 #pragma mark ===== newAccount =====
 #pragma --------------------------------------------------------------------------------------------
 
+- (void) loginSuccess:(NSInteger)loginType
+{
+    [[NSNotificationCenter defaultCenter] postNotificationName:@"initializeMain" object:nil];
+}
+
 - (void)newAccount
 {
     if ([app.activeAccount length] == 0) {
     
-        CCLogin *viewController = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"CCLoginNextcloud"];
+        CCLogin *loginVC = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"CCLoginNextcloud"];
+        loginVC.loginType = loginAddForced;
     
-        [self presentViewController:viewController animated:YES completion:nil];
+        [self presentViewController:loginVC animated:YES completion:nil];
     }
 }
 

+ 1 - 1
iOSClient/Settings/CCManageAccount.h

@@ -26,6 +26,6 @@
 #import "CCLogin.h"
 #import "CCCoreData.h"
 
-@interface CCManageAccount : XLFormViewController
+@interface CCManageAccount : XLFormViewController <CCLoginDelegate>
 
 @end

+ 26 - 24
iOSClient/Settings/CCManageAccount.m

@@ -33,8 +33,6 @@
 {
     if (self = [super initWithCoder:aDecoder])  {
         
-        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(UpdateForm) name:@"updateFormManageAccount" object:nil];
-        
         [self initializeForm];
     }
     
@@ -74,7 +72,7 @@
     row = [XLFormRowDescriptor formRowDescriptorWithTag:@"addAccountNextcloud" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_add_nextcloud_", nil)];
     [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
     [row.cellConfig setObject:[UIImage imageNamed:image_settingsAccountNextcloud] forKey:@"imageView.image"];
-    row.action.formSelector = @selector(addAccountNextcloud:);
+    row.action.formSelector = @selector(addAccount:);
     [section addFormRow:row];
     
     // delete Account
@@ -123,12 +121,24 @@
 }
 
 #pragma --------------------------------------------------------------------------------------------
-#pragma mark === Aggiungi Account ===
+#pragma mark === Delegate Login ===
 #pragma --------------------------------------------------------------------------------------------
 
-/*** NEXTCLOUD ***/
+- (void) loginSuccess:(NSInteger)loginType
+{
+    
+}
+
+- (void) loginCancel:(NSInteger)loginType
+{
+    
+}
+
+#pragma --------------------------------------------------------------------------------------------
+#pragma mark === Add Account ===
+#pragma --------------------------------------------------------------------------------------------
 
-- (void)addAccountNextcloud:(XLFormRowDescriptor *)sender
+- (void)addAccount:(XLFormRowDescriptor *)sender
 {
     [self deselectFormRow:sender];
     
@@ -136,22 +146,18 @@
     [[CCNetworking sharedNetworking] settingSessionsDownload:YES upload:YES taskStatus:k_taskStatusCancel activeAccount:app.activeAccount activeUser:app.activeUser activeUrl:app.activeUrl];
     
     CCLogin *loginVC = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"CCLoginNextcloud"];
-    
-    [loginVC setModifyOnlyPassword:NO];
-    [loginVC setTypeCloud:typeCloudNextcloud];
+    loginVC.loginType = loginAdd;
     
     [self presentViewController:loginVC animated:YES completion:nil];
 }
 
-- (void)openLoginSetupVC
-{    
-    // remove any message
-    [[NSNotificationCenter defaultCenter] removeObserver:self];
-    
-    CCLogin *viewController = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"CCLoginNextcloud"];
+- (void)addAccountFoced
+{
+    CCLogin *loginVC = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"CCLoginNextcloud"];
+    loginVC.loginType = loginAddForced;
     
     dispatch_async(dispatch_get_main_queue(), ^ {
-        [self presentViewController:viewController animated:YES completion:nil];
+        [self presentViewController:loginVC animated:YES completion:nil];
     });
 }
 
@@ -160,9 +166,7 @@
 #pragma --------------------------------------------------------------------------------------------
 
 - (void)modifyAccount:(XLFormRowDescriptor *)sender
-{
-    NSString *vcName;
-    
+{    
     [self deselectFormRow:sender];
     
     [app cancelAllOperations];
@@ -171,9 +175,7 @@
     if ([app.typeCloud isEqualToString:typeCloudNextcloud] || [app.typeCloud isEqualToString:typeCloudOwnCloud]) {
         
         CCLogin *loginVC = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier: @"CCLoginNextcloud"];
-        
-        [loginVC setModifyOnlyPassword:YES];
-        [loginVC setTypeCloud:app.typeCloud];
+        loginVC.loginType = loginModifyPasswordUser;
         
         [self presentViewController:loginVC animated:YES completion:nil];
     }
@@ -209,7 +211,7 @@
         
         if ([listAccount count] > 0) [self ChangeDefaultAccount:listAccount[0]];
         else {
-            [self openLoginSetupVC];
+            [self addAccountFoced];
             return;
         }
     }
@@ -278,7 +280,7 @@
     NSArray *listAccount = [CCCoreData getAllAccount];
     
     if (listAccount == nil) {
-        [self openLoginSetupVC];
+        [self addAccountFoced];
         return;
     }