ASInfomationSetController.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. //
  2. // ASInfomationSetController.m
  3. // Asteria
  4. //
  5. // Created by iOS on 2023/7/5.
  6. //
  7. #import "ASInfomationSetController.h"
  8. #import "KWTextField.h"
  9. #import <BRPickerView/BRPickerView.h>
  10. #import "ASHomeAlertWindow.h"
  11. @interface ASInfomationSetController () <UITextFieldDelegate>
  12. /// 0: 只有邮箱 1: 邮箱加密码 2: 邮箱+验证码+密码 3 邮箱+验证码
  13. @property (nonatomic,assign) NSInteger showType;
  14. @property (nonatomic, strong) UIScrollView *scrollV;
  15. @property (nonatomic, strong) UIStackView *totalStackV;
  16. @property (nonatomic, strong) KWTextField *lastNTf;
  17. @property (nonatomic, strong) KWTextField *nameTf;
  18. @property (nonatomic, strong) KWTextField *birthTf;
  19. @property (nonatomic, strong) UIStackView *passStackV;
  20. @property (nonatomic, strong) UIButton *changePassBt;
  21. @property (nonatomic, strong) KWTextField *emailTf;
  22. @property (nonatomic, strong) KWTextField *oldPassTf;
  23. @property (nonatomic, strong) KWTextField *nPassTf;
  24. @property (nonatomic, strong) UIButton *bottomBt;
  25. @property (nonatomic, strong) NSDate *currentDate;
  26. //@property (nonatomic, strong) KWInfoSettingViewModel *vm;
  27. @end
  28. @implementation ASInfomationSetController
  29. - (void)viewDidLoad {
  30. [super viewDidLoad];
  31. // self.vm = [KWInfoSettingViewModel new];
  32. self.titleStr = @"Information";
  33. [self setNavRightSearch:^{
  34. }];
  35. self.statusBgV.backgroundColor = Col_FFF;
  36. self.customNavBar.backgroundColor = Col_FFF;
  37. [self loadSubVs];
  38. [self setData];
  39. }
  40. - (void)setData {
  41. ASUserModel *m = ASUserInfoManager.shared.userInfo;
  42. NSString *bob = ASUserInfoManager.shared.birthDay;
  43. self.lastNTf.text = m.lastname;
  44. self.nameTf.text = m.firstname;
  45. self.currentDate = [NSDate dateWithString:bob format:@"MM dd, yyyy"];
  46. self.birthTf.text = [self.currentDate stringWithFormat:@"MM/dd/yyyy"];
  47. self.showType = 0;
  48. self.emailTf.text = m.email;
  49. [self configViewsByShowType];
  50. }
  51. - (void)loadSubVs {
  52. [self.view addSubview:self.scrollV];
  53. UIStackView *tStackV = [[UIStackView alloc] init];
  54. tStackV.axis = UILayoutConstraintAxisVertical;
  55. tStackV.alignment = UIStackViewAlignmentFill;
  56. tStackV.distribution = UIStackViewDistributionFill;
  57. tStackV.spacing = 20;
  58. self.totalStackV = tStackV;
  59. UIStackView *topStackV = [[UIStackView alloc] init];
  60. topStackV.axis = UILayoutConstraintAxisHorizontal;
  61. topStackV.alignment = UIStackViewAlignmentFill;
  62. topStackV.distribution = UIStackViewDistributionFillEqually;
  63. topStackV.spacing = 20;
  64. [self.totalStackV addArrangedSubview:topStackV];
  65. NSArray *titles = @[@"First Name", @"Last Name", @"Date Of Birth"];
  66. NSArray *placeHolders = @[@"Pleace Input Name", @"Pleace Input Last Name", @"Pleace Select Date Of Birth"];
  67. for (int i = 0; i<titles.count; i++) {
  68. UILabel *lb = [[UILabel alloc] init];
  69. lb.font = [UIFont fontWithName:Rob_Regular size:12];
  70. lb.textColor = UIColor.blackColor;
  71. KWTextField *tf = [[KWTextField alloc] init];
  72. UIStackView *stackV = [[UIStackView alloc] init];
  73. stackV.axis = UILayoutConstraintAxisVertical;
  74. stackV.alignment = UIStackViewAlignmentFill;
  75. stackV.distribution = UIStackViewDistributionFill;
  76. stackV.spacing = 5;
  77. [stackV addArrangedSubview:lb];
  78. [stackV addArrangedSubview:tf];
  79. // lb.text = titles[i];
  80. tf.placeholder = placeHolders[i];
  81. [lb mas_makeConstraints:^(MASConstraintMaker *make) {
  82. make.height.equalTo(@5);
  83. }];
  84. [tf mas_makeConstraints:^(MASConstraintMaker *make) {
  85. make.height.equalTo(@45);
  86. }];
  87. if (i == 0 || i == 1) {
  88. [topStackV addArrangedSubview:stackV];
  89. } else {
  90. [self.totalStackV addArrangedSubview:stackV];
  91. }
  92. switch (i) {
  93. case 0: {
  94. self.nameTf = tf;
  95. break;
  96. }
  97. case 1: {
  98. self.lastNTf = tf;
  99. break;
  100. }
  101. case 2: {
  102. self.birthTf = tf;
  103. break;
  104. }
  105. default:
  106. break;
  107. }
  108. }
  109. UIStackView *pStackV = [[UIStackView alloc] init];
  110. pStackV.axis = UILayoutConstraintAxisVertical;
  111. pStackV.alignment = UIStackViewAlignmentFill;
  112. pStackV.distribution = UIStackViewDistributionFillEqually;
  113. pStackV.spacing = 20;
  114. self.passStackV = pStackV;
  115. NSArray *titleArr = @[@"Email",@"",@""];
  116. NSArray *placeArr = @[@"Place Input Email",@"* OLD Password",@"* New Password"];
  117. for (int i=0; i<titleArr.count; i++) {
  118. KWTextField *tf = [[KWTextField alloc] init];
  119. UIStackView *stackV = [[UIStackView alloc] init];
  120. stackV.axis = UILayoutConstraintAxisVertical;
  121. stackV.alignment = UIStackViewAlignmentFill;
  122. stackV.distribution = UIStackViewDistributionFill;
  123. stackV.spacing = 5;
  124. if (![titleArr[i] isEqualToString:@""]) {
  125. UILabel *lb = [[UILabel alloc] init];
  126. lb.font = [UIFont fontWithName:Rob_Regular size:12];
  127. lb.textColor = UIColor.blackColor;
  128. [stackV addArrangedSubview:lb];
  129. // lb.text = titleArr[i];
  130. [lb mas_makeConstraints:^(MASConstraintMaker *make) {
  131. make.height.equalTo(@5);
  132. }];
  133. }
  134. [stackV addArrangedSubview:tf];
  135. tf.placeholder = placeArr[i];
  136. [tf mas_makeConstraints:^(MASConstraintMaker *make) {
  137. make.height.equalTo(@45);
  138. }];
  139. tf.delegate = self;
  140. [tf addTarget:self action:@selector(textfieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  141. [tf addTarget:self action:@selector(textfieldDidChange:) forControlEvents:UIControlEventValueChanged];
  142. switch (i) {
  143. case 0: {
  144. self.emailTf = tf;
  145. [self.totalStackV addArrangedSubview:stackV];
  146. UIView *v = [UIView new];
  147. v.backgroundColor = UIColor.clearColor;
  148. [v addSubview:self.changePassBt];
  149. [self.totalStackV addArrangedSubview:v];
  150. [v mas_makeConstraints:^(MASConstraintMaker *make) {
  151. make.height.equalTo(@30);
  152. }];
  153. [self.changePassBt mas_makeConstraints:^(MASConstraintMaker *make) {
  154. make.top.left.bottom.equalTo(v);
  155. }];
  156. break;
  157. }
  158. case 1: {
  159. self.oldPassTf = tf;
  160. self.oldPassTf.secureTextEntry = true;
  161. UIView *lv = [self getPassRightBt:800000];
  162. self.oldPassTf.rightView = lv;
  163. [lv mas_makeConstraints:^(MASConstraintMaker *make) {
  164. make.width.equalTo(@50);
  165. make.height.equalTo(@45);
  166. }];
  167. self.oldPassTf.rightViewMode = UITextFieldViewModeAlways;
  168. [self.totalStackV addArrangedSubview:self.passStackV];
  169. [self.passStackV addArrangedSubview:stackV];
  170. break;
  171. }
  172. case 2: {
  173. self.nPassTf = tf;
  174. self.nPassTf.secureTextEntry = true;
  175. UIView *lv = [self getPassRightBt:800001];
  176. lv.frame = CGRectMake(0, 0, 50, 45);
  177. self.nPassTf.rightView = lv;
  178. [lv mas_makeConstraints:^(MASConstraintMaker *make) {
  179. make.width.equalTo(@50);
  180. make.height.equalTo(@45);
  181. }];
  182. self.nPassTf.rightViewMode = UITextFieldViewModeAlways;
  183. [self.passStackV addArrangedSubview:stackV];
  184. break;
  185. }
  186. default:
  187. break;
  188. }
  189. }
  190. self.passStackV.hidden = true;
  191. [self.scrollV addSubview:self.totalStackV];
  192. [self.totalStackV mas_makeConstraints:^(MASConstraintMaker *make) {
  193. make.top.equalTo(self.scrollV).offset(20);
  194. make.left.equalTo(self.scrollV).offset(20);
  195. make.right.equalTo(self.view).offset(-20);
  196. make.bottom.equalTo(self.scrollV).offset(20);
  197. make.width.equalTo([NSNumber numberWithFloat:KScreenWidth-40]);
  198. }];
  199. [self.view addSubview:self.bottomBt];
  200. [self.bottomBt mas_makeConstraints:^(MASConstraintMaker *make) {
  201. make.left.equalTo(self.view).offset(20);
  202. make.right.equalTo(self.view).offset(-20);
  203. make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-20);
  204. make.height.equalTo(@45);
  205. }];
  206. [self.scrollV mas_makeConstraints:^(MASConstraintMaker *make) {
  207. make.left.right.equalTo(self.view);
  208. make.top.equalTo(self.customNavBar.mas_bottom);
  209. make.bottom.equalTo(self.bottomBt.mas_top).offset(-10);
  210. }];
  211. self.birthTf.delegate = self;
  212. }
  213. - (UIScrollView *)scrollV {
  214. if (!_scrollV) {
  215. UIScrollView *v = [[UIScrollView alloc] init];
  216. v.showsVerticalScrollIndicator = false;
  217. v.showsHorizontalScrollIndicator = false;
  218. v.alwaysBounceVertical = true;
  219. v.backgroundColor = [UIColor colorWithHexString:@"#f8f8f8"];
  220. _scrollV = v;
  221. }
  222. return _scrollV;
  223. }
  224. - (UIButton *)changePassBt {
  225. if (!_changePassBt) {
  226. UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
  227. [bt setImage:[UIImage imageNamed:@"login_selectRido"] forState:UIControlStateNormal];
  228. [bt setImage:[UIImage imageNamed:@"login_selectRido_select"] forState:UIControlStateSelected];
  229. [bt setTitle:@" Change Password" forState:UIControlStateNormal];
  230. bt.titleLabel.font = [UIFont fontWithName:Rob_Regular size:12];
  231. [bt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  232. [bt addTarget:self action:@selector(changePassTypeChanege) forControlEvents:UIControlEventTouchUpInside];
  233. _changePassBt = bt;
  234. }
  235. return _changePassBt;
  236. }
  237. - (void)changePassTypeChanege {
  238. switch (self.showType) {
  239. case 0:
  240. self.showType = 1;
  241. break;
  242. case 1:
  243. self.showType = 0;
  244. break;
  245. default:
  246. break;
  247. }
  248. [self configViewsByShowType];
  249. }
  250. - (void)configViewsByShowType {
  251. switch (self.showType) {
  252. case 0:
  253. self.changePassBt.selected = false;
  254. self.passStackV.hidden = true;
  255. [self.bottomBt setTitle:@"SAVE CHANGES" forState:UIControlStateNormal];
  256. break;
  257. case 1:
  258. self.changePassBt.selected = true;
  259. self.passStackV.hidden = false;
  260. [self.bottomBt setTitle:@"SAVE CHANGES" forState:UIControlStateNormal];
  261. break;
  262. default:
  263. break;
  264. }
  265. [self configBtState];
  266. }
  267. - (void)configBtState {
  268. switch (self.showType) {
  269. case 0:
  270. self.bottomBt.enabled = !self.emailTf.text.isEmpty && !self.nameTf.text.isEmpty && !self.lastNTf.text.isEmpty && !self.birthTf.text.isEmpty;
  271. break;
  272. case 1: {
  273. BOOL isflag = !self.emailTf.text.isEmpty && !self.oldPassTf.text.isEmpty && !self.nPassTf.text.isEmpty && !self.nameTf.text.isEmpty && !self.lastNTf.text.isEmpty && !self.birthTf.text.isEmpty;
  274. self.bottomBt.enabled = isflag;
  275. break;
  276. }
  277. default:
  278. break;
  279. }
  280. self.bottomBt.backgroundColor = self.bottomBt.isEnabled ? [UIColor blackColor] : [UIColor colorWithHexString:@"#ADADAD"];
  281. }
  282. - (UIView *)getPassRightBt:(NSInteger)tag {
  283. UIView *v = [UIView new];
  284. v.backgroundColor = UIColor.whiteColor;
  285. UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
  286. [bt setImage:[UIImage imageNamed:@"login_password_secure"] forState:UIControlStateNormal];
  287. [bt setImage:[UIImage imageNamed:@"login_password_secure_display"] forState:UIControlStateSelected];
  288. [bt addTarget:self action:@selector(tfBtAction:) forControlEvents:UIControlEventTouchUpInside];
  289. bt.tag = tag;
  290. [v addSubview:bt];
  291. [bt mas_makeConstraints:^(MASConstraintMaker *make) {
  292. make.width.height.equalTo(@30);
  293. make.right.equalTo(v).offset(-17);
  294. make.centerY.equalTo(v);
  295. }];
  296. return v;
  297. }
  298. - (void)tfBtAction:(UIButton *)bt {
  299. NSInteger tag = bt.tag - 800000;
  300. bt.selected = !bt.isSelected;
  301. if (tag == 0) {
  302. self.oldPassTf.secureTextEntry = !bt.isSelected;
  303. } else {
  304. self.nPassTf.secureTextEntry = !bt.isSelected;
  305. }
  306. }
  307. - (UIButton *)bottomBt {
  308. if (!_bottomBt) {
  309. UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
  310. [bt setTitle:@"NEXT" forState:UIControlStateNormal];
  311. bt.titleLabel.font = [UIFont fontWithName:Rob_Regular size:16];
  312. [bt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  313. [bt setBackgroundColor:[UIColor colorWithHexString:@"#ADADAD"]];
  314. bt.layer.cornerRadius = 4;
  315. bt.layer.masksToBounds = true;
  316. bt.enabled = false;
  317. [bt addTarget:self action:@selector(bottomBtAction) forControlEvents:UIControlEventTouchUpInside];
  318. _bottomBt = bt;
  319. }
  320. return _bottomBt;
  321. }
  322. - (void)bottomBtAction {
  323. if (self.emailTf.text == nil || self.emailTf.text.length == 0) {
  324. [self.view makeToast:@"The email address con't be empty"];
  325. return;
  326. }
  327. if (self.nameTf.text == nil || self.nameTf.text.length == 0) {
  328. [self.view makeToast:@"The firstName con't be empty"];
  329. return;
  330. }
  331. if (self.lastNTf.text == nil || self.lastNTf.text.length == 0) {
  332. [self.view makeToast:@"The lastName con't be empty"];
  333. return;
  334. }
  335. if (self.changePassBt.isSelected) {
  336. if (self.oldPassTf.text == nil || self.oldPassTf.text.length == 0) {
  337. [self.view makeToast:@"The old password con't be empty"];
  338. return;
  339. }
  340. if (self.nPassTf.text == nil || self.nPassTf.text.length == 0) {
  341. [self.view makeToast:@"The new password con't be empty"];
  342. return;
  343. }
  344. }
  345. NSMutableDictionary *param = [NSMutableDictionary dictionaryWithDictionary:@{
  346. @"email": self.emailTf.text,
  347. @"firstname": self.nameTf.text,
  348. @"lastname": self.lastNTf.text,
  349. }];
  350. if (self.birthTf.text != nil && self.birthTf.text.length > 0) {
  351. param[@"dob"] = self.birthTf.text;
  352. }
  353. BOOL __block isInfoSuc = false;
  354. BOOL __block isPassSuc = false;
  355. __weak typeof(self) weakSelf = self;
  356. [MBProgressHUD showHUDAddedTo:self.view animated:true];
  357. dispatch_group_t group = dispatch_group_create();
  358. dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
  359. dispatch_group_enter(group);
  360. dispatch_group_async(group, queue, ^{
  361. [ASNetTools.shared putWithPath:putUserInfo param: @{@"customer":param} success:^(id _Nonnull json) {
  362. isInfoSuc = true;
  363. dispatch_group_leave(group);
  364. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  365. [weakSelf.view makeToast:@"Bad Net"];
  366. isInfoSuc = false;
  367. dispatch_group_leave(group);
  368. }];
  369. });
  370. if (!self.changePassBt.isSelected) {
  371. isPassSuc = true;
  372. dispatch_group_notify(group, dispatch_get_main_queue(), ^{
  373. [MBProgressHUD hideHUDForView:weakSelf.view animated:true];
  374. BOOL flag = isInfoSuc;
  375. NSString *str = @"Successfully Change Information";
  376. if (weakSelf.changePassBt.isSelected) {
  377. flag = isInfoSuc && isPassSuc;
  378. str = @"Successfully Change Information, Please Log In Again";
  379. }
  380. if (!flag) {
  381. [ASUserInfoManager.shared getInfo];
  382. return;
  383. }
  384. [ASHomeAlertWindow alertMsg:str];
  385. if (weakSelf.changePassBt.isSelected) {
  386. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  387. [ASHomeAlertWindow clearWindow];
  388. [weakSelf popAndToLogin];
  389. });
  390. } else {
  391. [ASUserInfoManager.shared getInfo];
  392. }
  393. });
  394. return;
  395. }
  396. NSDictionary *dic = @{
  397. @"currentPassword": self.oldPassTf.text,
  398. @"newPassword": self.nPassTf.text,
  399. };
  400. dispatch_group_enter(group);
  401. dispatch_group_async(group, queue, ^{
  402. [ASNetTools.shared putWithPath:putUserPassword param:dic success:^(id _Nonnull json) {
  403. isPassSuc = true;
  404. dispatch_group_leave(group);
  405. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  406. [weakSelf.view makeToast:@"Bad Net"];
  407. isPassSuc = false;
  408. dispatch_group_leave(group);
  409. }];
  410. });
  411. dispatch_group_notify(group, dispatch_get_main_queue(), ^{
  412. [MBProgressHUD hideHUDForView:weakSelf.view animated:true];
  413. BOOL flag = isInfoSuc;
  414. NSString *str = @"Successfully Change Information";
  415. if (weakSelf.changePassBt.isSelected) {
  416. flag = isInfoSuc && isPassSuc;
  417. str = @"Successfully Change Information, Please Log In Again";
  418. }
  419. if (!flag) {
  420. [ASUserInfoManager.shared getInfo];
  421. return;
  422. }
  423. [ASHomeAlertWindow alertMsg:str];
  424. if (weakSelf.changePassBt.isSelected) {
  425. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  426. [ASHomeAlertWindow clearWindow];
  427. [weakSelf popAndToLogin];
  428. });
  429. } else {
  430. [ASUserInfoManager.shared getInfo];
  431. }
  432. });
  433. }
  434. - (void)textfieldDidChange:(UITextField *)tf {
  435. [self configBtState];
  436. }
  437. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
  438. if ([self.birthTf isEqual:textField]) {
  439. BRDatePickerView *datePickerView = [[BRDatePickerView alloc]init];
  440. datePickerView.pickerMode = BRDatePickerModeDate;
  441. datePickerView.title = @"Please Select The Date Of Birth";
  442. datePickerView.selectDate = self.currentDate;
  443. datePickerView.minDate = [NSDate br_setYear:1900 month:1 day:1];
  444. datePickerView.maxDate = [NSDate date];
  445. datePickerView.isAutoSelect = YES;
  446. @weakify(self);
  447. datePickerView.resultBlock = ^(NSDate *selectDate, NSString *selectValue) {
  448. weak_self.currentDate = selectDate;
  449. NSDateFormatter *formartter= [[NSDateFormatter alloc]init];
  450. [formartter setDateFormat:@"MM/dd/yyyy"];
  451. NSString *currentDateStr = [formartter stringFromDate:selectDate];
  452. weak_self.birthTf.text = currentDateStr;
  453. [weak_self configBtState];
  454. NSLog(@"selectValue=%@", selectValue);
  455. NSLog(@"selectDate=%@", selectDate);
  456. NSLog(@"---------------------------------");
  457. };
  458. [datePickerView show];
  459. return false;
  460. }
  461. return true;
  462. }
  463. @end