NCIntroViewController.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. @objc 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. if #available(iOS 13.0, *) {
  55. let navBarAppearance = UINavigationBarAppearance()
  56. navBarAppearance.configureWithTransparentBackground()
  57. navBarAppearance.shadowColor = .clear
  58. navBarAppearance.shadowImage = UIImage()
  59. self.navigationController?.navigationBar.standardAppearance = navBarAppearance
  60. } else {
  61. self.navigationController?.navigationBar.isTranslucent = true
  62. self.navigationController?.navigationBar.shadowImage = UIImage()
  63. self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
  64. self.navigationController?.navigationBar.backgroundColor = .clear
  65. self.navigationController?.navigationBar.barTintColor = NCBrandColor.shared.customer
  66. }
  67. self.navigationController?.navigationBar.tintColor = textColor
  68. self.pageControl.currentPageIndicatorTintColor = textColor
  69. self.pageControl.pageIndicatorTintColor = .lightGray
  70. self.buttonLogin.layer.cornerRadius = 20
  71. self.buttonLogin.setTitleColor(textColorOpponent, for: .normal)
  72. self.buttonLogin.backgroundColor = textColor
  73. self.buttonLogin.setTitle(NSLocalizedString("_log_in_", comment: ""), for: .normal)
  74. self.buttonSignUp.layer.cornerRadius = 20
  75. self.buttonSignUp.setTitleColor(.white, for: .normal)
  76. self.buttonSignUp.backgroundColor = UIColor(red: 25.0 / 255.0, green: 89.0 / 255.0, blue: 141.0 / 255.0, alpha: 1)
  77. self.buttonSignUp.setTitle(NSLocalizedString("_sign_up_", comment: ""), for: .normal)
  78. self.buttonHost.layer.cornerRadius = 20
  79. self.buttonHost.setTitle(NSLocalizedString("_host_your_own_server", comment: ""), for: .normal)
  80. self.buttonHost.setTitleColor(textColor.withAlphaComponent(0.5), for: .normal)
  81. self.introCollectionView.register(UINib(nibName: "NCIntroCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "introCell")
  82. self.introCollectionView.dataSource = self
  83. self.introCollectionView.delegate = self
  84. self.introCollectionView.backgroundColor = NCBrandColor.shared.customer
  85. self.pageControl.numberOfPages = self.titles.count
  86. self.view.backgroundColor = NCBrandColor.shared.customer
  87. self.timerAutoScroll = Timer.scheduledTimer(timeInterval: 5, target: self, selector: (#selector(NCIntroViewController.autoScroll)), userInfo: nil, repeats: true)
  88. }
  89. override var preferredStatusBarStyle: UIStatusBarStyle {
  90. if #available(iOS 13.0, *) {
  91. if traitCollection.userInterfaceStyle == .light {
  92. return .lightContent
  93. } else {
  94. return .darkContent
  95. }
  96. } else {
  97. return .lightContent
  98. }
  99. }
  100. override func viewWillDisappear(_ animated: Bool) {
  101. super.viewWillDisappear(animated)
  102. timerAutoScroll?.invalidate()
  103. }
  104. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  105. pageControl.currentPage = 0
  106. introCollectionView.collectionViewLayout.invalidateLayout()
  107. }
  108. @objc func autoScroll() {
  109. if(pageControl.currentPage + 1 >= titles.count) {
  110. pageControl.currentPage = 0
  111. }
  112. else {
  113. pageControl.currentPage += 1
  114. }
  115. introCollectionView.scrollToItem(at: IndexPath(row: pageControl.currentPage, section: 0), at: .centeredHorizontally, animated: true)
  116. }
  117. func collectionView(_ collectionView: UICollectionView, targetContentOffsetForProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  118. return CGPoint.zero
  119. }
  120. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  121. return titles.count
  122. }
  123. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  124. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "introCell", for: indexPath) as! NCIntroCollectionViewCell
  125. cell.backgroundColor = NCBrandColor.shared.customer
  126. cell.titleLabel.textColor = textColor
  127. cell.titleLabel.text = titles[indexPath.row]
  128. cell.imageView.image = images[indexPath.row]
  129. return cell
  130. }
  131. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  132. return collectionView.bounds.size
  133. }
  134. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  135. timerAutoScroll = Timer.scheduledTimer(timeInterval: 5, target: self, selector: (#selector(NCIntroViewController.autoScroll)), userInfo: nil, repeats: true)
  136. pageControl.currentPage = Int(scrollView.contentOffset.x) / Int(scrollView.frame.width)
  137. }
  138. func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
  139. timerAutoScroll?.invalidate()
  140. }
  141. @IBAction func login(_ sender: Any) {
  142. appDelegate.openLogin(viewController: navigationController, selector: NCGlobal.shared.introLogin, openLoginWeb: false)
  143. }
  144. @IBAction func signup(_ sender: Any) {
  145. appDelegate.openLogin(viewController: navigationController, selector: NCGlobal.shared.introSignup, openLoginWeb: false)
  146. }
  147. @IBAction func host(_ sender: Any) {
  148. guard let url = URL(string: NCBrandOptions.shared.linkLoginHost) else { return }
  149. UIApplication.shared.open(url)
  150. }
  151. }
  152. extension UINavigationController {
  153. open override var childForStatusBarStyle: UIViewController? {
  154. return topViewController?.childForStatusBarStyle ?? topViewController
  155. }
  156. }