OfflineSettingViewController.m 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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 = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  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. }];
  49. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  50. DebugLog(@"No");
  51. }];
  52. [alertControl addAction:actionOne];
  53. [alertControl addAction:alertthree];
  54. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  55. [self presentViewController:alertControl animated:YES completion:nil];
  56. }
  57. - (void)viewDidLoad {
  58. [super viewDidLoad];
  59. self.bunlock=false;
  60. // Do any additional setup after loading the view.
  61. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  62. bool offline = [defaults boolForKey:@"EnableOfflineMode"] ;
  63. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  64. NSString* lastsync = [defaults stringForKey:@"LastSync"] ;
  65. if(lastsync!=nil)
  66. self.label_sync.text = lastsync;
  67. if(ver==nil)
  68. self.label_ver.text=@"No offline data";
  69. else
  70. self.label_ver.text=ver;
  71. self.switch_offline.on = offline;
  72. // self.edgesForExtendedLayout = UIRectEdgeNone ;
  73. UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  74. style:UIBarButtonItemStylePlain
  75. target:self
  76. action:@selector( onCloseClick:)];
  77. self.navigationItem.rightBarButtonItem = closeButton;
  78. }
  79. - (IBAction)OnCheckUpdate:(id)sender {
  80. UIApplication * app = [UIApplication sharedApplication];
  81. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  82. // if(true)
  83. // {
  84. // [appDelegate handle_download1:@"https:/redant.contrast-inc.com:443/zipDatas/1b13b704-b663-4806-b8e8-823e64cca953/pictureZip/PicturesZip4.zip" start:true type:@"img" taskid:0];
  85. // return;
  86. // }
  87. if(appDelegate.bLogin==false)
  88. {
  89. LoginViewController * loginvc =[ [UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"LoginViewController"];
  90. loginvc.returnValue = ^(bool blogin){
  91. if(blogin)
  92. {
  93. if(appDelegate.user_type==USER_ROLE_EMPLOYEE)
  94. {
  95. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  96. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  97. if(sender==nil)
  98. {
  99. [appDelegate check_offline:ver useInternalAddress:self.switch_internal_address.isOn parentvc:self];
  100. // SyncControlPanelViewController * syncVC =[ [UIStoryboard storyboardWithName:@"OLM" bundle:nil] instantiateViewControllerWithIdentifier:@"SyncControlPanelViewController"];
  101. // syncVC.action_string=@"Download";
  102. // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:syncVC] ;
  103. //
  104. //
  105. //
  106. //
  107. //
  108. // // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  109. //
  110. // navi.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  111. // [self presentViewController:navi animated:YES completion:^{
  112. //
  113. // // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  114. //
  115. // DebugLog(@"about present.........");
  116. //
  117. // // self.btop = false;
  118. // // <#code#>
  119. // }];
  120. }
  121. else
  122. {
  123. 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];
  124. //block代码块取代了delegate
  125. UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  126. [appDelegate check_offline:ver useInternalAddress:self.switch_internal_address.isOn parentvc:self];
  127. // SyncControlPanelViewController * syncVC =[ [UIStoryboard storyboardWithName:@"OLM" bundle:nil] instantiateViewControllerWithIdentifier:@"SyncControlPanelViewController"];
  128. // syncVC.action_string=@"Download";
  129. // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:syncVC] ;
  130. //
  131. //
  132. //
  133. //
  134. //
  135. // // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  136. //
  137. // navi.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  138. // [self presentViewController:navi animated:YES completion:^{
  139. //
  140. // // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  141. //
  142. // DebugLog(@"about present.........");
  143. //
  144. // // self.btop = false;
  145. // // <#code#>
  146. // }];
  147. }];
  148. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  149. }];
  150. [alertControl addAction:actionTwo];
  151. [alertControl addAction:alertthree];
  152. [self presentViewController:alertControl animated:YES completion:nil];
  153. }
  154. }
  155. else
  156. {
  157. }
  158. }
  159. else
  160. {
  161. [RAUtils message_box:@"Data sync" message:@"Must login" completion:nil];
  162. // [RAUtils alert_view:@"Must login" title:@"Data sync"];
  163. }
  164. };
  165. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  166. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  167. navi.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  168. [self presentViewController:navi animated:YES completion:^{
  169. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  170. DebugLog(@"LoginViewController present.........");
  171. // self.btop = false;
  172. // <#code#>
  173. }];
  174. }
  175. else
  176. {
  177. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  178. sqlite3* db = [iSalesDB get_db];
  179. int offline_user_iscurrent=[iSalesDB get_recordcount:db table:@"offline_login" where:[NSString stringWithFormat:@"username='%@'",appDelegate.user ]];
  180. [iSalesDB close_db:db];
  181. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  182. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  183. if(offline_user_iscurrent==0 && ver!=nil)
  184. {
  185. if(!self.bunlock)
  186. {
  187. [RAUtils message_box:@"Sync denied." message:@"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. " completion:nil];
  188. return;
  189. }
  190. else
  191. {
  192. ver=nil;
  193. NSString *clearTableSQL = @"delete from offline_contact;delete from offline_order;delete from offline_cart;";
  194. [iSalesDB execSql:clearTableSQL];
  195. }
  196. } else if (ver && self.bunlock) {
  197. ver = nil;
  198. }
  199. if(sender==nil)
  200. {
  201. [appDelegate check_offline:ver useInternalAddress:self.switch_internal_address.isOn parentvc:self];
  202. }
  203. else
  204. {
  205. 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];
  206. //block代码块取代了delegate
  207. UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  208. [appDelegate check_offline:ver useInternalAddress:self.switch_internal_address.isOn parentvc:self];
  209. }];
  210. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  211. }];
  212. [alertControl addAction:actionTwo];
  213. [alertControl addAction:alertthree];
  214. [self presentViewController:alertControl animated:YES completion:nil];
  215. }
  216. }
  217. }
  218. - (void)onCloseClick:(UIButton *)sender {
  219. // if(self.onDismissVC)
  220. // self.onDismissVC();
  221. [self dismissViewControllerAnimated:true completion:nil];
  222. }
  223. - (void)didReceiveMemoryWarning {
  224. [super didReceiveMemoryWarning];
  225. // Dispose of any resources that can be recreated.
  226. }
  227. - (IBAction)onChangeOfflineSwitch:(id)sender {
  228. UIApplication * app = [UIApplication sharedApplication];
  229. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  230. if(self.switch_offline.isOn==false)
  231. {
  232. //disable offline
  233. if(appDelegate.bLogin)
  234. {
  235. // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Sign out" completion:^{
  236. PopWaitAlert* pop = [RAUtils waiting_pop:@"Sign out" completion:nil];
  237. [RANetwork request_logout:^(NSMutableDictionary *result) {
  238. NSMutableDictionary* return_json=result ;
  239. [pop hide];
  240. // [waitalert dismissViewControllerAnimated:YES completion:^{
  241. if([[return_json valueForKey:@"result"] intValue]==2)
  242. {
  243. [appDelegate Logout];
  244. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  245. [defaults removeObjectForKey:@"EnableOfflineMode"];
  246. [defaults setBool:self.switch_offline.isOn forKey:@"EnableOfflineMode"];
  247. [defaults synchronize];
  248. #ifdef OFFLINE_MODE
  249. appDelegate.offline_mode =self.switch_offline.isOn;
  250. #endif
  251. #ifdef RA_NOTIFICATION
  252. [ActiveViewController Notify:@"PortfolioViewController,WatchListViewController,CategoryViewController,PDFListViewController,CartViewController,OrderListViewController,ContactListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  253. #else
  254. [((MainViewController*)appDelegate.main_vc) reloadPortfolio:true immediately:false];
  255. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  256. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  257. [((MainViewController*)appDelegate.main_vc) reloadDocuments:true immediately:false];
  258. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  259. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  260. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  261. [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false];
  262. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  263. #endif
  264. [self dismissViewControllerAnimated:true completion:^{
  265. [((MainViewController*)appDelegate.main_vc) switchToHome];
  266. }];
  267. }
  268. else
  269. {
  270. // [RAUtils message_box:@"Sign Out" message:[return_json valueForKey:@"err_msg"] completion:nil] ;
  271. }
  272. // }];
  273. }];
  274. // }];
  275. }
  276. else
  277. {
  278. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  279. [defaults removeObjectForKey:@"EnableOfflineMode"];
  280. [defaults setBool:self.switch_offline.isOn forKey:@"EnableOfflineMode"];
  281. [defaults synchronize];
  282. #ifdef OFFLINE_MODE
  283. appDelegate.offline_mode =self.switch_offline.isOn;
  284. #endif
  285. #ifdef RA_NOTIFICATION
  286. [ActiveViewController Notify:@"PortfolioViewController,WatchListViewController,CategoryViewController,PDFListViewController,CartViewController,OrderListViewController,ContactListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  287. #else
  288. [((MainViewController*)appDelegate.main_vc) reloadPortfolio:true immediately:false];
  289. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  290. [((MainViewController*)appDelegate.main_vc) reloadDocuments:true immediately:false];
  291. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  292. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  293. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  294. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  295. [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false];
  296. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  297. #endif
  298. [self dismissViewControllerAnimated:true completion:^{
  299. [((MainViewController*)appDelegate.main_vc) switchToHome];
  300. }];
  301. }
  302. }
  303. else
  304. {
  305. bool haveofflineData=[OLDataProvider check_offlinedata];
  306. if(haveofflineData)
  307. {
  308. // have offline data
  309. if(appDelegate.bLogin)
  310. {
  311. // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Sign out" completion:^{
  312. PopWaitAlert* pop = [RAUtils waiting_pop:@"Sign out" completion:nil];
  313. [RANetwork request_logout:^(NSMutableDictionary *result) {
  314. NSMutableDictionary* return_json = result;
  315. [pop hide];
  316. // [waitalert dismissViewControllerAnimated:YES completion:^{
  317. if([[return_json valueForKey:@"result"] intValue]==2)
  318. {
  319. [appDelegate Logout];
  320. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  321. [defaults removeObjectForKey:@"EnableOfflineMode"];
  322. [defaults setBool:self.switch_offline.isOn forKey:@"EnableOfflineMode"];
  323. [defaults synchronize];
  324. #ifdef OFFLINE_MODE
  325. appDelegate.offline_mode =self.switch_offline.isOn;
  326. #endif
  327. #ifdef RA_NOTIFICATION
  328. [ActiveViewController Notify:@"PortfolioViewController,WatchListViewController,CategoryViewController,PDFListViewController,CartViewController,OrderListViewController,ContactListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  329. #else
  330. [((MainViewController*)appDelegate.main_vc) reloadPortfolio:true immediately:false];
  331. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  332. [((MainViewController*)appDelegate.main_vc) reloadDocuments:true immediately:false];
  333. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  334. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  335. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  336. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  337. [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false];
  338. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  339. #endif
  340. [self dismissViewControllerAnimated:true completion:^{
  341. [((MainViewController*)appDelegate.main_vc) switchToHome];
  342. }];
  343. }
  344. else
  345. {
  346. // [RAUtils message_box:@"Sign Out" message:[return_json valueForKey:@"err_msg"] completion:nil] ;
  347. }
  348. // }];
  349. }];
  350. // }];
  351. }
  352. }
  353. else
  354. {
  355. //no offline data download form server first.
  356. // restore switch state.
  357. [self.switch_offline setOn:!self.switch_offline.isOn];
  358. 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];
  359. //block代码块取代了delegate
  360. UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  361. [self OnCheckUpdate:nil];
  362. }];
  363. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  364. // [self.switch_offline setOn:false];
  365. }];
  366. [alertControl addAction:actionTwo];
  367. [alertControl addAction:alertthree];
  368. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  369. [self presentViewController:alertControl animated:YES completion:nil];
  370. }
  371. }
  372. }
  373. -(void) download_file:(NSString*)url type:(NSString*)type
  374. {
  375. }
  376. /*
  377. #pragma mark - Navigation
  378. // In a storyboard-based application, you will often want to do a little preparation before navigation
  379. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  380. // Get the new view controller using [segue destinationViewController].
  381. // Pass the selected object to the new view controller.
  382. }
  383. */
  384. @end