NCDisplayModel.swift 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // NCDisplayModel.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 30/05/24.
  6. // Copyright © 2024 Marino Faggiana. All rights reserved.
  7. //
  8. import Foundation
  9. import SwiftUI
  10. class NCDisplayModel: ObservableObject, ViewOnAppearHandling {
  11. /// Keychain access
  12. var keychain = NCKeychain()
  13. /// Root View Controller
  14. @Published var controller: NCMainTabBarController?
  15. /// State variable for enabling the automatic appreance
  16. @Published var appearanceAutomatic: Bool = false
  17. /// State variable for keeping the screen on or off during file transfering
  18. @Published var screenAwakeState = AwakeMode.off {
  19. didSet {
  20. keychain.screenAwakeMode = screenAwakeState
  21. }
  22. }
  23. /// Get session
  24. var session: NCSession.Session {
  25. NCSession.shared.getSession(controller: controller)
  26. }
  27. /// Initializes the view model with default values.
  28. init(controller: NCMainTabBarController?) {
  29. self.controller = controller
  30. onViewAppear()
  31. }
  32. /// Triggered when the view appears.
  33. func onViewAppear() {
  34. appearanceAutomatic = keychain.appearanceAutomatic
  35. screenAwakeState = keychain.screenAwakeMode
  36. }
  37. // MARK: - All functions
  38. /// Update window(s) style
  39. func userInterfaceStyle(_ style: UIUserInterfaceStyle) {
  40. let windowScenes = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }
  41. keychain.appearanceInterfaceStyle = style
  42. for windowScene in windowScenes {
  43. for window in windowScene.windows {
  44. window.overrideUserInterfaceStyle = style
  45. }
  46. }
  47. }
  48. /// Updates the value of `appearanceAutomatic` in the keychain.
  49. func updateAppearanceAutomatic() {
  50. keychain.appearanceAutomatic = appearanceAutomatic
  51. if appearanceAutomatic {
  52. userInterfaceStyle(.unspecified)
  53. }
  54. }
  55. }