NYFailSpecialExerciseDetailsViewModel.m 15 KB

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