説明なし

FKHomeViewModel.m 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // FKHomeViewModel.m
  3. // FirstLink
  4. //
  5. // Created by ascii on 16/8/12.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKHomeViewModel.h"
  9. #import "FKHomePersonInfoCell.h"
  10. #import "UserDefaultManager.h"
  11. #define SHOW_COUPON_NUMBER_DAY @"SHOW_COUPON_NUMBER_DAY"
  12. NSString * const kVipRedpointGuideKey = @"VipRedpointGuideKey";
  13. @interface FKHomeViewModel ()
  14. @property (nonatomic, strong) NSMutableDictionary *sectionTypeDict;
  15. @end
  16. @implementation FKHomeViewModel
  17. - (instancetype)init {
  18. self = [super init];
  19. if (self) {
  20. self.isHelpBuyable = [FKHomeViewModel configHelpBuyable];
  21. }
  22. return self;
  23. }
  24. + (BOOL)configHelpBuyable {
  25. return YES;
  26. }
  27. - (NSInteger)numberOfSectionsInTableView {
  28. [self.sectionTypeDict removeAllObjects];
  29. NSUInteger count = 3;
  30. [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kHomeSectionTypePerson] forKey:@"0"];
  31. [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kHomeSectionTypeOrder] forKey:@"1"];
  32. [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kHomeSectionTypePrivilege] forKey:@"2"];
  33. if (self.isHelpBuyable) {
  34. [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kHomeSectionTypeHelpBuy] forKey:[NSString stringWithFormat:@"%@", @(count++)]];
  35. }
  36. [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kHomeSectionTypePushAndConsult] forKey:[NSString stringWithFormat:@"%@", @(count++)]];
  37. return count;
  38. }
  39. - (NSInteger)numberOfRowsInSection:(NSInteger)section {
  40. kHomeSectionType sectionType = [self sectionTypeForSection:section];
  41. if (sectionType == kHomeSectionTypePerson
  42. || sectionType == kHomeSectionTypePrivilege
  43. || sectionType == kHomeSectionTypePushAndConsult) {
  44. return 2;
  45. } else if (sectionType == kHomeSectionTypeHelpBuy
  46. || sectionType == kHomeSectionTypeOrder) {
  47. return 1;
  48. }
  49. return 0;
  50. }
  51. - (kHomeSectionType)sectionTypeForSection:(NSInteger)section {
  52. NSNumber *value = self.sectionTypeDict[[NSString stringWithFormat:@"%lu", section]];
  53. if (value) {
  54. return (kHomeSectionType)(value.integerValue);
  55. }
  56. return kHomeSectionTypeUnknow;
  57. }
  58. - (kHomeCellType)cellTypeForIndexPath:(NSIndexPath *)indexPath {
  59. kHomeSectionType sectionType = [self sectionTypeForSection:indexPath.section];
  60. if (sectionType == kHomeSectionTypePerson) {
  61. return (indexPath.row == 0 ? kHomeCellTypePersonInfo : kHomeCellTypeFollow);
  62. } else if (sectionType == kHomeSectionTypeOrder) {
  63. return kHomeCellTypeOrder;
  64. } else if (sectionType == kHomeSectionTypePrivilege) {
  65. return (indexPath.row == 0 ? kHomeCellTypeVIP : kHomeCellTypeCoupon);
  66. } else if (sectionType == kHomeSectionTypeHelpBuy) {
  67. return kHomeCellTypeHelpBuy;
  68. } else if (sectionType == kHomeSectionTypePushAndConsult) {
  69. return (indexPath.row == 0 ? kHomeCellTypePush : kHomeCellTypeConsult);
  70. }
  71. return kHomeCellTypeNone;
  72. }
  73. - (CGFloat)heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  74. kHomeCellType cellType = [self cellTypeForIndexPath:indexPath];
  75. switch (cellType) {
  76. case kHomeCellTypePersonInfo: { return [FKHomePersonInfoCell height]; break; }
  77. case kHomeCellTypeFollow: { return 60; break; }
  78. case kHomeCellTypeOrder: { return 90; break; }
  79. default:{
  80. return 44;
  81. break;
  82. }
  83. }
  84. return 0;
  85. }
  86. - (BOOL)needShowInvalidCouponMsg {
  87. if ([FKUserManager sharedManager].applicationMode == ApplicationModeVisitor) return NO;
  88. if (self.couponCount <= 0) return NO;
  89. NSString *lastTime = [[UserDefaultManager sharedManager] getUserDefaultObject:SHOW_COUPON_NUMBER_DAY];
  90. if (self.serverTime.length == 0) return NO;
  91. if (lastTime.length == 0) return YES;
  92. NSDate *lastDate = [FLStringHelper convertToShortDateFromString:lastTime];
  93. NSDate *nowDate = [FLStringHelper convertToShortDateFromString:self.serverTime];
  94. NSTimeInterval interval = [nowDate timeIntervalSinceDate:lastDate];
  95. if (interval > 0) {
  96. return YES;
  97. }
  98. return NO;
  99. }
  100. - (BOOL)isNeedShowVipRedpointGuide {
  101. return ![[[NSUserDefaults standardUserDefaults] objectForKey:kVipRedpointGuideKey] boolValue];
  102. }
  103. - (void)shownVipRedpointGuide {
  104. [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kVipRedpointGuideKey];
  105. [[NSUserDefaults standardUserDefaults] synchronize];
  106. }
  107. - (NSAttributedString *)invalidCouponMsg {
  108. if (self.couponCount <= 0) return nil;
  109. NSString *message = [NSString stringWithFormat:@"有%ld张优惠即将过期",(long)(self.couponCount)];
  110. NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:message];
  111. [attrString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(1, 2)];
  112. return attrString;
  113. }
  114. - (void)saveLastShowInvalidCouponMsgTime {
  115. if (self.serverTime) {
  116. [[UserDefaultManager sharedManager] setUserDefaultObject:self.serverTime key:SHOW_COUPON_NUMBER_DAY];
  117. }
  118. }
  119. #pragma mark - method
  120. #pragma mark - Property
  121. - (NSMutableDictionary *)sectionTypeDict {
  122. if (!_sectionTypeDict) {
  123. _sectionTypeDict = [NSMutableDictionary dictionary];
  124. }
  125. return _sectionTypeDict;
  126. }
  127. @end