NYFailSpecialExerciseDetailsViewModel.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. //[NYFailGroupPointDataModel modelWithDictionary:self.dataDicts[keyStr]];
  69. NSMutableArray *mt_array = @[].mutableCopy;//解析json 数据daocell
  70. if(model){
  71. int indx = 1;
  72. for (NYFailItemPointDataModel *item in model.pointData) {
  73. NYFailSpecialExerciseDetailItemModel *itemModel = [[NYFailSpecialExerciseDetailItemModel alloc] initWithTitle:keyStr dataModel:item];
  74. itemModel.itemPointTitle = [NSString stringWithFormat:@"考点%d",indx];
  75. [mt_array addObject:itemModel];
  76. indx++;
  77. }
  78. }
  79. group0.itemViewModels = mt_array;
  80. return @[group0];
  81. }];
  82. }
  83. - (void)loadKeyDicts {
  84. // @weakify(self)
  85. if(self.datakeyDicts.count>0)return;
  86. self.datakeyDicts = [NSMutableDictionary dictionary];
  87. for (NSString *key in self.titleDict.allKeys) {
  88. NSString *key_value = self.titleDict[key];
  89. NYFailGroupPointDataModel *model = [NYFailGroupPointDataModel modelWithDictionary:self.dataDicts[key_value]];
  90. for (NYFailItemPointDataModel *item in model.pointData) {
  91. if(item.showType.intValue == 1) continue;
  92. if(item.con.count>0){//手势
  93. __block CGFloat itop_yOffset = 0; // 初始偏移量
  94. __block CGFloat yOffset = 0; // 初始偏移量
  95. __block CGFloat cellHeight = 35.f; // 每个 Cell 的高度(根据实际设计调整)
  96. CGFloat imgW = (RQ_SCREEN_WIDTH-22*2);
  97. for (int i= 0; i<item.con.count; i++) {
  98. NYFailItemPointDataCon *con = item.con[i];
  99. if(![con.title isEqualToString:@""]){
  100. itop_yOffset += cellHeight;
  101. item.iviewH += itop_yOffset;
  102. NSString *img_url = con.image;
  103. if(img_url.length>0){
  104. __block CGSize imageSize = CGSizeMake(imgW,cellHeight);
  105. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  106. // 处理耗时操作的代码块...
  107. imageSize = [UIImage getImageSizeWithURL:img_url];
  108. //通知主线程刷新
  109. dispatch_async(dispatch_get_main_queue(), ^{
  110. //回调或者说是通知主线程刷新,
  111. if (imageSize.width == 0 || imageSize.height == 0) {
  112. cellHeight = imgW * (175.f / 345.f);
  113. } else {
  114. cellHeight = imgW * (imageSize.height / imageSize.width);
  115. }
  116. yOffset += cellHeight;
  117. item.imgsH = yOffset;
  118. });
  119. });
  120. item.imgsH = yOffset;
  121. }
  122. }
  123. if(con.questions.count>0){
  124. CGFloat cellH = 0.f;
  125. for(int z = 0; z<con.questions.count; z++){//文字 + 图片
  126. NYFailItemPointDataQuesImage *img = con.questions[z];
  127. NSString *title_str = img.subTitle;
  128. NSString *img_url = img.image;
  129. if(title_str.length>0){
  130. cellH +=35.f;
  131. }
  132. if(img_url.length>0){
  133. __block CGSize imageSize = CGSizeMake(imgW,cellHeight);
  134. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  135. // 处理耗时操作的代码块...
  136. imageSize = [UIImage getImageSizeWithURL:img_url];
  137. //通知主线程刷新
  138. dispatch_async(dispatch_get_main_queue(), ^{
  139. //回调或者说是通知主线程刷新,
  140. if (imageSize.width == 0 || imageSize.height == 0) {
  141. cellHeight = imgW * (175.f / 345.f);
  142. } else {
  143. cellHeight = imgW * (imageSize.height / imageSize.width);
  144. }
  145. yOffset += cellHeight+12.f;
  146. item.imgsH = yOffset+60.f;
  147. });
  148. });
  149. item.imgsH = yOffset;
  150. }
  151. }
  152. // item.iviewH = cellH + 45.f;
  153. }
  154. }
  155. }
  156. if(item.tagquestions.count>0){//拘留
  157. CGFloat cellH = 0.f;
  158. for (int i= 0; i<item.tagquestions.count; i++) {
  159. NYFailItemPointDataQuestions *itemData = item.tagquestions[i];
  160. NSLog(@"tag=========%@",itemData.tag);
  161. for (int z= 0; z<itemData.tagCon.count; z++) {
  162. NYFailItemPointDataQuesTexts *texts = itemData.tagCon[z];
  163. NSString *title_str = texts.subTitle;
  164. CGSize str_size = [title_str rq_sizeWithFont:[UIFont boldSystemFontOfSize:15.f] limitWidth:(RQ_SCREEN_WIDTH-22*2)];
  165. if(str_size.height>cellH){
  166. cellH += str_size.height + 12.f;
  167. }else{
  168. cellH +=35.f;
  169. }
  170. if(texts.quesItem.count>0){
  171. cellH += 22.f*texts.quesItem.count;
  172. }
  173. }
  174. }
  175. item.iviewH = cellH + 45.f+45.f*item.tagquestions.count;
  176. }
  177. if(item.iquestions.count>0){//拘留
  178. __block CGFloat yOffset = 0; // 初始偏移量
  179. __block CGFloat cellHeight = 35.f; // 每个 Cell 的高度(根据实际设计调整)
  180. CGFloat imgW = (RQ_SCREEN_WIDTH-22*2);
  181. for (int i= 0; i<item.iquestions.count; i++) {
  182. NYFailItemPointDataQuesImage *img = item.iquestions[i];
  183. NSString *img_url = img.image;
  184. __block CGSize imageSize = CGSizeMake(imgW,cellHeight);
  185. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  186. // 处理耗时操作的代码块...
  187. imageSize = [UIImage getImageSizeWithURL:img_url];
  188. //通知主线程刷新
  189. dispatch_async(dispatch_get_main_queue(), ^{
  190. //回调或者说是通知主线程刷新,
  191. if (imageSize.width == 0 || imageSize.height == 0) {
  192. cellHeight = imgW * (175.f / 345.f);
  193. } else {
  194. cellHeight = imgW * (imageSize.height / imageSize.width);
  195. }
  196. yOffset += cellHeight+4.f;
  197. item.imgsH = yOffset;
  198. });
  199. });
  200. }
  201. }
  202. if(item.fquestions.count>0){//罚款//周期
  203. CGFloat cellH = 0.f;
  204. for (int i= 0; i<item.fquestions.count; i++) {
  205. NYFailItemPointDataQuesTexts *texts = item.fquestions[i];
  206. NSString *title_str = texts.subTitle;
  207. CGSize str_size = [title_str rq_sizeWithFont:[UIFont boldSystemFontOfSize:15.f] limitWidth:(RQ_SCREEN_WIDTH-22*2)];
  208. if(str_size.height>cellH){
  209. cellH += str_size.height + 12.f;
  210. }else{
  211. cellH +=35.f;
  212. }
  213. if(texts.quesItem.count>0){
  214. cellH += 22.f*texts.quesItem.count;
  215. }
  216. }
  217. item.iviewH = cellH + 20.f;
  218. }
  219. if(item.questions.quesTexts.count>0){
  220. CGFloat cellH = 0.f;
  221. for (int i= 0; i<item.questions.quesTexts.count; i++) {
  222. NYFailItemPointDataQuesTexts *texts = item.questions.quesTexts[i];
  223. NSString *title_str = texts.subTitle;
  224. CGSize str_size = [title_str rq_sizeWithFont:[UIFont boldSystemFontOfSize:15.f] limitWidth:(RQ_SCREEN_WIDTH-22*2)];
  225. if(str_size.height>cellH){
  226. cellH += str_size.height + 12.f;
  227. }else{
  228. cellH +=35.f;
  229. }
  230. if(texts.quesItem.count>0){
  231. cellH += 22.f*texts.quesItem.count;
  232. }
  233. }
  234. item.iviewH = cellH + 20.f;
  235. }
  236. if(item.questions.images.count > 0){
  237. __block CGFloat yOffset = 0; // 初始偏移量
  238. __block CGFloat cellHeight = 35.f; // 每个 Cell 的高度(根据实际设计调整)
  239. CGFloat imgW = (RQ_SCREEN_WIDTH-22*2);
  240. for (int i= 0; i<item.questions.images.count; i++) {
  241. NSString *img_url = item.questions.images[i];
  242. __block CGSize imageSize = CGSizeMake(imgW,cellHeight);
  243. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  244. // 处理耗时操作的代码块...
  245. imageSize = [UIImage getImageSizeWithURL:img_url];
  246. //通知主线程刷新
  247. dispatch_async(dispatch_get_main_queue(), ^{
  248. //回调或者说是通知主线程刷新,
  249. if (imageSize.width == 0 || imageSize.height == 0) {
  250. cellHeight = imgW * (175.f / 345.f);
  251. } else {
  252. cellHeight = imgW * (imageSize.height / imageSize.width);
  253. }
  254. yOffset += cellHeight;
  255. item.imgsH = yOffset;
  256. });
  257. });
  258. }
  259. }
  260. }
  261. self.datakeyDicts[key_value] = model;
  262. }
  263. }
  264. // 加载 JSON 文件并解析
  265. - (void)loadPointData {
  266. // 获取资源文件路径
  267. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"pointData" ofType:@"json"];
  268. if (filePath) {
  269. // 读取文件数据
  270. NSData *data = [NSData dataWithContentsOfFile:filePath];
  271. if (data) {
  272. NSError *error = nil;
  273. // 使用 JSONSerialization 解析 JSON 数据
  274. self.dataDicts = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  275. NSLog(@"%@",self.dataDicts.allKeys);
  276. if (error) {
  277. NSLog(@"JSON解析错误: %@", error.localizedDescription);
  278. }
  279. } else {
  280. NSLog(@"文件读取失败");
  281. }
  282. } else {
  283. NSLog(@"文件路径未找到");
  284. }
  285. }
  286. - (NSDictionary *)titleDict{
  287. if(!_titleDict){
  288. _titleDict = @{@"扣分":@"koufen",
  289. @"驾驶证":@"jiashizheng",
  290. @"罚款":@"fakuan",
  291. @"判刑扣留":@"juliu",
  292. @"安全常识":@"anquanchangshi",
  293. @"交警手势":@"shoushi",
  294. @"周期":@"zhouqi",
  295. @"特殊天气":@"tianqi",
  296. @"速度灯光":@"dengguang",
  297. @"通行原则":@"tongxing"};
  298. }
  299. return _titleDict;
  300. }
  301. @end