ActiveViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. // UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
  168. // if (UIInterfaceOrientationIsLandscape(orientation)|| UIInterfaceOrientationIsPortrait(orientation))
  169. // {
  170. // self.orientation = orientation;
  171. // [self.collectionview.collectionViewLayout invalidateLayout];
  172. // DebugLog(@"routed");
  173. // }
  174. }
  175. - (void)reRefreshView {
  176. }
  177. -(void) logout
  178. {
  179. self.refresh_type=REFRESH_NONE;
  180. }
  181. -(void) showHidePrice
  182. {
  183. // [self reload_container_getdata:false];
  184. }
  185. -(void) refresh_on_login
  186. {
  187. // useless ??? not sure
  188. UIApplication * app = [UIApplication sharedApplication];
  189. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  190. #ifndef RA_NOTIFICATION
  191. if(self==appDelegate.active_controller)
  192. {
  193. [self reload_container_getdata:true];
  194. }
  195. else
  196. self.refresh_type = REFRESH_DATA;
  197. #endif
  198. }
  199. #ifndef RA_NOTIFICATION
  200. -(void) reload_container_getdata:(bool) update_data
  201. {
  202. self.refresh_type=REFRESH_NONE;
  203. }
  204. #endif
  205. -(void)refresh_btn_click:(UILongPressGestureRecognizer*)gestureRecognizer
  206. {
  207. if([self respondsToSelector:@selector(reload_data)])
  208. [self reload_data];
  209. // [self reload_container_getdata:true];
  210. }
  211. /*
  212. #pragma mark - Navigation
  213. // In a storyboard-based application, you will often want to do a little preparation before navigation
  214. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  215. // Get the new view controller using [segue destinationViewController].
  216. // Pass the selected object to the new view controller.
  217. }
  218. */
  219. #pragma mark - Support scanner
  220. -(void) OnUpdateDevicesList
  221. {
  222. }
  223. -(void) onDecodedData:(NSString*) value
  224. {
  225. }
  226. -(void) onErrorInfo:(NSString*) errorInfo
  227. {
  228. }
  229. -(void) onStatusChanged:(NSString*) status
  230. {
  231. }
  232. -(void) onLogUpdate
  233. {
  234. }
  235. #pragma mark - Notification
  236. - (void)OnUpdatePrice: (NSNotification *)notification
  237. {
  238. if(self.disable_refresh)
  239. return;
  240. NSDictionary* dict = [notification userInfo];
  241. NSArray* arr= dict[@"receiver"];
  242. //如果当前对象不在不是指定接收者,也不是广播通信,就返回。
  243. if([arr indexOfObject:[self class_name]]==NSNotFound&& arr.count>0)
  244. return;
  245. //如果当前对象本身不支持此动作,则返回
  246. if(![self respondsToSelector:@selector(refresh_price)])
  247. return;
  248. //如果当前已有的处理层级更高(reload),则不处理,交给reload 处理。
  249. if(self.notification_refreshLevel<NotificationRefreshLevelView)
  250. {
  251. if([self isVisible])
  252. {
  253. [self refresh_price];
  254. self.notification_refreshLevel = NotificationRefreshLevelNone;
  255. }
  256. else
  257. {
  258. self.notification_refreshLevel = NotificationRefreshLevelView;
  259. }
  260. }
  261. }
  262. - (void)OnRefreshUI: (NSNotification *)notification
  263. {
  264. if(self.disable_refresh)
  265. return;
  266. NSDictionary* dict = [notification userInfo];
  267. NSArray* arr= dict[@"receiver"];
  268. if([arr indexOfObject:[self class_name]]==NSNotFound&& arr.count>0)
  269. return;
  270. if(![self respondsToSelector:@selector(refresh_ui)])
  271. return;
  272. if(self.notification_refreshLevel<NotificationRefreshLevelView)
  273. {
  274. if([self isVisible])
  275. {
  276. [self refresh_ui];
  277. self.notification_refreshLevel = NotificationRefreshLevelNone;
  278. }
  279. else
  280. {
  281. self.notification_refreshLevel = NotificationRefreshLevelView;
  282. }
  283. }
  284. }
  285. - (void)OnReloadData: (NSNotification *)notification
  286. {
  287. if(self.disable_refresh)
  288. return;
  289. NSDictionary* dict = [notification userInfo];
  290. NSArray* arr= dict[@"receiver"];
  291. if([arr indexOfObject:[self class_name]]==NSNotFound&& arr.count>0)
  292. return;
  293. if(![self respondsToSelector:@selector(reload_data)])
  294. return;
  295. if(self.notification_refreshLevel<NotificationRefreshLevelData)
  296. {
  297. if([self isVisible])
  298. {
  299. [self reload_data];
  300. self.notification_refreshLevel = NotificationRefreshLevelNone;
  301. }
  302. else
  303. {
  304. self.notification_refreshLevel = NotificationRefreshLevelData;
  305. }
  306. }
  307. }
  308. - (void)OnClearData: (NSNotification *)notification
  309. {
  310. if(![self respondsToSelector:@selector(clear_data)])
  311. return;
  312. if([self isVisible])
  313. {
  314. if([self respondsToSelector:@selector(clear_data)])
  315. {
  316. [self clear_data];
  317. if([self respondsToSelector:@selector(refresh_ui)])
  318. {
  319. [self refresh_ui];
  320. self.notification_refreshLevel = NotificationRefreshLevelNone;
  321. }
  322. }
  323. }
  324. else
  325. {
  326. if([self respondsToSelector:@selector(clear_data)])
  327. {
  328. [self clear_data];
  329. self.notification_refreshLevel = NotificationRefreshLevelView;
  330. }
  331. }
  332. }
  333. #pragma mark - utils
  334. -(BOOL)isVisible
  335. {
  336. return (self.isViewLoaded && self.view.window);
  337. }
  338. - (NSString *)class_name {
  339. const char *class_name_ch = object_getClassName([self class]);
  340. return [NSString stringWithUTF8String:class_name_ch];
  341. }
  342. +(void) Notify:(NSString*) receiver Message:(NSString*)notification_msg
  343. {
  344. dispatch_async(dispatch_get_main_queue(), ^{
  345. //NSArray* arr1 =[RAConvertor string2arr:@"abcde" separator:nil];
  346. // NSArray* arr2 =[RAConvertor string2arr:nil separator:nil];
  347. NSArray* arr_receiver = [RAConvertor string2arr:receiver separator:@","];
  348. NSMutableDictionary* dict = [NSMutableDictionary new];
  349. dict[@"receiver"]=arr_receiver;
  350. NSString* notificationname = nil;
  351. notificationname = notification_msg;
  352. NSNotification *notification =[NSNotification notificationWithName:notificationname object:nil userInfo:dict];
  353. //通过通知中心发送通知
  354. [[NSNotificationCenter defaultCenter] postNotification:notification];
  355. return;
  356. });
  357. }
  358. @end