RQBaseViewController.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // RQBaseViewController.m
  3. // RQCommon
  4. //
  5. // Created by 张嵘 on 2018/11/13.
  6. // Copyright © 2018 张嵘. All rights reserved.
  7. //
  8. #import "RQBaseViewController.h"
  9. @interface RQBaseViewController ()
  10. @property (nonatomic, readwrite, strong) RQBaseViewModel *viewModel;
  11. @end
  12. @implementation RQBaseViewController
  13. - (void)dealloc {
  14. /// 销毁时保存数据
  15. // [SBPhotoManager configureSelectOriginalPhoto:_isSelectOriginalPhoto];
  16. RQDealloc;
  17. }
  18. // when `BaseViewController ` created and call `viewDidLoad` method , execute `bindViewModel` method
  19. + (instancetype)allocWithZone:(struct _NSZone *)zone {
  20. RQBaseViewController *viewController = [super allocWithZone:zone];
  21. @weakify(viewController)
  22. [[viewController
  23. rac_signalForSelector:@selector(viewDidLoad)]
  24. subscribeNext:^(id x) {
  25. @strongify(viewController)
  26. [viewController bindViewModel];
  27. }];
  28. return viewController;
  29. }
  30. - (instancetype)initWithViewModel:(RQBaseViewModel *)viewModel {
  31. self = [super init];
  32. if (self) {
  33. self.viewModel = viewModel;
  34. }
  35. return self;
  36. }
  37. - (void)viewWillAppear:(BOOL)animated{
  38. [super viewWillAppear:animated];
  39. /// 隐藏导航栏细线
  40. self.viewModel.prefersNavigationBarBottomLineHidden?[(RQBaseNavigationController *)self.navigationController rq_hideNavigationBottomLine]:[(RQBaseNavigationController *)self.navigationController rq_showNavigationBottomLine];
  41. /// 配置键盘
  42. IQKeyboardManager.sharedManager.enable = self.viewModel.keyboardEnable;
  43. IQKeyboardManager.sharedManager.shouldResignOnTouchOutside = self.viewModel.shouldResignOnTouchOutside;
  44. IQKeyboardManager.sharedManager.keyboardDistanceFromTextField = self.viewModel.keyboardDistanceFromTextField;
  45. /// 这里做友盟统计
  46. // [MobClick beginLogPageView:SBPageName(self)];
  47. }
  48. - (void)viewWillDisappear:(BOOL)animated {
  49. [super viewWillDisappear:animated];
  50. [self.viewModel.willDisappearSignal sendNext:nil];
  51. // Being popped, take a snapshot
  52. if ([self isMovingFromParentViewController]) {
  53. self.snapshot = [self.navigationController.view snapshotViewAfterScreenUpdates:NO];
  54. }
  55. /// 这里做友盟统计
  56. // [MobClick endLogPageView:SBPageName(self)];
  57. }
  58. - (void)viewDidLoad {
  59. [super viewDidLoad];
  60. /// ignore adjust auto scroll 64
  61. /// RQ: 适配 iOS 11.0 ,iOS11以后,控制器的automaticallyAdjustsScrollViewInsets已经废弃,所以默认就会是YES
  62. /// iOS 11新增:adjustContentInset 和 contentInsetAdjustmentBehavior 来处理滚动区域
  63. ///
  64. if (@available(iOS 11.0, *)) {
  65. self.automaticallyAdjustsScrollViewInsets = YES;
  66. }else{
  67. self.automaticallyAdjustsScrollViewInsets = NO;
  68. }
  69. self.extendedLayoutIncludesOpaqueBars = YES;
  70. /// backgroundColor
  71. self.view.backgroundColor = RQ_MAIN_BACKGROUNDCOLOR;
  72. /// 导航栏隐藏 只能在ViewDidLoad里面加载,无法动态
  73. self.fd_prefersNavigationBarHidden = self.viewModel.prefersNavigationBarHidden;
  74. /// pop手势
  75. self.fd_interactivePopDisabled = self.viewModel.interactivePopDisabled;
  76. /// 先记录
  77. // self.isSelectOriginalPhoto = [SBPhotoManager isSelectOriginalPhoto];
  78. /// 后重置
  79. // [SBPhotoManager configureSelectOriginalPhoto:NO];
  80. }
  81. // bind the viewModel
  82. - (void)bindViewModel{
  83. /// set navgation title
  84. /// RQ Fixed: 这里只是单纯设置导航栏的title。 不然以免self.title同时设置了navigatiItem.title, 同时又设置了tabBarItem.title
  85. NSLog(@"--- %@" , self.viewModel.title);
  86. @weakify(self);
  87. RAC(self.titleView , title) = [RACObserve(self, viewModel.title) deliverOnMainThread];
  88. /// 绑定错误信息
  89. [self.viewModel.errors subscribeNext:^(NSError *error) {
  90. /// 这里可以统一处理某个错误,例如用户授权失效的的操作
  91. NSLog(@"...错误...");
  92. }];
  93. /// 动态改变
  94. [[[RACObserve(self.viewModel, interactivePopDisabled) distinctUntilChanged] deliverOnMainThread] subscribeNext:^(NSNumber * x) {
  95. @strongify(self);
  96. self.fd_interactivePopDisabled = x.boolValue;
  97. }];
  98. }
  99. #pragma mark - Orientation
  100. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {return UIInterfaceOrientationMaskPortrait;}
  101. - (BOOL)shouldAutorotate {return YES;}
  102. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {return UIInterfaceOrientationPortrait;}
  103. #pragma mark - Status bar
  104. //- (BOOL)prefersStatusBarHidden { return NO; }
  105. //- (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleDefault; }
  106. //- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation { return UIStatusBarAnimationFade; }
  107. @end