Files
TIMSDK/iOS/Demo/TUIKitDemo/Setting/ProfileController.m
T
2021-10-14 14:22:00 +08:00

384 lines
15 KiB
Objective-C

//
// SettingController.m
// TUIKitDemo
//
// Created by kennethmiao on 2018/10/19.
// Copyright © 2018年 Tencent. All rights reserved.
//
#import "ProfileController.h"
#import "LoginController.h"
#import "AppDelegate.h"
#import "TUIButtonCell.h"
#import "TUITextEditController.h"
#import "TUIDateEditController.h"
#import "TUICommonModel.h"
#import "TUICommonTextCell.h"
#import "TUICommonModel.h"
#import "ReactiveObjC/ReactiveObjC.h"
#import "TUIKit.h"
#import "TCUtil.h"
#import "TUIDefine.h"
#import "TUICommonAvatarCell.h"
#define SHEET_COMMON 1
#define SHEET_AGREE 2
#define SHEET_SEX 3
@interface ProfileController () <UIActionSheetDelegate, V2TIMSDKListener>
@property (nonatomic, strong) NSMutableArray *data;
@property V2TIMUserFullInfo *profile;
@property (nonatomic, weak) UIDatePicker *picker;
@property (nonatomic, strong) UIView *datePicker;
@end
@implementation ProfileController {
NSDateFormatter *_dateFormatter;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
[self addLongPressGesture];//添加长按手势
[self setupViews];
//如果不加这一行代码,依然可以实现点击反馈,但反馈会有轻微延迟,体验不好。
self.tableView.delaysContentTouches = NO;
_dateFormatter = [[NSDateFormatter alloc] init];
_dateFormatter.dateFormat = @"yyyy-MM-dd";
}
//实现该委托,保证数据刷新,同时保证 cell 的 select 能够符合显示逻辑
- (void)setupViews
{
self.title = NSLocalizedString(@"ProfileDetails", nil); // @"个人信息";
self.parentViewController.title = NSLocalizedString(@"ProfileDetails", nil); // @"个人信息";
//self.tableView.tableFooterView = [[UIView alloc] init];
self.tableView.backgroundColor = [UIColor d_colorWithColorLight:TController_Background_Color dark:TController_Background_Color_Dark];
self.clearsSelectionOnViewWillAppear = YES;
[self.tableView registerClass:[TUICommonTextCell class] forCellReuseIdentifier:@"textCell"];
[self.tableView registerClass:[TUICommonAvatarCell class] forCellReuseIdentifier:@"avatarCell"];
[[V2TIMManager sharedInstance] addIMSDKListener:self];
NSString *loginUser = [[V2TIMManager sharedInstance] getLoginUser];
[[V2TIMManager sharedInstance] getUsersInfo:@[loginUser] succ:^(NSArray<V2TIMUserFullInfo *> *infoList) {
self.profile = infoList.firstObject;
[self setupData];
} fail:nil];
}
- (void)setupData
{
_data = [NSMutableArray array];
TUICommonAvatarCellData *avatarData = [TUICommonAvatarCellData new];
avatarData.key = NSLocalizedString(@"ProfilePhoto", nil); // @"头像";
avatarData.showAccessory = YES;
avatarData.cselector = @selector(didSelectAvatar);
avatarData.avatarUrl = [NSURL URLWithString:self.profile.faceURL];
[_data addObject:@[avatarData]];
TUICommonTextCellData *nicknameData = [TUICommonTextCellData new];
nicknameData.key = NSLocalizedString(@"ProfileName", nil); // @"昵称";
nicknameData.value = self.profile.showName;
nicknameData.showAccessory = YES;
nicknameData.cselector = @selector(didSelectChangeNick);
TUICommonTextCellData *IDData = [TUICommonTextCellData new];
IDData.key = NSLocalizedString(@"ProfileAccount", nil); // @"帐号";
IDData.value = self.profile.userID;
IDData.showAccessory = NO;
[_data addObject:@[nicknameData, IDData]];
TUICommonTextCellData *signatureData = [TUICommonTextCellData new];
signatureData.key = NSLocalizedString(@"ProfileSignature", nil); // @"个性签名";
signatureData.value = self.profile.showSignature;
signatureData.showAccessory = YES;
signatureData.cselector = @selector(didSelectChangeSignature);
TUICommonTextCellData *sexData = [TUICommonTextCellData new];
sexData.key = NSLocalizedString(@"ProfileGender", nil); // @"性别";
sexData.value = [self.profile showGender];
sexData.showAccessory = YES;
sexData.cselector = @selector(didSelectSex);
TUICommonTextCellData *birthdayData = [TUICommonTextCellData new];
birthdayData.key = NSLocalizedString(@"birthday", nil); // @"生日";
birthdayData.value = [_dateFormatter stringFromDate:NSDate.new];
if (self.profile.birthday) {
NSInteger year = self.profile.birthday / 10000;
NSInteger month = (self.profile.birthday - year * 10000) / 100;
NSInteger day = (self.profile.birthday - year * 10000 - month * 100);
birthdayData.value = [NSString stringWithFormat:@"%04zd-%02zd-%02zd", year, month, day];
}
birthdayData.showAccessory = YES;
birthdayData.cselector = @selector(didSelectBirthday);
[_data addObject:@[signatureData, sexData, birthdayData]];
[self.tableView reloadData];
}
#pragma mark - V2TIMSDKListener
- (void)onSelfInfoUpdated:(V2TIMUserFullInfo *)Info {
self.profile = Info;
[self setupData];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return _data.count;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor clearColor];
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 20;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSMutableArray *array = _data[section];
return array.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *array = _data[indexPath.section];
TUICommonCellData *data = array[indexPath.row];
return [data heightOfWidth:Screen_Width];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableArray *array = _data[indexPath.section];
NSObject *data = array[indexPath.row];
if([data isKindOfClass:[TUICommonTextCellData class]]) {
TUICommonTextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"textCell" forIndexPath:indexPath];
[cell fillWithData:(TUICommonTextCellData *)data];
return cell;
} else if([data isKindOfClass:[TUICommonAvatarCellData class]]){
TUICommonAvatarCell *cell = [tableView dequeueReusableCellWithIdentifier:@"avatarCell" forIndexPath:indexPath];
[cell fillWithData:(TUICommonAvatarCellData *)data];
return cell;
}
return nil;
}
- (void)didSelectChangeNick
{
TUITextEditController *vc = [[TUITextEditController alloc] initWithText:self.profile.nickName];
vc.title = NSLocalizedString(@"ProfileEditName", nil); // @"修改昵称";
[self.navigationController pushViewController:vc animated:YES];
@weakify(self)
[[RACObserve(vc, textValue) skip:1] subscribeNext:^(NSString *x) {
@strongify(self)
V2TIMUserFullInfo *info = [[V2TIMUserFullInfo alloc] init];
info.nickName = x;
[[V2TIMManager sharedInstance] setSelfInfo:info succ:^{
self.profile.nickName = x;
[self setupData];
} fail:nil];
[TCUtil report:Action_Modifyselfprofile actionSub:Action_Sub_Modifynick code:@(0) msg:@"modifynick"];
}];
}
- (void)didSelectChangeSignature
{
TUITextEditController *vc = [[TUITextEditController alloc] initWithText:[self.profile showSignature]];
vc.title = NSLocalizedString(@"ProfileEditSignture", nil); // @"修改个性签名";
[self.navigationController pushViewController:vc animated:YES];
@weakify(self)
[[RACObserve(vc, textValue) skip:1] subscribeNext:^(NSString *x) {
@strongify(self)
V2TIMUserFullInfo *info = [[V2TIMUserFullInfo alloc] init];
info.selfSignature = x;
[[V2TIMManager sharedInstance] setSelfInfo:info succ:^{
self.profile.selfSignature = x;
[self setupData];
} fail:nil];
[TCUtil report:Action_Modifyselfprofile actionSub:Action_Sub_Modifysignature code:@(0) msg:@"modifysignature"];
}];
}
- (void)didSelectSex
{
UIActionSheet *sheet = [[UIActionSheet alloc] init];
sheet.tag = SHEET_SEX;
sheet.title = NSLocalizedString(@"ProfileEditGender", nil); // @"修改性别";
[sheet addButtonWithTitle:NSLocalizedString(@"Male", nil)];
[sheet addButtonWithTitle:NSLocalizedString(@"Female", nil)];
[sheet setCancelButtonIndex:[sheet addButtonWithTitle:NSLocalizedString(@"Canel", nil)]];
[sheet setDelegate:self];
[sheet showInView:self.view];
}
- (void)didSelectAvatar
{
//点击头像的响应函数,换头像,上传头像URL
UIAlertController *ac = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"choose_avatar_for_you", nil) message:nil preferredStyle:UIAlertControllerStyleAlert];
[ac addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *url = [TUITool randAvatarUrl];
V2TIMUserFullInfo *info = [[V2TIMUserFullInfo alloc] init];
info.faceURL = url;
[[V2TIMManager sharedInstance] setSelfInfo:info succ:^{
[self.profile setFaceURL:url];
[self setupData];
} fail:^(int code, NSString *desc) {
}];
[TCUtil report:Action_Modifyselfprofile actionSub:Action_Sub_Modifyfaceurl code:@(0) msg:@"modifyfaceurl"];
}]];
[ac addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:ac animated:YES completion:nil];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (actionSheet.tag == SHEET_SEX) {
V2TIMGender gender = V2TIM_GENDER_UNKNOWN;
if (buttonIndex == 0) {
gender = V2TIM_GENDER_MALE;
}
if (buttonIndex == 1) {
gender = V2TIM_GENDER_FEMALE;
}
V2TIMUserFullInfo *info = [[V2TIMUserFullInfo alloc] init];
info.gender = gender;
[[V2TIMManager sharedInstance] setSelfInfo:info succ:^{
self.profile.gender = gender;
[self setupData];
} fail:nil];
[TCUtil report:Action_Modifyselfprofile actionSub:Action_Sub_Modifygender code:@(0) msg:@"modifygender"];
}
}
/**
* 以下两个函数实现了长按的复制功能。
*/
- (void)addLongPressGesture{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(didLongPressAtCell:)];
[self.tableView addGestureRecognizer:longPress];
}
-(void) didLongPressAtCell:(UILongPressGestureRecognizer *)longPress {
if(longPress.state == UIGestureRecognizerStateBegan){
CGPoint point = [longPress locationInView:self.tableView];
NSIndexPath *pathAtView = [self.tableView indexPathForRowAtPoint:point];
NSObject *data = [self.tableView cellForRowAtIndexPath:pathAtView];
//长按 TUICommonTextCell,可以复制 cell 内的字符串。
if([data isKindOfClass:[TUICommonTextCell class]]){
TUICommonTextCell *textCell = (TUICommonTextCell *)data;
if(textCell.textData.value && ![textCell.textData.value isEqualToString:NSLocalizedString(@"no_set", nil)]){
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = textCell.textData.value;
NSString *toastString = [NSString stringWithFormat:@"copy %@",textCell.textData.key];
[TUITool makeToast:toastString];
}
}else if([data isKindOfClass:[TUIProfileCardCell class]]){
//长按 profileCard,复制自己的账号。
TUIProfileCardCell *profileCard = (TUIProfileCardCell *)data;
if(profileCard.cardData.identifier){
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = profileCard.cardData.identifier;
NSString *toastString = [NSString stringWithFormat:@"copy"];
[TUITool makeToast:toastString];
}
}
}
}
- (void)didSelectBirthday
{
[self hideDatePicker];
[UIApplication.sharedApplication.keyWindow addSubview:self.datePicker];
}
- (UIView *)datePicker
{
if (_datePicker == nil) {
UIView *cover = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height)];
cover.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
[cover addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideDatePicker)]];
UIView *menuView = [[UIView alloc] init];
menuView.backgroundColor = [UIColor whiteColor];
menuView.frame = CGRectMake(0, UIScreen.mainScreen.bounds.size.height - 340, UIScreen.mainScreen.bounds.size.width, 40);
[cover addSubview:menuView];
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cancelButton setTitle:@"cancel" forState:UIControlStateNormal];
[cancelButton setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
cancelButton.frame = CGRectMake(10, 0, 60, 35);
[cancelButton addTarget:self action:@selector(hideDatePicker) forControlEvents:UIControlEventTouchUpInside];
[menuView addSubview:cancelButton];
UIButton *okButton = [UIButton buttonWithType:UIButtonTypeCustom];
[okButton setTitle:@"ok" forState:UIControlStateNormal];
[okButton setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
okButton.frame = CGRectMake(cover.bounds.size.width - 10 - 60, 0, 60, 35);
[okButton addTarget:self action:@selector(onOKDatePicker) forControlEvents:UIControlEventTouchUpInside];
[menuView addSubview:okButton];
UIDatePicker *picker = [[UIDatePicker alloc] init];
picker.backgroundColor = [UIColor whiteColor];
picker.datePickerMode = UIDatePickerModeDate;
picker.frame = CGRectMake(0, CGRectGetMaxY(menuView.frame), cover.bounds.size.width, 300);
[cover addSubview:picker];
self.picker = picker;
_datePicker = cover;
}
return _datePicker;
}
- (void)hideDatePicker
{
[self.datePicker removeFromSuperview];
}
- (void)onOKDatePicker
{
[self hideDatePicker];
NSDate *date = self.picker.date;
NSString *dateStr = [_dateFormatter stringFromDate:date];
dateStr = [dateStr stringByReplacingOccurrencesOfString:@"-" withString:@""];
int birthday = [dateStr intValue];
V2TIMUserFullInfo *info = [[V2TIMUserFullInfo alloc] init];
info.birthday = birthday;
[[V2TIMManager sharedInstance] setSelfInfo:info succ:^{
self.profile.birthday = birthday;
[self setupData];
} fail:nil];
}
@end