RootViewController.m 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // ViewController.m
  3. // RA Image
  4. //
  5. // Created by Ray on 25/04/2017.
  6. // Copyright © 2017 USAI. All rights reserved.
  7. //
  8. #import "RootViewController.h"
  9. #import "LoginViewController.h"
  10. #import "RootModeCell.h"
  11. #import "PopModeViewController.h"
  12. #import "ModelModeViewController.h"
  13. #import "UploadSettingController.h"
  14. static NSString *kLastMode = @"lastChooseMode";
  15. @interface RootViewController ()<UITableViewDelegate,UITableViewDataSource>
  16. @property (strong, nonatomic) IBOutlet UILabel *companyNameLabel;
  17. @property (strong, nonatomic) IBOutlet UIImageView *companyIconView;
  18. @property (strong, nonatomic) IBOutlet UITableView *modeTable;
  19. @property (nonatomic,strong) NSArray *modeList;
  20. @property (nonatomic,assign) NSInteger lastChoosedIndex;
  21. @end
  22. @implementation RootViewController
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. // Do any additional setup after loading the view, typically from a nib.
  26. UIView *v = [UIView new];
  27. [self.view insertSubview:v atIndex:0];
  28. self.lastChoosedIndex = -1;
  29. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Logout" style:UIBarButtonItemStylePlain target:self action:@selector(logoutItemClick:)];
  30. self.title = @"Select Mode";
  31. self.modeTable.tableFooterView = [UIView new];
  32. [self removeFirstResponderTap]; // 不然cell接受不了消息
  33. self.companyIcon = [UIImage imageNamed:@"AppIcon40x40"];
  34. self.companyNameLabel.text = nil;
  35. }
  36. - (void)viewWillAppear:(BOOL)animated {
  37. [super viewWillAppear:animated];
  38. [self.modeTable reloadData];
  39. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  40. self.companyNameLabel.text = appDelegate.companyName;
  41. }
  42. - (void)viewDidAppear:(BOOL)animated {
  43. [super viewDidAppear:animated];
  44. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  45. NSString *lastChoosedMode = [self userDefaultsValue:kLastMode];
  46. NSArray *savedArr = [lastChoosedMode componentsSeparatedByString:@"<&&>"];
  47. NSString *lastLoginName = [savedArr firstObject];
  48. if (![lastLoginName isEqualToString:appDelegate.user]) { // 登录用户与上一次使用过模式的用户相同才进入上一次模式。
  49. appDelegate.shouldAutoShowModeVC = NO;
  50. return;
  51. }
  52. if (appDelegate.shouldAutoShowModeVC) {
  53. appDelegate.shouldAutoShowModeVC = NO;
  54. if (self.lastChoosedIndex > -1) {
  55. // RootModeCell *cell = [self.modeTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.lastChoosedIndex inSection:0]];
  56. // [self pushCurrentModeController:cell];
  57. [self tableView:self.modeTable didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:self.lastChoosedIndex inSection:0]];
  58. }
  59. }
  60. [self setUserDefaultsValue:nil forKey:kLastMode];
  61. self.lastChoosedIndex = -1;
  62. }
  63. - (void)didReceiveMemoryWarning {
  64. [super didReceiveMemoryWarning];
  65. // Dispose of any resources that can be recreated.
  66. }
  67. - (void)setCompanyIcon:(UIImage *)companyIcon {
  68. _companyIcon = companyIcon;
  69. self.companyIconView.image = companyIcon;
  70. }
  71. - (NSArray *)modeList {
  72. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  73. return appDelegate.modeList;
  74. }
  75. #pragma mark - DataSource
  76. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  77. return self.modeList.count;
  78. }
  79. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  80. RootModeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RootModeCell"];
  81. NSDictionary *mode = [self.modeList objectAtIndex:indexPath.row];
  82. NSString *name = [mode objectForKey:@"name"];
  83. cell.mode = name;
  84. NSString *codeName = [mode objectForKey:@"code_name"];
  85. cell.code_name = codeName;
  86. NSString *lastChoosedMode = [self userDefaultsValue:kLastMode];
  87. NSArray *savedArr = [lastChoosedMode componentsSeparatedByString:@"<&&>"];
  88. lastChoosedMode = [savedArr lastObject];
  89. if (lastChoosedMode.length) {
  90. if ([lastChoosedMode isEqualToString:name]) {
  91. self.lastChoosedIndex = indexPath.row;
  92. }
  93. }
  94. BOOL enable = [[mode objectForKey:@"enable"] boolValue];
  95. if (!enable) {
  96. cell.contentView.backgroundColor = [UIColor darkGrayColor];
  97. }
  98. else{
  99. cell.contentView.backgroundColor = [UIColor whiteColor];
  100. }
  101. return cell;
  102. }
  103. #pragma mark - Delegate
  104. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  105. RootModeCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  106. NSDictionary *mode = [self.modeList objectAtIndex:indexPath.row];
  107. BOOL enable = [[mode objectForKey:@"enable"] boolValue];
  108. if (enable) {
  109. NSString *name = cell.mode;
  110. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  111. appDelegate.shouldAutoShowModeVC = NO;
  112. [self setUserDefaultsValue:[NSString stringWithFormat:@"%@<&&>%@",appDelegate.user,name] forKey:kLastMode];
  113. [self pushCurrentModeController:cell];
  114. }
  115. }
  116. #pragma mark - Private
  117. - (void)pushCurrentModeController:(RootModeCell *)cell {
  118. if (!cell) {
  119. return;
  120. }
  121. NSIndexPath *indexPath = [self.modeTable indexPathForCell:cell];
  122. NSDictionary *mode = [self.modeList objectAtIndex:indexPath.row];
  123. BasicModeViewController *modeVC = nil;
  124. if ([cell.mode isEqualToString:@"Model"]) {
  125. ModelModeViewController *vc = (ModelModeViewController *)[self viewControllerInStoryboard:@"Mode" withId:@"ModelModeViewController"];
  126. vc.manufacturerList = [mode objectForKey:@"manifacturer"];
  127. modeVC = vc;
  128. }else if ([cell.mode isEqualToString:@"POP"]||[cell.mode isEqualToString:@"Receiving"]||[cell.mode isEqualToString:@"Returns"]) {
  129. PopModeViewController *vc = [[UIStoryboard storyboardWithName:@"Mode" bundle:nil] instantiateViewControllerWithIdentifier:@"PopModeViewController"];
  130. modeVC = vc;
  131. }
  132. if (modeVC) {
  133. modeVC.barcodeTitle = cell.code_name;
  134. modeVC.name = cell.mode;
  135. [self.navigationController pushViewController:modeVC animated:YES];
  136. }
  137. }
  138. - (void)userLogout:(NSNotification *)notification {
  139. [self.modeTable reloadData];
  140. [super userLogout:notification];
  141. }
  142. #pragma mark - Action
  143. - (void)logoutItemClick:(UIBarButtonItem *)sender {
  144. UIAlertView *alert = [RAUtils waiting_alert:@"Please wait..." title:@"Logout"];
  145. __weak typeof(self) weakself = self;
  146. dispatch_async(dispatch_get_main_queue(), ^{
  147. NSDictionary *logoutDic = [RAINetwork logout];
  148. int result = [[logoutDic objectForKey:@"result"] intValue];
  149. dispatch_async(dispatch_get_main_queue(), ^{
  150. [alert dismissWithClickedButtonIndex:0 animated:YES];
  151. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  152. if (result == RESULT_TRUE) {
  153. weakself.companyIcon = [UIImage imageNamed:@"AppIcon40x40"];
  154. [appDelegate logout];
  155. } else {
  156. __strong typeof(weakself) strongself = weakself;
  157. NSString *msg = [logoutDic objectForKey:@"err_msg"];
  158. [RAUtils message_alert:msg title:@"Warning" controller:strongself];
  159. }
  160. });
  161. });
  162. }
  163. - (IBAction)uploadSettingBtnClick:(UIButton *)sender {
  164. UploadSettingController *ups = (UploadSettingController *)[self viewControllerInStoryboard:@"Upload" withId:@"UploadSettingController"];
  165. [self.navigationController pushViewController:ups animated:YES];
  166. }
  167. @end