// // OfflineSettingViewController.m // iSales-NPD // // Created by Ray on 5/11/16. // Copyright © 2016 United Software Applications, Inc. All rights reserved. // #import "OfflineSettingViewController.h" #import "RANetwork.h" #import "MainViewController.h" #import "LoginViewController.h" #import "OLDataProvider.h" #import "OfflineUnlockViewController.h" //#import "SyncControlPanelViewController.h" @interface OfflineSettingViewController () @end @implementation OfflineSettingViewController - (IBAction)onAboutClick:(id)sender { } - (IBAction)onUnlockClicked:(id)sender { UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Unlock offline" message:@"Unlock offline to sync with another account. Current user data will lost!" preferredStyle:UIAlertControllerStyleAlert]; //block代码块取代了delegate UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Unlock" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; sqlite3* db = [iSalesDB get_db]; int offline_user_iscurrent=[iSalesDB get_recordcount:db table:@"offline_login" where:[NSString stringWithFormat:@"username='%@'",appDelegate.user ]]; [iSalesDB close_db:db]; if(offline_user_iscurrent>0) { self.bunlock=true; } else { OfflineUnlockViewController * unlockvc =[[UIStoryboard storyboardWithName:@"OLM" bundle:nil] instantiateViewControllerWithIdentifier:@"OfflineUnlockViewController"]; unlockvc.returnValue = ^(bool blogin){ self.bunlock=true; }; UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:unlockvc] ; // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi]; navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种 [self presentViewController:navi animated:YES completion:^{ // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320); DebugLog(@"LoginViewController present........."); // self.btop = false; // <#code#> }]; } }]; UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { DebugLog(@"No"); }]; [alertControl addAction:actionOne]; [alertControl addAction:alertthree]; //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField) [self presentViewController:alertControl animated:YES completion:nil]; } - (void)viewDidLoad { [super viewDidLoad]; self.bunlock=false; // Do any additional setup after loading the view. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults]; bool offline = [defaults boolForKey:@"EnableOfflineMode"] ; NSString* ver = [defaults stringForKey:@"OfflineVer"] ; NSString* lastsync = [defaults stringForKey:@"LastSync"] ; if(lastsync!=nil) self.label_sync.text = lastsync; if(ver==nil) self.label_ver.text=@"No offline data"; else self.label_ver.text=ver; self.switch_offline.on = offline; // self.edgesForExtendedLayout = UIRectEdgeNone ; UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic] style:UIBarButtonItemStylePlain target:self action:@selector( onCloseClick:)]; self.navigationItem.rightBarButtonItem = closeButton; } - (IBAction)OnCheckUpdate:(id)sender { UIApplication * app = [UIApplication sharedApplication]; AppDelegate *appDelegate = (AppDelegate *)[app delegate]; if(appDelegate.bLogin==false) { LoginViewController * loginvc =[ [UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"LoginViewController"]; loginvc.returnValue = ^(bool blogin){ if(blogin) { if(appDelegate.user_type==USER_ROLE_EMPLOYEE) { NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults]; NSString* ver = [defaults stringForKey:@"OfflineVer"] ; if(sender==nil) { [appDelegate check_offline:ver useInternalAddress:self.switch_internal_address.isOn parentvc:self]; // SyncControlPanelViewController * syncVC =[ [UIStoryboard storyboardWithName:@"OLM" bundle:nil] instantiateViewControllerWithIdentifier:@"SyncControlPanelViewController"]; // syncVC.action_string=@"Download"; // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:syncVC] ; // // // // // // // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi]; // // navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种 // [self presentViewController:navi animated:YES completion:^{ // // // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320); // // DebugLog(@"about present........."); // // // self.btop = false; // // <#code#> // }]; } else { UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Check offline update?" message:@"Please connect to WIFI network and plug charging cable, KEEP SCREEN ON AND KEEP APP RUNNING FOREGROUND, download data will take a while." preferredStyle:UIAlertControllerStyleAlert]; //block代码块取代了delegate UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [appDelegate check_offline:ver useInternalAddress:self.switch_internal_address.isOn parentvc:self]; // SyncControlPanelViewController * syncVC =[ [UIStoryboard storyboardWithName:@"OLM" bundle:nil] instantiateViewControllerWithIdentifier:@"SyncControlPanelViewController"]; // syncVC.action_string=@"Download"; // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:syncVC] ; // // // // // // // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi]; // // navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种 // [self presentViewController:navi animated:YES completion:^{ // // // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320); // // DebugLog(@"about present........."); // // // self.btop = false; // // <#code#> // }]; }]; UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]; [alertControl addAction:actionTwo]; [alertControl addAction:alertthree]; [self presentViewController:alertControl animated:YES completion:nil]; } } else { } } else { [RAUtils message_box:@"Data sync" message:@"Must login" completion:nil]; // [RAUtils alert_view:@"Must login" title:@"Data sync"]; } }; UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ; // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi]; navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种 [self presentViewController:navi animated:YES completion:^{ // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320); DebugLog(@"LoginViewController present........."); // self.btop = false; // <#code#> }]; } else { // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; sqlite3* db = [iSalesDB get_db]; int offline_user_iscurrent=[iSalesDB get_recordcount:db table:@"offline_login" where:[NSString stringWithFormat:@"username='%@'",appDelegate.user ]]; [iSalesDB close_db:db]; NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults]; NSString* ver = [defaults stringForKey:@"OfflineVer"] ; if(offline_user_iscurrent==0 && ver!=nil) { if(!self.bunlock) { [RAUtils message_box:@"Sync denied." message:@"Current login account is different from the offline account, to continue with sync, all current offline data will be erased, you must unlock offline lock first. " completion:nil]; return; } else { ver=nil; NSString *clearTableSQL = @"delete from offline_contact;delete from offline_order;delete from offline_cart;"; [iSalesDB execSql:clearTableSQL]; } } else if (ver && self.bunlock) { ver = nil; } if(sender==nil) { [appDelegate check_offline:ver useInternalAddress:self.switch_internal_address.isOn parentvc:self]; } else { UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Check offline update?" message:@"Please connect to WIFI network and plug charging cable, KEEP SCREEN ON AND KEEP APP RUNNING FOREGROUND, download data will take a while." preferredStyle:UIAlertControllerStyleAlert]; //block代码块取代了delegate UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [appDelegate check_offline:ver useInternalAddress:self.switch_internal_address.isOn parentvc:self]; }]; UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]; [alertControl addAction:actionTwo]; [alertControl addAction:alertthree]; [self presentViewController:alertControl animated:YES completion:nil]; } } } - (void)onCloseClick:(UIButton *)sender { // if(self.onDismissVC) // self.onDismissVC(); [self dismissViewControllerAnimated:true completion:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)onChangeOfflineSwitch:(id)sender { UIApplication * app = [UIApplication sharedApplication]; AppDelegate *appDelegate = (AppDelegate *)[app delegate]; if(self.switch_offline.isOn==false) { //disable offline if(appDelegate.bLogin) { // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Sign out" completion:^{ PopWaitAlert* pop = [RAUtils waiting_pop:@"Sign out" completion:nil]; [RANetwork request_logout:^(NSMutableDictionary *result) { NSMutableDictionary* return_json=result ; [pop hide]; // [waitalert dismissViewControllerAnimated:YES completion:^{ if([[return_json valueForKey:@"result"] intValue]==2) { [appDelegate Logout]; NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults]; [defaults removeObjectForKey:@"EnableOfflineMode"]; [defaults setBool:self.switch_offline.isOn forKey:@"EnableOfflineMode"]; [defaults synchronize]; #ifdef OFFLINE_MODE appDelegate.offline_mode =self.switch_offline.isOn; #endif #ifdef RA_NOTIFICATION [ActiveViewController Notify:@"PortfolioViewController,WatchListViewController,CategoryViewController,PDFListViewController,CartViewController,OrderListViewController,ContactListViewController" Message:RA_NOTIFICATION_RELOAD_DATA]; #else [((MainViewController*)appDelegate.main_vc) reloadPortfolio:true immediately:false]; [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false]; [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false]; [((MainViewController*)appDelegate.main_vc) reloadDocuments:true immediately:false]; [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false]; // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false]; [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false]; [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false]; // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false]; #endif [self dismissViewControllerAnimated:true completion:^{ [((MainViewController*)appDelegate.main_vc) switchToHome]; }]; } else { // [RAUtils message_box:@"Sign Out" message:[return_json valueForKey:@"err_msg"] completion:nil] ; } // }]; }]; // }]; } else { NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults]; [defaults removeObjectForKey:@"EnableOfflineMode"]; [defaults setBool:self.switch_offline.isOn forKey:@"EnableOfflineMode"]; [defaults synchronize]; #ifdef OFFLINE_MODE appDelegate.offline_mode =self.switch_offline.isOn; #endif #ifdef RA_NOTIFICATION [ActiveViewController Notify:@"PortfolioViewController,WatchListViewController,CategoryViewController,PDFListViewController,CartViewController,OrderListViewController,ContactListViewController" Message:RA_NOTIFICATION_RELOAD_DATA]; #else [((MainViewController*)appDelegate.main_vc) reloadPortfolio:true immediately:false]; [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false]; [((MainViewController*)appDelegate.main_vc) reloadDocuments:true immediately:false]; [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false]; [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false]; // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false]; [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false]; [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false]; // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false]; #endif [self dismissViewControllerAnimated:true completion:^{ [((MainViewController*)appDelegate.main_vc) switchToHome]; }]; } } else { bool haveofflineData=[OLDataProvider check_offlinedata]; if(haveofflineData) { // have offline data if(appDelegate.bLogin) { // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Sign out" completion:^{ PopWaitAlert* pop = [RAUtils waiting_pop:@"Sign out" completion:nil]; [RANetwork request_logout:^(NSMutableDictionary *result) { NSMutableDictionary* return_json = result; [pop hide]; // [waitalert dismissViewControllerAnimated:YES completion:^{ if([[return_json valueForKey:@"result"] intValue]==2) { [appDelegate Logout]; NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults]; [defaults removeObjectForKey:@"EnableOfflineMode"]; [defaults setBool:self.switch_offline.isOn forKey:@"EnableOfflineMode"]; [defaults synchronize]; #ifdef OFFLINE_MODE appDelegate.offline_mode =self.switch_offline.isOn; #endif #ifdef RA_NOTIFICATION [ActiveViewController Notify:@"PortfolioViewController,WatchListViewController,CategoryViewController,PDFListViewController,CartViewController,OrderListViewController,ContactListViewController" Message:RA_NOTIFICATION_RELOAD_DATA]; #else [((MainViewController*)appDelegate.main_vc) reloadPortfolio:true immediately:false]; [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false]; [((MainViewController*)appDelegate.main_vc) reloadDocuments:true immediately:false]; [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false]; [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false]; // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false]; [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false]; [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false]; // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false]; #endif [self dismissViewControllerAnimated:true completion:^{ [((MainViewController*)appDelegate.main_vc) switchToHome]; }]; } else { // [RAUtils message_box:@"Sign Out" message:[return_json valueForKey:@"err_msg"] completion:nil] ; } // }]; }]; // }]; } } else { //no offline data download form server first. // restore switch state. [self.switch_offline setOn:!self.switch_offline.isOn]; UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Offline data not found, download it from server?" message:@"Please connect to WIFI network and plug charging cable, KEEP SCREEN ON AND KEEP APP RUNNING FOREGROUND, download data will take a while." preferredStyle:UIAlertControllerStyleAlert]; //block代码块取代了delegate UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [self OnCheckUpdate:nil]; }]; UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { // [self.switch_offline setOn:false]; }]; [alertControl addAction:actionTwo]; [alertControl addAction:alertthree]; //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField) [self presentViewController:alertControl animated:YES completion:nil]; } } } -(void) download_file:(NSString*)url type:(NSString*)type { } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end