NCAccountSettingsView.swift 14 KB

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