NCAccountSettingsModel.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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?, controller: NCMainTabBarController?)
  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 tblAccounts: [tableAccount] = []
  38. /// Delegate
  39. weak var delegate: NCAccountSettingsModelDelegate?
  40. /// Token observe tableAccount
  41. var notificationToken: NotificationToken?
  42. /// Account now
  43. @Published var tblAccount: tableAccount?
  44. /// Index
  45. @Published var indexActiveAccount: Int = 0
  46. /// Current alias
  47. @Published var alias: String = ""
  48. /// Set true for dismiss the view
  49. @Published var dismissView = false
  50. /// DB
  51. let database = NCManageDatabase.shared
  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. database.previewCreateDB()
  58. }
  59. onViewAppear()
  60. observeTableAccount()
  61. }
  62. deinit {
  63. notificationToken?.invalidate()
  64. notificationToken = nil
  65. }
  66. /// Reload the view when change the tableAccount
  67. func observeTableAccount() {
  68. do {
  69. let realm = try Realm()
  70. let results = realm.objects(tableAccount.self)
  71. notificationToken = results.observe { [weak self] (changes: RealmCollectionChange) in
  72. switch changes {
  73. case .initial:
  74. break
  75. case .update:
  76. DispatchQueue.main.async {
  77. self?.objectWillChange.send()
  78. }
  79. case .error:
  80. break
  81. }
  82. }
  83. } catch let error as NSError {
  84. NSLog("Could not access database: ", error)
  85. }
  86. }
  87. /// Triggered when the view appears.
  88. func onViewAppear() {
  89. var indexActiveAccount = 0
  90. let tableAccounts = database.getAllTableAccount()
  91. var alias = ""
  92. for (index, account) in tableAccounts.enumerated() {
  93. if account.active {
  94. tblAccount = account
  95. indexActiveAccount = index
  96. alias = account.alias
  97. }
  98. }
  99. self.indexActiveAccount = indexActiveAccount
  100. self.tblAccounts = tableAccounts
  101. self.tblAccount = tblAccount
  102. self.alias = alias
  103. }
  104. /// Func to get the user display name + alias
  105. func getUserName() -> String {
  106. guard let tblAccount else { return "" }
  107. if alias.isEmpty {
  108. return tblAccount.displayName
  109. } else {
  110. return tblAccount.displayName + " (\(alias))"
  111. }
  112. }
  113. /// Func to set alias
  114. func setAlias(_ value: String) {
  115. guard let tblAccount else { return }
  116. database.setAccountAlias(tblAccount.account, alias: alias)
  117. }
  118. /// Function to update the user data
  119. func getUserStatus() -> (statusImage: UIImage?, statusMessage: String, descriptionMessage: String) {
  120. guard let tblAccount else { return (UIImage(), "", "") }
  121. if NCCapabilities.shared.getCapabilities(account: tblAccount.account).capabilityUserStatusEnabled,
  122. let tableAccount = database.getTableAccount(predicate: NSPredicate(format: "account == %@", tblAccount.account)) {
  123. return NCUtility().getUserStatus(userIcon: tableAccount.userStatusIcon, userStatus: tableAccount.userStatusStatus, userMessage: tableAccount.userStatusMessage)
  124. }
  125. return (nil, "", "")
  126. }
  127. /// Is the user an Admin
  128. func isAdminGroup() -> Bool {
  129. guard let tblAccount else { return false }
  130. let groups = database.getAccountGroups(account: tblAccount.account)
  131. return groups.contains(NCGlobal.shared.groupAdmin)
  132. }
  133. /// Function to know the height of "account" data
  134. func getTableViewHeight() -> CGFloat {
  135. guard let tblAccount else { return 0 }
  136. let capabilities = NCCapabilities.shared.getCapabilities(account: tblAccount.account)
  137. var height: CGFloat = capabilities.capabilityUserStatusEnabled ? 190 : 220
  138. if capabilities.capabilityUserStatusEnabled,
  139. let tableAccount = database.getTableAccount(predicate: NSPredicate(format: "account == %@", tblAccount.account)) {
  140. if !tableAccount.email.isEmpty { height += 30 }
  141. if !tableAccount.phone.isEmpty { height += 30 }
  142. if !tableAccount.address.isEmpty { height += 30 }
  143. }
  144. if height == 190 { return 170 }
  145. return height
  146. }
  147. /// Function to change account after 1.5 sec of change
  148. func setAccount(account: String) {
  149. if let tableAccount = database.getTableAccount(predicate: NSPredicate(format: "account == %@", account)) {
  150. self.tblAccount = tableAccount
  151. self.alias = tableAccount.alias
  152. }
  153. }
  154. /// Function to delete the current account
  155. func deleteAccount() {
  156. if let tblAccount {
  157. NCAccount().deleteAccount(tblAccount.account)
  158. if let account = database.getAllTableAccount().first?.account {
  159. NCAccount().changeAccount(account, userProfile: nil, controller: self.controller) {
  160. onViewAppear()
  161. }
  162. } else {
  163. dismissView = true
  164. appDelegate.openLogin(selector: NCGlobal.shared.introLogin)
  165. }
  166. }
  167. }
  168. }