123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- //
- // FKHomeViewModel.m
- // FirstLink
- //
- // Created by ascii on 16/8/12.
- // Copyright © 2016年 FirstLink. All rights reserved.
- //
- #import "FKHomeViewModel.h"
- #import "FKHomePersonInfoCell.h"
- #import "UserDefaultManager.h"
- #define SHOW_COUPON_NUMBER_DAY @"SHOW_COUPON_NUMBER_DAY"
- NSString * const kVipRedpointGuideKey = @"VipRedpointGuideKey";
- @interface FKHomeViewModel ()
- @property (nonatomic, strong) NSMutableDictionary *sectionTypeDict;
- @end
- @implementation FKHomeViewModel
- - (instancetype)init {
- self = [super init];
- if (self) {
- self.isHelpBuyable = [FKHomeViewModel configHelpBuyable];
- }
- return self;
- }
- + (BOOL)configHelpBuyable {
- return YES;
- }
- - (NSInteger)numberOfSectionsInTableView {
- [self.sectionTypeDict removeAllObjects];
-
- NSUInteger count = 3;
- [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kHomeSectionTypePerson] forKey:@"0"];
- [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kHomeSectionTypeOrder] forKey:@"1"];
- [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kHomeSectionTypePrivilege] forKey:@"2"];
-
- if (self.isHelpBuyable) {
- [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kHomeSectionTypeHelpBuy] forKey:[NSString stringWithFormat:@"%@", @(count++)]];
- }
- [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kHomeSectionTypePushAndConsult] forKey:[NSString stringWithFormat:@"%@", @(count++)]];
- return count;
- }
- - (NSInteger)numberOfRowsInSection:(NSInteger)section {
- kHomeSectionType sectionType = [self sectionTypeForSection:section];
- if (sectionType == kHomeSectionTypePerson
- || sectionType == kHomeSectionTypePrivilege
- || sectionType == kHomeSectionTypePushAndConsult) {
- return 2;
- } else if (sectionType == kHomeSectionTypeHelpBuy
- || sectionType == kHomeSectionTypeOrder) {
- return 1;
- }
- return 0;
- }
- - (kHomeSectionType)sectionTypeForSection:(NSInteger)section {
- NSNumber *value = self.sectionTypeDict[[NSString stringWithFormat:@"%lu", section]];
- if (value) {
- return (kHomeSectionType)(value.integerValue);
- }
- return kHomeSectionTypeUnknow;
- }
- - (kHomeCellType)cellTypeForIndexPath:(NSIndexPath *)indexPath {
- kHomeSectionType sectionType = [self sectionTypeForSection:indexPath.section];
-
- if (sectionType == kHomeSectionTypePerson) {
- return (indexPath.row == 0 ? kHomeCellTypePersonInfo : kHomeCellTypeFollow);
- } else if (sectionType == kHomeSectionTypeOrder) {
- return kHomeCellTypeOrder;
- } else if (sectionType == kHomeSectionTypePrivilege) {
- return (indexPath.row == 0 ? kHomeCellTypeVIP : kHomeCellTypeCoupon);
- } else if (sectionType == kHomeSectionTypeHelpBuy) {
- return kHomeCellTypeHelpBuy;
- } else if (sectionType == kHomeSectionTypePushAndConsult) {
- return (indexPath.row == 0 ? kHomeCellTypePush : kHomeCellTypeConsult);
- }
-
- return kHomeCellTypeNone;
- }
- - (CGFloat)heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- kHomeCellType cellType = [self cellTypeForIndexPath:indexPath];
- switch (cellType) {
- case kHomeCellTypePersonInfo: { return [FKHomePersonInfoCell height]; break; }
- case kHomeCellTypeFollow: { return 60; break; }
- case kHomeCellTypeOrder: { return 90; break; }
- default:{
- return 44;
- break;
- }
- }
- return 0;
- }
- - (BOOL)needShowInvalidCouponMsg {
- if ([FKUserManager sharedManager].applicationMode == ApplicationModeVisitor) return NO;
- if (self.couponCount <= 0) return NO;
-
- NSString *lastTime = [[UserDefaultManager sharedManager] getUserDefaultObject:SHOW_COUPON_NUMBER_DAY];
-
- if (self.serverTime.length == 0) return NO;
- if (lastTime.length == 0) return YES;
-
- NSDate *lastDate = [FLStringHelper convertToShortDateFromString:lastTime];
- NSDate *nowDate = [FLStringHelper convertToShortDateFromString:self.serverTime];
- NSTimeInterval interval = [nowDate timeIntervalSinceDate:lastDate];
-
- if (interval > 0) {
- return YES;
- }
- return NO;
- }
- - (BOOL)isNeedShowVipRedpointGuide {
- return ![[[NSUserDefaults standardUserDefaults] objectForKey:kVipRedpointGuideKey] boolValue];
- }
- - (void)shownVipRedpointGuide {
- [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kVipRedpointGuideKey];
- [[NSUserDefaults standardUserDefaults] synchronize];
- }
- - (NSAttributedString *)invalidCouponMsg {
- if (self.couponCount <= 0) return nil;
- NSString *message = [NSString stringWithFormat:@"有%ld张优惠即将过期",(long)(self.couponCount)];
- NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:message];
- [attrString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(1, 2)];
- return attrString;
- }
- - (void)saveLastShowInvalidCouponMsgTime {
- if (self.serverTime) {
- [[UserDefaultManager sharedManager] setUserDefaultObject:self.serverTime key:SHOW_COUPON_NUMBER_DAY];
- }
- }
- #pragma mark - method
- #pragma mark - Property
- - (NSMutableDictionary *)sectionTypeDict {
- if (!_sectionTypeDict) {
- _sectionTypeDict = [NSMutableDictionary dictionary];
- }
- return _sectionTypeDict;
- }
- @end
|