// // RootViewController.m // XenonSDK // // Created by SAGESSE on 2019/5/29. // Copyright © 2019 SAGESSE. All rights reserved. // #import "XenonSDK.h" #import "XSUtils.h" #import "RootViewController.h" #import "NavigationAnimator.h" #import "HelperViewController.h" #import "UserLoginPhoneViewController.h" #import "UserLoginAccountViewController.h" #import "UserRegisterAccountViewController.h" #import "UserProtocolViewController.h" #import "UserRestPasswordViewController.h" #import "UserRestPassword2ViewController.h" @interface RootViewController () @property (nonatomic, strong) id keybardInfo; @end @implementation RootViewController - (instancetype)initWithRootViewController:(UIViewController*)rootViewController { self = [self initWithNibName:nil bundle:XenonSDK.sharedSDK.bundle]; self.embedViewController = [[UINavigationController alloc] initWithRootViewController:rootViewController]; self.modalPresentationStyle = UIModalPresentationOverFullScreen; self.embedViewController.delegate = self; return self; } - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { return [super initWithNibName:@"RootViewController" bundle:nibBundleOrNil]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // self.view.backgroundColor = UIColor.clearColor; [self.view addSubview:self.embedView]; [self viewWillLayoutSubviews]; // Update font size dynamically. if (self.titleLabel != nil) { self.titleLabel.font = [self.titleLabel.font fontWithSize:CGFloatBasedI414(26)]; // foce load view. (void)self.embedViewController.topViewController.view; // self.titleLabel.text = (self.embedViewController.topViewController as? BaseViewController)?.topTitle } if (self.theme == nil) { UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:self.view.bounds]; toolbar.barStyle = UIBarStyleDefault; toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; toolbar.userInteractionEnabled = false; //toolbar.setShadowImage(UIImage(), forToolbarPosition: .any) [self.embedView insertSubview:toolbar atIndex:0]; } //self.embedController.isNavigationBarHidden = true self.embedViewController.view.frame = self.embedView.bounds; self.embedViewController.view.backgroundColor = UIColor.clearColor; self.embedViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; // self.embedViewController.navigationBar.isTranslucent = true // self.embedViewController.navigationBar.setBackgroundImage(.init(), for: .default) // self.embedViewController.navigationBar.shadowImage = .init() self.embedViewController.navigationBarHidden = true; [self addChildViewController:self.embedViewController]; [self.embedView addSubview:self.embedViewController.view]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(textFieldWillEditing:) name:UITextFieldTextDidBeginEditingNotification object:nil]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [NSNotificationCenter.defaultCenter removeObserver:self]; } - (id)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC { return [[NavigationAnimator alloc] initWithOperation:operation titleView:_titleLabel]; } // MARK: - - (void)textFieldWillEditing:(id)sender { if (self.keybardInfo != nil) { [self keyboardWillShow:self.keybardInfo]; } } // Get current firset responder in ui hierarchy. - (id)firstResponderInResponder:(id)responder { if ([responder isFirstResponder]) { return responder; } if ([responder isKindOfClass:UIViewController.class]) { return [self firstResponderInResponder:[responder view]]; } if ([responder isKindOfClass:UIView.class]) { for (UIView* view in [responder subviews]) { id r = [self firstResponderInResponder:view]; if (r != nil) { return r; } } } return nil; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesEnded:touches withEvent:event]; // Resign all first responder. [self.view endEditing:YES]; } /// When show keyboard add view animation. - (void)keyboardWillShow:(NSNotification*)sender { // Gets the UITextField in first responder. NSDictionary* userInfo = sender.userInfo; UITextField* firstResponder = (id)[self firstResponderInResponder:self.view]; if (userInfo == nil || firstResponder == nil) { return; } CGRect textField = [firstResponder convertRect:firstResponder.bounds toView:self.view]; // Get animation duration. NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; CGRect frame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGFloat offset = fmin((CGRectGetMinY(frame) - textField.size.height) / 2 - CGRectGetMinY(textField), 0); // Make animation. [UIView animateWithDuration:duration animations:^{ UIViewAnimationCurve type = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];; [UIView setAnimationCurve:type]; self.view.transform = CGAffineTransformMakeTranslation(0, offset); }]; // Save keyboard info. self.keybardInfo = sender; } /// When hide keyboard add view animation. - (void)keyboardWillHide:(NSNotification*)sender { // Get animation duration. NSDictionary* userInfo = sender.userInfo; NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; [UIView animateWithDuration:duration animations:^{ UIViewAnimationCurve type = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];; [UIView setAnimationCurve:type]; self.view.transform = CGAffineTransformIdentity; }]; // Clean keybard info. self.keybardInfo = nil; } @end @implementation UIViewController (XenonSDK) - (void)sdk_userLoginForFast:(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)sdk_dismissViewController:(id)sender { if (self.navigationController.viewControllers.count <= 1) { [self dismissViewControllerAnimated:YES completion:nil]; return; } [self.navigationController popViewControllerAnimated:YES]; } - (void)sdk_showViewControllerForPhoneLogin:(id)sender { [self.view endEditing:YES]; [self.navigationController pushViewController:UserLoginPhoneViewController.new animated:YES]; } - (void)sdk_showViewControllerForAccountLogin:(id)sender { [self.view endEditing:YES]; [self.navigationController pushViewController:UserLoginAccountViewController.new animated:YES]; } - (void)sdk_showViewControllerForUserRegister:(id)sender { [self.view endEditing:YES]; [self.navigationController pushViewController:UserRegisterAccountViewController.new animated:YES]; } // MARK: - - (void)sdk_showViewControllerForForgetPasswod:(id)sender { [self.view endEditing:YES]; [self.navigationController pushViewController:UserRestPasswordViewController.new animated:YES]; } - (void)sdk_showViewControllerForResetPasswod:(id)sender { [self.view endEditing:YES]; [self.navigationController pushViewController:UserRestPassword2ViewController.new animated:YES]; } // MARK: - - (void)sdk_showViewControllerForUserProtocol:(id)sender { [self.view endEditing:YES]; [self.navigationController pushViewController:UserProtocolViewController.new animated:YES]; } // @IBAction func sdk_showViewController(forUserCenter sender: Any) { // self.view.endEditing(true) // // let viewController = HelperViewController() // self.navigationController?.pushViewController(viewController, animated: true) // } // // - (void)sdk_showViewControllerForCustomerService:(id)sender { [self.view endEditing:YES]; [self.navigationController pushViewController:HelperViewController.new animated:YES]; } @end @implementation UITextField (XenonSDK) - (void)setPlaceholderColor:(UIColor*)color { if (color == nil) { self.placeholder = self.placeholder; return; } if (self.placeholder == nil) { return; } id mas = [[NSMutableAttributedString alloc] initWithString:self.placeholder attributes:@{NSForegroundColorAttributeName: color}]; self.attributedPlaceholder = mas; } - (UIColor*)placeholderColor { return nil; } @end @implementation CALayer (XenonSDK) - (void)setCornerRadiusBasedI375:(CGFloat)radius { self.cornerRadius = CGFloatBasedI375(radius); } - (CGFloat)cornerRadiusBasedI375 { return self.cornerRadius; } @end