| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607 |
- //
- // 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 = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
- [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 = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
- // [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 = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
- // [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 = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
- [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
|