OfflineSettingViewController.m 24 KB

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