NYFailSpecialExerciseDetailsViewModel.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. #import "RQVideoModule.h"
  10. @interface NYFailSpecialExerciseDetailsViewModel ()
  11. @property (nonatomic, readwrite, assign) RQHomePageSubjectType homePageSubjectType;
  12. @property (nonatomic, readwrite, strong) NSDictionary *dataDicts;
  13. @property (nonatomic, readwrite, strong) NSDictionary *titleDict;
  14. @property (nonatomic, readwrite, strong) NSMutableDictionary *datakeyDicts;
  15. @end
  16. @implementation NYFailSpecialExerciseDetailsViewModel
  17. #pragma mark - Public Method
  18. - (instancetype)initWithServices:(id<RQViewModelServices>)services params:(NSDictionary *)params {
  19. if (self = [super initWithServices:services params:params]) {
  20. self.homePageSubjectType = [params[RQHomePageSubjectTypeKey] integerValue];
  21. self.title = params[RQHomePageSpTitleKey];
  22. }
  23. return self;
  24. }
  25. - (void)initialize {
  26. [super initialize];
  27. [self loadPointData];
  28. [self loadKeyDicts];
  29. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  30. [self rq_configureData];
  31. });
  32. // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  33. // [self postVideoDate];
  34. // });
  35. }
  36. - (void)postVideoDate {
  37. NYFailSpecialVideoGroupViewModel *model = [self.dataSource firstObject];
  38. if(model.videoDate.vipPublicityMediumUrl.length>0){
  39. VideosItem *videosItem = [[VideosItem alloc ] init];
  40. videosItem.video_url = model.videoDate.vipPublicityMediumUrl;
  41. videosItem.coverFileUrl = model.videoDate.poster;
  42. videosItem.title = model.videoDate.endButtonText;
  43. videosItem.videoDescribe = model.videoDate.endButtonText;
  44. videosItem.videoType = RQVideoType_Default;
  45. videosItem.permission = 1;
  46. videosItem.subject = 0;//科目一
  47. [RQNotificationCenter postNotificationName:RQPlayVideoUrlNotification object:@{
  48. RQPlayVideoItemKey : videosItem,
  49. RQPlayVideoItemArrKey : @[videosItem],
  50. }];
  51. }
  52. }
  53. #pragma mark - ConfigureData
  54. - (void)rq_configureData {
  55. /// 数据源
  56. RAC(self, dataSource) = [RACSignal
  57. combineLatest:@[RACObserve(RQ_USER_MANAGER, isCykh)]
  58. reduce:^(NSNumber *isCykh) {
  59. NYFailSpecialVideoGroupViewModel *group0 = [NYFailSpecialVideoGroupViewModel groupViewModel];
  60. NSString *keyStr = self.titleDict[self.title];
  61. NYFailGroupPointDataModel *model = self.datakeyDicts[keyStr];
  62. group0.videoDate = model.videoDate;
  63. // if(model.videoDate.vipPublicityMediumUrl.length>0){
  64. // group0.headerHeight = RQ_FIT_HORIZONTAL(218);
  65. // }else{
  66. // group0.headerHeight = 0;
  67. // }
  68. group0.headerHeight = 0;
  69. //[NYFailGroupPointDataModel modelWithDictionary:self.dataDicts[keyStr]];
  70. NSMutableArray *mt_array = @[].mutableCopy;//解析json 数据daocell
  71. if(model){
  72. int indx = 1;
  73. for (NYFailItemPointDataModel *item in model.pointData) {
  74. if(item.questionIds.length>0){//有题目才显示
  75. NYFailSpecialExerciseDetailItemModel *itemModel = [[NYFailSpecialExerciseDetailItemModel alloc] initWithTitle:keyStr dataModel:item];
  76. itemModel.itemPointTitle = [NSString stringWithFormat:@"考点%d",indx];
  77. [mt_array addObject:itemModel];
  78. indx++;
  79. }
  80. }
  81. }
  82. group0.itemViewModels = mt_array;
  83. return @[group0];
  84. }];
  85. }
  86. - (void)loadKeyDicts {
  87. // @weakify(self)
  88. if(self.datakeyDicts.count>0)return;
  89. self.datakeyDicts = [NSMutableDictionary dictionary];
  90. for (NSString *key in self.titleDict.allKeys) {
  91. NSString *key_value = self.titleDict[key];
  92. NYFailGroupPointDataModel *model = [NYFailGroupPointDataModel modelWithDictionary:self.dataDicts[key_value]];
  93. for (NYFailItemPointDataModel *item in model.pointData) {
  94. if(item.showType.intValue == 1) continue;
  95. if(item.con.count>0){//手势
  96. __block CGFloat itop_yOffset = 0; // 初始偏移量
  97. __block CGFloat yOffset = 0; // 初始偏移量
  98. __block CGFloat cellHeight = 35.f; // 每个 Cell 的高度(根据实际设计调整)
  99. CGFloat imgW = (RQ_SCREEN_WIDTH-22*2);
  100. for (int i= 0; i<item.con.count; i++) {
  101. NYFailItemPointDataCon *con = item.con[i];
  102. if(![con.title isEqualToString:@""]){
  103. itop_yOffset += cellHeight;
  104. item.iviewH += itop_yOffset;
  105. NSString *img_url = con.image;
  106. if(img_url.length>0){
  107. __block CGSize imageSize = CGSizeMake(imgW,cellHeight);
  108. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  109. // 处理耗时操作的代码块...
  110. imageSize = [UIImage getImageSizeWithURL:img_url];
  111. //通知主线程刷新
  112. dispatch_async(dispatch_get_main_queue(), ^{
  113. //回调或者说是通知主线程刷新,
  114. if (imageSize.width == 0 || imageSize.height == 0) {
  115. cellHeight = imgW * (175.f / 345.f);
  116. } else {
  117. cellHeight = imgW * (imageSize.height / imageSize.width);
  118. }
  119. yOffset += cellHeight;
  120. item.imgsH = yOffset;
  121. });
  122. });
  123. item.imgsH = yOffset;
  124. }
  125. }
  126. if(con.questions.count>0){
  127. CGFloat cellH = 0.f;
  128. for(int z = 0; z<con.questions.count; z++){//文字 + 图片
  129. NYFailItemPointDataQuesImage *img = con.questions[z];
  130. NSString *title_str = img.subTitle;
  131. NSString *img_url = img.image;
  132. if(title_str.length>0){
  133. cellH +=35.f;
  134. }
  135. if(img_url.length>0){
  136. __block CGSize imageSize = CGSizeMake(imgW,cellHeight);
  137. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  138. // 处理耗时操作的代码块...
  139. imageSize = [UIImage getImageSizeWithURL:img_url];
  140. //通知主线程刷新
  141. dispatch_async(dispatch_get_main_queue(), ^{
  142. //回调或者说是通知主线程刷新,
  143. if (imageSize.width == 0 || imageSize.height == 0) {
  144. cellHeight = imgW * (175.f / 345.f);
  145. } else {
  146. cellHeight = imgW * (imageSize.height / imageSize.width);
  147. }
  148. yOffset += cellHeight+12.f;
  149. item.imgsH = yOffset+60.f;
  150. });
  151. });
  152. item.imgsH = yOffset;
  153. }
  154. }
  155. // item.iviewH = cellH + 45.f;
  156. }
  157. }
  158. }
  159. if(item.tagquestions.count>0){
  160. CGFloat cellH = 0.f;
  161. for (int i= 0; i<item.tagquestions.count; i++) {
  162. NYFailItemPointDataQuestions *itemData = item.tagquestions[i];
  163. NSLog(@"tag=========%@",itemData.tag);
  164. for (int z= 0; z<itemData.tagCon.count; z++) {
  165. NYFailItemPointDataQuesTexts *texts = itemData.tagCon[z];
  166. NSString *title_str = texts.subTitle;
  167. CGSize str_size = [title_str rq_sizeWithFont:[UIFont boldSystemFontOfSize:15.f] limitWidth:(RQ_SCREEN_WIDTH-22*2)];
  168. if(str_size.height>cellH){
  169. cellH += str_size.height + 12.f;
  170. }else{
  171. cellH +=35.f;
  172. }
  173. if(texts.quesItem.count>0){
  174. cellH += 22.f*texts.quesItem.count;
  175. }
  176. }
  177. }
  178. item.iviewH = cellH + 45.f+45.f*item.tagquestions.count;
  179. }
  180. if(item.iquestions.count>0){//拘留
  181. __block CGFloat yOffset = 0; // 初始偏移量
  182. __block CGFloat cellHeight = 35.f; // 每个 Cell 的高度(根据实际设计调整)
  183. CGFloat imgW = (RQ_SCREEN_WIDTH-22*2);
  184. for (int i= 0; i<item.iquestions.count; i++) {
  185. NYFailItemPointDataQuesImage *img = item.iquestions[i];
  186. NSString *img_url = img.image;
  187. __block CGSize imageSize = CGSizeMake(imgW,cellHeight);
  188. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  189. // 处理耗时操作的代码块...
  190. imageSize = [UIImage getImageSizeWithURL:img_url];
  191. //通知主线程刷新
  192. dispatch_async(dispatch_get_main_queue(), ^{
  193. //回调或者说是通知主线程刷新,
  194. if (imageSize.width == 0 || imageSize.height == 0) {
  195. cellHeight = imgW * (175.f / 345.f);
  196. } else {
  197. cellHeight = imgW * (imageSize.height / imageSize.width);
  198. }
  199. yOffset += cellHeight+4.f;
  200. item.imgsH = yOffset;
  201. });
  202. });
  203. }
  204. }
  205. if(item.fquestions.count>0){//罚款//周期//特殊天气
  206. CGFloat cellH = 0.f;
  207. for (int i= 0; i<item.fquestions.count; i++) {
  208. NYFailItemPointDataQuesTexts *texts = item.fquestions[i];
  209. NSString *title_str = texts.subTitle;
  210. CGSize str_size = [title_str rq_sizeWithFont:[UIFont boldSystemFontOfSize:15.f] limitWidth:(RQ_SCREEN_WIDTH-22*2)];
  211. if(str_size.height>cellH){
  212. cellH += str_size.height + 12.f;
  213. }else{
  214. cellH +=35.f;
  215. }
  216. if(texts.quesItem.count>0){
  217. for (int z= 0; z<texts.quesItem.count; z++) {
  218. NSString *itemStr = texts.quesItem[z];
  219. CGSize itemstr_size = [itemStr rq_sizeWithFont:[UIFont systemFontOfSize:11.f] limitWidth:(RQ_SCREEN_WIDTH-22*2-10)];
  220. if(itemstr_size.height>20.f){
  221. cellH += itemstr_size.height + 10.f;
  222. }else{
  223. cellH +=22.f;
  224. }
  225. }
  226. }
  227. }
  228. item.iviewH = cellH + 20.f;
  229. }
  230. if(item.questions.quesTexts.count>0){
  231. CGFloat cellH = 0.f;
  232. for (int i= 0; i<item.questions.quesTexts.count; i++) {
  233. NYFailItemPointDataQuesTexts *texts = item.questions.quesTexts[i];
  234. NSString *title_str = texts.subTitle;
  235. CGSize str_size = [title_str rq_sizeWithFont:[UIFont boldSystemFontOfSize:15.f] limitWidth:(RQ_SCREEN_WIDTH-22*2)];
  236. if(str_size.height>cellH){
  237. cellH += str_size.height + 12.f;
  238. }else{
  239. cellH +=35.f;
  240. }
  241. if(texts.quesItem.count>0){
  242. cellH += 22.f*texts.quesItem.count;
  243. }
  244. }
  245. item.iviewH = cellH + 20.f;
  246. }
  247. if(item.questions.images.count > 0){
  248. __block CGFloat yOffset = 0; // 初始偏移量
  249. __block CGFloat cellHeight = 35.f; // 每个 Cell 的高度(根据实际设计调整)
  250. CGFloat imgW = (RQ_SCREEN_WIDTH-22*2);
  251. for (int i= 0; i<item.questions.images.count; i++) {
  252. NSString *img_url = item.questions.images[i];
  253. __block CGSize imageSize = CGSizeMake(imgW,cellHeight);
  254. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  255. // 处理耗时操作的代码块...
  256. imageSize = [UIImage getImageSizeWithURL:img_url];
  257. //通知主线程刷新
  258. dispatch_async(dispatch_get_main_queue(), ^{
  259. //回调或者说是通知主线程刷新,
  260. if (imageSize.width == 0 || imageSize.height == 0) {
  261. cellHeight = imgW * (175.f / 345.f);
  262. } else {
  263. cellHeight = imgW * (imageSize.height / imageSize.width);
  264. }
  265. yOffset += cellHeight;
  266. item.imgsH = yOffset;
  267. });
  268. });
  269. }
  270. }
  271. }
  272. self.datakeyDicts[key_value] = model;
  273. }
  274. }
  275. // 加载 JSON 文件并解析
  276. - (void)loadPointData {
  277. // 获取资源文件路径
  278. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"pointData" ofType:@"json"];
  279. if (filePath) {
  280. // 读取文件数据
  281. NSData *data = [NSData dataWithContentsOfFile:filePath];
  282. if (data) {
  283. NSError *error = nil;
  284. // 使用 JSONSerialization 解析 JSON 数据
  285. self.dataDicts = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  286. NSLog(@"%@",self.dataDicts.allKeys);
  287. if (error) {
  288. NSLog(@"JSON解析错误: %@", error.localizedDescription);
  289. }
  290. } else {
  291. NSLog(@"文件读取失败");
  292. }
  293. } else {
  294. NSLog(@"文件路径未找到");
  295. }
  296. }
  297. - (NSDictionary *)titleDict{
  298. if(!_titleDict){
  299. _titleDict = @{@"扣分":@"koufen",
  300. @"驾驶证":@"jiashizheng",
  301. @"罚款":@"fakuan",
  302. @"判刑扣留":@"juliu",
  303. @"安全常识":@"anquanchangshi",
  304. @"交警手势":@"shoushi",
  305. @"周期":@"zhouqi",
  306. @"特殊天气":@"tianqi",
  307. @"速度灯光":@"dengguang",
  308. @"通行原则":@"tongxing"};
  309. }
  310. return _titleDict;
  311. }
  312. @end