ActiveViewController.m 11 KB

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