NCIntroViewController.swift 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // NCIntroViewController.swift
  3. // Nextcloud
  4. //
  5. // Created by Philippe Weidmann on 24.12.19.
  6. // Copyright © 2019 Philippe Weidmann. All rights reserved.
  7. // Copyright © 2019 Marino Faggiana All rights reserved.
  8. //
  9. // Author Philippe Weidmann
  10. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  11. //
  12. // This program is free software: you can redistribute it and/or modify
  13. // it under the terms of the GNU General Public License as published by
  14. // the Free Software Foundation, either version 3 of the License, or
  15. // (at your option) any later version.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of the GNU General Public License
  23. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. //
  25. import UIKit
  26. class NCIntroViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  27. @IBOutlet weak var buttonLogin: UIButton!
  28. @IBOutlet weak var buttonSignUp: UIButton!
  29. @IBOutlet weak var buttonHost: UIButton!
  30. @IBOutlet weak var introCollectionView: UICollectionView!
  31. @IBOutlet weak var pageControl: UIPageControl!
  32. weak var delegate: NCIntroViewController?
  33. private let appDelegate = (UIApplication.shared.delegate as? AppDelegate)!
  34. private let titles = [NSLocalizedString("_intro_1_title_", comment: ""), NSLocalizedString("_intro_2_title_", comment: ""), NSLocalizedString("_intro_3_title_", comment: ""), NSLocalizedString("_intro_4_title_", comment: "")]
  35. private let images = [UIImage(named: "intro1"), UIImage(named: "intro2"), UIImage(named: "intro3"), UIImage(named: "intro4")]
  36. private var timerAutoScroll: Timer?
  37. private var textColor: UIColor = .white
  38. private var textColorOpponent: UIColor = .black
  39. // MARK: - View Life Cycle
  40. override func viewDidLoad() {
  41. super.viewDidLoad()
  42. let isTooLight = NCBrandColor.shared.customer.isTooLight()
  43. let isTooDark = NCBrandColor.shared.customer.isTooDark()
  44. if isTooLight {
  45. textColor = .black
  46. textColorOpponent = .white
  47. } else if isTooDark {
  48. textColor = .white
  49. textColorOpponent = .black
  50. } else {
  51. textColor = .white
  52. textColorOpponent = .black
  53. }
  54. let navBarAppearance = UINavigationBarAppearance()
  55. navBarAppearance.configureWithTransparentBackground()
  56. navBarAppearance.shadowColor = .clear
  57. navBarAppearance.shadowImage = UIImage()
  58. self.navigationController?.navigationBar.standardAppearance = navBarAppearance
  59. self.navigationController?.view.backgroundColor = NCBrandColor.shared.customer
  60. self.navigationController?.navigationBar.tintColor = textColor
  61. pageControl.currentPageIndicatorTintColor = textColor
  62. pageControl.pageIndicatorTintColor = .lightGray
  63. buttonLogin.layer.cornerRadius = 20
  64. buttonLogin.setTitleColor(NCBrandColor.shared.customer, for: .normal)
  65. buttonLogin.backgroundColor = textColor
  66. buttonLogin.setTitle(NSLocalizedString("_log_in_", comment: ""), for: .normal)
  67. buttonSignUp.layer.cornerRadius = 20
  68. buttonSignUp.layer.borderColor = textColor.cgColor
  69. buttonSignUp.layer.borderWidth = 1.0
  70. buttonSignUp.setTitleColor(textColor, for: .normal)
  71. buttonSignUp.backgroundColor = NCBrandColor.shared.customer
  72. buttonSignUp.titleLabel?.adjustsFontSizeToFitWidth = true
  73. buttonSignUp.titleEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
  74. buttonSignUp.setTitle(NSLocalizedString("_sign_up_", comment: ""), for: .normal)
  75. buttonHost.layer.cornerRadius = 20
  76. buttonHost.setTitle(NSLocalizedString("_host_your_own_server", comment: ""), for: .normal)
  77. buttonHost.setTitleColor(textColor.withAlphaComponent(0.5), for: .normal)
  78. introCollectionView.register(UINib(nibName: "NCIntroCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "introCell")
  79. introCollectionView.dataSource = self
  80. introCollectionView.delegate = self
  81. introCollectionView.backgroundColor = NCBrandColor.shared.customer
  82. pageControl.numberOfPages = self.titles.count
  83. view.backgroundColor = NCBrandColor.shared.customer
  84. timerAutoScroll = Timer.scheduledTimer(timeInterval: 5, target: self, selector: (#selector(NCIntroViewController.autoScroll)), userInfo: nil, repeats: true)
  85. NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeUser), object: nil, queue: nil) { notification in
  86. if let userInfo = notification.userInfo,
  87. let account = userInfo["account"] as? String {
  88. let window = UIApplication.shared.firstWindow
  89. if let controller = window?.rootViewController as? NCMainTabBarController {
  90. controller.account = account
  91. self.dismiss(animated: true)
  92. } else {
  93. if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() as? NCMainTabBarController {
  94. controller.account = account
  95. controller.modalPresentationStyle = .fullScreen
  96. controller.view.alpha = 0
  97. window?.rootViewController = controller
  98. window?.makeKeyAndVisible()
  99. if let scene = window?.windowScene {
  100. SceneManager.shared.register(scene: scene, withRootViewController: controller)
  101. }
  102. UIView.animate(withDuration: 0.5) {
  103. controller.view.alpha = 1
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. override var preferredStatusBarStyle: UIStatusBarStyle {
  111. if traitCollection.userInterfaceStyle == .light {
  112. return .lightContent
  113. } else {
  114. return .darkContent
  115. }
  116. }
  117. override func viewWillDisappear(_ animated: Bool) {
  118. super.viewWillDisappear(animated)
  119. timerAutoScroll?.invalidate()
  120. }
  121. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  122. super.viewWillTransition(to: size, with: coordinator)
  123. coordinator.animate(alongsideTransition: nil) { _ in
  124. self.pageControl?.currentPage = 0
  125. self.introCollectionView?.collectionViewLayout.invalidateLayout()
  126. }
  127. }
  128. @objc func autoScroll() {
  129. if pageControl.currentPage + 1 >= titles.count {
  130. pageControl.currentPage = 0
  131. } else {
  132. pageControl.currentPage += 1
  133. }
  134. introCollectionView.scrollToItem(at: IndexPath(row: pageControl.currentPage, section: 0), at: .centeredHorizontally, animated: true)
  135. }
  136. func collectionView(_ collectionView: UICollectionView, targetContentOffsetForProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  137. return CGPoint.zero
  138. }
  139. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  140. return titles.count
  141. }
  142. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  143. let cell = (collectionView.dequeueReusableCell(withReuseIdentifier: "introCell", for: indexPath) as? NCIntroCollectionViewCell)!
  144. cell.backgroundColor = NCBrandColor.shared.customer
  145. cell.indexPath = indexPath
  146. cell.titleLabel.textColor = textColor
  147. cell.titleLabel.text = titles[indexPath.row]
  148. cell.imageView.image = images[indexPath.row]
  149. return cell
  150. }
  151. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  152. return collectionView.bounds.size
  153. }
  154. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  155. timerAutoScroll = Timer.scheduledTimer(timeInterval: 5, target: self, selector: (#selector(NCIntroViewController.autoScroll)), userInfo: nil, repeats: true)
  156. pageControl.currentPage = Int(scrollView.contentOffset.x) / Int(scrollView.frame.width)
  157. }
  158. func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
  159. timerAutoScroll?.invalidate()
  160. }
  161. @IBAction func login(_ sender: Any) {
  162. appDelegate.openLogin(selector: NCGlobal.shared.introLogin)
  163. }
  164. @IBAction func signup(_ sender: Any) {
  165. appDelegate.openLogin(selector: NCGlobal.shared.introSignup)
  166. }
  167. @IBAction func host(_ sender: Any) {
  168. guard let url = URL(string: NCBrandOptions.shared.linkLoginHost) else { return }
  169. UIApplication.shared.open(url)
  170. }
  171. }
  172. extension UINavigationController {
  173. open override var childForStatusBarStyle: UIViewController? {
  174. return topViewController?.childForStatusBarStyle ?? topViewController
  175. }
  176. }