WFCUModifyMyProfileViewController.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // ModifyMyProfileViewController.m
  3. // WildFireChat
  4. //
  5. // Created by heavyrain.lee on 2018/5/20.
  6. // Copyright © 2018 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUModifyMyProfileViewController.h"
  9. #import "MBProgressHUD.h"
  10. #import <WFChatClient/WFCChatClient.h>
  11. #import "WFCUConfigManager.h"
  12. #import "UIView+Toast.h"
  13. #import "WFCUUtilities.h"
  14. @interface WFCUModifyMyProfileViewController () <UITextFieldDelegate>
  15. @property(nonatomic, strong)UITextField *textField;
  16. @property(nonatomic, assign)BOOL isAccount;
  17. @end
  18. @implementation WFCUModifyMyProfileViewController
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:[WFCCNetworkService sharedInstance].userId refresh:NO];
  22. NSString *title = nil;
  23. NSString *defaultValue = nil;
  24. self.isAccount = NO;
  25. switch (self.modifyType) {
  26. case Modify_Email:
  27. title = WFCString(@"ModifyEmail");
  28. defaultValue = userInfo.email;
  29. self.textField.keyboardType = UIKeyboardTypeEmailAddress;
  30. break;
  31. case Modify_Mobile:
  32. title = WFCString(@"ModifyMobile");
  33. defaultValue = userInfo.mobile;
  34. self.textField.keyboardType = UIKeyboardTypePhonePad;
  35. break;
  36. case Modify_Social:
  37. title = WFCString(@"ModifySocialAccount");
  38. defaultValue = userInfo.social;
  39. break;
  40. case Modify_Address:
  41. title = WFCString(@"ModifyAddress");
  42. defaultValue = userInfo.address;
  43. break;
  44. case Modify_Company:
  45. title = WFCString(@"ModifyCompanyInfo");
  46. defaultValue = userInfo.company;
  47. break;
  48. case Modify_DisplayName:
  49. title = WFCString(@"ModifyNickname");
  50. defaultValue = userInfo.displayName;
  51. break;
  52. case 100:
  53. title = WFCString(@"ChangeAccount");
  54. defaultValue = userInfo.name;
  55. self.isAccount = YES;
  56. self.textField.keyboardType = UIKeyboardTypeASCIICapable;
  57. break;
  58. default:
  59. break;
  60. }
  61. self.textField.text = defaultValue;
  62. self.textField.returnKeyType = UIReturnKeyDone;
  63. [self setTitle:title];
  64. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:WFCString(@"Ok") style:UIBarButtonItemStyleDone target:self action:@selector(onDone:)];
  65. self.view.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  66. [self.textField becomeFirstResponder];
  67. }
  68. - (void)onDone:(id)sender {
  69. [self.textField resignFirstResponder];
  70. __weak typeof(self) ws = self;
  71. __block MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  72. hud.label.text = WFCString(@"Updating");
  73. [hud showAnimated:YES];
  74. if (self.modifyType == 100) {
  75. [[WFCUConfigManager globalManager].appServiceProvider changeName:self.textField.text success:^{
  76. [hud hideAnimated:NO];
  77. self.onModified(self.modifyType, self.textField.text);
  78. [ws.navigationController popViewControllerAnimated:YES];
  79. } error:^(int errorCode, NSString * _Nonnull message) {
  80. [hud hideAnimated:NO];
  81. hud = [MBProgressHUD showHUDAddedTo:ws.view animated:YES];
  82. hud.mode = MBProgressHUDModeText;
  83. hud.label.text = message;
  84. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  85. [hud hideAnimated:YES afterDelay:1.f];
  86. }];
  87. } else {
  88. [[WFCCIMService sharedWFCIMService] modifyMyInfo:@{@(self.modifyType):self.textField.text} success:^{
  89. [hud hideAnimated:NO];
  90. self.onModified(self.modifyType, self.textField.text);
  91. [ws.navigationController popViewControllerAnimated:YES];
  92. } error:^(int error_code) {
  93. [hud hideAnimated:NO];
  94. hud = [MBProgressHUD showHUDAddedTo:ws.view animated:YES];
  95. hud.mode = MBProgressHUDModeText;
  96. hud.label.text = WFCString(@"UpdateFailure");
  97. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  98. [hud hideAnimated:YES afterDelay:1.f];
  99. }];
  100. }
  101. }
  102. - (void)didReceiveMemoryWarning {
  103. [super didReceiveMemoryWarning];
  104. // Dispose of any resources that can be recreated.
  105. }
  106. - (UITextField *)textField {
  107. if(!_textField) {
  108. _textField = [[UITextField alloc] initWithFrame:CGRectMake(0, [WFCUUtilities wf_navigationFullHeight] + 20, [UIScreen mainScreen].bounds.size.width, 32)];
  109. _textField.borderStyle = UITextBorderStyleRoundedRect;
  110. _textField.clearButtonMode = UITextFieldViewModeAlways;
  111. _textField.delegate = self;
  112. [_textField addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventEditingChanged];
  113. [self.view addSubview:_textField];
  114. }
  115. return _textField;
  116. }
  117. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  118. if (self.modifyType == 100) {
  119. NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"] invertedSet];
  120. NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
  121. BOOL ret = [string isEqualToString:filtered];
  122. if(!ret) {
  123. [self.view makeToast:@"不支持的字符!仅支持英文字母和数字!" duration:0.5 position:CSToastPositionCenter];
  124. }
  125. return ret;
  126. }
  127. return YES;
  128. }
  129. - (void)textFieldChange:(UITextField *)field {
  130. if (self.textField.text.length) {
  131. self.navigationItem.rightBarButtonItem.enabled = YES;
  132. } else {
  133. self.navigationItem.rightBarButtonItem.enabled = NO;
  134. }
  135. }
  136. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  137. [self onDone:textField];
  138. return YES;
  139. }
  140. @end