NCAccountSettingsView.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. Button(action: {
  179. showServerCertificate.toggle()
  180. }, label: {
  181. HStack {
  182. Image(systemName: "lock")
  183. .resizable()
  184. .scaledToFit()
  185. .font(Font.system(.body).weight(.light))
  186. .frame(width: 20, height: 20)
  187. .foregroundStyle(Color(NCBrandColor.shared.iconImageColor))
  188. Text(NSLocalizedString("_certificate_details_", comment: ""))
  189. .lineLimit(1)
  190. .truncationMode(.middle)
  191. .foregroundStyle(Color(NCBrandColor.shared.textColor))
  192. .padding(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 20))
  193. }
  194. .font(.system(size: 14))
  195. })
  196. .sheet(isPresented: $showServerCertificate) {
  197. if let url = URL(string: model.activeAccount?.urlBase), let host = url.host {
  198. certificateDetailsView(host: host, title: NSLocalizedString("_certificate_view_", comment: ""))
  199. }
  200. }
  201. ///
  202. /// Certificate push
  203. Button(action: {
  204. showPushCertificate.toggle()
  205. }, label: {
  206. HStack {
  207. Image(systemName: "lock")
  208. .resizable()
  209. .scaledToFit()
  210. .font(Font.system(.body).weight(.light))
  211. .frame(width: 20, height: 20)
  212. .foregroundStyle(Color(NCBrandColor.shared.iconImageColor))
  213. Text(NSLocalizedString("_certificate_pn_details_", comment: ""))
  214. .lineLimit(1)
  215. .truncationMode(.middle)
  216. .foregroundStyle(Color(NCBrandColor.shared.textColor))
  217. .padding(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 20))
  218. }
  219. .font(.system(size: 14))
  220. })
  221. .sheet(isPresented: $showPushCertificate) {
  222. if let url = URL(string: NCBrandOptions.shared.pushNotificationServerProxy), let host = url.host {
  223. certificateDetailsView(host: host, title: NSLocalizedString("_certificate_pn_view_", comment: ""))
  224. }
  225. }
  226. })
  227. ///
  228. /// Delete account
  229. Section(content: {
  230. Button(action: {
  231. showDeleteAccountAlert.toggle()
  232. }, label: {
  233. HStack {
  234. Image(systemName: "trash")
  235. .resizable()
  236. .scaledToFit()
  237. .font(Font.system(.body).weight(.light))
  238. .frame(width: 20, height: 20)
  239. .foregroundStyle(.red)
  240. Text(NSLocalizedString("_remove_local_account_", comment: ""))
  241. .lineLimit(1)
  242. .truncationMode(.middle)
  243. .foregroundStyle(.red)
  244. .padding(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 20))
  245. }
  246. .font(.system(size: 14))
  247. })
  248. .alert(NSLocalizedString("_want_delete_account_", comment: ""), isPresented: $showDeleteAccountAlert) {
  249. Button(NSLocalizedString("_remove_local_account_", comment: ""), role: .destructive) {
  250. model.deleteAccount()
  251. }
  252. Button(NSLocalizedString("_cancel_", comment: ""), role: .cancel) { }
  253. }
  254. })
  255. }
  256. .navigationBarTitle(NSLocalizedString("_account_settings_", comment: ""))
  257. .navigationBarTitleDisplayMode(.inline)
  258. .navigationBarItems(trailing: Button(action: {
  259. presentationMode.wrappedValue.dismiss()
  260. }) {
  261. Image(systemName: "xmark")
  262. .font(Font.system(.body).weight(.light))
  263. .foregroundStyle(Color(NCBrandColor.shared.iconImageColor))
  264. })
  265. }
  266. .defaultViewModifier(model)
  267. .navigationViewStyle(StackNavigationViewStyle())
  268. .onReceive(model.$dismissView) { newValue in
  269. if newValue {
  270. presentationMode.wrappedValue.dismiss()
  271. }
  272. }
  273. .onDisappear {
  274. model.delegate?.accountSettingsDidDismiss(tableAccount: model.activeAccount)
  275. }
  276. }
  277. }
  278. #Preview {
  279. NCAccountSettingsView(model: NCAccountSettingsModel(controller: nil, delegate: nil))
  280. }