HCEditProfile.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. //
  2. // HCEditProfile.swift
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 17/04/19.
  6. // Copyright (c) 2019 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. class HCEditProfile: XLFormViewController {
  25. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  26. var tableAccount: tableAccount?
  27. required init(coder aDecoder: NSCoder) {
  28. super.init(coder: aDecoder)
  29. }
  30. override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  31. super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  32. }
  33. func initializeForm() {
  34. let form : XLFormDescriptor = XLFormDescriptor() as XLFormDescriptor
  35. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  36. var section : XLFormSectionDescriptor
  37. var row : XLFormRowDescriptor
  38. tableAccount = NCManageDatabase.sharedInstance.getAccountActive()
  39. section = XLFormSectionDescriptor.formSection()
  40. form.addFormSection(section)
  41. row = XLFormRowDescriptor(tag: "userfullname", rowType: XLFormRowDescriptorTypeText, title: NSLocalizedString("_user_full_name_", comment: ""))
  42. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  43. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  44. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  45. row.cellConfig["imageView.image"] = CCGraphics.changeThemingColorImage(UIImage.init(named: "user"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)
  46. row.value = tableAccount?.displayName
  47. if row.value == nil { row.value = "" }
  48. section.addFormRow(row)
  49. row = XLFormRowDescriptor(tag: "useraddress", rowType: XLFormRowDescriptorTypeText, title: NSLocalizedString("_user_address_", comment: ""))
  50. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  51. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  52. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  53. row.cellConfig["imageView.image"] = CCGraphics.changeThemingColorImage(UIImage.init(named: "address"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)
  54. row.value = tableAccount?.address
  55. if row.value == nil { row.value = "" }
  56. section.addFormRow(row)
  57. row = XLFormRowDescriptor(tag: "usercity", rowType: XLFormRowDescriptorTypeText, title: NSLocalizedString("_user_city_", comment: ""))
  58. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  59. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  60. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  61. row.cellConfig["imageView.image"] = CCGraphics.changeThemingColorImage(UIImage.init(named: "city"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)
  62. row.value = tableAccount?.city
  63. if row.value == nil { row.value = "" }
  64. section.addFormRow(row)
  65. row = XLFormRowDescriptor(tag: "userzip", rowType: XLFormRowDescriptorTypeZipCode, title: NSLocalizedString("_user_zip_", comment: ""))
  66. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  67. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  68. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  69. row.cellConfig["imageView.image"] = CCGraphics.changeThemingColorImage(UIImage.init(named: "cityzip"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)
  70. row.value = tableAccount?.zip
  71. if row.value == nil { row.value = "" }
  72. section.addFormRow(row)
  73. row = XLFormRowDescriptor(tag: "usercountry", rowType: XLFormRowDescriptorTypeSelectorPickerView, title: NSLocalizedString("_user_country_", comment: ""))
  74. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  75. row.cellConfig["detailTextLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  76. row.cellConfig["imageView.image"] = CCGraphics.changeThemingColorImage(UIImage.init(named: "country"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)
  77. var locales = [String]()
  78. for localeCode in NSLocale.isoCountryCodes {
  79. let countryName = (Locale.current as NSLocale).displayName(forKey: .countryCode, value: localeCode) ?? ""
  80. if localeCode == tableAccount?.country {
  81. row.value = countryName
  82. }
  83. locales.append(countryName)
  84. }
  85. row.selectorOptions = locales.sorted()
  86. if row.value == nil { row.value = "" }
  87. section.addFormRow(row)
  88. row = XLFormRowDescriptor(tag: "userphone", rowType: XLFormRowDescriptorTypePhone, title: NSLocalizedString("_user_phone_", comment: ""))
  89. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  90. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  91. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  92. row.cellConfig["imageView.image"] = CCGraphics.changeThemingColorImage(UIImage.init(named: "phone"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)
  93. row.value = tableAccount?.phone
  94. if row.value == nil { row.value = "" }
  95. section.addFormRow(row)
  96. row = XLFormRowDescriptor(tag: "useremail", rowType: XLFormRowDescriptorTypeEmail, title: NSLocalizedString("_user_email_", comment: ""))
  97. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  98. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  99. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  100. row.cellConfig["imageView.image"] = CCGraphics.changeThemingColorImage(UIImage.init(named: "email"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)
  101. row.value = tableAccount?.email
  102. if row.value == nil { row.value = "" }
  103. section.addFormRow(row)
  104. row = XLFormRowDescriptor(tag: "userweb", rowType: XLFormRowDescriptorTypeEmail, title: NSLocalizedString("_user_web_", comment: ""))
  105. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  106. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  107. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  108. row.cellConfig["imageView.image"] = CCGraphics.changeThemingColorImage(UIImage.init(named: "web"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)
  109. row.value = tableAccount?.webpage
  110. if row.value == nil { row.value = "" }
  111. section.addFormRow(row)
  112. row = XLFormRowDescriptor(tag: "usertwitter", rowType: XLFormRowDescriptorTypeTwitter, title: NSLocalizedString("_user_twitter_", comment: ""))
  113. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  114. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  115. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  116. row.cellConfig["imageView.image"] = CCGraphics.changeThemingColorImage(UIImage.init(named: "twitter"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)
  117. row.value = tableAccount?.twitter
  118. if row.value == nil { row.value = "" }
  119. section.addFormRow(row)
  120. row = XLFormRowDescriptor(tag: "userbusinesstype", rowType: XLFormRowDescriptorTypeMultipleSelector, title: NSLocalizedString("_user_businesstype_", comment: ""))
  121. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  122. row.cellConfig["detailTextLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  123. row.cellConfig["imageView.image"] = CCGraphics.changeThemingColorImage(UIImage.init(named: "businesstype"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)
  124. row.selectorOptions = ["ARCHITECT", "BRICKLAYER", "CARPENTER", "ELECTRICIAN", "INSTALLER", "PAINTER", "PLUMBER", "ROOFER", "STOVEBUILDER","STUCCOER", "WINDOWBUILDER", "OTHER"]
  125. row.value = tableAccount?.businessType.components(separatedBy: ",")
  126. if row.value == nil { row.value = "" }
  127. section.addFormRow(row)
  128. row = XLFormRowDescriptor(tag: "userbusinesssize", rowType: XLFormRowDescriptorTypeSelectorPickerView, title: NSLocalizedString("_user_businesssize_", comment: ""))
  129. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  130. row.cellConfig["detailTextLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  131. row.cellConfig["imageView.image"] = CCGraphics.changeThemingColorImage(UIImage.init(named: "users"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)
  132. row.selectorOptions = ["1-4", "5-9", "10-19", "20-49", "50-99", "100-249", "250-499", "500-999", "1000+"];
  133. row.value = tableAccount?.businessSize;
  134. section.addFormRow(row)
  135. row = XLFormRowDescriptor(tag: "userrole", rowType: XLFormRowDescriptorTypeSelectorPickerView, title: NSLocalizedString("_user_role_", comment: ""))
  136. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  137. row.cellConfig["detailTextLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  138. row.cellConfig["imageView.image"] = CCGraphics.changeThemingColorImage(UIImage.init(named: "role"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)
  139. row.selectorOptions = [NSLocalizedString("_user_owner_", comment: ""),NSLocalizedString("_user_employee_", comment: ""),NSLocalizedString("_user_contractor_", comment: "")];
  140. if tableAccount?.role == "owner" { row.value = NSLocalizedString("_user_owner_", comment: "") }
  141. else if tableAccount?.role == "employee" { row.value = NSLocalizedString("_user_employee_", comment: "") }
  142. else if tableAccount?.role == "contractor" { row.value = NSLocalizedString("_user_contractor_", comment: "") }
  143. else { row.value = "" }
  144. section.addFormRow(row)
  145. row = XLFormRowDescriptor(tag: "usercompany", rowType: XLFormRowDescriptorTypeText, title: NSLocalizedString("_user_company_", comment: ""))
  146. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  147. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  148. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  149. row.cellConfig["imageView.image"] = CCGraphics.changeThemingColorImage(UIImage.init(named: "company"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)
  150. row.value = tableAccount?.company
  151. if row.value == nil { row.value = "" }
  152. section.addFormRow(row)
  153. self.form = form
  154. }
  155. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  156. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  157. }
  158. // MARK: - View Life Cycle
  159. override func viewDidLoad() {
  160. super.viewDidLoad()
  161. self.navigationItem.title = NSLocalizedString("_user_editprofile_", comment: "")
  162. self.navigationController?.navigationBar.isTranslucent = false
  163. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  164. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
  165. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: NCBrandColor.sharedInstance.brandText]
  166. self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
  167. self.navigationItem.setHidesBackButton(true, animated: false)
  168. self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(save))
  169. self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(cancel))
  170. self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  171. self.tableView.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  172. self.tableView.showsVerticalScrollIndicator = false
  173. initializeForm()
  174. }
  175. @objc func cancel() {
  176. self.navigationController?.popViewController(animated: true)
  177. self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
  178. }
  179. @objc func save() {
  180. NCUtility.sharedInstance.startActivityIndicator(view: self.view, bottom: 0)
  181. // Full name
  182. if self.form.formRow(withTag: "userfullname")?.value == nil {
  183. tableAccount?.displayName = ""
  184. } else {
  185. tableAccount?.displayName = self.form.formRow(withTag: "userfullname")?.value as! String
  186. }
  187. // Address
  188. if self.form.formRow(withTag: "useraddress")?.value == nil {
  189. tableAccount?.address = ""
  190. } else {
  191. tableAccount?.address = self.form.formRow(withTag: "useraddress")?.value as! String
  192. }
  193. // City
  194. if self.form.formRow(withTag: "usercity")?.value == nil {
  195. tableAccount?.city = ""
  196. } else {
  197. tableAccount?.city = self.form.formRow(withTag: "usercity")?.value as! String
  198. }
  199. // Zip
  200. if self.form.formRow(withTag: "userzip")?.value == nil {
  201. tableAccount?.zip = ""
  202. } else {
  203. tableAccount?.zip = self.form.formRow(withTag: "userzip")?.value as! String
  204. }
  205. let countryNameRow = self.form.formRow(withTag: "usercountry")!.value as? String
  206. for localeCode in NSLocale.isoCountryCodes {
  207. let countryName = (Locale.current as NSLocale).displayName(forKey: .countryCode, value: localeCode) ?? ""
  208. if countryNameRow == countryName {
  209. tableAccount?.country = localeCode
  210. break
  211. }
  212. }
  213. // Phone
  214. if self.form.formRow(withTag: "userphone")?.value == nil {
  215. tableAccount?.phone = ""
  216. } else {
  217. tableAccount?.phone = self.form.formRow(withTag: "userphone")?.value as! String
  218. }
  219. // Email
  220. if self.form.formRow(withTag: "useremail")?.value == nil {
  221. tableAccount?.email = ""
  222. } else {
  223. tableAccount?.email = self.form.formRow(withTag: "useremail")?.value as! String
  224. }
  225. // Web
  226. if self.form.formRow(withTag: "userweb")?.value == nil {
  227. tableAccount?.webpage = ""
  228. } else {
  229. tableAccount?.webpage = self.form.formRow(withTag: "userweb")?.value as! String
  230. }
  231. // Twitter
  232. if self.form.formRow(withTag: "usertwitter")?.value == nil {
  233. tableAccount?.twitter = ""
  234. } else {
  235. tableAccount?.twitter = self.form.formRow(withTag: "usertwitter")?.value as! String
  236. }
  237. // Company
  238. if self.form.formRow(withTag: "usercompany")?.value == nil {
  239. tableAccount?.company = ""
  240. } else {
  241. tableAccount?.company = self.form.formRow(withTag: "usercompany")?.value as! String
  242. }
  243. tableAccount?.businessSize = self.form.formRow(withTag: "userbusinesssize")!.value as! String
  244. let roleRow = self.form.formRow(withTag: "userrole")!.value as! String
  245. switch roleRow {
  246. case NSLocalizedString("_user_owner_", comment: ""): tableAccount?.role = "owner"
  247. case NSLocalizedString("_user_employee_", comment: ""): tableAccount?.role = "employee"
  248. case NSLocalizedString("_user_contractor_", comment: ""): tableAccount?.role = "contractor"
  249. default: break
  250. }
  251. let businesstypeArray = self.form.formRow(withTag: "userbusinesstype")!.value
  252. tableAccount?.businessType = (businesstypeArray as! [String]).joined(separator: ",")
  253. OCNetworking.sharedManager()?.putHCUserProfile(withAccount: appDelegate.activeAccount, serverUrl: appDelegate.activeUrl, address: tableAccount?.address, businesssize: tableAccount?.businessSize, businesstype: tableAccount?.businessType, city: tableAccount?.city, company: tableAccount?.company, country: tableAccount?.country, displayname: tableAccount?.displayName, email: tableAccount?.email, phone: tableAccount?.phone, role_: tableAccount?.role, twitter: tableAccount?.twitter, website: tableAccount?.webpage, zip: tableAccount?.zip, completion: { (account, message, errorCode) in
  254. if errorCode == 0 && account == self.appDelegate.activeAccount {
  255. NCManageDatabase.sharedInstance.updateAccount(self.tableAccount!)
  256. self.navigationController?.popViewController(animated: true)
  257. self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
  258. } else if errorCode != 0 {
  259. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  260. }
  261. NCUtility.sharedInstance.stopActivityIndicator()
  262. })
  263. }
  264. }