UserLoginFastViewController.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // UserLoginFastViewController.m
  3. // XenonSDK
  4. //
  5. // Created by SAGESSE on 2019/5/30.
  6. // Copyright © 2019 SAGESSE. All rights reserved.
  7. //
  8. #import "XenonSDK.h"
  9. #import "UserLoginPhoneViewController.h"
  10. #import "UserLoginFastViewController.h"
  11. @interface UserLoginFastViewController () <UITableViewDelegate, UITableViewDataSource>
  12. @property (nonatomic, strong) UITableView* selectedTableView;
  13. @end
  14. @implementation UserLoginFastViewController
  15. - (void)loginForFast:(id)sender {
  16. // Resign editing.
  17. [self.view endEditing:YES];
  18. [XSNetwork showHudLoading];
  19. [XSNetwork loginWithComplete:^(id object, NSError *error) {
  20. if (error != nil) {
  21. [XSNetwork showHudFailure:error];
  22. return;
  23. }
  24. if (XenonSDK.sharedSDK.sdk_loginCallback) {
  25. XenonSDK.sharedSDK.sdk_loginCallback(object);
  26. }
  27. }];
  28. }
  29. - (void)loginForPhone:(id)sender {
  30. // If it's a phone login, try automatic login.
  31. XSUser* user = self.defaultCell.user;
  32. if (user.account.length == 0) {
  33. return;
  34. }
  35. // Try authorization login.
  36. XenonSDK.sharedSDK.token = user.token;
  37. [XSNetwork authWithUser:user.uid complete:^(XSUser* object, NSError *error) {
  38. // Check for general errors.
  39. if (error != nil && ![error.domain isEqual:@"XSNetwork"]) {
  40. [XSNetwork showHudFailure:error];
  41. return;
  42. }
  43. // Login successful.
  44. if (user.token.length != 0 && error == nil) {
  45. // Update user info.
  46. user.token = object.token;
  47. user.isRegister = NO;
  48. XenonSDK.sharedSDK.user = user;
  49. if (XenonSDK.sharedSDK.sdk_loginCallback) {
  50. XenonSDK.sharedSDK.sdk_loginCallback(user);
  51. }
  52. return;
  53. }
  54. [XSNetwork showHudFailure:@"登录时效期已过,请重新登录"];
  55. // Login failed to login page.
  56. UserLoginPhoneViewController* viewController = UserLoginPhoneViewController.new;
  57. [viewController automatic:user.account];
  58. [self.navigationController pushViewController:viewController animated:YES];
  59. }];
  60. }
  61. - (void)loginForPassword:(id)sender {
  62. // Resign editing.
  63. [self.view endEditing:YES];
  64. XSUser* user = self.defaultCell.user;
  65. if (user == nil) {
  66. return;
  67. }
  68. if (user.account.length == 0) {
  69. [self loginForFast:sender];
  70. return;
  71. }
  72. if (user.pwd.length == 0) {
  73. [self loginForPhone:sender];
  74. return;
  75. }
  76. [XSNetwork showHudLoading];
  77. [XSNetwork loginWithAccount:user.account password:user.pwd complete:^(id object, NSError *error) {
  78. if (error != nil) {
  79. [XSNetwork showHudFailure:error];
  80. return;
  81. }
  82. if (XenonSDK.sharedSDK.sdk_loginCallback) {
  83. XenonSDK.sharedSDK.sdk_loginCallback(object);
  84. }
  85. }];
  86. }
  87. - (void)viewDidLoad {
  88. [super viewDidLoad];
  89. // The container view must be provided first.
  90. [self.containerView layoutIfNeeded];
  91. [self.view layoutIfNeeded];
  92. // Create the default Cell.
  93. self.defaultCell = [[UINib nibWithNibName:@"UserLoginFastCell" bundle:XenonSDK.sharedSDK.bundle] instantiateWithOwner:nil options:nil].firstObject;
  94. // Setup layout and over bounds
  95. self.defaultCell.frame = UIEdgeInsetsInsetRect(self.containerView.bounds, UIEdgeInsetsMake(-1, 0, -1, 0));
  96. self.defaultCell.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  97. [self.defaultCell.selecteButton addTarget:self action:@selector(showList:) forControlEvents:UIControlEventTouchUpInside];
  98. self.defaultCell.user = XSUser.history.firstObject;
  99. //containerView.alpha = 0.8
  100. [self.containerView insertSubview:self.defaultCell atIndex:0];
  101. }
  102. - (void)showList:(id)sender {
  103. // Selecting, disable any operation.
  104. self.containerView.userInteractionEnabled = NO;
  105. CGRect rect = CGRectOffset(self.containerView.frame, 0, self.containerView.frame.size.height);
  106. CGRect begin = CGRectMake(CGRectGetMinX(rect), CGRectGetMinY(rect), CGRectGetWidth(rect), 0);
  107. CGRect end = CGRectMake(CGRectGetMinX(rect), CGRectGetMinY(rect), CGRectGetWidth(rect), CGRectGetHeight(rect) * 2);
  108. UIButton* maskView = [[UIButton alloc] initWithFrame:self.view.bounds];
  109. [maskView addTarget:self action:@selector(hideList:) forControlEvents:UIControlEventTouchUpInside];
  110. UITableView* tableView = [[UITableView alloc] initWithFrame:begin style:UITableViewStylePlain];
  111. [tableView registerNib:[UINib nibWithNibName:@"UserLoginFastCell" bundle:XenonSDK.sharedSDK.bundle] forCellReuseIdentifier:@"Item"];
  112. tableView.dataSource = self;
  113. tableView.delegate = self;
  114. tableView.tableFooterView = UIView.new;
  115. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  116. tableView.separatorColor = UIColor.clearColor;
  117. tableView.backgroundColor = self.defaultCell.backgroundColor;
  118. maskView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  119. [maskView addSubview:tableView];
  120. [self.view addSubview:maskView];
  121. if (@available(iOS 11.0, *)) {
  122. tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  123. }
  124. [UIView animateWithDuration:0.25 animations:^{
  125. tableView.frame = end;
  126. }];
  127. self.selectedTableView = tableView;
  128. }
  129. - (void)hideList:(id)sender {
  130. // Gets the list being displayed.
  131. UITableView* tableView = self.selectedTableView;
  132. if (tableView == nil) {
  133. return;
  134. }
  135. // add hide aniatmion.
  136. CGRect rect = tableView.frame;
  137. rect.size.height = 0;
  138. [UIView animateWithDuration:0.25 animations:^{
  139. tableView.frame = rect;
  140. } completion:^(BOOL finished) {
  141. [tableView.superview removeFromSuperview];
  142. self.selectedTableView = nil;
  143. self.containerView.userInteractionEnabled = YES;
  144. }];
  145. }
  146. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  147. return XSUser.history.count;
  148. }
  149. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  150. return self.containerView.bounds.size.height;
  151. }
  152. - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  153. UserLoginFastCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Item" forIndexPath:indexPath];
  154. if (cell.selecteButton.allTargets.count == 0) {
  155. [cell.selecteButton addTarget:self action:@selector(selectItem:) forControlEvents:UIControlEventTouchUpInside];
  156. }
  157. cell.user = XSUser.history[indexPath.row];
  158. if ([cell viewWithTag:-1] == nil) {
  159. CGFloat height = 1 / UIScreen.mainScreen.scale;
  160. UIView* line = [[UIView alloc] initWithFrame:CGRectMake(0, cell.frame.size.height - height, cell.frame.size.width, height)];
  161. line.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
  162. line.backgroundColor = [UIColor.lightGrayColor colorWithAlphaComponent:0.2];
  163. line.tag = -1;
  164. [cell addSubview:line];
  165. }
  166. return cell;
  167. }
  168. - (void)selectItem:(UIButton*)sender {
  169. if (self.selectedTableView == nil) {
  170. return;
  171. }
  172. NSIndexPath* indexPath = [self.selectedTableView indexPathForRowAtPoint:[self.selectedTableView convertRect:sender.bounds fromView:sender].origin];
  173. if (indexPath == nil) {
  174. return;
  175. }
  176. self.defaultCell.user = XSUser.history[indexPath.row];
  177. [self hideList:sender];
  178. }
  179. @end