OfflineSettingViewController.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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 "iSalesNetwork.h"
  10. #import "MainViewController.h"
  11. #import "LoginViewController.h"
  12. #import "OLDataProvider.h"
  13. //#import "SyncControlPanelViewController.h"
  14. @interface OfflineSettingViewController ()
  15. @end
  16. @implementation OfflineSettingViewController
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19. // Do any additional setup after loading the view.
  20. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  21. bool offline = [defaults boolForKey:@"EnableOfflineMode"] ;
  22. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  23. NSString* lastsync = [defaults stringForKey:@"LastSync"] ;
  24. if(lastsync!=nil)
  25. self.label_sync.text = lastsync;
  26. if(ver==nil)
  27. self.label_ver.text=@"No offline data";
  28. else
  29. self.label_ver.text=ver;
  30. self.switch_offline.on = offline;
  31. self.edgesForExtendedLayout = UIRectEdgeNone ;
  32. UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  33. style:UIBarButtonItemStylePlain
  34. target:self
  35. action:@selector( onCloseClick:)];
  36. self.navigationItem.rightBarButtonItem = closeButton;
  37. }
  38. - (IBAction)OnCheckUpdate:(id)sender {
  39. UIApplication * app = [UIApplication sharedApplication];
  40. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  41. if(appDelegate.bLogin==false)
  42. {
  43. LoginViewController * loginvc =[ [UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"LoginViewController"];
  44. loginvc.returnValue = ^(bool blogin){
  45. if(blogin)
  46. {
  47. if(appDelegate.user_type==USER_ROLE_EMPLOYEE)
  48. {
  49. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  50. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  51. if(sender==nil)
  52. {
  53. [appDelegate check_offline:ver useInternalAddress:self.switch_internal_address.isOn parentvc:self];
  54. // SyncControlPanelViewController * syncVC =[ [UIStoryboard storyboardWithName:@"OLM" bundle:nil] instantiateViewControllerWithIdentifier:@"SyncControlPanelViewController"];
  55. // syncVC.action_string=@"Download";
  56. // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:syncVC] ;
  57. //
  58. //
  59. //
  60. //
  61. //
  62. // // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  63. //
  64. // navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  65. // [self presentViewController:navi animated:YES completion:^{
  66. //
  67. // // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  68. //
  69. // NSLog(@"about present.........");
  70. //
  71. // // self.btop = false;
  72. // // <#code#>
  73. // }];
  74. }
  75. else
  76. {
  77. 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];
  78. //block代码块取代了delegate
  79. UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  80. [appDelegate check_offline:ver useInternalAddress:self.switch_internal_address.isOn parentvc:self];
  81. // SyncControlPanelViewController * syncVC =[ [UIStoryboard storyboardWithName:@"OLM" bundle:nil] instantiateViewControllerWithIdentifier:@"SyncControlPanelViewController"];
  82. // syncVC.action_string=@"Download";
  83. // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:syncVC] ;
  84. //
  85. //
  86. //
  87. //
  88. //
  89. // // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  90. //
  91. // navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  92. // [self presentViewController:navi animated:YES completion:^{
  93. //
  94. // // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  95. //
  96. // NSLog(@"about present.........");
  97. //
  98. // // self.btop = false;
  99. // // <#code#>
  100. // }];
  101. }];
  102. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  103. }];
  104. [alertControl addAction:actionTwo];
  105. [alertControl addAction:alertthree];
  106. [self presentViewController:alertControl animated:YES completion:nil];
  107. }
  108. }
  109. else
  110. {
  111. }
  112. }
  113. else
  114. {
  115. [RAUtils alert_view:@"Must login" title:@"Data sync"];
  116. }
  117. };
  118. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  119. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  120. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  121. [self presentViewController:navi animated:YES completion:^{
  122. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  123. NSLog(@"LoginViewController present.........");
  124. // self.btop = false;
  125. // <#code#>
  126. }];
  127. }
  128. else
  129. {
  130. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  131. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  132. if(sender==nil)
  133. {
  134. [appDelegate check_offline:ver useInternalAddress:self.switch_internal_address.isOn parentvc:self];
  135. // SyncControlPanelViewController * syncVC =[ [UIStoryboard storyboardWithName:@"OLM" bundle:nil] instantiateViewControllerWithIdentifier:@"SyncControlPanelViewController"];
  136. // syncVC.action_string=@"Download";
  137. // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:syncVC] ;
  138. //
  139. //
  140. //
  141. //
  142. //
  143. // // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  144. //
  145. // navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  146. // [self presentViewController:navi animated:YES completion:^{
  147. //
  148. // // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  149. //
  150. // NSLog(@"about present.........");
  151. //
  152. // // self.btop = false;
  153. // // <#code#>
  154. // }];
  155. }
  156. else
  157. {
  158. 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];
  159. //block代码块取代了delegate
  160. UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  161. [appDelegate check_offline:ver useInternalAddress:self.switch_internal_address.isOn parentvc:self];
  162. }];
  163. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  164. }];
  165. [alertControl addAction:actionTwo];
  166. [alertControl addAction:alertthree];
  167. [self presentViewController:alertControl animated:YES completion:nil];
  168. }
  169. }
  170. }
  171. - (void)onCloseClick:(UIButton *)sender {
  172. // if(self.onDismissVC)
  173. // self.onDismissVC();
  174. [self dismissViewControllerAnimated:true completion:nil];
  175. }
  176. - (void)didReceiveMemoryWarning {
  177. [super didReceiveMemoryWarning];
  178. // Dispose of any resources that can be recreated.
  179. }
  180. - (IBAction)onChangeOfflineSwitch:(id)sender {
  181. UIApplication * app = [UIApplication sharedApplication];
  182. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  183. if(self.switch_offline.isOn==false)
  184. {
  185. //disable offline
  186. if(appDelegate.bLogin)
  187. {
  188. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Sign out"];
  189. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  190. NSDictionary* return_json = [iSalesNetwork logout];
  191. dispatch_async(dispatch_get_main_queue(), ^{
  192. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  193. if([[return_json valueForKey:@"result"] intValue]==2)
  194. {
  195. [appDelegate Logout];
  196. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  197. [defaults removeObjectForKey:@"EnableOfflineMode"];
  198. [defaults setBool:self.switch_offline.isOn forKey:@"EnableOfflineMode"];
  199. [defaults synchronize];
  200. appDelegate.offline_mode =self.switch_offline.isOn;
  201. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  202. [((MainViewController*)appDelegate.main_vc) reloadDocuments:true immediately:false];
  203. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  204. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  205. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  206. [((MainViewController*)appDelegate.main_vc) reloadPortfolio:true immediately:false];
  207. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  208. [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false];
  209. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  210. [((MainViewController*)appDelegate.main_vc) switchToHome];
  211. }
  212. else
  213. {
  214. // [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Sign Out" controller:self] ;
  215. }
  216. });
  217. });
  218. }
  219. else
  220. {
  221. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  222. [defaults removeObjectForKey:@"EnableOfflineMode"];
  223. [defaults setBool:self.switch_offline.isOn forKey:@"EnableOfflineMode"];
  224. [defaults synchronize];
  225. appDelegate.offline_mode =self.switch_offline.isOn;
  226. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  227. [((MainViewController*)appDelegate.main_vc) reloadDocuments:true immediately:false];
  228. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  229. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  230. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  231. [((MainViewController*)appDelegate.main_vc) reloadPortfolio:true immediately:false];
  232. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  233. [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false];
  234. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  235. [((MainViewController*)appDelegate.main_vc) switchToHome];
  236. }
  237. }
  238. else
  239. {
  240. bool haveofflineData=[OLDataProvider check_offlinedata];
  241. if(haveofflineData)
  242. {
  243. // have offline data
  244. if(appDelegate.bLogin)
  245. {
  246. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Sign out"];
  247. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  248. NSDictionary* return_json = [iSalesNetwork logout];
  249. dispatch_async(dispatch_get_main_queue(), ^{
  250. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  251. if([[return_json valueForKey:@"result"] intValue]==2)
  252. {
  253. [appDelegate Logout];
  254. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  255. [defaults removeObjectForKey:@"EnableOfflineMode"];
  256. [defaults setBool:self.switch_offline.isOn forKey:@"EnableOfflineMode"];
  257. [defaults synchronize];
  258. appDelegate.offline_mode =self.switch_offline.isOn;
  259. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  260. [((MainViewController*)appDelegate.main_vc) reloadDocuments:true immediately:false];
  261. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  262. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  263. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  264. [((MainViewController*)appDelegate.main_vc) reloadPortfolio:true immediately:false];
  265. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  266. [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false];
  267. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  268. [((MainViewController*)appDelegate.main_vc) switchToHome];
  269. }
  270. else
  271. {
  272. // [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Sign Out" controller:self] ;
  273. }
  274. });
  275. });
  276. }
  277. }
  278. else
  279. {
  280. //no offline data download form server first.
  281. // restore switch state.
  282. [self.switch_offline setOn:!self.switch_offline.isOn];
  283. 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];
  284. //block代码块取代了delegate
  285. UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  286. [self OnCheckUpdate:nil];
  287. }];
  288. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  289. // [self.switch_offline setOn:false];
  290. //[RAUtils message_alert:@"No item in the cart" title:@"Offline" controller:self] ;
  291. }];
  292. [alertControl addAction:actionTwo];
  293. [alertControl addAction:alertthree];
  294. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  295. [self presentViewController:alertControl animated:YES completion:nil];
  296. }
  297. //
  298. // UIApplication * app = [UIApplication sharedApplication];
  299. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  300. //
  301. // NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  302. // //
  303. // // [appDelegate check_offline:ver];
  304. // // return;
  305. //
  306. // // if(ver.length==0)
  307. // {
  308. //
  309. // UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Check offline update?" message:nil preferredStyle:UIAlertControllerStyleAlert];
  310. // //block代码块取代了delegate
  311. //
  312. //
  313. // UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  314. // // UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Checking offline data"];
  315. // //init offline mode
  316. // // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  317. // //
  318. // //
  319. // // // [self check_offline];
  320. // // });
  321. // [appDelegate check_offline:ver];
  322. //
  323. //
  324. //
  325. // }];
  326. //
  327. // UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  328. // // [self.switch_offline setOn:false];
  329. // //[RAUtils message_alert:@"No item in the cart" title:@"Offline" controller:self] ;
  330. // }];
  331. //
  332. // [alertControl addAction:actionTwo];
  333. // [alertControl addAction:alertthree];
  334. //
  335. // //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  336. //
  337. //
  338. // [self presentViewController:alertControl animated:YES completion:nil];
  339. //
  340. //
  341. //
  342. //
  343. //
  344. //
  345. //
  346. //
  347. // }
  348. // else
  349. // {
  350. //
  351. // }
  352. }
  353. }
  354. -(void) download_file:(NSString*)url type:(NSString*)type
  355. {
  356. }
  357. /*
  358. #pragma mark - Navigation
  359. // In a storyboard-based application, you will often want to do a little preparation before navigation
  360. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  361. // Get the new view controller using [segue destinationViewController].
  362. // Pass the selected object to the new view controller.
  363. }
  364. */
  365. @end