OfflineSettingViewController.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. //
  2. // OfflineSettingViewController.m
  3. // iSales-NPD
  4. //
  5. // Created by Ray on 5/11/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "OfflineSettingViewController.h"
  9. #import "RANetwork.h"
  10. #import "MainViewController.h"
  11. #import "LoginViewController.h"
  12. #import "OLDataProvider.h"
  13. #import "OfflineUnlockViewController.h"
  14. //#import "SyncControlPanelViewController.h"
  15. @interface OfflineSettingViewController ()
  16. @end
  17. @implementation OfflineSettingViewController
  18. - (IBAction)onUnlockClicked:(id)sender {
  19. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Unlock offline" message:@"Unlock offline to sync with another account. Current user data will lost!" preferredStyle:UIAlertControllerStyleAlert];
  20. //block代码块取代了delegate
  21. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Unlock" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  22. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  23. sqlite3* db = [iSalesDB get_db];
  24. int offline_user_iscurrent=[iSalesDB get_recordcount:db table:@"offline_login" where:[NSString stringWithFormat:@"username='%@'",appDelegate.user ]];
  25. [iSalesDB close_db:db];
  26. if(offline_user_iscurrent>0)
  27. {
  28. self.bunlock=true;
  29. }
  30. else
  31. {
  32. OfflineUnlockViewController * unlockvc =[[UIStoryboard storyboardWithName:@"OLM" bundle:nil] instantiateViewControllerWithIdentifier:@"OfflineUnlockViewController"];
  33. unlockvc.returnValue = ^(bool blogin){
  34. self.bunlock=true;
  35. };
  36. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:unlockvc] ;
  37. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  38. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  39. [self presentViewController:navi animated:YES completion:^{
  40. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  41. DebugLog(@"LoginViewController present.........");
  42. // self.btop = false;
  43. // <#code#>
  44. }];
  45. }
  46. // UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Are you sure remove all models from cart?" message:nil preferredStyle:UIAlertControllerStyleAlert];
  47. // //block代码块取代了delegate
  48. //
  49. //
  50. //
  51. // UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  52. // UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Remove Models From Cart"];
  53. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  54. //
  55. // NSDictionary* cart_json = [iSalesNetwork cart_remove:ids];
  56. //
  57. // dispatch_async(dispatch_get_main_queue(), ^{
  58. //
  59. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  60. //
  61. // if([[cart_json valueForKey:@"result"] intValue]==2)
  62. // {
  63. //
  64. // [self end_edit];
  65. // AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  66. // [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:true];
  67. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:true];
  68. // }
  69. // else
  70. // {
  71. // [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Delete Model" controller:self] ;
  72. // }
  73. //
  74. //
  75. //
  76. // });
  77. // });
  78. //
  79. //
  80. // }];
  81. //
  82. // UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  83. // DebugLog(@"No");
  84. // }];
  85. // [alertControl addAction:actionOne];
  86. //
  87. // [alertControl addAction:alertthree];
  88. //
  89. // //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  90. //
  91. //
  92. // [self presentViewController:alertControl animated:YES completion:nil];
  93. //
  94. // DebugLog(@"%@",self.edit_select_arr);
  95. //
  96. //
  97. }];
  98. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  99. DebugLog(@"No");
  100. }];
  101. [alertControl addAction:actionOne];
  102. [alertControl addAction:alertthree];
  103. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  104. [self presentViewController:alertControl animated:YES completion:nil];
  105. }
  106. - (void)viewDidLoad {
  107. [super viewDidLoad];
  108. self.bunlock=false;
  109. // Do any additional setup after loading the view.
  110. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  111. bool offline = [defaults boolForKey:@"EnableOfflineMode"] ;
  112. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  113. NSString* lastsync = [defaults stringForKey:@"LastSync"] ;
  114. if(lastsync!=nil)
  115. self.label_sync.text = lastsync;
  116. if(ver==nil)
  117. self.label_ver.text=@"No offline data";
  118. else
  119. self.label_ver.text=ver;
  120. self.switch_offline.on = offline;
  121. self.edgesForExtendedLayout = UIRectEdgeNone ;
  122. UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  123. style:UIBarButtonItemStylePlain
  124. target:self
  125. action:@selector( onCloseClick:)];
  126. self.navigationItem.rightBarButtonItem = closeButton;
  127. }
  128. - (IBAction)OnCheckUpdate:(id)sender {
  129. UIApplication * app = [UIApplication sharedApplication];
  130. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  131. if(appDelegate.bLogin==false)
  132. {
  133. LoginViewController * loginvc =[ [UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"LoginViewController"];
  134. loginvc.returnValue = ^(bool blogin){
  135. if(blogin)
  136. {
  137. if(appDelegate.user_type==USER_ROLE_EMPLOYEE)
  138. {
  139. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  140. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  141. if(sender==nil)
  142. {
  143. [appDelegate check_offline:ver useInternalAddress:self.switch_internal_address.isOn parentvc:self];
  144. // SyncControlPanelViewController * syncVC =[ [UIStoryboard storyboardWithName:@"OLM" bundle:nil] instantiateViewControllerWithIdentifier:@"SyncControlPanelViewController"];
  145. // syncVC.action_string=@"Download";
  146. // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:syncVC] ;
  147. //
  148. //
  149. //
  150. //
  151. //
  152. // // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  153. //
  154. // navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  155. // [self presentViewController:navi animated:YES completion:^{
  156. //
  157. // // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  158. //
  159. // DebugLog(@"about present.........");
  160. //
  161. // // self.btop = false;
  162. // // <#code#>
  163. // }];
  164. }
  165. else
  166. {
  167. 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];
  168. //block代码块取代了delegate
  169. UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  170. [appDelegate check_offline:ver useInternalAddress:self.switch_internal_address.isOn parentvc:self];
  171. // SyncControlPanelViewController * syncVC =[ [UIStoryboard storyboardWithName:@"OLM" bundle:nil] instantiateViewControllerWithIdentifier:@"SyncControlPanelViewController"];
  172. // syncVC.action_string=@"Download";
  173. // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:syncVC] ;
  174. //
  175. //
  176. //
  177. //
  178. //
  179. // // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  180. //
  181. // navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  182. // [self presentViewController:navi animated:YES completion:^{
  183. //
  184. // // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  185. //
  186. // DebugLog(@"about present.........");
  187. //
  188. // // self.btop = false;
  189. // // <#code#>
  190. // }];
  191. }];
  192. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  193. }];
  194. [alertControl addAction:actionTwo];
  195. [alertControl addAction:alertthree];
  196. [self presentViewController:alertControl animated:YES completion:nil];
  197. }
  198. }
  199. else
  200. {
  201. }
  202. }
  203. else
  204. {
  205. [RAUtils message_alert:@"Must login" title:@"Data sync" controller:self];
  206. // [RAUtils alert_view:@"Must login" title:@"Data sync"];
  207. }
  208. };
  209. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  210. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  211. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  212. [self presentViewController:navi animated:YES completion:^{
  213. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  214. DebugLog(@"LoginViewController present.........");
  215. // self.btop = false;
  216. // <#code#>
  217. }];
  218. }
  219. else
  220. {
  221. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  222. sqlite3* db = [iSalesDB get_db];
  223. int offline_user_iscurrent=[iSalesDB get_recordcount:db table:@"offline_login" where:[NSString stringWithFormat:@"username='%@'",appDelegate.user ]];
  224. [iSalesDB close_db:db];
  225. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  226. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  227. if(offline_user_iscurrent==0 && ver!=nil)
  228. {
  229. if(!self.bunlock)
  230. {
  231. [RAUtils message_alert:@"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. " title:@"Sync denied." controller:self];
  232. // [RAUtils alert_view:@"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. " title:@"Sync denied."];
  233. return;
  234. }
  235. else
  236. {
  237. ver=nil;
  238. NSString *clearTableSQL = @"delete from offline_contact;delete from offline_order;delete from offline_cart;";
  239. [iSalesDB execSql:clearTableSQL];
  240. }
  241. } else if (ver && self.bunlock) {
  242. ver = nil;
  243. }
  244. if(sender==nil)
  245. {
  246. [appDelegate check_offline:ver useInternalAddress:self.switch_internal_address.isOn parentvc:self];
  247. // SyncControlPanelViewController * syncVC =[ [UIStoryboard storyboardWithName:@"OLM" bundle:nil] instantiateViewControllerWithIdentifier:@"SyncControlPanelViewController"];
  248. // syncVC.action_string=@"Download";
  249. // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:syncVC] ;
  250. //
  251. //
  252. //
  253. //
  254. //
  255. // // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  256. //
  257. // navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  258. // [self presentViewController:navi animated:YES completion:^{
  259. //
  260. // // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  261. //
  262. // DebugLog(@"about present.........");
  263. //
  264. // // self.btop = false;
  265. // // <#code#>
  266. // }];
  267. }
  268. else
  269. {
  270. 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];
  271. //block代码块取代了delegate
  272. UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  273. [appDelegate check_offline:ver useInternalAddress:self.switch_internal_address.isOn parentvc:self];
  274. }];
  275. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  276. }];
  277. [alertControl addAction:actionTwo];
  278. [alertControl addAction:alertthree];
  279. [self presentViewController:alertControl animated:YES completion:nil];
  280. }
  281. }
  282. }
  283. - (void)onCloseClick:(UIButton *)sender {
  284. // if(self.onDismissVC)
  285. // self.onDismissVC();
  286. [self dismissViewControllerAnimated:true completion:nil];
  287. }
  288. - (void)didReceiveMemoryWarning {
  289. [super didReceiveMemoryWarning];
  290. // Dispose of any resources that can be recreated.
  291. }
  292. - (IBAction)onChangeOfflineSwitch:(id)sender {
  293. UIApplication * app = [UIApplication sharedApplication];
  294. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  295. if(self.switch_offline.isOn==false)
  296. {
  297. //disable offline
  298. if(appDelegate.bLogin)
  299. {
  300. UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Sign out"];
  301. [RANetwork request_logout:^(NSMutableDictionary *result) {
  302. NSMutableDictionary* return_json=result ;
  303. [waitalert dismissViewControllerAnimated:YES completion:nil];
  304. if([[return_json valueForKey:@"result"] intValue]==2)
  305. {
  306. [appDelegate Logout];
  307. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  308. [defaults removeObjectForKey:@"EnableOfflineMode"];
  309. [defaults setBool:self.switch_offline.isOn forKey:@"EnableOfflineMode"];
  310. [defaults synchronize];
  311. #ifdef OFFLINE_MODE
  312. appDelegate.offline_mode =self.switch_offline.isOn;
  313. #endif
  314. #ifdef RA_NOTIFICATION
  315. [ActiveViewController Notify:@"PortfolioViewController,WatchListViewController,CategoryViewController,PDFListViewController,CartViewController,OrderListViewController,ContactListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  316. #else
  317. [((MainViewController*)appDelegate.main_vc) reloadPortfolio:true immediately:false];
  318. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  319. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  320. [((MainViewController*)appDelegate.main_vc) reloadDocuments:true immediately:false];
  321. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  322. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  323. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  324. [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false];
  325. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  326. #endif
  327. [((MainViewController*)appDelegate.main_vc) switchToHome];
  328. }
  329. else
  330. {
  331. // [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Sign Out" controller:self] ;
  332. }
  333. }];
  334. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  335. //
  336. // NSDictionary* return_json = [RANetwork logout];
  337. //
  338. // dispatch_async(dispatch_get_main_queue(), ^{
  339. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  340. //
  341. //
  342. // if([[return_json valueForKey:@"result"] intValue]==2)
  343. // {
  344. //
  345. // [appDelegate Logout];
  346. // NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  347. // [defaults removeObjectForKey:@"EnableOfflineMode"];
  348. // [defaults setBool:self.switch_offline.isOn forKey:@"EnableOfflineMode"];
  349. //
  350. // [defaults synchronize];
  351. //#ifdef OFFLINE_MODE
  352. // appDelegate.offline_mode =self.switch_offline.isOn;
  353. //#endif
  354. //
  355. //#ifdef RA_NOTIFICATION
  356. //
  357. // [ActiveViewController Notify:@"PortfolioViewController,WatchListViewController,CategoryViewController,PDFListViewController,CartViewController,OrderListViewController,ContactListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  358. //#else
  359. // [((MainViewController*)appDelegate.main_vc) reloadPortfolio:true immediately:false];
  360. // [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  361. //
  362. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  363. // [((MainViewController*)appDelegate.main_vc) reloadDocuments:true immediately:false];
  364. // [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  365. //
  366. //// [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  367. //
  368. // [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  369. // [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false];
  370. //// [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  371. //
  372. //
  373. //#endif
  374. //
  375. //
  376. //
  377. // [((MainViewController*)appDelegate.main_vc) switchToHome];
  378. // }
  379. // else
  380. // {
  381. // // [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Sign Out" controller:self] ;
  382. // }
  383. //
  384. //
  385. //
  386. //
  387. // });
  388. // });
  389. }
  390. else
  391. {
  392. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  393. [defaults removeObjectForKey:@"EnableOfflineMode"];
  394. [defaults setBool:self.switch_offline.isOn forKey:@"EnableOfflineMode"];
  395. [defaults synchronize];
  396. #ifdef OFFLINE_MODE
  397. appDelegate.offline_mode =self.switch_offline.isOn;
  398. #endif
  399. #ifdef RA_NOTIFICATION
  400. [ActiveViewController Notify:@"PortfolioViewController,WatchListViewController,CategoryViewController,PDFListViewController,CartViewController,OrderListViewController,ContactListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  401. #else
  402. [((MainViewController*)appDelegate.main_vc) reloadPortfolio:true immediately:false];
  403. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  404. [((MainViewController*)appDelegate.main_vc) reloadDocuments:true immediately:false];
  405. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  406. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  407. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  408. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  409. [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false];
  410. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  411. #endif
  412. [((MainViewController*)appDelegate.main_vc) switchToHome];
  413. }
  414. }
  415. else
  416. {
  417. bool haveofflineData=[OLDataProvider check_offlinedata];
  418. if(haveofflineData)
  419. {
  420. // have offline data
  421. if(appDelegate.bLogin)
  422. {
  423. UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Sign out"];
  424. [RANetwork request_logout:^(NSMutableDictionary *result) {
  425. NSMutableDictionary* return_json = result;
  426. [waitalert dismissViewControllerAnimated:YES completion:nil];
  427. if([[return_json valueForKey:@"result"] intValue]==2)
  428. {
  429. [appDelegate Logout];
  430. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  431. [defaults removeObjectForKey:@"EnableOfflineMode"];
  432. [defaults setBool:self.switch_offline.isOn forKey:@"EnableOfflineMode"];
  433. [defaults synchronize];
  434. #ifdef OFFLINE_MODE
  435. appDelegate.offline_mode =self.switch_offline.isOn;
  436. #endif
  437. #ifdef RA_NOTIFICATION
  438. [ActiveViewController Notify:@"PortfolioViewController,WatchListViewController,CategoryViewController,PDFListViewController,CartViewController,OrderListViewController,ContactListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  439. #else
  440. [((MainViewController*)appDelegate.main_vc) reloadPortfolio:true immediately:false];
  441. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  442. [((MainViewController*)appDelegate.main_vc) reloadDocuments:true immediately:false];
  443. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  444. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  445. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  446. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  447. [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false];
  448. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  449. #endif
  450. [((MainViewController*)appDelegate.main_vc) switchToHome];
  451. }
  452. else
  453. {
  454. // [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Sign Out" controller:self] ;
  455. }
  456. }];
  457. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  458. //
  459. // NSDictionary* return_json = [RANetwork logout];
  460. //
  461. // dispatch_async(dispatch_get_main_queue(), ^{
  462. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  463. //
  464. //
  465. // if([[return_json valueForKey:@"result"] intValue]==2)
  466. // {
  467. //
  468. // [appDelegate Logout];
  469. //
  470. // NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  471. // [defaults removeObjectForKey:@"EnableOfflineMode"];
  472. // [defaults setBool:self.switch_offline.isOn forKey:@"EnableOfflineMode"];
  473. //
  474. // [defaults synchronize];
  475. //#ifdef OFFLINE_MODE
  476. // appDelegate.offline_mode =self.switch_offline.isOn;
  477. //#endif
  478. //
  479. //#ifdef RA_NOTIFICATION
  480. // [ActiveViewController Notify:@"PortfolioViewController,WatchListViewController,CategoryViewController,PDFListViewController,CartViewController,OrderListViewController,ContactListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  481. //#else
  482. // [((MainViewController*)appDelegate.main_vc) reloadPortfolio:true immediately:false];
  483. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  484. // [((MainViewController*)appDelegate.main_vc) reloadDocuments:true immediately:false];
  485. // [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  486. // [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  487. //// [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  488. // [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  489. // [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false];
  490. //// [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  491. //#endif
  492. //
  493. //
  494. //
  495. //
  496. // [((MainViewController*)appDelegate.main_vc) switchToHome];
  497. // }
  498. // else
  499. // {
  500. // // [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Sign Out" controller:self] ;
  501. // }
  502. //
  503. //
  504. //
  505. //
  506. // });
  507. // });
  508. }
  509. }
  510. else
  511. {
  512. //no offline data download form server first.
  513. // restore switch state.
  514. [self.switch_offline setOn:!self.switch_offline.isOn];
  515. 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];
  516. //block代码块取代了delegate
  517. UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  518. [self OnCheckUpdate:nil];
  519. }];
  520. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  521. // [self.switch_offline setOn:false];
  522. //[RAUtils message_alert:@"No item in the cart" title:@"Offline" controller:self] ;
  523. }];
  524. [alertControl addAction:actionTwo];
  525. [alertControl addAction:alertthree];
  526. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  527. [self presentViewController:alertControl animated:YES completion:nil];
  528. }
  529. //
  530. // UIApplication * app = [UIApplication sharedApplication];
  531. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  532. //
  533. // NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  534. // //
  535. // // [appDelegate check_offline:ver];
  536. // // return;
  537. //
  538. // // if(ver.length==0)
  539. // {
  540. //
  541. // UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Check offline update?" message:nil preferredStyle:UIAlertControllerStyleAlert];
  542. // //block代码块取代了delegate
  543. //
  544. //
  545. // UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  546. // // UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Checking offline data"];
  547. // //init offline mode
  548. // // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  549. // //
  550. // //
  551. // // // [self check_offline];
  552. // // });
  553. // [appDelegate check_offline:ver];
  554. //
  555. //
  556. //
  557. // }];
  558. //
  559. // UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  560. // // [self.switch_offline setOn:false];
  561. // //[RAUtils message_alert:@"No item in the cart" title:@"Offline" controller:self] ;
  562. // }];
  563. //
  564. // [alertControl addAction:actionTwo];
  565. // [alertControl addAction:alertthree];
  566. //
  567. // //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  568. //
  569. //
  570. // [self presentViewController:alertControl animated:YES completion:nil];
  571. //
  572. //
  573. //
  574. //
  575. //
  576. //
  577. //
  578. //
  579. // }
  580. // else
  581. // {
  582. //
  583. // }
  584. }
  585. }
  586. -(void) download_file:(NSString*)url type:(NSString*)type
  587. {
  588. }
  589. /*
  590. #pragma mark - Navigation
  591. // In a storyboard-based application, you will often want to do a little preparation before navigation
  592. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  593. // Get the new view controller using [segue destinationViewController].
  594. // Pass the selected object to the new view controller.
  595. }
  596. */
  597. @end