123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- //
- // UserLoginFastViewController.m
- // XenonSDK
- //
- // Created by SAGESSE on 2019/5/30.
- // Copyright © 2019 SAGESSE. All rights reserved.
- //
- #import "XenonSDK.h"
- #import "UserLoginPhoneViewController.h"
- #import "UserLoginFastViewController.h"
- @interface UserLoginFastViewController () <UITableViewDelegate, UITableViewDataSource>
- @property (nonatomic, strong) UITableView* selectedTableView;
- @end
- @implementation UserLoginFastViewController
- - (void)loginForFast:(id)sender {
- // Resign editing.
- [self.view endEditing:YES];
-
- [XSNetwork showHudLoading];
- [XSNetwork loginWithComplete:^(id object, NSError *error) {
-
-
- if (error != nil) {
- [XSNetwork showHudFailure:error];
- return;
- }
-
- if (XenonSDK.sharedSDK.sdk_loginCallback) {
- XenonSDK.sharedSDK.sdk_loginCallback(object);
- }
- }];
- }
- - (void)loginForPhone:(id)sender {
-
- // If it's a phone login, try automatic login.
- XSUser* user = self.defaultCell.user;
- if (user.account.length == 0) {
- return;
- }
-
- // Try authorization login.
- XenonSDK.sharedSDK.token = user.token;
- [XSNetwork authWithUser:user.uid complete:^(XSUser* object, NSError *error) {
-
- // Check for general errors.
- if (error != nil && ![error.domain isEqual:@"XSNetwork"]) {
- [XSNetwork showHudFailure:error];
- return;
- }
-
- // Login successful.
- if (user.token.length != 0 && error == nil) {
- // Update user info.
- user.token = object.token;
- user.isRegister = NO;
- XenonSDK.sharedSDK.user = user;
- if (XenonSDK.sharedSDK.sdk_loginCallback) {
- XenonSDK.sharedSDK.sdk_loginCallback(user);
- }
- return;
- }
- [XSNetwork showHudFailure:@"登录时效期已过,请重新登录"];
- // Login failed to login page.
- UserLoginPhoneViewController* viewController = UserLoginPhoneViewController.new;
- [viewController automatic:user.account];
- [self.navigationController pushViewController:viewController animated:YES];
- }];
-
-
- }
- - (void)loginForPassword:(id)sender {
- // Resign editing.
- [self.view endEditing:YES];
-
- XSUser* user = self.defaultCell.user;
- if (user == nil) {
- return;
- }
-
- if (user.account.length == 0) {
- [self loginForFast:sender];
- return;
- }
- if (user.pwd.length == 0) {
- [self loginForPhone:sender];
- return;
- }
-
- [XSNetwork showHudLoading];
- [XSNetwork loginWithAccount:user.account password:user.pwd complete:^(id object, NSError *error) {
-
- if (error != nil) {
- [XSNetwork showHudFailure:error];
- return;
- }
-
- if (XenonSDK.sharedSDK.sdk_loginCallback) {
- XenonSDK.sharedSDK.sdk_loginCallback(object);
- }
- }];
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- // The container view must be provided first.
- [self.containerView layoutIfNeeded];
- [self.view layoutIfNeeded];
-
- // Create the default Cell.
- self.defaultCell = [[UINib nibWithNibName:@"UserLoginFastCell" bundle:XenonSDK.sharedSDK.bundle] instantiateWithOwner:nil options:nil].firstObject;
-
- // Setup layout and over bounds
- self.defaultCell.frame = UIEdgeInsetsInsetRect(self.containerView.bounds, UIEdgeInsetsMake(-1, 0, -1, 0));
- self.defaultCell.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
- [self.defaultCell.selecteButton addTarget:self action:@selector(showList:) forControlEvents:UIControlEventTouchUpInside];
- self.defaultCell.user = XSUser.history.firstObject;
-
- //containerView.alpha = 0.8
- [self.containerView insertSubview:self.defaultCell atIndex:0];
- }
- - (void)showList:(id)sender {
-
- // Selecting, disable any operation.
- self.containerView.userInteractionEnabled = NO;
- CGRect rect = CGRectOffset(self.containerView.frame, 0, self.containerView.frame.size.height);
- CGRect begin = CGRectMake(CGRectGetMinX(rect), CGRectGetMinY(rect), CGRectGetWidth(rect), 0);
- CGRect end = CGRectMake(CGRectGetMinX(rect), CGRectGetMinY(rect), CGRectGetWidth(rect), CGRectGetHeight(rect) * 2);
-
- UIButton* maskView = [[UIButton alloc] initWithFrame:self.view.bounds];
- [maskView addTarget:self action:@selector(hideList:) forControlEvents:UIControlEventTouchUpInside];
- UITableView* tableView = [[UITableView alloc] initWithFrame:begin style:UITableViewStylePlain];
- [tableView registerNib:[UINib nibWithNibName:@"UserLoginFastCell" bundle:XenonSDK.sharedSDK.bundle] forCellReuseIdentifier:@"Item"];
- tableView.dataSource = self;
- tableView.delegate = self;
- tableView.tableFooterView = UIView.new;
- tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
- tableView.separatorColor = UIColor.clearColor;
- tableView.backgroundColor = self.defaultCell.backgroundColor;
- maskView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
- [maskView addSubview:tableView];
- [self.view addSubview:maskView];
- if (@available(iOS 11.0, *)) {
- tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
- }
-
- [UIView animateWithDuration:0.25 animations:^{
- tableView.frame = end;
- }];
- self.selectedTableView = tableView;
- }
- - (void)hideList:(id)sender {
-
- // Gets the list being displayed.
- UITableView* tableView = self.selectedTableView;
- if (tableView == nil) {
- return;
- }
-
- // add hide aniatmion.
- CGRect rect = tableView.frame;
- rect.size.height = 0;
- [UIView animateWithDuration:0.25 animations:^{
- tableView.frame = rect;
- } completion:^(BOOL finished) {
- [tableView.superview removeFromSuperview];
- self.selectedTableView = nil;
- self.containerView.userInteractionEnabled = YES;
- }];
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return XSUser.history.count;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- return self.containerView.bounds.size.height;
- }
- - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- UserLoginFastCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Item" forIndexPath:indexPath];
- if (cell.selecteButton.allTargets.count == 0) {
- [cell.selecteButton addTarget:self action:@selector(selectItem:) forControlEvents:UIControlEventTouchUpInside];
- }
- cell.user = XSUser.history[indexPath.row];
- if ([cell viewWithTag:-1] == nil) {
- CGFloat height = 1 / UIScreen.mainScreen.scale;
- UIView* line = [[UIView alloc] initWithFrame:CGRectMake(0, cell.frame.size.height - height, cell.frame.size.width, height)];
- line.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
- line.backgroundColor = [UIColor.lightGrayColor colorWithAlphaComponent:0.2];
- line.tag = -1;
- [cell addSubview:line];
- }
- return cell;
- }
- - (void)selectItem:(UIButton*)sender {
-
- if (self.selectedTableView == nil) {
- return;
- }
- NSIndexPath* indexPath = [self.selectedTableView indexPathForRowAtPoint:[self.selectedTableView convertRect:sender.bounds fromView:sender].origin];
- if (indexPath == nil) {
- return;
- }
-
- self.defaultCell.user = XSUser.history[indexPath.row];
- [self hideList:sender];
- }
- @end
|