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. // ver= nil;
  184. if(offline_user_iscurrent==0 && ver!=nil)
  185. {
  186. if(!self.bunlock)
  187. {
  188. [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];
  189. return;
  190. }
  191. else
  192. {
  193. ver=nil;
  194. NSString *clearTableSQL = @"delete from offline_contact;delete from offline_order;delete from offline_cart;";
  195. [iSalesDB execSql:clearTableSQL];
  196. }
  197. } else if (ver && self.bunlock) {
  198. ver = nil;
  199. }
  200. if(sender==nil)
  201. {
  202. [appDelegate check_offline:ver useInternalAddress:self.switch_internal_address.isOn parentvc:self];
  203. }
  204. else
  205. {
  206. 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];
  207. //block代码块取代了delegate
  208. UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  209. [appDelegate check_offline:ver useInternalAddress:self.switch_internal_address.isOn parentvc:self];
  210. }];
  211. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  212. }];
  213. [alertControl addAction:actionTwo];
  214. [alertControl addAction:alertthree];
  215. [self presentViewController:alertControl animated:YES completion:nil];
  216. }
  217. }
  218. }
  219. - (void)onCloseClick:(UIButton *)sender {
  220. // if(self.onDismissVC)
  221. // self.onDismissVC();
  222. [self dismissViewControllerAnimated:true completion:nil];
  223. }
  224. - (void)didReceiveMemoryWarning {
  225. [super didReceiveMemoryWarning];
  226. // Dispose of any resources that can be recreated.
  227. }
  228. - (IBAction)onChangeOfflineSwitch:(id)sender {
  229. UIApplication * app = [UIApplication sharedApplication];
  230. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  231. if(self.switch_offline.isOn==false)
  232. {
  233. //disable offline
  234. if(appDelegate.bLogin)
  235. {
  236. // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Sign out" completion:^{
  237. PopWaitAlert* pop = [RAUtils waiting_pop:@"Sign out" completion:nil];
  238. [RANetwork request_logout:^(NSMutableDictionary *result) {
  239. NSMutableDictionary* return_json=result ;
  240. [pop hide];
  241. // [waitalert dismissViewControllerAnimated:YES completion:^{
  242. if([[return_json valueForKey:@"result"] intValue]==2)
  243. {
  244. [appDelegate Logout];
  245. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  246. [defaults removeObjectForKey:@"EnableOfflineMode"];
  247. [defaults setBool:self.switch_offline.isOn forKey:@"EnableOfflineMode"];
  248. [defaults synchronize];
  249. #ifdef OFFLINE_MODE
  250. appDelegate.offline_mode =self.switch_offline.isOn;
  251. #endif
  252. #ifdef RA_NOTIFICATION
  253. [ActiveViewController Notify:@"PortfolioViewController,WatchListViewController,CategoryViewController,PDFListViewController,CartViewController,OrderListViewController,ContactListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  254. #else
  255. [((MainViewController*)appDelegate.main_vc) reloadPortfolio:true immediately:false];
  256. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  257. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  258. [((MainViewController*)appDelegate.main_vc) reloadDocuments:true immediately:false];
  259. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  260. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  261. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  262. [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false];
  263. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  264. #endif
  265. [self dismissViewControllerAnimated:true completion:^{
  266. [((MainViewController*)appDelegate.main_vc) switchToHome];
  267. }];
  268. }
  269. else
  270. {
  271. // [RAUtils message_box:@"Sign Out" message:[return_json valueForKey:@"err_msg"] completion:nil] ;
  272. }
  273. // }];
  274. }];
  275. // }];
  276. }
  277. else
  278. {
  279. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  280. [defaults removeObjectForKey:@"EnableOfflineMode"];
  281. [defaults setBool:self.switch_offline.isOn forKey:@"EnableOfflineMode"];
  282. [defaults synchronize];
  283. #ifdef OFFLINE_MODE
  284. appDelegate.offline_mode =self.switch_offline.isOn;
  285. #endif
  286. #ifdef RA_NOTIFICATION
  287. [ActiveViewController Notify:@"PortfolioViewController,WatchListViewController,CategoryViewController,PDFListViewController,CartViewController,OrderListViewController,ContactListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  288. #else
  289. [((MainViewController*)appDelegate.main_vc) reloadPortfolio:true immediately:false];
  290. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  291. [((MainViewController*)appDelegate.main_vc) reloadDocuments:true immediately:false];
  292. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  293. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  294. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  295. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  296. [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false];
  297. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  298. #endif
  299. [self dismissViewControllerAnimated:true completion:^{
  300. [((MainViewController*)appDelegate.main_vc) switchToHome];
  301. }];
  302. }
  303. }
  304. else
  305. {
  306. bool haveofflineData=[OLDataProvider check_offlinedata];
  307. if(haveofflineData)
  308. {
  309. // have offline data
  310. if(appDelegate.bLogin)
  311. {
  312. // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Sign out" completion:^{
  313. PopWaitAlert* pop = [RAUtils waiting_pop:@"Sign out" completion:nil];
  314. [RANetwork request_logout:^(NSMutableDictionary *result) {
  315. NSMutableDictionary* return_json = result;
  316. [pop hide];
  317. // [waitalert dismissViewControllerAnimated:YES completion:^{
  318. if([[return_json valueForKey:@"result"] intValue]==2)
  319. {
  320. [appDelegate Logout];
  321. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  322. [defaults removeObjectForKey:@"EnableOfflineMode"];
  323. [defaults setBool:self.switch_offline.isOn forKey:@"EnableOfflineMode"];
  324. [defaults synchronize];
  325. #ifdef OFFLINE_MODE
  326. appDelegate.offline_mode =self.switch_offline.isOn;
  327. #endif
  328. #ifdef RA_NOTIFICATION
  329. [ActiveViewController Notify:@"PortfolioViewController,WatchListViewController,CategoryViewController,PDFListViewController,CartViewController,OrderListViewController,ContactListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  330. #else
  331. [((MainViewController*)appDelegate.main_vc) reloadPortfolio:true immediately:false];
  332. [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  333. [((MainViewController*)appDelegate.main_vc) reloadDocuments:true immediately:false];
  334. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  335. [((MainViewController*)appDelegate.main_vc) reloadWish:true immediately:false];
  336. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  337. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  338. [((MainViewController*)appDelegate.main_vc) reloadContact:true immediately:false];
  339. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  340. #endif
  341. [self dismissViewControllerAnimated:true completion:^{
  342. [((MainViewController*)appDelegate.main_vc) switchToHome];
  343. }];
  344. }
  345. else
  346. {
  347. // [RAUtils message_box:@"Sign Out" message:[return_json valueForKey:@"err_msg"] completion:nil] ;
  348. }
  349. // }];
  350. }];
  351. // }];
  352. }
  353. }
  354. else
  355. {
  356. //no offline data download form server first.
  357. // restore switch state.
  358. [self.switch_offline setOn:!self.switch_offline.isOn];
  359. 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];
  360. //block代码块取代了delegate
  361. UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  362. [self OnCheckUpdate:nil];
  363. }];
  364. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  365. // [self.switch_offline setOn:false];
  366. }];
  367. [alertControl addAction:actionTwo];
  368. [alertControl addAction:alertthree];
  369. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  370. [self presentViewController:alertControl animated:YES completion:nil];
  371. }
  372. }
  373. }
  374. -(void) download_file:(NSString*)url type:(NSString*)type
  375. {
  376. }
  377. /*
  378. #pragma mark - Navigation
  379. // In a storyboard-based application, you will often want to do a little preparation before navigation
  380. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  381. // Get the new view controller using [segue destinationViewController].
  382. // Pass the selected object to the new view controller.
  383. }
  384. */
  385. @end