RootViewController.m 11 KB

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