| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- //
- // LoginViewController.m
- // RA Image
- //
- // Created by Ray on 27/04/2017.
- // Copyright © 2017 USAI. All rights reserved.
- //
- #import "LoginViewController.h"
- #import "LoginTextFiledCell.h"
- #import "LoginSwitchCell.h"
- #import "LoginSettingViewController.h"
- #import "RAModeListViewController.h"
- static NSString *kRememberLogin = @"RememberLogin";
- static NSString *kLoginUserInfo = @"UserInfo";
- @interface LoginViewController ()<UITableViewDelegate,UITableViewDataSource,LoginSwitchDelegate,UITextFieldDelegate>
- {
- BOOL _autoShowSetting;
- }
- @property (strong, nonatomic) IBOutlet UITableView *loginTable;
- @property (nonatomic,strong) NSDictionary *dataDic;
- @property (strong, nonatomic) IBOutlet UILabel *versionLabel;
- @property (strong, nonatomic) IBOutlet UIButton *loginBtn;
- @property (strong, nonatomic) IBOutlet UIButton *settingBtn;
- @property (nonatomic,copy) NSString *ServerName;
- @property (nonatomic,copy) NSString *user;
- @property (nonatomic,copy) NSString *pwd;
- @end
- @implementation LoginViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- _autoShowSetting = NO;
- [self initUserData];
- [self loadData];
- [self.loginTable reloadData];
- [self registListenKeyboard];
-
- self.loginBtn.layer.cornerRadius = 25.0f;
- self.loginBtn.clipsToBounds = YES;
-
- NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
- NSString* build =[infoDict objectForKey:@"CFBundleVersion"];
- NSString* short_version =[infoDict objectForKey:@"CFBundleShortVersionString"];
- self.versionLabel.text = [NSString stringWithFormat:@"Ver: %@.%@",short_version,build];
- }
- -(void) viewWillAppear:(BOOL)animated
- {
- [self.navigationController setNavigationBarHidden:true];
- }
- -(void)viewWillDisappear:(BOOL)animated
- {
- [self.navigationController setNavigationBarHidden:false];
- }
- - (void)viewDidAppear:(BOOL)animated {
- [super viewDidAppear:animated];
- if (_autoShowSetting) {
- [self settingBtnClick:self.settingBtn];
- _autoShowSetting = NO;
- }
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- #pragma mark - Private
- - (void)setServerName:(NSString *)server {
- _ServerName = server;
- if (!server.length) {
- _autoShowSetting = YES;
- } else {
- _autoShowSetting = NO;
- }
- }
- - (void)loadData {
- NSString *path = [[NSBundle mainBundle] pathForResource:@"loginUI.json" ofType:nil];
- NSData *data = [NSData dataWithContentsOfFile:path options:NSDataReadingMappedIfSafe error:nil];
- NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
- self.dataDic = dic;
- }
- - (void)initUserData {
- NSDictionary *addressDic = [self userDefaultsValue:kScanAddress];
- if (addressDic) {
- // NSUInteger selectedIndex = [[addressDic objectForKey:@"selectedIndex"] integerValue];
- // if (selectedIndex == 1) {
- // self.address = [addressDic objectForKey:@"serverAddress"];
- // }
- // if (selectedIndex == 2) {
- // self.address = [addressDic objectForKey:@"serverAddress"];
- // }
- self.ServerName = [addressDic objectForKey:@"serverName"];
- }
-
- if (!self.ServerName.length){
- _autoShowSetting = YES;
- return;
- }
- _autoShowSetting = NO;
-
- NSDictionary *userData = [self userDefaultsValue:kLoginUserInfo];
- if (userData) {
- // self.address = [userData objectForKey:@"address"];
- self.user = [userData objectForKey:@"user"];
- self.pwd = [userData objectForKey:@"pwd"];
- }
- }
- - (void)prepareUserData { // 登录取数据
-
- int count = [[self.dataDic objectForKey:@"count"] intValue];
- for (int i = 0; i < count; i++) {
- NSDictionary *itemDic = [self.dataDic objectForKey:[NSString stringWithFormat:@"item_%d",i]];
- if ([[itemDic objectForKey:@"type"] isEqualToString:@"text"]) {
- LoginTextFiledCell *cell = [self.loginTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
- NSString *placeOrder = cell.textBox.placeholder;
- if ([placeOrder isEqualToString:@"Address"]) {
- self.ServerName = cell.textBox.text;
- } else if ([placeOrder isEqualToString:@"user name"]) {
- self.user = cell.textBox.text;
- } else if ([placeOrder isEqualToString:@"password"]) {
- self.pwd = cell.textBox.text;
- }
- }
- }
-
- }
- #pragma mark - Orientation
- - (BOOL)shouldAutorotate {
- return YES;
- }
- - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
- return UIInterfaceOrientationPortrait;
- }
- - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
- return UIInterfaceOrientationMaskPortrait;
- }
- #pragma mark - DataSource
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return [[self.dataDic objectForKey:@"count"] intValue];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- NSDictionary *itemDic = [self.dataDic objectForKey:[NSString stringWithFormat:@"item_%ld",indexPath.row]];
-
- NSString *type = [itemDic objectForKey:@"type"];
- BOOL rememberValue = [[self userDefaultsValue:kRememberLogin] boolValue];
-
- if ([type isEqualToString:@"text"]) {
-
- LoginTextFiledCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LoginTextFiledCell"];
- cell.textBox.placeholder = [itemDic objectForKey:@"place_order"];
- BOOL editable = [[itemDic objectForKey:@"edit"] boolValue];
- cell.textBox.enabled = editable;
- NSString *placeOrder = cell.textBox.placeholder;
- if ([placeOrder isEqualToString:@"Server"]) {
- cell.textBox.text = self.ServerName;
- } else if ([placeOrder isEqualToString:@"user name"]) {
- cell.textBox.text = self.user;
- } else if ([placeOrder isEqualToString:@"password"]) {
- cell.textBox.text = self.pwd;
- }
- cell.textBox.secureTextEntry = [[itemDic objectForKey:@"security"] boolValue];
- return cell;
-
- } else if ([type isEqualToString:@"switch"]) {
-
- LoginSwitchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LoginSwitchCell"];
- cell.msgLabel.text = [itemDic objectForKey:@"tip_msg"];
- cell.stateSwitch.on = rememberValue;
- cell.delegate = self;
- return cell;
- }
-
- return nil;
- }
- #pragma mark - Delegate
- - (void)switchButton:(UISwitch *)switchBtn valueChange:(BOOL)value {
-
- [self setUserDefaultsValue:@(value) forKey:kRememberLogin];
- }
- #pragma mark - Button Click
- - (IBAction)loginBtnClick:(UIButton *)sender {
- [self prepareUserData];
- // if (!self.ServerName.length) {
- // [RAUtils message_alert:@"Please choose an address in setting" title:@"Warning" controller:self];
- // return;
- // }
- //
- if (!self.user.length) {
- [RAUtils message_alert:@"User name cann't be blank" title:@"Warning" controller:self];
- return;
- }
- if (!self.pwd.length) {
- [RAUtils message_alert:@"Password cann't be blank" title:@"Warning" controller:self];
- return;
- }
- AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
- // appDelegate.address = self.address;
-
- __weak typeof(self) weakself = self;
-
- __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Please wait" completion:^{
- [RANetwork Login:weakself.user password:weakself.pwd completionHandler:^(NSMutableDictionary *result) {
- NSMutableDictionary* return_json = result;
-
- [waitalert dismissViewControllerAnimated:YES completion:^{
- int resulti=[[return_json valueForKey:@"result"] intValue];
- BOOL rememberLogin = [[weakself userDefaultsValue:kRememberLogin] boolValue];
- if(resulti==RESULT_TRUE)
- {
- NSArray *modeList = [return_json objectForKey:@"mode_list"];
- if (modeList != nil) {
-
- RAModeListViewController *rootVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"RARootViewController"];
- rootVC.title = @"RedAnt Mobile";
- rootVC.isroot = true;
- rootVC.modelist = modeList;
-
- [self.navigationController pushViewController:rootVC animated:false];
- // [appDelegate showNormalRootVCWithModeList:modeList];
- } else {
- [RAUtils message_alert:@"Something error" title:@"Warning" controller:weakself];
- }
-
- if (!rememberLogin) {
- [self setUserDefaultsValue:nil forKey:kLoginUserInfo];
- } else
- {
- [weakself setUserDefaultsValue:@{@"user" : weakself.user,@"pwd" : weakself.pwd} forKey:kLoginUserInfo];
- }
- // appDelegate.bLogin = YES;
- appDelegate.user = weakself.user;
- appDelegate.password = weakself.pwd;
- }
- else
- {
- NSString *msg = [return_json objectForKey:@"err_msg"];
- [RAUtils message_alert:msg title:@"Warning" controller:weakself];
- }
- }];
- }];
-
- }];
-
-
-
- // __weak typeof(self) weakself = self;
- //#pragma clang diagnostic push
- //#pragma clang diagnostic ignored "-Wdeprecated-declarations"
- // UIAlertView *alert = [RAUtils waiting_alert:@"Please wait" title:@"Login"];
- //#pragma clang diagnostic pop
- // dispatch_async(dispatch_get_global_queue(0, 0), ^{
- // if (weakself) {
- // __strong typeof(weakself) strongself = weakself;
- // NSDictionary *loginDic = [RANetwork Login:strongself.user password:strongself.pwd];
- // dispatch_async(dispatch_get_main_queue(), ^{
- // [alert dismissWithClickedButtonIndex:0 animated:YES];
- // int result = [[loginDic objectForKey:@"result"] intValue];
- // BOOL rememberLogin = [[strongself userDefaultsValue:kRememberLogin] boolValue];
- // if (result == RESULT_TRUE) {
- // NSArray *modeList = [loginDic objectForKey:@"mode_list"];
- // if (modeList != nil) {
- //
- // RAModeListViewController *rootVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"RARootViewController"];
- // rootVC.title = @"RedAnt Mobile";
- // rootVC.isroot = true;
- // rootVC.modelist = modeList;
- //// rootVC.onRBClick = ^(RAModeListViewController* bself){
- ////
- //// UIAlertView *alert = [RAUtils waiting_alert:@"Please wait..." title:@"Logout"];
- ////// __weak typeof(self) weakself = self;
- //// dispatch_async(dispatch_get_main_queue(), ^{
- ////
- //// NSDictionary *logoutDic = [RANetwork logout];
- //// int result = [[logoutDic objectForKey:@"result"] intValue];
- //// dispatch_async(dispatch_get_main_queue(), ^{
- //// [alert dismissWithClickedButtonIndex:0 animated:YES];
- ////// AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
- //// if (result == RESULT_TRUE) {
- ////
- //// [bself.navigationController popViewControllerAnimated:false];
- //// // weakself.companyIcon = [UIImage imageNamed:@"AppIcon40x40"];
- //// // [appDelegate logout];
- //// } else {
- //// __strong typeof(bself) strongself = bself;
- //// NSString *msg = [logoutDic objectForKey:@"err_msg"];
- //// [RAUtils message_alert:msg title:@"Warning" controller:strongself];
- //// }
- //// });
- ////
- ////
- //// });
- //// ;
- //// };
- //// rootVC.onRBName = ^NSString *{
- //// return @"Logout";
- //// };
- //// [rootVC setModeList:modeList];
- // [self.navigationController pushViewController:rootVC animated:false];
- //// [appDelegate showNormalRootVCWithModeList:modeList];
- // } else {
- // [RAUtils message_alert:@"Something error" title:@"Warning" controller:strongself];
- // }
- //
- // if (!rememberLogin) {
- // [self setUserDefaultsValue:nil forKey:kLoginUserInfo];
- // } else {
- // [strongself setUserDefaultsValue:@{@"user" : strongself.user,@"pwd" : strongself.pwd} forKey:kLoginUserInfo];
- // }
- //// appDelegate.bLogin = YES;
- // appDelegate.user = strongself.user;
- // appDelegate.password = strongself.pwd;
- //// appDelegate.companyName = [loginDic objectForKey:@"company_name"];
- //// [appDelegate loadCompanyIcon:[loginDic objectForKey:@"company_icon"]];
- //// appDelegate.modeList = [loginDic objectForKey:@"modellist"];
- //// [appDelegate showNormalRootVC];
- // } else {
- // NSString *msg = [loginDic objectForKey:@"err_msg"];
- // [RAUtils message_alert:msg title:@"Warning" controller:strongself];
- // }
- // });
- // }
- // });
- }
- - (IBAction)settingBtnClick:(UIButton *)sender {
-
- LoginSettingViewController *loginSettingVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"LoginSettingViewController"];
- __weak typeof(self) weakself = self;
- loginSettingVC.returnValue = ^(NSString *address) {
- if (weakself) {
- __strong typeof(weakself) strongself = weakself;
- strongself.ServerName = address;
- [strongself.loginTable reloadData];
- }
- };
- // UINavigationController *settingRootNav = [[UINavigationController alloc] initWithRootViewController:loginSettingVC];
- // [self presentViewController:settingRootNav animated:YES completion:nil];
- [self.navigationController pushViewController:loginSettingVC animated:false];
- }
- - (IBAction)retrieveBtnClick:(UIButton *)sender {
-
- UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Retrieve Password" message:nil preferredStyle:UIAlertControllerStyleAlert];
-
- __block UITextField *userTextField;
- [alertVC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
-
- textField.placeholder = @"user name";
- userTextField = textField;
- }];
-
- __block UITextField *emailTextField;
- [alertVC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
-
- textField.placeholder = @"email address";
- emailTextField = textField;
- }];
-
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
-
- }];
- UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
-
- NSLog(@"Retrieve Password\nuser: %@\nemail: %@",userTextField.text,emailTextField.text);
-
- }];
-
- [alertVC addAction:cancelAction];
- [alertVC addAction:okAction];
-
- [self presentViewController:alertVC animated:YES completion:nil];
-
- }
- #pragma mark - TextField Delegate
- - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
-
- return YES;
- }
- - (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
-
- return YES;
- }
- - (void)textFieldDidEndEditing:(UITextField *)textField {
- self.currentFirstResponder = nil;
- }
- - (void)textFieldDidBeginEditing:(UITextField *)textField {
- self.currentFirstResponder = textField;
- }
- @end
|