OfflineSettingViewController.m 26 KB

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