NCSplitViewController.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // NCSplitViewController.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 30/01/2020.
  6. // Copyright © 2020 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. class NCSplitViewController: UISplitViewController {
  25. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  26. var timer: Timer?
  27. override func viewDidLoad() {
  28. super.viewDidLoad()
  29. self.delegate = self
  30. self.preferredDisplayMode = .allVisible
  31. if UIScreen.main.bounds.width > UIScreen.main.bounds.height {
  32. self.maximumPrimaryColumnWidth = UIScreen.main.bounds.width
  33. } else {
  34. self.maximumPrimaryColumnWidth = UIScreen.main.bounds.height
  35. }
  36. self.preferredPrimaryColumnWidthFraction = 0.4
  37. let navigationController = viewControllers.first as? UINavigationController
  38. if let tabBarController = navigationController?.topViewController as? UITabBarController {
  39. appDelegate.createTabBarController(tabBarController)
  40. }
  41. //setPrimaryColumn()
  42. }
  43. override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
  44. if #available(iOS 13.0, *) {
  45. if CCUtility.getDarkModeDetect() {
  46. if traitCollection.userInterfaceStyle == .dark {
  47. CCUtility.setDarkMode(true)
  48. } else {
  49. CCUtility.setDarkMode(false)
  50. }
  51. // Use a timer for fix the twice call of traitCollectionDidChange for detect the Dark mode
  52. if !(timer?.isValid ?? false) {
  53. timer = Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(timerHandlerChangeTheming(_:)), userInfo: nil, repeats: false)
  54. }
  55. }
  56. }
  57. }
  58. override func viewDidLayoutSubviews() {
  59. //setPrimaryColumn()
  60. }
  61. private func setPrimaryColumn() {
  62. var fraction: CGFloat = 0.4
  63. let gap = 1.0 / self.traitCollection.displayScale
  64. let device = UIDevice.current.userInterfaceIdiom
  65. var collectionView: UICollectionView?
  66. if device == .pad {
  67. if let detailNavigationController = self.viewControllers.last as? NCDetailNavigationController {
  68. if let detailViewController = detailNavigationController.topViewController as? NCDetailViewController {
  69. if detailViewController.metadata == nil {
  70. fraction = 1
  71. }
  72. }
  73. }
  74. }
  75. if fraction == 1 || self.isCollapsed {
  76. if UIScreen.main.bounds.width > UIScreen.main.bounds.height {
  77. self.maximumPrimaryColumnWidth = max(UIScreen.main.bounds.width - gap, UIScreen.main.bounds.height - gap)
  78. } else {
  79. self.maximumPrimaryColumnWidth = min(UIScreen.main.bounds.width - gap, UIScreen.main.bounds.height - gap)
  80. }
  81. } else {
  82. if UIScreen.main.bounds.width > UIScreen.main.bounds.height {
  83. self.maximumPrimaryColumnWidth = UIScreen.main.bounds.width
  84. } else {
  85. self.maximumPrimaryColumnWidth = UIScreen.main.bounds.height
  86. }
  87. }
  88. if appDelegate.activeMedia?.view?.window != nil {
  89. collectionView = self.appDelegate.activeMedia?.collectionView
  90. } else if appDelegate.activeFavorite?.view?.window != nil {
  91. collectionView = self.appDelegate.activeFavorite?.collectionView
  92. } else if appDelegate.activeOffline?.view?.window != nil {
  93. collectionView = self.appDelegate.activeOffline?.collectionView
  94. } else if appDelegate.activeTrash?.view?.window != nil {
  95. collectionView = self.appDelegate.activeTrash?.collectionView
  96. } else {
  97. self.preferredPrimaryColumnWidthFraction = fraction
  98. }
  99. UIView.animate(withDuration: 0.1) {
  100. self.preferredPrimaryColumnWidthFraction = fraction
  101. collectionView?.collectionViewLayout.invalidateLayout()
  102. }
  103. }
  104. @objc func timerHandlerChangeTheming(_ timer: Timer) {
  105. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_changeTheming)
  106. }
  107. }
  108. extension NCSplitViewController: UISplitViewControllerDelegate {
  109. func splitViewController(_ splitViewController: UISplitViewController, collapseSecondary secondaryViewController: UIViewController, onto primaryViewController: UIViewController) -> Bool {
  110. return true
  111. }
  112. func splitViewController(_ svc: UISplitViewController, willChangeTo displayMode: UISplitViewController.DisplayMode) {
  113. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_splitViewChangeDisplayMode)
  114. }
  115. }