CoursewareViewModel.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // CoursewareViewModel.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by 王智 on 2022/11/15.
  6. //
  7. #import "CoursewareViewModel.h"
  8. #import "CoursewareListModel.h"
  9. @interface CoursewareViewModel ()
  10. @property (nonatomic, assign) NSInteger pages; // 页数
  11. @property (nonatomic, assign) NSInteger rows; // 每页条数
  12. @property (nonatomic, strong) NSMutableArray *sourceArray;
  13. @end
  14. @implementation CoursewareViewModel
  15. - (void)resetParamenter {
  16. self.pages = 1;
  17. self.rows = 10;
  18. self.isLoadMore = YES;
  19. self.sourceArray = [NSMutableArray array];
  20. }
  21. - (void)refreshAndRequest {
  22. [self resetParamenter];
  23. [self requestData];
  24. }
  25. - (void)requestData {
  26. [LOADING_MANAGER showHUD];
  27. [KSNetworkingManager courseCoursewareRequest:KS_POST status:0 searchKey:self.searchKey page:self.pages rows:self.rows success:^(NSDictionary * _Nonnull dic) {
  28. [LOADING_MANAGER removeHUD];
  29. [self endRefresh];
  30. if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  31. NSArray *dataArray = [[dic dictionaryValueForKey:@"data"] arrayValueForKey:@"rows"];
  32. for (NSDictionary *parm in dataArray) {
  33. CoursewareListModel *model = [[CoursewareListModel alloc] initWithDictionary:parm];
  34. [self.sourceArray addObject:model];
  35. }
  36. if (dataArray.count < self.rows) {
  37. self.isLoadMore = NO;
  38. }
  39. }
  40. else {
  41. [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
  42. }
  43. if (self.delegate && [self.delegate respondsToSelector:@selector(loadSourceSuccess:)]) {
  44. NSArray *array = [self.sourceArray copy];
  45. [self.delegate loadSourceSuccess:array];
  46. }
  47. } faliure:^(NSError * _Nonnull error) {
  48. [LOADING_MANAGER removeHUD];
  49. [self endRefresh];
  50. }];
  51. }
  52. - (void)endRefresh {
  53. if (self.delegate && [self.delegate respondsToSelector:@selector(endRefresh)]) {
  54. [self.delegate endRefresh];
  55. }
  56. }
  57. - (void)loadNextPage {
  58. if (self.isLoadMore) {
  59. self.pages++;
  60. [self requestData];
  61. }
  62. }
  63. - (void)removeCoursewareAction {
  64. NSMutableArray *arrayList = [NSMutableArray array];
  65. for (CoursewareListModel *model in self.sourceArray) {
  66. if (model.isChoose) {
  67. [arrayList addObject:[NSString stringWithFormat:@"%@",model.internalBaseClassIdentifier]];
  68. }
  69. }
  70. if (arrayList.count == 0) {
  71. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"请选择课件"];
  72. return;
  73. }
  74. NSString *ids = [arrayList componentsJoinedByString:@","];
  75. [LOADING_MANAGER showHUD];
  76. [KSNetworkingManager courseCoursewareRemoveRequest:KS_POST ids:ids success:^(NSDictionary * _Nonnull dic) {
  77. [LOADING_MANAGER removeHUD];
  78. if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  79. [LOADING_MANAGER KSShowMsg:@"删除成功" promptCompletion:^{
  80. [self refreshAndRequest];
  81. }];
  82. }
  83. else {
  84. [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
  85. }
  86. } faliure:^(NSError * _Nonnull error) {
  87. [LOADING_MANAGER removeHUD];
  88. }];
  89. }
  90. - (void)resetAllChooseStatus {
  91. for (CoursewareListModel *model in self.sourceArray) {
  92. model.isChoose = NO;
  93. }
  94. if (self.delegate && [self.delegate respondsToSelector:@selector(loadSourceSuccess:)]) {
  95. NSArray *array = [self.sourceArray copy];
  96. [self.delegate loadSourceSuccess:array];
  97. }
  98. }
  99. - (void)chooseAllMusic {
  100. for (CoursewareListModel *model in self.sourceArray) {
  101. model.isChoose = YES;
  102. }
  103. if (self.delegate && [self.delegate respondsToSelector:@selector(loadSourceSuccess:)]) {
  104. NSArray *array = [self.sourceArray copy];
  105. [self.delegate loadSourceSuccess:array];
  106. }
  107. }
  108. - (void)setSearchKey:(NSString *)searchKey {
  109. _searchKey = searchKey;
  110. }
  111. - (NSMutableArray *)sourceArray {
  112. if (!_sourceArray) {
  113. _sourceArray = [NSMutableArray array];
  114. }
  115. return _sourceArray;
  116. }
  117. @end