ActiveViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. //
  2. // ActiveViewController.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 12/15/15.
  6. // Copyright © 2015 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "ActiveViewController.h"
  9. #import "config.h"
  10. #import "AppDelegate.h"
  11. #import "RAConvertor.h"
  12. //#import "NotificationNameCenter.h"
  13. @interface ActiveViewController ()
  14. @end
  15. @implementation ActiveViewController
  16. -(void)routerEvent:(NSString*)event Info:(id)info
  17. {
  18. return;
  19. // if([event isEqualToString:@"RA_NOTIFICATION_RELEASE_SCANNER"])
  20. // {
  21. // if(self.support_scanner)
  22. // {
  23. // int a=0;
  24. //#ifdef RA_NOTIFICATION
  25. // UIApplication * app = [UIApplication sharedApplication];
  26. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  27. //
  28. // if(self.support_scanner)
  29. // {
  30. // appDelegate.eventsFeedback = self;
  31. // }
  32. //#endif
  33. // }
  34. // else
  35. // [super routerEvent:event Info:info];
  36. // }
  37. // else
  38. // [super routerEvent:event Info:info];
  39. }
  40. - (void)viewDidLoad {
  41. [super viewDidLoad];
  42. // [self becomeFirstResponder];
  43. #ifdef RA_NOTIFICATION
  44. self.support_scanner = false;
  45. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(OnRefreshUI:) name:RA_NOTIFICATION_REFRESH_UI object:nil];
  46. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(OnReloadData:) name:RA_NOTIFICATION_RELOAD_DATA object:nil];
  47. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(OnClearData:) name:RA_NOTIFICATION_CLEAR_DATA object:nil];
  48. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(OnUpdatePrice:) name:RA_NOTIFICATION_UPDATE_PRICE object:nil];
  49. self.notification_refreshLevel = NotificationRefreshLevelData;
  50. #else
  51. self.refresh_type = REFRESH_DATA;
  52. #endif
  53. // Do any additional setup after loading the view.
  54. }
  55. - (void)dealloc
  56. {
  57. #ifdef RA_NOTIFICATION
  58. // [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
  59. [[NSNotificationCenter defaultCenter] removeObserver:self];
  60. #endif
  61. }
  62. - (void)didReceiveMemoryWarning {
  63. [super didReceiveMemoryWarning];
  64. // Dispose of any resources that can be recreated.
  65. }
  66. //- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  67. //{
  68. //#ifdef RA_NOTIFICATION
  69. // UIApplication * app = [UIApplication sharedApplication];
  70. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  71. //
  72. // if(self.support_scanner)
  73. // {
  74. // [appDelegate setFeedback:self];
  75. // // appDelegate.eventsFeedback = self;
  76. // }
  77. //#endif
  78. // return [super touchesBegan:touches withEvent:event];
  79. //}
  80. //- (BOOL)becomeFirstResponder
  81. //{
  82. //#ifdef RA_NOTIFICATION
  83. // UIApplication * app = [UIApplication sharedApplication];
  84. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  85. //
  86. // if(self.support_scanner)
  87. // {
  88. // [appDelegate setFeedback:self];
  89. // // appDelegate.eventsFeedback = self;
  90. // }
  91. //#endif
  92. // return [super becomeFirstResponder];
  93. //}
  94. -(void)viewDidAppear:(BOOL)animated
  95. {
  96. [super viewDidAppear:animated];
  97. #ifdef RA_NOTIFICATION
  98. UIApplication * app = [UIApplication sharedApplication];
  99. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  100. if(self.support_scanner)
  101. {
  102. [appDelegate pushFeedback:self];
  103. // appDelegate.eventsFeedback = self;
  104. }
  105. #endif
  106. }
  107. -(void)viewDidDisappear:(BOOL)animated
  108. {
  109. #ifdef RA_NOTIFICATION
  110. UIApplication * app = [UIApplication sharedApplication];
  111. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  112. if(self.support_scanner)
  113. {
  114. [appDelegate popFeedback:self];
  115. // appDelegate.eventsFeedback = self;
  116. }
  117. // [super routerEvent:RA_NOTIFICATION_RELEASE_SCANNER Info:nil];
  118. #endif
  119. [super viewDidDisappear:animated];
  120. }
  121. - (void)viewWillAppear:(BOOL)animated
  122. {
  123. [super viewWillAppear:animated];
  124. #ifdef RA_NOTIFICATION
  125. switch (self.notification_refreshLevel)
  126. {
  127. case NotificationRefreshLevelView:
  128. if([self respondsToSelector:@selector(refresh_ui)])
  129. {
  130. [self refresh_ui];
  131. self.notification_refreshLevel = NotificationRefreshLevelNone;
  132. }
  133. break;
  134. case NotificationRefreshLevelData:
  135. if([self respondsToSelector:@selector(reload_data)])
  136. {
  137. [self reload_data];
  138. self.notification_refreshLevel = NotificationRefreshLevelNone;
  139. }
  140. break;
  141. default:
  142. break;
  143. }
  144. #else
  145. UIApplication * app = [UIApplication sharedApplication];
  146. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  147. appDelegate.active_controller = self;
  148. switch (self.refresh_type)
  149. {
  150. case REFRESH_VIEW:
  151. [self reload_container_getdata:false];
  152. break;
  153. case REFRESH_DATA: {
  154. [self reRefreshView];
  155. [self reload_container_getdata:true];
  156. }
  157. break;
  158. }
  159. #endif
  160. // [[self navigationController] setNavigationBarHidden:YES animated:NO];
  161. }
  162. //-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  163. //{
  164. //// UIApplication * app = [UIApplication sharedApplication];
  165. //// AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  166. //// [appDelegate didRotated];
  167. //
  168. //// UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
  169. //// if (UIInterfaceOrientationIsLandscape(orientation)|| UIInterfaceOrientationIsPortrait(orientation))
  170. //// {
  171. //// self.orientation = orientation;
  172. //// [self.collectionview.collectionViewLayout invalidateLayout];
  173. //// DebugLog(@"routed");
  174. //// }
  175. //
  176. //
  177. //
  178. //
  179. //}
  180. - (void)reRefreshView {
  181. }
  182. -(void) logout
  183. {
  184. self.refresh_type=REFRESH_NONE;
  185. }
  186. -(void) showHidePrice
  187. {
  188. // [self reload_container_getdata:false];
  189. }
  190. -(void) refresh_on_login
  191. {
  192. // useless ??? not sure
  193. #ifndef RA_NOTIFICATION
  194. UIApplication * app = [UIApplication sharedApplication];
  195. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  196. if(self==appDelegate.active_controller)
  197. {
  198. [self reload_container_getdata:true];
  199. }
  200. else
  201. self.refresh_type = REFRESH_DATA;
  202. #endif
  203. }
  204. #ifndef RA_NOTIFICATION
  205. -(void) reload_container_getdata:(bool) update_data
  206. {
  207. self.refresh_type=REFRESH_NONE;
  208. }
  209. #endif
  210. -(void)refresh_btn_click:(UILongPressGestureRecognizer*)gestureRecognizer
  211. {
  212. if([self respondsToSelector:@selector(reload_data)])
  213. [self reload_data];
  214. // [self reload_container_getdata:true];
  215. }
  216. /*
  217. #pragma mark - Navigation
  218. // In a storyboard-based application, you will often want to do a little preparation before navigation
  219. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  220. // Get the new view controller using [segue destinationViewController].
  221. // Pass the selected object to the new view controller.
  222. }
  223. */
  224. #pragma mark - Support scanner
  225. -(void) OnUpdateDevicesList
  226. {
  227. }
  228. -(void) onDecodedData:(NSString*) value
  229. {
  230. }
  231. -(void) onErrorInfo:(NSString*) errorInfo
  232. {
  233. }
  234. -(void) onStatusChanged:(NSString*) status
  235. {
  236. }
  237. -(void) onLogUpdate
  238. {
  239. }
  240. #pragma mark - Notification
  241. //-(void)OnReleaseScanner:(NSNotification *)notification
  242. //{
  243. // if(!self.support_scanner)
  244. // return;
  245. // else
  246. // {
  247. // int a=0;
  248. //
  249. // UIApplication * app = [UIApplication sharedApplication];
  250. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  251. //
  252. // if(self.support_scanner)
  253. // {
  254. // appDelegate.eventsFeedback = self;
  255. // }
  256. //
  257. // }
  258. // NSLog(@"release scanner !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
  259. //}
  260. - (void)OnUpdatePrice: (NSNotification *)notification
  261. {
  262. if(self.disable_refresh)
  263. return;
  264. NSDictionary* dict = [notification userInfo];
  265. NSArray* arr= dict[@"receiver"];
  266. //如果当前对象不在不是指定接收者,也不是广播通信,就返回。
  267. if([arr indexOfObject:[self class_name]]==NSNotFound&& arr.count>0)
  268. return;
  269. //如果当前对象本身不支持此动作,则返回
  270. if(![self respondsToSelector:@selector(refresh_price)])
  271. return;
  272. //如果当前已有的处理层级更高(reload),则不处理,交给reload 处理。
  273. if(self.notification_refreshLevel<NotificationRefreshLevelView)
  274. {
  275. if([self isVisible])
  276. {
  277. [self refresh_price];
  278. self.notification_refreshLevel = NotificationRefreshLevelNone;
  279. }
  280. else
  281. {
  282. self.notification_refreshLevel = NotificationRefreshLevelView;
  283. }
  284. }
  285. }
  286. - (void)OnRefreshUI: (NSNotification *)notification
  287. {
  288. if(self.disable_refresh)
  289. return;
  290. NSDictionary* dict = [notification userInfo];
  291. NSArray* arr= dict[@"receiver"];
  292. if([arr indexOfObject:[self class_name]]==NSNotFound&& arr.count>0)
  293. return;
  294. if(![self respondsToSelector:@selector(refresh_ui)])
  295. return;
  296. if(self.notification_refreshLevel<NotificationRefreshLevelView)
  297. {
  298. if([self isVisible])
  299. {
  300. [self refresh_ui];
  301. self.notification_refreshLevel = NotificationRefreshLevelNone;
  302. }
  303. else
  304. {
  305. self.notification_refreshLevel = NotificationRefreshLevelView;
  306. }
  307. }
  308. }
  309. - (void)OnReloadData: (NSNotification *)notification
  310. {
  311. if(self.disable_refresh)
  312. return;
  313. NSDictionary* dict = [notification userInfo];
  314. NSArray* arr= dict[@"receiver"];
  315. if([arr indexOfObject:[self class_name]]==NSNotFound&& arr.count>0)
  316. return;
  317. if(![self respondsToSelector:@selector(reload_data)])
  318. return;
  319. if(self.notification_refreshLevel<NotificationRefreshLevelData)
  320. {
  321. if([self isVisible])
  322. {
  323. [self reload_data];
  324. self.notification_refreshLevel = NotificationRefreshLevelNone;
  325. }
  326. else
  327. {
  328. self.notification_refreshLevel = NotificationRefreshLevelData;
  329. }
  330. }
  331. }
  332. - (void)OnClearData: (NSNotification *)notification
  333. {
  334. if(![self respondsToSelector:@selector(clear_data)])
  335. return;
  336. if([self isVisible])
  337. {
  338. if([self respondsToSelector:@selector(clear_data)])
  339. {
  340. [self clear_data];
  341. if([self respondsToSelector:@selector(refresh_ui)])
  342. {
  343. [self refresh_ui];
  344. self.notification_refreshLevel = NotificationRefreshLevelNone;
  345. }
  346. }
  347. }
  348. else
  349. {
  350. if([self respondsToSelector:@selector(clear_data)])
  351. {
  352. [self clear_data];
  353. self.notification_refreshLevel = NotificationRefreshLevelView;
  354. }
  355. }
  356. }
  357. #pragma mark - utils
  358. -(BOOL)isVisible
  359. {
  360. return (self.isViewLoaded && self.view.window);
  361. }
  362. - (NSString *)class_name {
  363. const char *class_name_ch = object_getClassName([self class]);
  364. return [NSString stringWithUTF8String:class_name_ch];
  365. }
  366. +(void) Notify:(NSString*) receiver Message:(NSString*)notification_msg
  367. {
  368. dispatch_async(dispatch_get_main_queue(), ^{
  369. //NSArray* arr1 =[RAConvertor string2arr:@"abcde" separator:nil];
  370. // NSArray* arr2 =[RAConvertor string2arr:nil separator:nil];
  371. NSArray* arr_receiver = [RAConvertor string2arr:receiver separator:@","];
  372. NSMutableDictionary* dict = [NSMutableDictionary new];
  373. dict[@"receiver"]=arr_receiver;
  374. NSString* notificationname = nil;
  375. notificationname = notification_msg;
  376. NSNotification *notification =[NSNotification notificationWithName:notificationname object:nil userInfo:dict];
  377. //通过通知中心发送通知
  378. [[NSNotificationCenter defaultCenter] postNotification:notification];
  379. return;
  380. });
  381. }
  382. -(void) refresh_ui
  383. {
  384. NSAssert(true, @"subclass %@ refresh_ui does not impl.", [self class_name]);
  385. }
  386. -(void) reload_data
  387. {
  388. NSAssert(true, @"subclass %@ reload_data does not impl.", [self class_name]);
  389. }
  390. -(void) clear_data
  391. {
  392. NSAssert(true, @"subclass %@ clear_data does not impl.", [self class_name]);
  393. }
  394. -(void) refresh_price
  395. {
  396. NSAssert(true, @"subclass %@ refresh_price does not impl.", [self class_name]);
  397. }
  398. @end