NYFailSpecialExerciseDetailsViewModel.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. //
  2. // NYFailSpecialExerciseDetailsViewModel.m
  3. // jiaPei
  4. //
  5. // Created by Ning.ge on 2024/12/12.
  6. // Copyright © 2024 JCZ. All rights reserved.
  7. //
  8. #import "NYFailSpecialExerciseDetailsViewModel.h"
  9. @interface NYFailSpecialExerciseDetailsViewModel ()
  10. @property (nonatomic, readwrite, assign) RQHomePageSubjectType homePageSubjectType;
  11. @property (nonatomic, readwrite, strong) NSDictionary *dataDicts;
  12. @property (nonatomic, readwrite, strong) NSDictionary *titleDict;
  13. @property (nonatomic, readwrite, strong) NSMutableDictionary *datakeyDicts;
  14. @end
  15. @implementation NYFailSpecialExerciseDetailsViewModel
  16. #pragma mark - Public Method
  17. - (instancetype)initWithServices:(id<RQViewModelServices>)services params:(NSDictionary *)params {
  18. if (self = [super initWithServices:services params:params]) {
  19. self.homePageSubjectType = [params[RQHomePageSubjectTypeKey] integerValue];
  20. self.title = params[RQHomePageSpTitleKey];
  21. }
  22. return self;
  23. }
  24. - (void)initialize {
  25. [super initialize];
  26. [self loadPointData];
  27. [self loadKeyDicts];
  28. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  29. [self rq_configureData];
  30. });
  31. }
  32. #pragma mark - ConfigureData
  33. - (void)rq_configureData {
  34. /// 数据源
  35. RAC(self, dataSource) = [RACSignal
  36. combineLatest:@[RACObserve(RQ_USER_MANAGER, isCykh)]
  37. reduce:^(NSNumber *isCykh) {
  38. NYFailSpecialExerciseGroupViewModel *group0 = [NYFailSpecialExerciseGroupViewModel groupViewModel];
  39. group0.headerHeight = RQ_FIT_HORIZONTAL(130);
  40. NSString *keyStr = self.titleDict[self.title];
  41. NYFailGroupPointDataModel *model = self.datakeyDicts[keyStr];
  42. //[NYFailGroupPointDataModel modelWithDictionary:self.dataDicts[keyStr]];
  43. NSMutableArray *mt_array = @[].mutableCopy;//解析json 数据daocell
  44. if(model){
  45. int indx = 1;
  46. for (NYFailItemPointDataModel *item in model.pointData) {
  47. NYFailSpecialExerciseDetailItemModel *itemModel = [[NYFailSpecialExerciseDetailItemModel alloc] initWithTitle:keyStr dataModel:item];
  48. itemModel.itemPointTitle = [NSString stringWithFormat:@"考点%d",indx];
  49. [mt_array addObject:itemModel];
  50. indx++;
  51. }
  52. }
  53. group0.itemViewModels = mt_array;
  54. return @[group0];
  55. }];
  56. }
  57. - (void)loadKeyDicts {
  58. // @weakify(self)
  59. if(self.datakeyDicts.count>0)return;
  60. self.datakeyDicts = [NSMutableDictionary dictionary];
  61. for (NSString *key in self.titleDict.allKeys) {
  62. NSString *key_value = self.titleDict[key];
  63. NYFailGroupPointDataModel *model = [NYFailGroupPointDataModel modelWithDictionary:self.dataDicts[key_value]];
  64. for (NYFailItemPointDataModel *item in model.pointData) {
  65. if(item.showType.intValue == 1) continue;
  66. if(item.con.count>0){//手势
  67. __block CGFloat yOffset = 0; // 初始偏移量
  68. __block CGFloat cellHeight = 35.f; // 每个 Cell 的高度(根据实际设计调整)
  69. CGFloat imgW = (RQ_SCREEN_WIDTH-22*2);
  70. for (int i= 0; i<item.con.count; i++) {
  71. NYFailItemPointDataCon *con = item.con[i];
  72. NSString *img_url = con.image;
  73. if(img_url.length>0){
  74. __block CGSize imageSize = CGSizeMake(imgW,cellHeight);
  75. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  76. // 处理耗时操作的代码块...
  77. imageSize = [UIImage getImageSizeWithURL:img_url];
  78. //通知主线程刷新
  79. dispatch_async(dispatch_get_main_queue(), ^{
  80. //回调或者说是通知主线程刷新,
  81. if (imageSize.width == 0 || imageSize.height == 0) {
  82. cellHeight = imgW * (175.f / 345.f);
  83. } else {
  84. cellHeight = imgW * (imageSize.height / imageSize.width);
  85. }
  86. yOffset += cellHeight+4.f;
  87. item.imgsH = yOffset;
  88. });
  89. });
  90. }
  91. }
  92. item.imgsH = yOffset;
  93. }
  94. if(item.tagquestions.count>0){//拘留
  95. CGFloat cellH = 0.f;
  96. for (int i= 0; i<item.tagquestions.count; i++) {
  97. NYFailItemPointDataQuestions *itemData = item.tagquestions[i];
  98. NSLog(@"tag=========%@",itemData.tag);
  99. for (int z= 0; z<itemData.tagCon.count; z++) {
  100. NYFailItemPointDataQuesTexts *texts = itemData.tagCon[z];
  101. NSString *title_str = texts.subTitle;
  102. CGSize str_size = [title_str rq_sizeWithFont:[UIFont boldSystemFontOfSize:15.f] limitWidth:(RQ_SCREEN_WIDTH-22*2)];
  103. if(str_size.height>cellH){
  104. cellH += str_size.height + 12.f;
  105. }else{
  106. cellH +=35.f;
  107. }
  108. if(texts.quesItem.count>0){
  109. cellH += 22.f*texts.quesItem.count;
  110. }
  111. }
  112. }
  113. item.iviewH = cellH + 45.f+45.f*item.tagquestions.count;
  114. }
  115. if(item.iquestions.count>0){//拘留
  116. __block CGFloat yOffset = 0; // 初始偏移量
  117. __block CGFloat cellHeight = 35.f; // 每个 Cell 的高度(根据实际设计调整)
  118. CGFloat imgW = (RQ_SCREEN_WIDTH-22*2);
  119. for (int i= 0; i<item.iquestions.count; i++) {
  120. NYFailItemPointDataQuesImage *img = item.iquestions[i];
  121. NSString *img_url = img.image;
  122. __block CGSize imageSize = CGSizeMake(imgW,cellHeight);
  123. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  124. // 处理耗时操作的代码块...
  125. imageSize = [UIImage getImageSizeWithURL:img_url];
  126. //通知主线程刷新
  127. dispatch_async(dispatch_get_main_queue(), ^{
  128. //回调或者说是通知主线程刷新,
  129. if (imageSize.width == 0 || imageSize.height == 0) {
  130. cellHeight = imgW * (175.f / 345.f);
  131. } else {
  132. cellHeight = imgW * (imageSize.height / imageSize.width);
  133. }
  134. yOffset += cellHeight+4.f;
  135. item.imgsH = yOffset;
  136. });
  137. });
  138. }
  139. }
  140. if(item.fquestions.count>0){//罚款
  141. CGFloat cellH = 0.f;
  142. for (int i= 0; i<item.fquestions.count; i++) {
  143. NYFailItemPointDataQuesTexts *texts = item.fquestions[i];
  144. NSString *title_str = texts.subTitle;
  145. CGSize str_size = [title_str rq_sizeWithFont:[UIFont boldSystemFontOfSize:15.f] limitWidth:(RQ_SCREEN_WIDTH-22*2)];
  146. if(str_size.height>cellH){
  147. cellH += str_size.height + 12.f;
  148. }else{
  149. cellH +=35.f;
  150. }
  151. if(texts.quesItem.count>0){
  152. cellH += 22.f*texts.quesItem.count;
  153. }
  154. }
  155. item.iviewH = cellH + 20.f;
  156. }
  157. if(item.questions.quesTexts.count>0){
  158. CGFloat cellH = 0.f;
  159. for (int i= 0; i<item.questions.quesTexts.count; i++) {
  160. NYFailItemPointDataQuesTexts *texts = item.questions.quesTexts[i];
  161. NSString *title_str = texts.subTitle;
  162. CGSize str_size = [title_str rq_sizeWithFont:[UIFont boldSystemFontOfSize:15.f] limitWidth:(RQ_SCREEN_WIDTH-22*2)];
  163. if(str_size.height>cellH){
  164. cellH += str_size.height + 12.f;
  165. }else{
  166. cellH +=35.f;
  167. }
  168. if(texts.quesItem.count>0){
  169. cellH += 22.f*texts.quesItem.count;
  170. }
  171. }
  172. item.iviewH = cellH + 20.f;
  173. }
  174. if(item.questions.images.count > 0){
  175. __block CGFloat yOffset = 0; // 初始偏移量
  176. __block CGFloat cellHeight = 35.f; // 每个 Cell 的高度(根据实际设计调整)
  177. CGFloat imgW = (RQ_SCREEN_WIDTH-22*2);
  178. for (int i= 0; i<item.questions.images.count; i++) {
  179. NSString *img_url = item.questions.images[i];
  180. __block CGSize imageSize = CGSizeMake(imgW,cellHeight);
  181. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  182. // 处理耗时操作的代码块...
  183. imageSize = [UIImage getImageSizeWithURL:img_url];
  184. //通知主线程刷新
  185. dispatch_async(dispatch_get_main_queue(), ^{
  186. //回调或者说是通知主线程刷新,
  187. if (imageSize.width == 0 || imageSize.height == 0) {
  188. cellHeight = imgW * (175.f / 345.f);
  189. } else {
  190. cellHeight = imgW * (imageSize.height / imageSize.width);
  191. }
  192. yOffset += cellHeight;
  193. item.imgsH = yOffset;
  194. });
  195. });
  196. }
  197. }
  198. }
  199. self.datakeyDicts[key_value] = model;
  200. }
  201. }
  202. // 加载 JSON 文件并解析
  203. - (void)loadPointData {
  204. // 获取资源文件路径
  205. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"pointData" ofType:@"json"];
  206. if (filePath) {
  207. // 读取文件数据
  208. NSData *data = [NSData dataWithContentsOfFile:filePath];
  209. if (data) {
  210. NSError *error = nil;
  211. // 使用 JSONSerialization 解析 JSON 数据
  212. self.dataDicts = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  213. NSLog(@"%@",self.dataDicts.allKeys);
  214. if (error) {
  215. NSLog(@"JSON解析错误: %@", error.localizedDescription);
  216. }
  217. } else {
  218. NSLog(@"文件读取失败");
  219. }
  220. } else {
  221. NSLog(@"文件路径未找到");
  222. }
  223. }
  224. - (NSDictionary *)titleDict{
  225. if(!_titleDict){
  226. _titleDict = @{@"扣分":@"koufen",
  227. @"驾驶证":@"jiashizheng",
  228. @"罚款":@"fakuan",
  229. @"判刑扣留":@"juliu",
  230. @"安全常识":@"anquanchangshi",
  231. @"交警手势":@"shoushi",
  232. @"周期":@"zhouqi",
  233. @"特殊天气":@"tianqi",
  234. @"速度灯光":@"dengguang",
  235. @"通行原则":@"tongxing"};
  236. }
  237. return _titleDict;
  238. }
  239. @end