RootViewController.m 8.3 KB

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