ActiveViewController.m 10 KB

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