NCAccountSettingsView.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. //
  2. // NCAccountSettingsView.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 SwiftUI
  24. struct NCAccountSettingsView: View {
  25. @ObservedObject var model: NCAccountSettingsModel
  26. @State private var isExpanded: Bool = false
  27. @State private var showUserStatus = false
  28. @State private var showServerCertificate = false
  29. @State private var showPushCertificate = false
  30. @State private var showDeleteAccountAlert: Bool = false
  31. @State private var showAddAccount: Bool = false
  32. @State private var animation: Bool = false
  33. @Environment(\.presentationMode) var presentationMode
  34. var body: some View {
  35. NavigationView {
  36. Form {
  37. Section(content: {
  38. TabView(selection: $model.indexActiveAccount) {
  39. ForEach(0..<model.tblAccounts.count, id: \.self) { index in
  40. let status = model.getUserStatus()
  41. let avatar = NCUtility().loadUserImage(for: model.tblAccounts[index].user, displayName: model.tblAccounts[index].displayName, urlBase: model.tblAccounts[index].urlBase)
  42. ///
  43. /// User
  44. VStack {
  45. ZStack {
  46. Image(uiImage: avatar)
  47. .resizable()
  48. .scaledToFit()
  49. .frame(width: UIScreen.main.bounds.width, height: 75)
  50. if let statusImage = status.statusImage {
  51. ZStack {
  52. Circle()
  53. .fill(.white)
  54. .frame(width: 30, height: 30)
  55. Image(uiImage: statusImage)
  56. .resizable()
  57. .scaledToFit()
  58. .frame(width: 30, height: 30)
  59. }
  60. .offset(x: 30, y: 30)
  61. }
  62. }
  63. .frame(maxWidth: .infinity, maxHeight: .infinity)
  64. Text(model.getUserName())
  65. .font(.system(size: 16))
  66. Spacer()
  67. .frame(height: 10)
  68. Text(status.statusMessage)
  69. .font(.system(size: 10))
  70. Spacer()
  71. .frame(height: 20)
  72. ///
  73. /// Personal data
  74. if let tblAccount = model.tblAccount {
  75. if !tblAccount.email.isEmpty {
  76. HStack {
  77. Image(systemName: "mail")
  78. .resizable()
  79. .scaledToFit()
  80. .font(Font.system(.body).weight(.light))
  81. .frame(width: 20, height: 20)
  82. Text(tblAccount.email)
  83. .lineLimit(1)
  84. .truncationMode(.middle)
  85. .frame(maxWidth: .infinity, alignment: .leading)
  86. }
  87. .frame(maxWidth: .infinity, maxHeight: 30)
  88. }
  89. if !tblAccount.phone.isEmpty {
  90. HStack {
  91. Image(systemName: "phone")
  92. .resizable()
  93. .scaledToFit()
  94. .font(Font.system(.body).weight(.light))
  95. .frame(width: 20, height: 20)
  96. Text(tblAccount.phone)
  97. .lineLimit(1)
  98. .truncationMode(.middle)
  99. .frame(maxWidth: .infinity, alignment: .leading)
  100. }
  101. .frame(maxWidth: .infinity, maxHeight: 30)
  102. }
  103. if !tblAccount.address.isEmpty {
  104. HStack {
  105. Image(systemName: "house")
  106. .resizable()
  107. .scaledToFit()
  108. .font(Font.system(.body).weight(.light))
  109. .frame(width: 20, height: 20)
  110. Text(tblAccount.address)
  111. .lineLimit(1)
  112. .truncationMode(.middle)
  113. .frame(maxWidth: .infinity, alignment: .leading)
  114. }
  115. .frame(maxWidth: .infinity, maxHeight: 30)
  116. }
  117. }
  118. }
  119. }
  120. }
  121. .font(.system(size: 14))
  122. .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
  123. .frame(height: model.getTableViewHeight())
  124. .animation(.easeIn(duration: 0.3), value: animation)
  125. .onChange(of: model.indexActiveAccount) { index in
  126. animation.toggle()
  127. model.setAccount(account: model.tblAccounts[index].account)
  128. }
  129. ///
  130. /// Change alias
  131. VStack {
  132. HStack {
  133. Text(NSLocalizedString("_alias_", comment: "") + ":")
  134. .font(.system(size: 17))
  135. .fontWeight(.medium)
  136. Spacer()
  137. TextField(NSLocalizedString("_alias_placeholder_", comment: ""), text: $model.alias)
  138. .font(.system(size: 16))
  139. .multilineTextAlignment(.trailing)
  140. .onChange(of: model.alias) { newValue in
  141. model.setAlias(newValue)
  142. }
  143. }
  144. Text(NSLocalizedString("_alias_footer_", comment: ""))
  145. .frame(maxWidth: .infinity, alignment: .leading)
  146. .font(.system(size: 12))
  147. .lineLimit(2)
  148. .foregroundStyle(Color(UIColor.lightGray))
  149. }
  150. ///
  151. /// User Status
  152. if NCCapabilities.shared.getCapabilities(account: model.tblAccount?.account).capabilityUserStatusEnabled {
  153. Button(action: {
  154. showUserStatus = true
  155. }, label: {
  156. HStack {
  157. Image(systemName: "moon.fill")
  158. .resizable()
  159. .scaledToFit()
  160. .font(Font.system(.body).weight(.light))
  161. .frame(width: 20, height: 20)
  162. .foregroundStyle(Color(NCBrandColor.shared.iconImageColor))
  163. Text(NSLocalizedString("_set_user_status_", comment: ""))
  164. .lineLimit(1)
  165. .truncationMode(.middle)
  166. .foregroundStyle(Color(NCBrandColor.shared.textColor))
  167. .padding(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 20))
  168. }
  169. .font(.system(size: 14))
  170. })
  171. .sheet(isPresented: $showUserStatus) {
  172. if let account = model.tblAccount?.account {
  173. UserStatusView(showUserStatus: $showUserStatus, account: account)
  174. }
  175. }
  176. .onChange(of: showUserStatus) { _ in }
  177. }
  178. ///
  179. /// Certificate server
  180. if model.isAdminGroup() {
  181. Button(action: {
  182. showServerCertificate.toggle()
  183. }, label: {
  184. HStack {
  185. Image(systemName: "lock")
  186. .resizable()
  187. .scaledToFit()
  188. .font(Font.system(.body).weight(.light))
  189. .frame(width: 20, height: 20)
  190. .foregroundStyle(Color(NCBrandColor.shared.iconImageColor))
  191. Text(NSLocalizedString("_certificate_details_", comment: ""))
  192. .lineLimit(1)
  193. .truncationMode(.middle)
  194. .foregroundStyle(Color(NCBrandColor.shared.textColor))
  195. .padding(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 20))
  196. }
  197. .font(.system(size: 14))
  198. })
  199. .sheet(isPresented: $showServerCertificate) {
  200. if let url = URL(string: model.tblAccount?.urlBase), let host = url.host {
  201. certificateDetailsView(host: host, title: NSLocalizedString("_certificate_view_", comment: ""))
  202. }
  203. }
  204. ///
  205. /// Certificate push
  206. Button(action: {
  207. showPushCertificate.toggle()
  208. }, label: {
  209. HStack {
  210. Image(systemName: "lock")
  211. .resizable()
  212. .scaledToFit()
  213. .font(Font.system(.body).weight(.light))
  214. .frame(width: 20, height: 20)
  215. .foregroundStyle(Color(NCBrandColor.shared.iconImageColor))
  216. Text(NSLocalizedString("_certificate_pn_details_", comment: ""))
  217. .lineLimit(1)
  218. .truncationMode(.middle)
  219. .foregroundStyle(Color(NCBrandColor.shared.textColor))
  220. .padding(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 20))
  221. }
  222. .font(.system(size: 14))
  223. })
  224. .sheet(isPresented: $showPushCertificate) {
  225. if let url = URL(string: NCBrandOptions.shared.pushNotificationServerProxy), let host = url.host {
  226. certificateDetailsView(host: host, title: NSLocalizedString("_certificate_pn_view_", comment: ""))
  227. }
  228. }
  229. }
  230. })
  231. ///
  232. /// Delete account
  233. Section(content: {
  234. Button(action: {
  235. showDeleteAccountAlert.toggle()
  236. }, label: {
  237. HStack {
  238. Image(systemName: "trash")
  239. .resizable()
  240. .scaledToFit()
  241. .font(Font.system(.body).weight(.light))
  242. .frame(width: 20, height: 20)
  243. .foregroundStyle(.red)
  244. Text(NSLocalizedString("_remove_local_account_", comment: ""))
  245. .lineLimit(1)
  246. .truncationMode(.middle)
  247. .foregroundStyle(.red)
  248. .padding(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 20))
  249. }
  250. .font(.system(size: 14))
  251. })
  252. .alert(NSLocalizedString("_want_delete_account_", comment: ""), isPresented: $showDeleteAccountAlert) {
  253. Button(NSLocalizedString("_remove_local_account_", comment: ""), role: .destructive) {
  254. model.deleteAccount()
  255. }
  256. Button(NSLocalizedString("_cancel_", comment: ""), role: .cancel) { }
  257. }
  258. })
  259. }
  260. .navigationBarTitle(NSLocalizedString("_account_settings_", comment: ""))
  261. .navigationBarTitleDisplayMode(.inline)
  262. .navigationBarItems(trailing: Button(action: {
  263. presentationMode.wrappedValue.dismiss()
  264. }) {
  265. Image(systemName: "xmark")
  266. .font(Font.system(.body).weight(.light))
  267. .foregroundStyle(Color(NCBrandColor.shared.iconImageColor))
  268. })
  269. }
  270. .defaultViewModifier(model)
  271. .navigationViewStyle(StackNavigationViewStyle())
  272. .onReceive(model.$dismissView) { newValue in
  273. if newValue {
  274. presentationMode.wrappedValue.dismiss()
  275. }
  276. }
  277. .onDisappear {
  278. model.delegate?.accountSettingsDidDismiss(tableAccount: model.tblAccount, controller: model.controller)
  279. }
  280. }
  281. }
  282. #Preview {
  283. NCAccountSettingsView(model: NCAccountSettingsModel(controller: nil, delegate: nil))
  284. }