NCDisplayModel.swift 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. /// AppDelegate
  12. let appDelegate = (UIApplication.shared.delegate as? AppDelegate)!
  13. /// Keychain access
  14. var keychain = NCKeychain()
  15. /// Root View Controller
  16. @Published var controller: NCMainTabBarController?
  17. /// State variable for enabling the automatic appreance
  18. @Published var appearanceAutomatic: Bool = false
  19. /// Initializes the view model with default values.
  20. init(controller: NCMainTabBarController?) {
  21. self.controller = controller
  22. onViewAppear()
  23. }
  24. /// Triggered when the view appears.
  25. func onViewAppear() {
  26. appearanceAutomatic = keychain.appearanceAutomatic
  27. }
  28. // MARK: - All functions
  29. /// Update window(s) style
  30. func userInterfaceStyle(_ style: UIUserInterfaceStyle) {
  31. let windowScenes = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }
  32. keychain.appearanceInterfaceStyle = style
  33. for windowScene in windowScenes {
  34. for window in windowScene.windows {
  35. window.overrideUserInterfaceStyle = style
  36. }
  37. }
  38. }
  39. /// Updates the value of `appearanceAutomatic` in the keychain.
  40. func updateAppearanceAutomatic() {
  41. keychain.appearanceAutomatic = appearanceAutomatic
  42. if appearanceAutomatic {
  43. userInterfaceStyle(.unspecified)
  44. }
  45. }
  46. }