NCAccountSettingsView.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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.accounts.count, id: \.self) { index in
  40. let status = model.getUserStatus()
  41. let avatar = NCUtility().loadUserImage(for: model.accounts[index].user, displayName: model.accounts[index].displayName, userBaseUrl: model.accounts[index])
  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 activeAccount = model.activeAccount {
  75. if !activeAccount.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(activeAccount.email)
  83. .lineLimit(1)
  84. .truncationMode(.middle)
  85. .frame(maxWidth: .infinity, alignment: .leading)
  86. }
  87. .frame(maxWidth: .infinity, maxHeight: 30)
  88. }
  89. if !activeAccount.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(activeAccount.phone)
  97. .lineLimit(1)
  98. .truncationMode(.middle)
  99. .frame(maxWidth: .infinity, alignment: .leading)
  100. }
  101. .frame(maxWidth: .infinity, maxHeight: 30)
  102. }
  103. if !activeAccount.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(activeAccount.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.accounts[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 NCGlobal.shared.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. UserStatusView(showUserStatus: $showUserStatus)
  173. }
  174. .onChange(of: showUserStatus) { _ in }
  175. }
  176. ///
  177. /// Certificate server
  178. if model.isAdminGroup() {
  179. Button(action: {
  180. showServerCertificate.toggle()
  181. }, label: {
  182. HStack {
  183. Image(systemName: "lock")
  184. .resizable()
  185. .scaledToFit()
  186. .font(Font.system(.body).weight(.light))
  187. .frame(width: 20, height: 20)
  188. .foregroundStyle(Color(NCBrandColor.shared.iconImageColor))
  189. Text(NSLocalizedString("_certificate_details_", comment: ""))
  190. .lineLimit(1)
  191. .truncationMode(.middle)
  192. .foregroundStyle(Color(NCBrandColor.shared.textColor))
  193. .padding(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 20))
  194. }
  195. .font(.system(size: 14))
  196. })
  197. .sheet(isPresented: $showServerCertificate) {
  198. if let url = URL(string: model.activeAccount?.urlBase), let host = url.host {
  199. certificateDetailsView(host: host, title: NSLocalizedString("_certificate_view_", comment: ""))
  200. }
  201. }
  202. ///
  203. /// Certificate push
  204. Button(action: {
  205. showPushCertificate.toggle()
  206. }, label: {
  207. HStack {
  208. Image(systemName: "lock")
  209. .resizable()
  210. .scaledToFit()
  211. .font(Font.system(.body).weight(.light))
  212. .frame(width: 20, height: 20)
  213. .foregroundStyle(Color(NCBrandColor.shared.iconImageColor))
  214. Text(NSLocalizedString("_certificate_pn_details_", comment: ""))
  215. .lineLimit(1)
  216. .truncationMode(.middle)
  217. .foregroundStyle(Color(NCBrandColor.shared.textColor))
  218. .padding(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 20))
  219. }
  220. .font(.system(size: 14))
  221. })
  222. .sheet(isPresented: $showPushCertificate) {
  223. if let url = URL(string: NCBrandOptions.shared.pushNotificationServerProxy), let host = url.host {
  224. certificateDetailsView(host: host, title: NSLocalizedString("_certificate_pn_view_", comment: ""))
  225. }
  226. }
  227. }
  228. })
  229. ///
  230. /// Delete account
  231. Section(content: {
  232. Button(action: {
  233. showDeleteAccountAlert.toggle()
  234. }, label: {
  235. HStack {
  236. Image(systemName: "trash")
  237. .resizable()
  238. .scaledToFit()
  239. .font(Font.system(.body).weight(.light))
  240. .frame(width: 20, height: 20)
  241. .foregroundStyle(.red)
  242. Text(NSLocalizedString("_remove_local_account_", comment: ""))
  243. .lineLimit(1)
  244. .truncationMode(.middle)
  245. .foregroundStyle(.red)
  246. .padding(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 20))
  247. }
  248. .font(.system(size: 14))
  249. })
  250. .alert(NSLocalizedString("_want_delete_account_", comment: ""), isPresented: $showDeleteAccountAlert) {
  251. Button(NSLocalizedString("_remove_local_account_", comment: ""), role: .destructive) {
  252. model.deleteAccount()
  253. }
  254. Button(NSLocalizedString("_cancel_", comment: ""), role: .cancel) { }
  255. }
  256. })
  257. }
  258. .navigationBarTitle(NSLocalizedString("_account_settings_", comment: ""))
  259. .navigationBarTitleDisplayMode(.inline)
  260. .navigationBarItems(trailing: Button(action: {
  261. presentationMode.wrappedValue.dismiss()
  262. }) {
  263. Image(systemName: "xmark")
  264. .font(Font.system(.body).weight(.light))
  265. .foregroundStyle(Color(NCBrandColor.shared.iconImageColor))
  266. })
  267. }
  268. .defaultViewModifier(model)
  269. .navigationViewStyle(StackNavigationViewStyle())
  270. .onReceive(model.$dismissView) { newValue in
  271. if newValue {
  272. presentationMode.wrappedValue.dismiss()
  273. }
  274. }
  275. .onDisappear {
  276. model.delegate?.accountSettingsDidDismiss(tableAccount: model.activeAccount)
  277. }
  278. }
  279. }
  280. #Preview {
  281. NCAccountSettingsView(model: NCAccountSettingsModel(controller: nil, delegate: nil))
  282. }