// // XYModifyUserInfoViewModel.m // Starbuds // // Created by 翟玉磊 on 2019/12/24. // Copyright © 2019 翟玉磊. All rights reserved. // #import "XYModifyUserInfoViewModel.h" #import "XYModifyUserInfoListModel.h" #import "XYUserOtherInfoManager.h" #import "XYCountryPhoneCodeModel.h" @interface XYModifyUserInfoViewModel(){ } @property (nonatomic, strong) NSArray *profileNewArrs; @property (nonatomic, strong) NSArray *photoAblumArrs; @end @implementation XYModifyUserInfoViewModel - (instancetype)initWithInfoType:(ModifyUserInfo_Type)infoType; { if (self = [super init]) { _infoType = infoType; if (_infoType != ModifyUserInfo_Type_Main) { _userId = [XYUserInfoManager nowUser].userId; } [self initValue]; } return self; } - (void)initValue { [self.dataSource removeAllObjects]; XYUserInfoModel *nowUser = [XYUserInfoManager nowUser]; if (self.infoType == ModifyUserInfo_Type_User || self.infoType == ModifyUserInfo_Type_Main) { if (self.infoType == ModifyUserInfo_Type_User) { XYModifyUserInfoListModel *userAvatarModel = [[XYModifyUserInfoListModel alloc] init]; userAvatarModel.itemName = kLocalizedString(@"头像"); userAvatarModel.isShowMore = YES; userAvatarModel.isShowBottomLine = YES; userAvatarModel.itemValue = nowUser.userAvatar; userAvatarModel.cellHeight = 58; [self.dataSource addObject:userAvatarModel]; XYModifyUserInfoListModel *userNameModel = [[XYModifyUserInfoListModel alloc] init]; userNameModel.itemName = kLocalizedString(@"昵称"); userNameModel.isShowMore = YES; userNameModel.isShowBottomLine = YES; userNameModel.itemValue = nowUser.userName; userNameModel.cellHeight = 50; [self.dataSource addObject:userNameModel]; NSString *sexStr = @""; if (nowUser.userSex == 0) { sexStr = kLocalizedString(@"保密"); }else if (nowUser.userSex == 1) { sexStr = kLocalizedString(@"男士"); }else if (nowUser.userSex == 2) { sexStr = kLocalizedString(@"女士"); } XYModifyUserInfoListModel *sexModel = [[XYModifyUserInfoListModel alloc] init]; sexModel.itemName = kLocalizedString(@"性别"); sexModel.isShowMore = YES; sexModel.isShowBottomLine = YES; sexModel.itemValue = sexStr; sexModel.cellHeight = 50; [self.dataSource addObject:sexModel]; XYModifyUserInfoListModel *ageModel = [[XYModifyUserInfoListModel alloc] init]; ageModel.itemName = kLocalizedString(@"生日"); ageModel.isShowMore = YES; ageModel.isShowBottomLine = YES; NSString *strTime = [XYUserInfoManager nowUser].userBornTime; ageModel.itemValue = [SystemTimeObject timestampSwitchTime:strTime andFormatter:@"yyyy-MM-dd"]; ageModel.cellHeight = 50; [self.dataSource addObject:ageModel]; if ([[XYUserInfoManager nowUser].countryId isEqualToString:@"1"]) { XYModifyUserInfoListModel *countryModel = [[XYModifyUserInfoListModel alloc] init]; countryModel.itemName = kLocalizedString(@"国家"); countryModel.isShowMore = YES; countryModel.isShowBottomLine = YES; countryModel.itemValue = StringIsEmpty(nowUser.countryName)?kLocalizedString(@"请选择"):nowUser.countryName; countryModel.cellHeight = 50; [self.dataSource addObject:countryModel]; XYModifyUserInfoListModel *cityModel = [[XYModifyUserInfoListModel alloc] init]; cityModel.itemName = kLocalizedString(@"城市"); cityModel.isShowMore = YES; cityModel.isShowBottomLine = YES; cityModel.itemValue = StringIsEmpty(nowUser.areaCity)?kLocalizedString(@"请选择"):nowUser.areaCity; cityModel.cellHeight = 50; [self.dataSource addObject:cityModel]; }else { XYModifyUserInfoListModel *countryModel = [[XYModifyUserInfoListModel alloc] init]; countryModel.itemName = kLocalizedString(@"国家"); countryModel.isShowMore = YES; countryModel.isShowBottomLine = YES; countryModel.itemValue = StringIsEmpty(nowUser.countryName)?kLocalizedString(@"请选择"):nowUser.countryName; countryModel.cellHeight = 50; [self.dataSource addObject:countryModel]; } XYModifyUserInfoListModel *sectionlyModel0 = [[XYModifyUserInfoListModel alloc] init]; sectionlyModel0.groupName = kLocalizedString(@"个性签名"); sectionlyModel0.isSection = YES; sectionlyModel0.cellHeight = 38; [self.dataSource addObject:sectionlyModel0]; XYModifyUserInfoListModel *userIntroModel = [[XYModifyUserInfoListModel alloc] init]; userIntroModel.itemType = XYModifyTtem_INPUT_SIGN; userIntroModel.cellHeight = 72; userIntroModel.itemValue = nowUser.userSign; [self.dataSource addObject:userIntroModel]; //if (userIntroModel.itemValue.length) { sectionlyModel0.groupScore = -1; //} }else{ if (self.photoAblumArrs.count) { //相册 XYModifyUserInfoListModel *photoModel = [[XYModifyUserInfoListModel alloc] init]; photoModel.itemType = XYModifyTtem_PHOTO_ABLUM; photoModel.cellHeight = 162; photoModel.groupScore = 100; [self.dataSource addObject:photoModel]; photoModel.photoAlbumArrs = [[NSMutableArray alloc] init]; for (NSDictionary *dict in self.photoAblumArrs) { XYFeedModel *model = XYFeedModel.new; [model yy_modelSetWithDictionary:dict]; [photoModel.photoAlbumArrs addObject:model]; } } } if (([XYUserInfoManager nowUser].userScore < 50) && (self.infoType == ModifyUserInfo_Type_Main) && ([self.userId isEqualToString:[XYUserInfoManager nowUser].userId])) { XYModifyUserInfoListModel *userInfoEditAlertModel = [[XYModifyUserInfoListModel alloc] init]; userInfoEditAlertModel.itemType = XYModifyTtem_USERINFO_EDITALERT; userInfoEditAlertModel.cellHeight = 100; userInfoEditAlertModel.groupScore = 100; [self.dataSource addObject:userInfoEditAlertModel]; } NSMutableArray *answerArrs = [[NSMutableArray alloc] init]; //profileNew接口数据(资料配置项数据) for (NSDictionary *groupDict in self.profileNewArrs) { XYModifyUserInfoListModel *sectionlyModel0 = [[XYModifyUserInfoListModel alloc] init]; [sectionlyModel0 yy_modelSetWithDictionary:groupDict]; sectionlyModel0.isSection = YES; sectionlyModel0.cellHeight = 38;//section默认高 sectionlyModel0.userInfoType = self.infoType; [self.dataSource addObject:sectionlyModel0]; NSArray *childArrs = groupDict[@"childItems"]; for (NSDictionary *childDIct in childArrs) { XYModifyUserInfoListModel *model = [[XYModifyUserInfoListModel alloc] init]; [model yy_modelSetWithDictionary:groupDict]; [model yy_modelSetWithDictionary:childDIct]; model.userInfoType = self.infoType; if (self.infoType == ModifyUserInfo_Type_Main) { model.groupScore = 0; } model.tagColor = model.tagColor?model.tagColor:@"000000"; model.textColor = model.textColor?model.textColor:@"FFFFFF"; model.cellHeight = 50;//cell默认高 [self.dataSource addObject:model]; if(model.itemType == XYModifyTtem_SINGLE_OPTION){ //选项数组(用逗号分隔字符串) NSData *itemOptionsData = [model.itemOptions dataUsingEncoding:NSUTF8StringEncoding]; NSArray *optionsarray = [NSJSONSerialization JSONObjectWithData:itemOptionsData options:NSJSONReadingMutableContainers error:nil]; for (int i= 0 ; i< optionsarray.count;i++) { NSDictionary *dict = optionsarray[i]; if (i == 0) { model.itemOptions = dict[@"content"]; }else{ model.itemOptions = [NSString stringWithFormat:@"%@,%@",model.itemOptions,dict[@"content"]]; } } }else if(model.itemType == XYModifyTtem_MULTI_OPTION){ //选项数组(用逗号分隔字符串) NSData *itemOptionsData = [model.itemOptions dataUsingEncoding:NSUTF8StringEncoding]; NSArray *optionsarray = [NSJSONSerialization JSONObjectWithData:itemOptionsData options:NSJSONReadingMutableContainers error:nil]; for (int i= 0 ; i< optionsarray.count;i++) { NSDictionary *dict = optionsarray[i]; if (i == 0) { model.itemOptions = dict[@"content"]; }else{ model.itemOptions = [NSString stringWithFormat:@"%@,%@",model.itemOptions,dict[@"content"]]; } } //值数组 if (model.itemValue.length) { float tagWidth = kScreenWidth; if (self.infoType == ModifyUserInfo_Type_Main) { //和标题下行显示 model.cellHeight = 40; XYModifyUserInfoListModel *sectionModel; for (XYModifyUserInfoListModel *tempModel in self.dataSource) { if (tempModel.isSection) { sectionModel = tempModel; } } //和标题同行显示 if ([sectionModel.groupName isEqualToString:model.itemName]) { tagWidth = kScreenWidth + 1; model.cellHeight = 10; } } model.tagWidth = tagWidth; NSArray *tags = [model.itemValue componentsSeparatedByString:@","]; XYTagListView *taglistView = [XYTagListView getTagView:tags andBgColor:[UIColor colorWithHexString:model.tagColor alpha:1] andTextColor:[UIColor colorWithHexString:model.textColor alpha:1] andWidth:tagWidth-10]; model.cellHeight = model.cellHeight + taglistView.height + 12; [taglistView removeFromSuperview]; taglistView = nil; } }else if(model.itemType == XYModifyTtem_INPUT_CONTENT){ float itemValueHeight = [HandleString autoLabelWith:model.itemValue withSize:CGSizeMake(kScreenWidth-140, MAXFLOAT) withFont:Font(14) withLines:0].height; model.cellHeight = (itemValueHeight+30)>50?(itemValueHeight+30):50; }else if(model.itemType == XYModifyTtem_ADD_QA){//用数组单独处理 //[self parseAnswerData:groupDict]; //break; //问题标题和答案 yymodel已处理 //"itemName" : "想去哪儿?", //"itemValue" : "阿萨德", [self.dataSource removeLastObject]; [answerArrs addObject:model]; //model.itemOptions 处理为字符串逗号分隔 //问题选项数据处理 NSData *itemOptionsData = [model.itemOptions dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *itemoptionsDict = [NSJSONSerialization JSONObjectWithData:itemOptionsData options:NSJSONReadingMutableContainers error:nil]; model.moduleId = [itemoptionsDict[@"moduleId"] integerValue]; NSArray *optionsarray = itemoptionsDict[@"list"]; for (int i = 0 ; i< optionsarray.count;i++) { NSDictionary *dict = optionsarray[i]; if (i == 0) { model.itemOptions = dict[@"content"]; }else{ model.itemOptions = [NSString stringWithFormat:@"%@,%@",model.itemOptions,dict[@"content"]]; } } } } } //查看用户主页childItems有答案时加入数组 int answerItemValueCount = 0; for (XYModifyUserInfoListModel *tempmodel in answerArrs) { answerItemValueCount++; if (self.infoType == ModifyUserInfo_Type_Main) { if (!StringIsEmpty(tempmodel.itemValue)) { [self.dataSource addObject:tempmodel]; } }else{ [self.dataSource addObject:tempmodel]; } float questionHeight = [HandleString autoLabelWith:tempmodel.itemName withSize:CGSizeMake(kScreenWidth-85, MAXFLOAT) withFont:Font(14) withLines:0].height; float answerHeight = [HandleString autoLabelWith:tempmodel.itemValue withSize:CGSizeMake(kScreenWidth-85, MAXFLOAT) withFont:Font(14) withLines:0].height; tempmodel.cellHeight = 15 + (questionHeight>10?questionHeight:20) + 4 + (answerHeight>10?answerHeight:20); } // //childItems没有答案,默认一个入口答题model // if (!answerItemValueCount) { // XYModifyUserInfoListModel *tempmodel = [answerArrs firstObject]; // tempmodel.cellHeight = 260; // [self.dataSource addObject:tempmodel]; // } if (self.infoType == ModifyUserInfo_Type_Main) { NSMutableArray *arr = [[NSMutableArray alloc] init]; for (int i = 0 ; i < self.dataSource.count ; i++) { XYModifyUserInfoListModel *model = self.dataSource[i]; model.userInfoType = self.infoType; if (!StringIsEmpty(model.itemValue) || model.groupScore > 0) { [arr addObject:model]; if ([model.groupKey isEqualToString:@"profile"]) { model.groupName = @"基本信息"; } } } [self.dataSource removeAllObjects]; self.dataSource = arr; } // [self.dataSource addObject:[EmptyCellModel createModelWithIsShowTopLine:NO isShowBottomLine:NO cellHeight:10.0f]]; // // XYModifyUserInfoListModel *phoneModel = [XYModifyUserInfoListModel createModelWithTitle:kLocalizedString(@"绑定手机") isShowMore:StringIsNotEmpty(self.userPhone)?NO:YES isShowBottomLine:NO content:StringIsEmpty(self.userPhone)?kLocalizedString(@"去绑定"):[NSString stringWithFormat:@"已绑定 %@", [self.userPhone substringWithRange:NSMakeRange(self.userPhone.length-4, 4)]]]; // [self.dataSource addObject:phoneModel]; // [self.dataSource addObject:[EmptyCellModel createModelWithIsShowTopLine:NO isShowBottomLine:NO cellHeight:10.0f]]; /*//改为ProfileNew接口获取数据 XYModifyUserInfoListModel *sectionlyModel = [XYModifyUserInfoListModel createModelWithTitle:kLocalizedString(@"其他信息") isShowMore:NO isShowBottomLine:NO content:@""]; sectionlyModel.isSection = YES; sectionlyModel.cellHeight = 38.0f; [self.dataSource addObject:sectionlyModel]; if ([[XYUserInfoManager nowUser].countryId isEqualToString:@"1"]) { XYModifyUserInfoListModel *countryModel = [XYModifyUserInfoListModel createModelWithTitle:kLocalizedString(@"国家") isShowMore:NO isShowBottomLine:YES content:StringIsEmpty(nowUser.countryName)?kLocalizedString(@"请选择"):nowUser.countryName]; [self.dataSource addObject:countryModel]; XYModifyUserInfoListModel *cityModel = [XYModifyUserInfoListModel createModelWithTitle:kLocalizedString(@"城市") isShowMore:NO isShowBottomLine:YES content:StringIsEmpty(nowUser.areaCity)?kLocalizedString(@"请选择"):nowUser.areaCity]; [self.dataSource addObject:cityModel]; }else { XYModifyUserInfoListModel *countryModel = [XYModifyUserInfoListModel createModelWithTitle:kLocalizedString(@"国家") isShowMore:NO isShowBottomLine:YES content:StringIsEmpty(nowUser.countryName)?kLocalizedString(@"请选择"):nowUser.countryName]; [self.dataSource addObject:countryModel]; } XYModifyUserInfoListModel *incomeModel = [XYModifyUserInfoListModel createModelWithTitle:kLocalizedString(@"收入") isShowMore:YES isShowBottomLine:YES content:StringIsEmpty(nowUser.userIncome)?@"":nowUser.userIncome]; [self.dataSource addObject:incomeModel]; XYModifyUserInfoListModel *weightModel = [XYModifyUserInfoListModel createModelWithTitle:kLocalizedString(@"体重") isShowMore:YES isShowBottomLine:YES content:nowUser.userWeight == 0?@"":[NSString stringWithFormat:@"%dkg", (int)nowUser.userWeight]]; [self.dataSource addObject:weightModel]; XYModifyUserInfoListModel *heightModel = [XYModifyUserInfoListModel createModelWithTitle:kLocalizedString(@"身高") isShowMore:YES isShowBottomLine:YES content:nowUser.userHeight == 0?@"":[NSString stringWithFormat:@"%dcm", (int)nowUser.userHeight]]; [self.dataSource addObject:heightModel]; XYModifyUserInfoListModel *userConstellationModel = [XYModifyUserInfoListModel createModelWithTitle:kLocalizedString(@"星座") isShowMore:YES isShowBottomLine:YES content:nowUser.userConstellation == 0?@"":[XYUserOtherInfoManager getConstellationValueWithKey:nowUser.userConstellation]]; [self.dataSource addObject:userConstellationModel]; XYModifyUserInfoListModel *purposeModel = [XYModifyUserInfoListModel createModelWithTitle:kLocalizedString(@"交友目的") isShowMore:YES isShowBottomLine:YES content:StringIsEmpty(nowUser.userMarkingFriends)?@"":nowUser.userMarkingFriends]; [self.dataSource addObject:purposeModel]; XYModifyUserInfoListModel *emotionModel = [XYModifyUserInfoListModel createModelWithTitle:kLocalizedString(@"情感状况") isShowMore:YES isShowBottomLine:YES content:StringIsEmpty(nowUser.userMarriage)?@"":nowUser.userMarriage]; [self.dataSource addObject:emotionModel]; XYModifyUserInfoListModel *hobbyModel = [XYModifyUserInfoListModel createModelWithTitle:kLocalizedString(@"兴趣爱好") isShowMore:YES isShowBottomLine:YES content:StringIsEmpty(nowUser.userHobby)?@"":nowUser.userHobby]; [self.dataSource addObject:hobbyModel]; XYModifyUserInfoListModel *individualityModel = [XYModifyUserInfoListModel createModelWithTitle:kLocalizedString(@"个性标签") isShowMore:YES isShowBottomLine:YES content:StringIsEmpty(nowUser.userTags)?@"":nowUser.userTags]; [self.dataSource addObject:individualityModel]; // [self.dataSource addObject:[EmptyCellModel createModelWithIsShowTopLine:NO isShowBottomLine:NO cellHeight:40.0f]]; */ }else { XYModifyUserInfoListModel *noModel = [XYModifyUserInfoListModel createModelWithTitle:@"ID" isShowMore:NO isShowBottomLine:YES content:[XYUserInfoManager nowUser].userNo]; [self.dataSource addObject:noModel]; XYModifyUserInfoListModel *coverModel = [XYModifyUserInfoListModel createModelWithTitle:kLocalizedString(@"颜值封面") isShowMore:YES isShowBottomLine:NO content:@""]; [self.dataSource addObject:coverModel]; } } //- (void)parseAnswerData:(NSDictionary *)groupDict{ // XYModifyUserInfoListModel *sectionlyModel0 = [[XYModifyUserInfoListModel alloc] init]; // [sectionlyModel0 yy_modelSetWithDictionary:groupDict]; // [self.dataSource addObject:sectionlyModel0]; // // sectionlyModel0.answerChildArrs = [[NSMutableArray alloc] init]; // // NSArray *childArrs = groupDict[@"childItems"]; // for (NSDictionary *childDict in childArrs) { // XYModifyUserInfoListModel *childmodel = [[XYModifyUserInfoListModel alloc] init]; // [childmodel yy_modelSetWithDictionary:childDict]; // [sectionlyModel0.answerChildArrs addObject:childmodel]; // sectionlyModel0.itemType = childmodel.itemType; // // childmodel.answerChildItemOptionsArrs = [[NSMutableArray alloc] init]; // // NSData *itemOptionsData = [childmodel.itemOptions dataUsingEncoding:NSUTF8StringEncoding]; // NSDictionary *itemoptionsDict = [NSJSONSerialization JSONObjectWithData:itemOptionsData options:NSJSONReadingMutableContainers error:nil]; // NSArray *optionsarray = itemoptionsDict[@"list"]; // for (NSDictionary *itemDict in optionsarray) { // XYModifyUserInfoListModel *model1 = [[XYModifyUserInfoListModel alloc] init]; // [childmodel.answerChildItemOptionsArrs addObject:model1]; // model1.itemName = itemDict[@"content"]; // model1.questionId = [itemDict[@"questionId"] integerValue]; // model1.moduleId = [itemoptionsDict[@"moduleId"]integerValue]; // } // } // // sectionlyModel0.answerView = [XYUserMainInformationAnswerView getAnswerView:sectionlyModel0]; // sectionlyModel0.cellHeight = 15 + sectionlyModel0.cellHeight + sectionlyModel0.answerView.height + 15; // //} - (void)loadData:(SuccessHandler)success failure:(FailureHandler)failure { dispatch_group_t group = dispatch_group_create(); dispatch_queue_t queue = dispatch_queue_create("modify.user.info.queue", DISPATCH_QUEUE_SERIAL); if (self.infoType != ModifyUserInfo_Type_Main) {//用户主页外层以获取 dispatch_group_enter(group); dispatch_async(queue, ^{ [[XYUserAPIManager new] getProfileSuccessHandler:^(ZYLResponseModel *responseModel) { dispatch_group_leave(group); } failureHandler:^(ZYLNetworkError *error) { dispatch_group_leave(group); }]; }); } if (self.infoType != ModifyUserInfo_Type_Main) { dispatch_group_enter(group); dispatch_async(queue, ^{ [[XYUserAPIManager new] getBingAccountSuccessHandler:^(ZYLResponseModel *responseModel) { self.userPhone = responseModel.data[@"userPhone"]; dispatch_group_leave(group); } failureHandler:^(ZYLNetworkError *error) { dispatch_group_leave(group); }]; }); } dispatch_group_enter(group); dispatch_async(queue, ^{ [[XYUserAPIManager new] getProfileNew:self.userId successHandler:^(ZYLResponseModel *responseModel) { self.profileNewArrs = responseModel.data[@"list"]; dispatch_group_leave(group); } failureHandler:^(ZYLNetworkError *error) { dispatch_group_leave(group); }]; }); if (self.infoType == ModifyUserInfo_Type_Main) { dispatch_group_enter(group); dispatch_async(queue, ^{ [[XYFeedAPIManager new] getPhotoListWithLastId:self.lastId userId:self.userId successHandler:^(ZYLResponseModel *responseModel) { self.photoAblumArrs = responseModel.data[@"list"]; dispatch_group_leave(group); // NSArray *list = responseModel.data[@"list"]; // if ([self.lastId isEqualToString:@"0"]) { // [self.dataSource removeAllObjects]; // if (list.count > 0 && [self.userId isEqualToString:[XYUserInfoManager nowUser].userId]) { // XYFeedModel *model = XYFeedModel.new; // [self.dataSource addObject:model]; // } // } // for (NSDictionary *dict in list) { // XYFeedModel *model = XYFeedModel.new; // [model yy_modelSetWithDictionary:dict]; // [self.dataSource addObject:model]; // } // self.lastId = responseModel.data[@"lastId"]; // self.perPage = [responseModel.data[@"pageSize"] integerValue]; // !success?:success(self.dataSource); } failureHandler:^(ZYLNetworkError *error) { // !failure?:failure(error); dispatch_group_leave(group); }]; }); } dispatch_group_notify(group, queue, ^{ dispatch_async(dispatch_get_main_queue(), ^{ [self initValue]; !success?:success(self.dataSource); }); }); } - (void)getCountryList:(SuccessHandler)success failure:(FailureHandler)failure { if (self.infoType != ModifyUserInfo_Type_Main) { [[XYConfigAPIManager new] getCountryPhoneCodeSuccessHandler:^(ZYLResponseModel *responseModel) { [self.countryList removeAllObjects]; for (NSDictionary *dict in responseModel.data[@"list"]) { XYCountryPhoneCodeModel *model = [XYCountryPhoneCodeModel new]; [model yy_modelSetWithDictionary:dict]; [self.countryList addObject:model]; } !success?:success(self.dataSource); } failureHandler:^(ZYLNetworkError *error) { !failure?:failure(error); }]; } } - (NSMutableArray *)countryList { if (!_countryList) { _countryList = [NSMutableArray array]; } return _countryList; } @end