ActiveViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
  58. [[NSNotificationCenter defaultCenter] removeObserver:self];
  59. }
  60. - (void)didReceiveMemoryWarning {
  61. [super didReceiveMemoryWarning];
  62. // Dispose of any resources that can be recreated.
  63. }
  64. //- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  65. //{
  66. //#ifdef RA_NOTIFICATION
  67. // UIApplication * app = [UIApplication sharedApplication];
  68. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  69. //
  70. // if(self.support_scanner)
  71. // {
  72. // [appDelegate setFeedback:self];
  73. // // appDelegate.eventsFeedback = self;
  74. // }
  75. //#endif
  76. // return [super touchesBegan:touches withEvent:event];
  77. //}
  78. //- (BOOL)becomeFirstResponder
  79. //{
  80. //#ifdef RA_NOTIFICATION
  81. // UIApplication * app = [UIApplication sharedApplication];
  82. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  83. //
  84. // if(self.support_scanner)
  85. // {
  86. // [appDelegate setFeedback:self];
  87. // // appDelegate.eventsFeedback = self;
  88. // }
  89. //#endif
  90. // return [super becomeFirstResponder];
  91. //}
  92. -(void)viewDidAppear:(BOOL)animated
  93. {
  94. [super viewWillAppear:animated];
  95. #ifdef RA_NOTIFICATION
  96. UIApplication * app = [UIApplication sharedApplication];
  97. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  98. if(self.support_scanner)
  99. {
  100. [appDelegate pushFeedback:self];
  101. // appDelegate.eventsFeedback = self;
  102. }
  103. #endif
  104. }
  105. -(void)viewDidDisappear:(BOOL)animated
  106. {
  107. #ifdef RA_NOTIFICATION
  108. UIApplication * app = [UIApplication sharedApplication];
  109. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  110. if(self.support_scanner)
  111. {
  112. [appDelegate popFeedback:self];
  113. // appDelegate.eventsFeedback = self;
  114. }
  115. [super routerEvent:RA_NOTIFICATION_RELEASE_SCANNER Info:nil];
  116. #endif
  117. [super viewDidDisappear:animated];
  118. }
  119. - (void)viewWillAppear:(BOOL)animated
  120. {
  121. [super viewWillAppear:animated];
  122. #ifdef RA_NOTIFICATION
  123. switch (self.notification_refreshLevel)
  124. {
  125. case NotificationRefreshLevelView:
  126. if([self respondsToSelector:@selector(refresh_ui)])
  127. {
  128. [self refresh_ui];
  129. self.notification_refreshLevel = NotificationRefreshLevelNone;
  130. }
  131. break;
  132. case NotificationRefreshLevelData:
  133. if([self respondsToSelector:@selector(reload_data)])
  134. {
  135. [self reload_data];
  136. self.notification_refreshLevel = NotificationRefreshLevelNone;
  137. }
  138. break;
  139. default:
  140. break;
  141. }
  142. #else
  143. UIApplication * app = [UIApplication sharedApplication];
  144. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  145. appDelegate.active_controller = self;
  146. switch (self.refresh_type)
  147. {
  148. case REFRESH_VIEW:
  149. [self reload_container_getdata:false];
  150. break;
  151. case REFRESH_DATA: {
  152. [self reRefreshView];
  153. [self reload_container_getdata:true];
  154. }
  155. break;
  156. }
  157. #endif
  158. // [[self navigationController] setNavigationBarHidden:YES animated:NO];
  159. }
  160. -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  161. {
  162. // UIApplication * app = [UIApplication sharedApplication];
  163. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  164. // [appDelegate didRotated];
  165. // UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
  166. // if (UIInterfaceOrientationIsLandscape(orientation)|| UIInterfaceOrientationIsPortrait(orientation))
  167. // {
  168. // self.orientation = orientation;
  169. // [self.collectionview.collectionViewLayout invalidateLayout];
  170. // DebugLog(@"routed");
  171. // }
  172. }
  173. - (void)reRefreshView {
  174. }
  175. -(void) logout
  176. {
  177. self.refresh_type=REFRESH_NONE;
  178. }
  179. -(void) showHidePrice
  180. {
  181. // [self reload_container_getdata:false];
  182. }
  183. -(void) refresh_on_login
  184. {
  185. // useless ??? not sure
  186. UIApplication * app = [UIApplication sharedApplication];
  187. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  188. #ifndef RA_NOTIFICATION
  189. if(self==appDelegate.active_controller)
  190. {
  191. [self reload_container_getdata:true];
  192. }
  193. else
  194. self.refresh_type = REFRESH_DATA;
  195. #endif
  196. }
  197. #ifndef RA_NOTIFICATION
  198. -(void) reload_container_getdata:(bool) update_data
  199. {
  200. self.refresh_type=REFRESH_NONE;
  201. }
  202. #endif
  203. -(void)refresh_btn_click:(UILongPressGestureRecognizer*)gestureRecognizer
  204. {
  205. if([self respondsToSelector:@selector(reload_data)])
  206. [self reload_data];
  207. // [self reload_container_getdata:true];
  208. }
  209. /*
  210. #pragma mark - Navigation
  211. // In a storyboard-based application, you will often want to do a little preparation before navigation
  212. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  213. // Get the new view controller using [segue destinationViewController].
  214. // Pass the selected object to the new view controller.
  215. }
  216. */
  217. #pragma mark - Support scanner
  218. -(void) OnUpdateDevicesList
  219. {
  220. }
  221. -(void) onDecodedData:(NSString*) value
  222. {
  223. }
  224. -(void) onErrorInfo:(NSString*) errorInfo
  225. {
  226. }
  227. -(void) onStatusChanged:(NSString*) status
  228. {
  229. }
  230. -(void) onLogUpdate
  231. {
  232. }
  233. #pragma mark - Notification
  234. - (void)OnUpdatePrice: (NSNotification *)notification
  235. {
  236. if(self.disable_refresh)
  237. return;
  238. NSDictionary* dict = [notification userInfo];
  239. NSArray* arr= dict[@"receiver"];
  240. //如果当前对象不在不是指定接收者,也不是广播通信,就返回。
  241. if([arr indexOfObject:[self class_name]]==NSNotFound&& arr.count>0)
  242. return;
  243. //如果当前对象本身不支持此动作,则返回
  244. if(![self respondsToSelector:@selector(refresh_price)])
  245. return;
  246. //如果当前已有的处理层级更高(reload),则不处理,交给reload 处理。
  247. if(self.notification_refreshLevel<NotificationRefreshLevelView)
  248. {
  249. if([self isVisible])
  250. {
  251. [self refresh_price];
  252. self.notification_refreshLevel = NotificationRefreshLevelNone;
  253. }
  254. else
  255. {
  256. self.notification_refreshLevel = NotificationRefreshLevelView;
  257. }
  258. }
  259. }
  260. - (void)OnRefreshUI: (NSNotification *)notification
  261. {
  262. if(self.disable_refresh)
  263. return;
  264. NSDictionary* dict = [notification userInfo];
  265. NSArray* arr= dict[@"receiver"];
  266. if([arr indexOfObject:[self class_name]]==NSNotFound&& arr.count>0)
  267. return;
  268. if(![self respondsToSelector:@selector(refresh_ui)])
  269. return;
  270. if(self.notification_refreshLevel<NotificationRefreshLevelView)
  271. {
  272. if([self isVisible])
  273. {
  274. [self refresh_ui];
  275. self.notification_refreshLevel = NotificationRefreshLevelNone;
  276. }
  277. else
  278. {
  279. self.notification_refreshLevel = NotificationRefreshLevelView;
  280. }
  281. }
  282. }
  283. - (void)OnReloadData: (NSNotification *)notification
  284. {
  285. if(self.disable_refresh)
  286. return;
  287. NSDictionary* dict = [notification userInfo];
  288. NSArray* arr= dict[@"receiver"];
  289. if([arr indexOfObject:[self class_name]]==NSNotFound&& arr.count>0)
  290. return;
  291. if(![self respondsToSelector:@selector(reload_data)])
  292. return;
  293. if(self.notification_refreshLevel<NotificationRefreshLevelData)
  294. {
  295. if([self isVisible])
  296. {
  297. [self reload_data];
  298. self.notification_refreshLevel = NotificationRefreshLevelNone;
  299. }
  300. else
  301. {
  302. self.notification_refreshLevel = NotificationRefreshLevelData;
  303. }
  304. }
  305. }
  306. - (void)OnClearData: (NSNotification *)notification
  307. {
  308. if(![self respondsToSelector:@selector(clear_data)])
  309. return;
  310. if([self isVisible])
  311. {
  312. if([self respondsToSelector:@selector(clear_data)])
  313. {
  314. [self clear_data];
  315. if([self respondsToSelector:@selector(refresh_ui)])
  316. {
  317. [self refresh_ui];
  318. self.notification_refreshLevel = NotificationRefreshLevelNone;
  319. }
  320. }
  321. }
  322. else
  323. {
  324. if([self respondsToSelector:@selector(clear_data)])
  325. {
  326. [self clear_data];
  327. self.notification_refreshLevel = NotificationRefreshLevelView;
  328. }
  329. }
  330. }
  331. #pragma mark - utils
  332. -(BOOL)isVisible
  333. {
  334. return (self.isViewLoaded && self.view.window);
  335. }
  336. - (NSString *)class_name {
  337. const char *class_name_ch = object_getClassName([self class]);
  338. return [NSString stringWithUTF8String:class_name_ch];
  339. }
  340. +(void) Notify:(NSString*) receiver Message:(NSString*)notification_msg
  341. {
  342. //NSArray* arr1 =[RAConvertor string2arr:@"abcde" separator:nil];
  343. // NSArray* arr2 =[RAConvertor string2arr:nil separator:nil];
  344. NSArray* arr_receiver = [RAConvertor string2arr:receiver separator:@","];
  345. NSMutableDictionary* dict = [NSMutableDictionary new];
  346. dict[@"receiver"]=arr_receiver;
  347. NSString* notificationname = nil;
  348. notificationname = notification_msg;
  349. NSNotification *notification =[NSNotification notificationWithName:notificationname object:nil userInfo:dict];
  350. //通过通知中心发送通知
  351. [[NSNotificationCenter defaultCenter] postNotification:notification];
  352. return;
  353. }
  354. @end