OfflineSettingViewController.m 23 KB

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