OfflineSettingViewController.m 37 KB

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