| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535 |
- //
- // OfflineSettingViewController.m
- // iSales-NPD
- //
- // Created by Ray on 5/11/16.
- // Copyright © 2016 United Software Applications, Inc. All rights reserved.
- //
- #import "OfflineSettingViewController.h"
- #import "iSalesNetwork.h"
- #import "MainViewController.h"
- #import "LoginViewController.h"
- #import "OLDataProvider.h"
- #import "SyncControlPanelViewController.h"
- @interface OfflineSettingViewController ()
- @end
- @implementation OfflineSettingViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // 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);
-
- NSLog(@"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);
-
- NSLog(@"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 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);
-
- NSLog(@"LoginViewController present.........");
-
- // self.btop = false;
- // <#code#>
- }];
- }
- else
- {
- 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);
-
- NSLog(@"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);
-
- NSLog(@"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];
- }
-
-
-
- }
-
-
-
-
-
-
-
-
-
- }
- - (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)
- {
- UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Sign out"];
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
-
- NSDictionary* return_json = [iSalesNetwork logout];
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
-
-
- 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];
- appDelegate.offline_mode =self.switch_offline.isOn;
-
- [((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) reloadPortfolio: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];
-
-
- [((MainViewController*)appDelegate.main_vc) switchToHome];
- }
- else
- {
- // [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Sign Out" controller:self] ;
- }
-
-
-
-
- });
- });
- }
- else
- {
- NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
- [defaults removeObjectForKey:@"EnableOfflineMode"];
- [defaults setBool:self.switch_offline.isOn forKey:@"EnableOfflineMode"];
-
- [defaults synchronize];
- appDelegate.offline_mode =self.switch_offline.isOn;
-
- [((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) reloadPortfolio: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];
-
-
- [((MainViewController*)appDelegate.main_vc) switchToHome];
- }
- }
- else
- {
- bool haveofflineData=[OLDataProvider check_offlinedata];
- if(haveofflineData)
- {
- // have offline data
- if(appDelegate.bLogin)
- {
- UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Sign out"];
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
-
- NSDictionary* return_json = [iSalesNetwork logout];
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
-
-
- 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];
- appDelegate.offline_mode =self.switch_offline.isOn;
-
- [((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) reloadPortfolio: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];
-
-
- [((MainViewController*)appDelegate.main_vc) switchToHome];
- }
- else
- {
- // [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Sign Out" controller:self] ;
- }
-
-
-
-
- });
- });
- }
- }
- 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];
- //[RAUtils message_alert:@"No item in the cart" title:@"Offline" controller:self] ;
- }];
-
- [alertControl addAction:actionTwo];
- [alertControl addAction:alertthree];
-
- //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
-
-
- [self presentViewController:alertControl animated:YES completion:nil];
- }
-
- //
- // UIApplication * app = [UIApplication sharedApplication];
- // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
- //
- // NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
- // //
- // // [appDelegate check_offline:ver];
- // // return;
- //
- // // if(ver.length==0)
- // {
- //
- // UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Check offline update?" message:nil preferredStyle:UIAlertControllerStyleAlert];
- // //block代码块取代了delegate
- //
- //
- // UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
- // // UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Checking offline data"];
- // //init offline mode
- // // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- // //
- // //
- // // // [self check_offline];
- // // });
- // [appDelegate check_offline:ver];
- //
- //
- //
- // }];
- //
- // UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
- // // [self.switch_offline setOn:false];
- // //[RAUtils message_alert:@"No item in the cart" title:@"Offline" controller:self] ;
- // }];
- //
- // [alertControl addAction:actionTwo];
- // [alertControl addAction:alertthree];
- //
- // //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
- //
- //
- // [self presentViewController:alertControl animated:YES completion:nil];
- //
- //
- //
- //
- //
- //
- //
- //
- // }
- // else
- // {
- //
- // }
- }
- }
- -(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
|