NCAccountSettingsModel.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // NCAccountSettingsModel.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 06/06/24.
  6. // Copyright © 2024 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 Foundation
  24. import UIKit
  25. import RealmSwift
  26. /// Protocol for know when the Account Settings has dimissed
  27. protocol NCAccountSettingsModelDelegate: AnyObject {
  28. func accountSettingsDidDismiss(tableAccount: tableAccount?)
  29. }
  30. /// A model that allows the user to configure the account
  31. class NCAccountSettingsModel: ObservableObject, ViewOnAppearHandling {
  32. /// AppDelegate
  33. let appDelegate = (UIApplication.shared.delegate as? AppDelegate)!
  34. /// Root View Controller
  35. var controller: NCMainTabBarController?
  36. /// All account
  37. var accounts: [tableAccount] = []
  38. /// Delegate
  39. weak var delegate: NCAccountSettingsModelDelegate?
  40. /// Timer change user
  41. var timerChangeAccount: Timer?
  42. /// Token observe tableAccount
  43. var notificationToken: NotificationToken?
  44. /// Account now active
  45. @Published var activeAccount: tableAccount?
  46. /// Index
  47. @Published var indexActiveAccount: Int = 0
  48. /// Current alias
  49. @Published var alias: String = ""
  50. /// Set true for dismiss the view
  51. @Published var dismissView = false
  52. /// Initialization code to set up the ViewModel with the active account
  53. init(controller: NCMainTabBarController?, delegate: NCAccountSettingsModelDelegate?) {
  54. self.controller = controller
  55. self.delegate = delegate
  56. if ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" {
  57. NCManageDatabase.shared.previewCreateDB()
  58. }
  59. onViewAppear()
  60. observeTableAccount()
  61. }
  62. deinit {
  63. timerChangeAccount?.invalidate()
  64. timerChangeAccount = nil
  65. notificationToken?.invalidate()
  66. notificationToken = nil
  67. }
  68. /// Reload the view when change the tableAccount
  69. func observeTableAccount() {
  70. do {
  71. let realm = try Realm()
  72. let results = realm.objects(tableAccount.self)
  73. notificationToken = results.observe { [weak self] (changes: RealmCollectionChange) in
  74. switch changes {
  75. case .initial:
  76. break
  77. case .update:
  78. DispatchQueue.main.async {
  79. self?.objectWillChange.send()
  80. }
  81. case .error:
  82. break
  83. }
  84. }
  85. } catch let error as NSError {
  86. NSLog("Could not access database: ", error)
  87. }
  88. }
  89. /// Triggered when the view appears.
  90. func onViewAppear() {
  91. var indexActiveAccount = 0
  92. let accounts = NCManageDatabase.shared.getAllAccount()
  93. var activeAccount = NCManageDatabase.shared.getActiveAccount()
  94. var alias = ""
  95. for (index, account) in accounts.enumerated() {
  96. if account.active {
  97. activeAccount = account
  98. indexActiveAccount = index
  99. alias = account.alias
  100. }
  101. }
  102. self.indexActiveAccount = indexActiveAccount
  103. self.accounts = accounts
  104. self.activeAccount = activeAccount
  105. self.alias = alias
  106. }
  107. /// Func to get the user display name + alias
  108. func getUserName() -> String {
  109. guard let activeAccount else { return "" }
  110. if alias.isEmpty {
  111. return activeAccount.displayName
  112. } else {
  113. return activeAccount.displayName + " (\(alias))"
  114. }
  115. }
  116. /// Func to set alias
  117. func setAlias(_ value: String) {
  118. guard let activeAccount else { return }
  119. NCManageDatabase.shared.setAccountAlias(activeAccount.account, alias: alias)
  120. }
  121. /// Function to update the user data
  122. func getUserStatus() -> (statusImage: UIImage, statusMessage: String, descriptionMessage: String) {
  123. guard let activeAccount else { return (UIImage(), "", "") }
  124. if NCGlobal.shared.capabilityUserStatusEnabled,
  125. let tableAccount = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", activeAccount.account)) {
  126. return NCUtility().getUserStatus(userIcon: tableAccount.userStatusIcon, userStatus: tableAccount.userStatusStatus, userMessage: tableAccount.userStatusMessage)
  127. }
  128. return (UIImage(), "", "")
  129. }
  130. /// Function to know the height of "account" data
  131. func getTableViewHeight() -> CGFloat {
  132. guard let activeAccount else { return 0 }
  133. var height: CGFloat = 190
  134. if NCGlobal.shared.capabilityUserStatusEnabled,
  135. let tableAccount = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", activeAccount.account)) {
  136. if !tableAccount.email.isEmpty { height += 30 }
  137. if !tableAccount.phone.isEmpty { height += 30 }
  138. if !tableAccount.address.isEmpty { height += 30 }
  139. }
  140. if height == 190 { return 170 }
  141. return height
  142. }
  143. /// Function to change account after 1.5 sec of change
  144. func setAccount(account: String) {
  145. if let tableAccount = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", account)), self.activeAccount?.account != tableAccount.account {
  146. self.activeAccount = tableAccount
  147. self.alias = tableAccount.alias
  148. /// Change active account
  149. timerChangeAccount?.invalidate()
  150. timerChangeAccount = Timer.scheduledTimer(timeInterval: 1.5, target: self, selector: #selector(changeAccount), userInfo: nil, repeats: false)
  151. }
  152. }
  153. @objc func changeAccount() {
  154. if let activeAccount {
  155. self.appDelegate.changeAccount(activeAccount.account, userProfile: nil)
  156. }
  157. }
  158. /// Function to delete the current account
  159. func deleteAccount() {
  160. if let activeAccount {
  161. appDelegate.deleteAccount(activeAccount.account, wipe: false)
  162. if let account = NCManageDatabase.shared.getAllAccount().first?.account {
  163. appDelegate.changeAccount(account, userProfile: nil)
  164. } else {
  165. dismissView = true
  166. }
  167. onViewAppear()
  168. }
  169. }
  170. }