OfflineSettingViewController.m 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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. __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Sign out" completion:^{
  301. [RANetwork request_logout:^(NSMutableDictionary *result) {
  302. NSMutableDictionary* return_json=result ;
  303. [waitalert dismissViewControllerAnimated:YES completion:^{
  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. [self dismissViewControllerAnimated:true completion:^{
  328. [((MainViewController*)appDelegate.main_vc) switchToHome];
  329. }];
  330. }
  331. else
  332. {
  333. // [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Sign Out" controller:self] ;
  334. }
  335. }];
  336. }];
  337. }];
  338. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  339. //
  340. // NSDictionary* return_json = [RANetwork logout];
  341. //
  342. // dispatch_async(dispatch_get_main_queue(), ^{
  343. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  344. //
  345. //
  346. // if([[return_json valueForKey:@"result"] intValue]==2)
  347. // {
  348. //
  349. // [appDelegate Logout];
  350. // NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  351. // [defaults removeObjectForKey:@"EnableOfflineMode"];
  352. // [defaults setBool:self.switch_offline.isOn forKey:@"EnableOfflineMode"];
  353. //
  354. // [defaults synchronize];
  355. //#ifdef OFFLINE_MODE
  356. // appDelegate.offline_mode =self.switch_offline.isOn;
  357. //#endif
  358. //
  359. //#ifdef RA_NOTIFICATION
  360. //
  361. // [ActiveViewController Notify:@"PortfolioViewController,WatchListViewController,CategoryViewController,PDFListViewController,CartViewController,OrderListViewController,ContactListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  362. //#else
  363. // [((MainViewController*)appDelegate.main_vc) reloadPortfolio:true immediately:false];
  364. // [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  365. //
  366. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  367. // [((MainViewController*)appDelegate.main_vc) reloadDocuments:true immediately:false];
  368. // [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  369. //
  370. //// [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  371. //
  372. // [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  373. // [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false];
  374. //// [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  375. //
  376. //
  377. //#endif
  378. //
  379. //
  380. //
  381. // [((MainViewController*)appDelegate.main_vc) switchToHome];
  382. // }
  383. // else
  384. // {
  385. // // [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Sign Out" controller:self] ;
  386. // }
  387. //
  388. //
  389. //
  390. //
  391. // });
  392. // });
  393. }
  394. else
  395. {
  396. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  397. [defaults removeObjectForKey:@"EnableOfflineMode"];
  398. [defaults setBool:self.switch_offline.isOn forKey:@"EnableOfflineMode"];
  399. [defaults synchronize];
  400. #ifdef OFFLINE_MODE
  401. appDelegate.offline_mode =self.switch_offline.isOn;
  402. #endif
  403. #ifdef RA_NOTIFICATION
  404. [ActiveViewController Notify:@"PortfolioViewController,WatchListViewController,CategoryViewController,PDFListViewController,CartViewController,OrderListViewController,ContactListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  405. #else
  406. [((MainViewController*)appDelegate.main_vc) reloadPortfolio:true immediately:false];
  407. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  408. [((MainViewController*)appDelegate.main_vc) reloadDocuments:true immediately:false];
  409. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  410. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  411. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  412. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  413. [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false];
  414. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  415. #endif
  416. [self dismissViewControllerAnimated:true completion:^{
  417. [((MainViewController*)appDelegate.main_vc) switchToHome];
  418. }];
  419. }
  420. }
  421. else
  422. {
  423. bool haveofflineData=[OLDataProvider check_offlinedata];
  424. if(haveofflineData)
  425. {
  426. // have offline data
  427. if(appDelegate.bLogin)
  428. {
  429. __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Sign out" completion:^{
  430. [RANetwork request_logout:^(NSMutableDictionary *result) {
  431. NSMutableDictionary* return_json = result;
  432. [waitalert dismissViewControllerAnimated:YES completion:^{
  433. if([[return_json valueForKey:@"result"] intValue]==2)
  434. {
  435. [appDelegate Logout];
  436. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  437. [defaults removeObjectForKey:@"EnableOfflineMode"];
  438. [defaults setBool:self.switch_offline.isOn forKey:@"EnableOfflineMode"];
  439. [defaults synchronize];
  440. #ifdef OFFLINE_MODE
  441. appDelegate.offline_mode =self.switch_offline.isOn;
  442. #endif
  443. #ifdef RA_NOTIFICATION
  444. [ActiveViewController Notify:@"PortfolioViewController,WatchListViewController,CategoryViewController,PDFListViewController,CartViewController,OrderListViewController,ContactListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  445. #else
  446. [((MainViewController*)appDelegate.main_vc) reloadPortfolio:true immediately:false];
  447. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  448. [((MainViewController*)appDelegate.main_vc) reloadDocuments:true immediately:false];
  449. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  450. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  451. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  452. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  453. [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false];
  454. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  455. #endif
  456. [self dismissViewControllerAnimated:true completion:^{
  457. [((MainViewController*)appDelegate.main_vc) switchToHome];
  458. }];
  459. }
  460. else
  461. {
  462. // [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Sign Out" controller:self] ;
  463. }
  464. }];
  465. }];
  466. }];
  467. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  468. //
  469. // NSDictionary* return_json = [RANetwork logout];
  470. //
  471. // dispatch_async(dispatch_get_main_queue(), ^{
  472. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  473. //
  474. //
  475. // if([[return_json valueForKey:@"result"] intValue]==2)
  476. // {
  477. //
  478. // [appDelegate Logout];
  479. //
  480. // NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  481. // [defaults removeObjectForKey:@"EnableOfflineMode"];
  482. // [defaults setBool:self.switch_offline.isOn forKey:@"EnableOfflineMode"];
  483. //
  484. // [defaults synchronize];
  485. //#ifdef OFFLINE_MODE
  486. // appDelegate.offline_mode =self.switch_offline.isOn;
  487. //#endif
  488. //
  489. //#ifdef RA_NOTIFICATION
  490. // [ActiveViewController Notify:@"PortfolioViewController,WatchListViewController,CategoryViewController,PDFListViewController,CartViewController,OrderListViewController,ContactListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  491. //#else
  492. // [((MainViewController*)appDelegate.main_vc) reloadPortfolio:true immediately:false];
  493. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  494. // [((MainViewController*)appDelegate.main_vc) reloadDocuments:true immediately:false];
  495. // [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  496. // [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  497. //// [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  498. // [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  499. // [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false];
  500. //// [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  501. //#endif
  502. //
  503. //
  504. //
  505. //
  506. // [((MainViewController*)appDelegate.main_vc) switchToHome];
  507. // }
  508. // else
  509. // {
  510. // // [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Sign Out" controller:self] ;
  511. // }
  512. //
  513. //
  514. //
  515. //
  516. // });
  517. // });
  518. }
  519. }
  520. else
  521. {
  522. //no offline data download form server first.
  523. // restore switch state.
  524. [self.switch_offline setOn:!self.switch_offline.isOn];
  525. 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];
  526. //block代码块取代了delegate
  527. UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  528. [self OnCheckUpdate:nil];
  529. }];
  530. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  531. // [self.switch_offline setOn:false];
  532. //[RAUtils message_alert:@"No item in the cart" title:@"Offline" controller:self] ;
  533. }];
  534. [alertControl addAction:actionTwo];
  535. [alertControl addAction:alertthree];
  536. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  537. [self presentViewController:alertControl animated:YES completion:nil];
  538. }
  539. //
  540. // UIApplication * app = [UIApplication sharedApplication];
  541. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  542. //
  543. // NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  544. // //
  545. // // [appDelegate check_offline:ver];
  546. // // return;
  547. //
  548. // // if(ver.length==0)
  549. // {
  550. //
  551. // UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Check offline update?" message:nil preferredStyle:UIAlertControllerStyleAlert];
  552. // //block代码块取代了delegate
  553. //
  554. //
  555. // UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  556. // // UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Checking offline data"];
  557. // //init offline mode
  558. // // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  559. // //
  560. // //
  561. // // // [self check_offline];
  562. // // });
  563. // [appDelegate check_offline:ver];
  564. //
  565. //
  566. //
  567. // }];
  568. //
  569. // UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  570. // // [self.switch_offline setOn:false];
  571. // //[RAUtils message_alert:@"No item in the cart" title:@"Offline" controller:self] ;
  572. // }];
  573. //
  574. // [alertControl addAction:actionTwo];
  575. // [alertControl addAction:alertthree];
  576. //
  577. // //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  578. //
  579. //
  580. // [self presentViewController:alertControl animated:YES completion:nil];
  581. //
  582. //
  583. //
  584. //
  585. //
  586. //
  587. //
  588. //
  589. // }
  590. // else
  591. // {
  592. //
  593. // }
  594. }
  595. }
  596. -(void) download_file:(NSString*)url type:(NSString*)type
  597. {
  598. }
  599. /*
  600. #pragma mark - Navigation
  601. // In a storyboard-based application, you will often want to do a little preparation before navigation
  602. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  603. // Get the new view controller using [segue destinationViewController].
  604. // Pass the selected object to the new view controller.
  605. }
  606. */
  607. @end