ActiveViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 viewWillAppear: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)OnUpdatePrice: (NSNotification *)notification
  242. {
  243. if(self.disable_refresh)
  244. return;
  245. NSDictionary* dict = [notification userInfo];
  246. NSArray* arr= dict[@"receiver"];
  247. //如果当前对象不在不是指定接收者,也不是广播通信,就返回。
  248. if([arr indexOfObject:[self class_name]]==NSNotFound&& arr.count>0)
  249. return;
  250. //如果当前对象本身不支持此动作,则返回
  251. if(![self respondsToSelector:@selector(refresh_price)])
  252. return;
  253. //如果当前已有的处理层级更高(reload),则不处理,交给reload 处理。
  254. if(self.notification_refreshLevel<NotificationRefreshLevelView)
  255. {
  256. if([self isVisible])
  257. {
  258. [self refresh_price];
  259. self.notification_refreshLevel = NotificationRefreshLevelNone;
  260. }
  261. else
  262. {
  263. self.notification_refreshLevel = NotificationRefreshLevelView;
  264. }
  265. }
  266. }
  267. - (void)OnRefreshUI: (NSNotification *)notification
  268. {
  269. if(self.disable_refresh)
  270. return;
  271. NSDictionary* dict = [notification userInfo];
  272. NSArray* arr= dict[@"receiver"];
  273. if([arr indexOfObject:[self class_name]]==NSNotFound&& arr.count>0)
  274. return;
  275. if(![self respondsToSelector:@selector(refresh_ui)])
  276. return;
  277. if(self.notification_refreshLevel<NotificationRefreshLevelView)
  278. {
  279. if([self isVisible])
  280. {
  281. [self refresh_ui];
  282. self.notification_refreshLevel = NotificationRefreshLevelNone;
  283. }
  284. else
  285. {
  286. self.notification_refreshLevel = NotificationRefreshLevelView;
  287. }
  288. }
  289. }
  290. - (void)OnReloadData: (NSNotification *)notification
  291. {
  292. if(self.disable_refresh)
  293. return;
  294. NSDictionary* dict = [notification userInfo];
  295. NSArray* arr= dict[@"receiver"];
  296. if([arr indexOfObject:[self class_name]]==NSNotFound&& arr.count>0)
  297. return;
  298. if(![self respondsToSelector:@selector(reload_data)])
  299. return;
  300. if(self.notification_refreshLevel<NotificationRefreshLevelData)
  301. {
  302. if([self isVisible])
  303. {
  304. [self reload_data];
  305. self.notification_refreshLevel = NotificationRefreshLevelNone;
  306. }
  307. else
  308. {
  309. self.notification_refreshLevel = NotificationRefreshLevelData;
  310. }
  311. }
  312. }
  313. - (void)OnClearData: (NSNotification *)notification
  314. {
  315. if(![self respondsToSelector:@selector(clear_data)])
  316. return;
  317. if([self isVisible])
  318. {
  319. if([self respondsToSelector:@selector(clear_data)])
  320. {
  321. [self clear_data];
  322. if([self respondsToSelector:@selector(refresh_ui)])
  323. {
  324. [self refresh_ui];
  325. self.notification_refreshLevel = NotificationRefreshLevelNone;
  326. }
  327. }
  328. }
  329. else
  330. {
  331. if([self respondsToSelector:@selector(clear_data)])
  332. {
  333. [self clear_data];
  334. self.notification_refreshLevel = NotificationRefreshLevelView;
  335. }
  336. }
  337. }
  338. #pragma mark - utils
  339. -(BOOL)isVisible
  340. {
  341. return (self.isViewLoaded && self.view.window);
  342. }
  343. - (NSString *)class_name {
  344. const char *class_name_ch = object_getClassName([self class]);
  345. return [NSString stringWithUTF8String:class_name_ch];
  346. }
  347. +(void) Notify:(NSString*) receiver Message:(NSString*)notification_msg
  348. {
  349. dispatch_async(dispatch_get_main_queue(), ^{
  350. //NSArray* arr1 =[RAConvertor string2arr:@"abcde" separator:nil];
  351. // NSArray* arr2 =[RAConvertor string2arr:nil separator:nil];
  352. NSArray* arr_receiver = [RAConvertor string2arr:receiver separator:@","];
  353. NSMutableDictionary* dict = [NSMutableDictionary new];
  354. dict[@"receiver"]=arr_receiver;
  355. NSString* notificationname = nil;
  356. notificationname = notification_msg;
  357. NSNotification *notification =[NSNotification notificationWithName:notificationname object:nil userInfo:dict];
  358. //通过通知中心发送通知
  359. [[NSNotificationCenter defaultCenter] postNotification:notification];
  360. return;
  361. });
  362. }
  363. -(void) refresh_ui
  364. {
  365. NSAssert(true, @"subclass %@ refresh_ui does not impl.", [self class_name]);
  366. }
  367. -(void) reload_data
  368. {
  369. NSAssert(true, @"subclass %@ reload_data does not impl.", [self class_name]);
  370. }
  371. -(void) clear_data
  372. {
  373. NSAssert(true, @"subclass %@ clear_data does not impl.", [self class_name]);
  374. }
  375. -(void) refresh_price
  376. {
  377. NSAssert(true, @"subclass %@ refresh_price does not impl.", [self class_name]);
  378. }
  379. @end