123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480 |
- //
- // MessageViewModel.m
- // FirstLink
- //
- // Created by unicode on 14-11-6.
- // Copyright (c) 2014年 FirstLink. All rights reserved.
- //
- #import "MessageViewModel.h"
- #import "FKAppMessage.h"
- #import "FKMessageManager.h"
- #import "FKPersonOrderReform.h"
- #import "FKHomeReform.h"
- @interface MessageViewModel ()
- @property (nonatomic, strong) NSMutableArray *todayMessages;
- @property (nonatomic, strong) NSMutableArray *last7dayMessages;
- @property (nonatomic, strong) NSMutableArray *last30dayMessages;
- @property (nonatomic, strong) NSMutableArray *earilerMessages;
- @property (nonatomic, strong) NSMutableDictionary *sectionTypeDict;
- @end
- @implementation MessageViewModel
- + (void)requestUsers:(NSArray *)userIDs success:(void (^)(MSGHeader *, id))success failure:(void (^)(MSGHeader *, NSError *))failure {
- if (userIDs.count == 0) {
- return;
- }
- NSString *stringID = [MessageViewModel stringIDs:userIDs];
- NSString *URLString = [NSString stringWithFormat:@"%@/link-site/api/find_em_users.json?ids=%@", [[FKServerUtil sharedInstance] apiServer], stringID];
-
- FLDataCenter *dataCenter = [FLDataCenter sharedDataCenter];
- [dataCenter POST:URLString parameters:[FLRequestHelper commonParamater] success:^(MSGHeader *header, id responseObject) {
- if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
-
- success(header, [MessageViewModel parserUser:responseObject]);
- } else {
- success(header, responseObject);
- }
- } failure:^(MSGHeader *header, NSError *error) {
- failure(header, error);
- }];
- }
- + (NSString*)stringIDs:(NSArray*)userIDs {
- if (userIDs.count == 0) {
- return @"";
- }
- NSMutableString *mutableString = [NSMutableString string];
- for (NSString *stringID in userIDs) {
- [mutableString appendFormat:@"%@,", stringID];
- }
- return [mutableString substringToIndex:(mutableString.length - 1)];
- }
- + (NSMutableDictionary*)parserUser:(NSDictionary*)json {
- if (json[@"data"] != [NSNull null]) {
- NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionary];
-
- NSDictionary *tempDict = nil;
- for (NSDictionary *dict in json[@"data"][@"users"]) {
- if (dict[@"id"] != [NSNull null]) {
- tempDict = @{@"headURL": dict[@"head_pic"],
- @"nickname": dict[@"nickname"]};
- [mutableDictionary setValue:tempDict forKey:[NSString stringWithFormat:@"%@", dict[@"id"]]];
- }
- }
- return mutableDictionary;
- }
- return nil;
- }
- #pragma mark - Request MSG
- - (void)requestMessageWithIndex:(NSString *)index
- AnchorID:(NSString *)anchorID
- startRow:(NSString *)startRow
- pageSize:(NSString *)pageSize
- type:(NSString *)type
- success:(void (^)(MSGHeader *, id, NSString *))success
- failure:(void (^)(MSGHeader *, NSError *))failure {
- NSString *itemJSONString;
- if (startRow && pageSize ) {
- itemJSONString = [self itemJSONStringWithIndex:index
- AnchorID:anchorID
- startRow:startRow
- pageSize:pageSize
- type:type];
- }
-
- NSMutableDictionary *para = [NSMutableDictionary dictionary];
- [para setValue:itemJSONString forKey:@"user_notify_message_json"];
- [para addEntriesFromDictionary:[FLRequestHelper commonParamater]];
-
- NSString *URLString = [NSString stringWithFormat:@"%@/link-site/api/message/find_user_notify_messages.json?", [[FKServerUtil sharedInstance] apiServer]];
-
- WeakSelf(weakSelf);
- FLDataCenter *dataCenter = [FLDataCenter sharedDataCenter];
- [dataCenter POST:URLString
- parameters:para
- success:^(MSGHeader *header, id responseObject)
- {
- if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
- weakSelf.pageHeader = [PageHeader parserHeader:responseObject];
- success(header, [weakSelf parseMessages:responseObject], [MessageViewModel parseServerTime:responseObject]);
- } else {
- success(header, responseObject, nil);
- }
- } failure:^(MSGHeader *header, NSError *error) {
- failure(header, error);
- }];
- }
- - (NSString*)itemJSONStringWithIndex:(NSString*)index
- AnchorID:(NSString*)anchorID
- startRow:(NSString*)startRow
- pageSize:(NSString*)pageSize
- type:(NSString*)type {
- NSDictionary *itemDict = [NSMutableDictionary dictionary];
- if (index) {
- [itemDict setValue:index forKey:@"index"];
- }
- if (anchorID) {
- [itemDict setValue:anchorID forKey:@"anchor_id"];
- }
- if (startRow) {
- [itemDict setValue:startRow forKey:@"start_row"];
- }
- if (pageSize) {
- [itemDict setValue:pageSize forKey:@"page_size"];
- }
- if (type) {
- [itemDict setValue:type forKey:@"message_type"];
- }
-
- NSError *error;
- NSData *jsonData = [NSJSONSerialization dataWithJSONObject:itemDict
- options:NSJSONWritingPrettyPrinted
- error:&error];
-
- if (!jsonData) {
- NSLog(@"itemJSONString error:%@", error.localizedDescription);
- return nil;
- }
- return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
- }
- - (NSMutableArray*)parseMessages:(NSDictionary *)json {
- if (json[@"data"] != [NSNull null]) {
- NSMutableArray *msgArray = [NSMutableArray array];
-
- for (NSDictionary *dict in json[@"data"][@"list"]) {
- FKAppMessage *msg = [[FKAppMessage alloc] init];
- [msg mts_setValuesForKeysWithDictionary:dict];
-
- if (msg) {
- [msgArray addObject:msg];
- }
- }
- return msgArray;
- }
- return nil;
- }
- + (NSString *)parseServerTime:(NSDictionary *)json {
- if ([json[@"data"][@"server_time"] isKindOfClass:[NSString class]]) {
- return json[@"data"][@"server_time"];
- }
- return nil;
- }
- #pragma mark -
- + (void)requestBadgeSuccess:(void (^)(MSGHeader *header, FKBadgeMessage *badge, FKAppMessage *comment))success
- failure:(void (^)(MSGHeader *, NSError *))failure {
- NSString *URLString = [NSString stringWithFormat:@"%@/link-site/api/message/find_unread_msg_num.json", [[FKServerUtil sharedInstance] apiServer]];
-
- FLDataCenter *dataCenter = [FLDataCenter sharedDataCenter];
- [dataCenter POST:URLString
- parameters:[FLRequestHelper commonParamater]
- success:^(MSGHeader *header, id responseObject)
- {
- if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
- success(header, [MessageViewModel parseBadge:responseObject], [MessageViewModel parseComment:responseObject]);
- } else {
- success(header, nil, nil);
- }
- } failure:^(MSGHeader *header, NSError *error) {
- failure(header, error);
- }];
- }
- + (void)requestOrderBadgeSuccess:(void (^)(MSGHeader *, FKPersonOrderBadgeItem *))success
- failure:(void (^)(MSGHeader *, NSError *))failure {
- NSString *URLString = [NSString stringWithFormat:@"%@/link-site/api/payment_order/find_payment_order_count.json", [[FKServerUtil sharedInstance] apiServer]];
-
- FLDataCenter *dataCenter = [FLDataCenter sharedDataCenter];
- [dataCenter POST:URLString
- parameters:[FLRequestHelper commonParamater]
- success:^(MSGHeader *header, id responseObject)
- {
- if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
- success(header, [FKPersonOrderReform parseOrderBadge:responseObject]);
- } else {
- success(header, nil);
- }
- } failure:^(MSGHeader *header, NSError *error) {
- failure(header, error);
- }];
- }
- + (void)requestExpiredCouponSuccess:(void (^)(MSGHeader *, NSInteger))success
- failure:(void (^)(MSGHeader *, NSError *))failure {
- if (![FKUserManager isUserLogin]) {
- return;
- }
-
- NSString *URLString = [NSString stringWithFormat:@"%@/link-site/api/cash_coupon/get_expired_cash_coupon_count.json", [[FKServerUtil sharedInstance] apiServer]];
-
- FLDataCenter *dataCenter = [FLDataCenter sharedDataCenter];
- [dataCenter POST:URLString
- parameters:[FLRequestHelper commonParamater]
- success:^(MSGHeader *header, id responseObject)
- {
- if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
- success(header, [FKHomeReform parseCouponCount:responseObject]);
- } else {
- success(header, 0);
- }
- } failure:^(MSGHeader *header, NSError *error) {
- failure(header, error);
- }];
- }
- + (FKBadgeMessage*)parseBadge:(NSDictionary *)json {
- if (json[@"data"] != [NSNull null]) {
- FKBadgeMessage *badge = [[FKBadgeMessage alloc] init];
-
- [badge mts_setValuesForKeysWithDictionary:json[@"data"]];
-
- return badge;
- }
- return nil;
- }
- + (FKAppMessage*)parseComment:(NSDictionary *)json {
- if (json[@"data"][@"latest_comment_msg"] != [NSNull null]) {
- FKAppMessage *item = [[FKAppMessage alloc] init];
- [item mts_setValuesForKeysWithDictionary:json[@"data"][@"latest_comment_msg"]];
-
- return item;
- }
- return nil;
- }
- #pragma mark -
- + (void)markMessageRead:(NSString *)msgID
- msgType:(NSString *)type
- success:(void (^)(MSGHeader *, id))success
- failure:(void (^)(MSGHeader *, NSError *))failure
- {
-
- NSString *itemJSONString;
- NSArray *keys;
- NSArray *values;
- if (msgID) {
- keys = @[@"id", @"message_type"];
- values = @[msgID, type];
- } else {
- keys = @[@"message_type"];
- values = @[type];
- }
- itemJSONString = [FLRequestHelper JSONStringWithKeys:keys
- values:values];
-
- NSMutableDictionary *para = [NSMutableDictionary dictionary];
- [para setValue:itemJSONString forKey:@"user_notify_message_json"];
- [para addEntriesFromDictionary:[FLRequestHelper commonParamater]];
-
- NSString *URLString = [NSString stringWithFormat:@"%@/link-site/api/message/read_msgs.json", [[FKServerUtil sharedInstance] apiServer]];
-
- FLDataCenter *dataCenter = [FLDataCenter sharedDataCenter];
- [dataCenter POST:URLString
- parameters:para
- success:^(MSGHeader *header, id responseObject)
- {
- if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
- success(header, nil);
- } else {
- success(header, responseObject);
- }
- } failure:^(MSGHeader *header, NSError *error) {
- failure(header, error);
- }];
- }
- #pragma mark -
- - (NSMutableArray*)appMessages {
- if (!_appMessages) {
- _appMessages = [NSMutableArray array];
- }
- return _appMessages;
- }
- - (NSMutableArray*)todayMessages {
- if (!_todayMessages) {
- _todayMessages = [NSMutableArray array];
- }
- return _todayMessages;
- }
- - (NSMutableArray*)last7dayMessages {
- if (!_last7dayMessages) {
- _last7dayMessages = [NSMutableArray array];
- }
- return _last7dayMessages;
- }
- - (NSMutableArray*)last30dayMessages {
- if (!_last30dayMessages) {
- _last30dayMessages = [NSMutableArray array];
- }
- return _last30dayMessages;
- }
- - (NSMutableArray*)earilerMessages {
- if (!_earilerMessages) {
- _earilerMessages = [NSMutableArray array];
- }
- return _earilerMessages;
- }
- - (void)removeAllMessages {
- [self.appMessages removeAllObjects];
- }
- - (void)removeTimelineMessages {
- [self.todayMessages removeAllObjects];
- [self.last7dayMessages removeAllObjects];
- [self.last30dayMessages removeAllObjects];
- [self.earilerMessages removeAllObjects];
- }
- - (void)addMessages:(NSArray *)messages {
- if ([messages isKindOfClass:[NSArray class]]) {
- [self.appMessages addObjectsFromArray:messages];
- }
- }
- - (NSDateComponents *)convertToComponentsFormateFromString:(NSString *)timeString baseTime:(NSString *)baseTime{
- if (!timeString || timeString.length == 0) return nil;
-
- NSDate *nowDate = [NSDate date];
- if ([FLStringHelper isValidString:baseTime]) {
- nowDate = [FLStringHelper convertToDateFromString:baseTime];
- }
-
- NSRange range = [timeString rangeOfString:@"T"];
- if (range.length > 0) {
- timeString = [NSString stringWithFormat:@"%@T23:59:59", [timeString substringToIndex:range.location]];
- }
-
- NSDate *compareTime = [FLStringHelper convertToDateFromString:timeString];
- NSTimeInterval countTimeSecond = [compareTime timeIntervalSinceDate:nowDate];
- if (countTimeSecond <= 0) return nil;
-
- NSDateComponents *dateComponent = [FLStringHelper convertSecondToComponents:countTimeSecond baseTime:baseTime];
- return dateComponent;
-
- }
- - (void)addTimelineMessages:(NSArray *)messages serverTime:(NSString *)serverTime {
- NSDateComponents *compoment;
- for (FKAppMessage *message in messages) {
- compoment = [self convertToComponentsFormateFromString:serverTime baseTime:message.createTime];
- if (compoment.day <= 0) {
- [self.todayMessages addObject:message];
- } else if (compoment.day <= 6) {
- [self.last7dayMessages addObject:message];
- } else if (compoment.day <= 29) {
- [self.last30dayMessages addObject:message];
- } else if (compoment.day > 29) {
- [self.earilerMessages addObject:message];
- }
- }
- }
- - (NSMutableDictionary *)sectionTypeDict {
- if (!_sectionTypeDict) {
- _sectionTypeDict = [NSMutableDictionary dictionary];
- }
- return _sectionTypeDict;
- }
- - (kMessageSectionType)sectionTypeForSection:(NSInteger)section {
- NSNumber *value = self.sectionTypeDict[[NSString stringWithFormat:@"%lu", section]];
- if (value) {
- return (kMessageSectionType)(value.integerValue);
- }
- return kMessageSectionTypeNone;
- }
- - (NSInteger)numberOfTimelineSectionsInTableView {
- [self.sectionTypeDict removeAllObjects];
-
- NSUInteger count = 0;
- if (self.todayMessages.count > 0) {
- [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kMessageSectionTypeToday] forKey:[NSString stringWithFormat:@"%lu", count++]];
- }
- if (self.last7dayMessages.count > 0) {
- [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kMessageSectionTypeLast7Day] forKey:[NSString stringWithFormat:@"%lu", count++]];
- }
- if (self.last30dayMessages.count > 0) {
- [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kMessageSectionTypeLast30Day] forKey:[NSString stringWithFormat:@"%lu", count++]];
- }
- if (self.earilerMessages.count > 0) {
- [self.sectionTypeDict setValue:[NSNumber numberWithInteger:kMessageSectionTypeEarlier] forKey:[NSString stringWithFormat:@"%lu", count++]];
- }
- return count;
- }
- - (NSInteger)numberOfRowsInTimelineSection:(NSInteger)section {
- kMessageSectionType sectionType = [self sectionTypeForSection:section];
- if (sectionType == kMessageSectionTypeToday) {
- return (self.todayMessages.count + 1);
- } else if (sectionType == kMessageSectionTypeLast7Day) {
- return (self.last7dayMessages.count + 1);
- } else if (sectionType == kMessageSectionTypeLast30Day){
- return (self.last30dayMessages.count + 1);
- } else if (sectionType == kMessageSectionTypeEarlier){
- return (self.earilerMessages.count + 1);
- }
- return 0;
- }
- - (FKAppMessage*)timelineItemAtIndex:(NSIndexPath *)indexPath {
- kMessageSectionType sectionType = [self sectionTypeForSection:indexPath.section];
-
- NSArray *array;
- if (sectionType == kMessageSectionTypeToday) {
- array = self.todayMessages;
- } else if (sectionType == kMessageSectionTypeLast7Day) {
- array = self.last7dayMessages;
- } else if (sectionType == kMessageSectionTypeLast30Day){
- array = self.last30dayMessages;
- } else if (sectionType == kMessageSectionTypeEarlier){
- array = self.earilerMessages;
- }
-
- if ((indexPath.row - 1) >= 0 && (indexPath.row - 1) < array.count) {
- return array[(indexPath.row - 1)];
- }
- return nil;
- }
- - (NSString *)timelineHeaderMessageAtIndexPath:(NSIndexPath *)indexPath {
- kMessageSectionType sectionType = [self sectionTypeForSection:indexPath.section];
- if (sectionType == kMessageSectionTypeToday) {
- return @"今天";
- } else if (sectionType == kMessageSectionTypeLast7Day) {
- return @"最近7天";
- } else if (sectionType == kMessageSectionTypeLast30Day){
- return @"最近30天";
- } else if (sectionType == kMessageSectionTypeEarlier){
- return @"一个月以前";
- }
- return nil;
- }
- - (BOOL)hasTimelineMessage {
- return (self.todayMessages.count + self.last7dayMessages.count + self.last30dayMessages.count + self.earilerMessages.count);
- }
- @end
|