RQProfileViewModel.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // RQProfileViewModel.m
  3. // RQCommon
  4. //
  5. // Created by 张嵘 on 2018/11/21.
  6. // Copyright © 2018 张嵘. All rights reserved.
  7. //
  8. #import "RQProfileViewModel.h"
  9. @interface RQProfileViewModel ()
  10. @property (nonatomic, readwrite, copy) NSArray *group1DicArr;
  11. @property (nonatomic, readwrite, copy) NSArray *group2DicArr;
  12. /// 登出的命令
  13. @property (nonatomic, readwrite, strong) RACCommand *logoutCommand;
  14. /// 登出回调
  15. @property (nonatomic, readwrite, strong) RACSubject *logoutSubject;
  16. @end
  17. @implementation RQProfileViewModel
  18. #pragma mark - Public Method
  19. - (instancetype)initWithServices:(id<RQViewModelServices>)services params:(NSDictionary *)params {
  20. if (self = [super initWithServices:services params:params]) {
  21. }
  22. return self;
  23. }
  24. - (void)initialize {
  25. [super initialize];
  26. self.title = @"用户中心";
  27. /// 退出回调
  28. self.logoutSubject = [RACSubject subject];
  29. ///配置数据
  30. [self rq_configureData];
  31. }
  32. #pragma mark - PrivateMethod
  33. - (void)rq_configureData {
  34. @weakify(self);
  35. /// 第零组
  36. RQCommonGroupViewModel *group0 = [RQCommonGroupViewModel groupViewModel];
  37. group0.footerHeight = 10;
  38. RQProfileUserInfoItemViewModel *profileUserInfoItemViewModel = [[RQProfileUserInfoItemViewModel alloc] init];
  39. RAC(profileUserInfoItemViewModel, headImageName) = RACObserve(RQ_USER_MANAGER, headImage);
  40. RAC(profileUserInfoItemViewModel, nickname) = RACObserve(RQ_USER_MANAGER, nickName);
  41. // profileUserInfoItemViewModel.telePhone = @"未绑定手机号";
  42. profileUserInfoItemViewModel.rowHeight = RQ_FIT_HORIZONTAL(74.f);
  43. profileUserInfoItemViewModel.operation = ^{
  44. @strongify(self);
  45. RQUserInfoViewModel *viewModel = [[RQUserInfoViewModel alloc] initWithServices:self.services params:nil];
  46. [self.services pushViewModel:viewModel animated:YES];
  47. };
  48. group0.itemViewModels = @[profileUserInfoItemViewModel];
  49. /// 第一组
  50. RQCommonGroupViewModel *group1 = [RQCommonGroupViewModel groupViewModel];
  51. group1.headerHeight = CGFLOAT_MIN;
  52. group1.footerHeight = 10;
  53. NSMutableArray *group1Arr = @[].mutableCopy;
  54. [self.group1DicArr.rac_sequence.signal subscribeNext:^(NSDictionary *dic) {
  55. RQCommonArrowItemViewModel *item = [[RQCommonArrowItemViewModel alloc] init];
  56. item.title = dic[@"title"];
  57. item.icon = dic[@"icon"];
  58. item.rowHeight = RQ_FIT_HORIZONTAL(54.f);
  59. if ([dic[@"title"] isEqualToString:@"激活码"]) {
  60. RAC(item , subtitle) = [RACObserve(RQ_USER_MANAGER, isVip) map:^id _Nullable(id _Nullable value) {
  61. return (RQ_USER_MANAGER.isVip > 0)? @"已激活" : @"";
  62. }];
  63. RAC(item, operation) = [RACObserve(RQ_USER_MANAGER, isVip) map:^id _Nullable(id _Nullable value) {
  64. return ^{
  65. if (RQ_USER_MANAGER.isVip > 0) {
  66. [MBProgressHUD rq_showTips:@"用户已激活VIP会员!"];
  67. } else {
  68. RQActivateVIPViewModel *activateVIPViewModel = [[RQActivateVIPViewModel alloc] initWithServices:self.services params:nil];
  69. [self.services pushViewModel:activateVIPViewModel animated:YES];
  70. }
  71. };
  72. }];
  73. } else if ([dic[@"title"] isEqualToString:@"免费体验"]) {
  74. item.operation = ^{
  75. RQFreeTryViewModel *freeTryViewModel = [[RQFreeTryViewModel alloc] initWithServices:self.services params:nil];
  76. [self.services pushViewModel:freeTryViewModel animated:YES];
  77. };
  78. } else if ([dic[@"title"] isEqualToString:@"资料下载"]) {
  79. item.operation = ^{
  80. RQLearningMaterialsViewModel *learningMaterialsViewModel = [[RQLearningMaterialsViewModel alloc] initWithServices:self.services params:nil];
  81. [self.services pushViewModel:learningMaterialsViewModel animated:YES];
  82. };
  83. }
  84. [group1Arr addObject:item];
  85. } completed:^{
  86. [RACObserve(RQ_USER_MANAGER, isLogin) subscribeNext:^(id _Nullable x) {
  87. if (RQ_USER_MANAGER.isLogin) {
  88. [RQ_COMMON_MANAGER checkVersionWithComplete:^(BOOL isOpen) {
  89. if (isOpen) {
  90. group1.itemViewModels = @[];
  91. group1.footerHeight = CGFLOAT_MIN;
  92. } else {
  93. [RACObserve(RQ_HTTP_Service, networkReachabilityStatus) subscribeNext:^(id _Nullable x) {
  94. if (RQ_HTTP_Service.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
  95. group1.footerHeight = 0;
  96. group1.itemViewModels = @[];
  97. } else {
  98. group1.footerHeight = 10;
  99. group1.itemViewModels = group1Arr.copy;
  100. }
  101. }];
  102. }
  103. }];
  104. } else {
  105. group1.itemViewModels = @[];
  106. group1.footerHeight = CGFLOAT_MIN;
  107. }
  108. }];
  109. }];
  110. /// 第二组
  111. RQCommonGroupViewModel *group2 = [RQCommonGroupViewModel groupViewModel];
  112. NSMutableArray *group2Arr = @[].mutableCopy;
  113. [self.group2DicArr.rac_sequence.signal subscribeNext:^(NSDictionary *dic) {
  114. RQCommonArrowItemViewModel *item = [[RQCommonArrowItemViewModel alloc] init];
  115. item.title = dic[@"title"];
  116. item.icon = dic[@"icon"];
  117. item.rowHeight = RQ_FIT_HORIZONTAL(54.f);
  118. if ([item.title isEqualToString:@"版本更新"]) {
  119. item.operation = ^{
  120. RQVersionUpdateViewModel *versionUpdateViewModel = [[RQVersionUpdateViewModel alloc] initWithServices:self.services params:nil];
  121. [self.services pushViewModel:versionUpdateViewModel animated:YES];
  122. };
  123. } else if ([item.title isEqualToString:@"反馈帮助"]) {
  124. item.operation = ^{
  125. RQFeedbackAndHelpViewModel *feedbackAndHelpViewModel = [[RQFeedbackAndHelpViewModel alloc] initWithServices:self.services params:nil];
  126. [self.services pushViewModel:feedbackAndHelpViewModel animated:YES];
  127. };
  128. } else if ([item.title isEqualToString:@"隐私政策"]) {
  129. item.operation = ^{
  130. NSString *urlStr = @"https://ys.zzxcx.net/xy_sdjk.html";
  131. RQWebViewModel *webViewModel = [[RQWebViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:@{RQViewModelRequestKey:urlStr}];
  132. [RQ_APPDELEGATE.services pushViewModel:webViewModel animated:YES];
  133. };
  134. } else if ([item.title isEqualToString:@"联系客服"]) {
  135. item.operation = ^{
  136. [RQ_WECHAT_MANAGER lianXiKeFu];
  137. };
  138. }
  139. [group2Arr addObject:item];
  140. } completed:^{
  141. /// 调试模式
  142. #if defined(DEBUG)||defined(_DEBUG)
  143. RQCommonArrowItemViewModel *debug = [RQCommonArrowItemViewModel itemViewModelWithTitle:@"打开/关闭调试器" icon:@"rq_profile_debug_25x25"];
  144. debug.rowHeight = RQ_FIT_HORIZONTAL(54.f);
  145. debug.operation = ^{
  146. [[RQDebugTouchView sharedInstance] setHide:![RQDebugTouchView sharedInstance].isHide];
  147. [RQSharedAppDelegate.window bringSubviewToFront:[RQDebugTouchView sharedInstance]];
  148. };
  149. [group2Arr addObject:debug];
  150. #else
  151. /// 发布模式
  152. #endif
  153. group2.itemViewModels = group2Arr.copy;
  154. }];
  155. /// 第三组
  156. RQCommonGroupViewModel *group3 = [RQCommonGroupViewModel groupViewModel];
  157. RQProfileLogouItemViewModel *profileLogouItemViewModel = [[RQProfileLogouItemViewModel alloc] initWithTitle:@"退出登录"];
  158. self.logoutCommand = profileLogouItemViewModel.logoutCommand;
  159. group3.itemViewModels = @[profileLogouItemViewModel];
  160. [[RACSignal combineLatest:@[RACObserve(group0, itemViewModels), RACObserve(group1, itemViewModels), RACObserve(group2, itemViewModels), RACObserve(group3, itemViewModels), RACObserve(RQ_USER_MANAGER, isVip)] reduce:^(NSArray *group0ItemViewModels, NSArray *group1ItemViewModels, NSArray *group2ItemViewModels, NSArray *group3ItemViewModels, NSNumber *isVip){
  161. RQCommonGroupViewModel *group = [RQCommonGroupViewModel groupViewModel];
  162. return @[group0, (RQ_USER_MANAGER.isVip > 2)? group : group1, group2, group3];
  163. }] subscribeNext:^(NSArray *allArr) {
  164. self.dataSource = allArr;
  165. }] ;
  166. }
  167. #pragma mark - LazyLoad
  168. - (NSArray *)group1DicArr {
  169. return @[
  170. @{@"title" : @"激活码", @"icon" : @"ActivationCode"},
  171. @{@"title" : @"免费体验", @"icon" : @"免费体验"},
  172. @{@"title" : @"资料下载", @"icon" : @"资料下载"},
  173. ];
  174. }
  175. - (NSArray *)group2DicArr {
  176. return @[
  177. @{@"title" : @"版本更新", @"icon" : @"VersionUpdate"},
  178. @{@"title" : @"反馈帮助", @"icon" : @"FeedbackHelp"},
  179. @{@"title" : @"隐私政策", @"icon" : @"PrivacyPolicy"},
  180. @{@"title" : @"联系客服", @"icon" : @"kf"},
  181. ];
  182. }
  183. @end