HCEditProfile.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. switch tableAccount?.businessSize {
  134. case 1: row.value = "1-4"
  135. case 5: row.value = "5-9"
  136. case 10: row.value = "10-19"
  137. case 20: row.value = "20-49"
  138. case 50: row.value = "50-99"
  139. case 100: row.value = "100-249"
  140. case 250: row.value = "250-499"
  141. case 500: row.value = "500-999"
  142. case 1000: row.value = "1000+"
  143. default: row.value = ""
  144. }
  145. section.addFormRow(row)
  146. row = XLFormRowDescriptor(tag: "userrole", rowType: XLFormRowDescriptorTypeSelectorPickerView, title: NSLocalizedString("_user_role_", comment: ""))
  147. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  148. row.cellConfig["detailTextLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  149. row.cellConfig["imageView.image"] = CCGraphics.changeThemingColorImage(UIImage.init(named: "role"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)
  150. row.selectorOptions = [NSLocalizedString("_user_owner_", comment: ""),NSLocalizedString("_user_employee_", comment: ""),NSLocalizedString("_user_contractor_", comment: "")];
  151. if tableAccount?.role == "owner" { row.value = NSLocalizedString("_user_owner_", comment: "") }
  152. else if tableAccount?.role == "employee" { row.value = NSLocalizedString("_user_employee_", comment: "") }
  153. else if tableAccount?.role == "contractor" { row.value = NSLocalizedString("_user_contractor_", comment: "") }
  154. else { row.value = "" }
  155. section.addFormRow(row)
  156. row = XLFormRowDescriptor(tag: "usercompany", rowType: XLFormRowDescriptorTypeText, title: NSLocalizedString("_user_company_", comment: ""))
  157. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  158. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  159. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  160. row.cellConfig["imageView.image"] = CCGraphics.changeThemingColorImage(UIImage.init(named: "company"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)
  161. row.value = tableAccount?.company
  162. if row.value == nil { row.value = "" }
  163. section.addFormRow(row)
  164. self.form = form
  165. }
  166. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  167. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  168. }
  169. // MARK: - View Life Cycle
  170. override func viewDidLoad() {
  171. super.viewDidLoad()
  172. self.navigationItem.title = NSLocalizedString("_user_editprofile_", comment: "")
  173. self.navigationController?.navigationBar.isTranslucent = false
  174. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  175. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
  176. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: NCBrandColor.sharedInstance.brandText]
  177. self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
  178. self.navigationItem.setHidesBackButton(true, animated: false)
  179. self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(save))
  180. self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(cancel))
  181. self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  182. initializeForm()
  183. }
  184. @objc func cancel() {
  185. self.navigationController?.popViewController(animated: true)
  186. self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
  187. }
  188. @objc func save() {
  189. NCUtility.sharedInstance.startActivityIndicator(view: self.view, bottom: 0)
  190. tableAccount?.displayName = self.form.formRow(withTag: "userfullname")!.value as! String
  191. tableAccount?.address = self.form.formRow(withTag: "useraddress")!.value as! String
  192. tableAccount?.city = self.form.formRow(withTag: "usercity")!.value as! String
  193. tableAccount?.zip = self.form.formRow(withTag: "userzip")!.value as! String
  194. let countryNameRow = self.form.formRow(withTag: "usercountry")!.value as? String
  195. for localeCode in NSLocale.isoCountryCodes {
  196. let countryName = (Locale.current as NSLocale).displayName(forKey: .countryCode, value: localeCode) ?? ""
  197. if countryNameRow == countryName {
  198. tableAccount?.country = localeCode
  199. break
  200. }
  201. }
  202. tableAccount?.phone = self.form.formRow(withTag: "userphone")!.value as! String
  203. tableAccount?.email = self.form.formRow(withTag: "useremail")!.value as! String
  204. tableAccount?.webpage = self.form.formRow(withTag: "userweb")!.value as! String
  205. tableAccount?.twitter = self.form.formRow(withTag: "usertwitter")!.value as! String
  206. tableAccount?.company = self.form.formRow(withTag: "usercompany")!.value as! String
  207. let businesssizeRow = self.form.formRow(withTag: "userbusinesssize")!.value as! String
  208. switch businesssizeRow {
  209. case "1-4": tableAccount?.businessSize = 1
  210. case "5-9": tableAccount?.businessSize = 5
  211. case "10-19": tableAccount?.businessSize = 10
  212. case "20-49": tableAccount?.businessSize = 20
  213. case "50-99": tableAccount?.businessSize = 50
  214. case "100-249": tableAccount?.businessSize = 100
  215. case "250-499": tableAccount?.businessSize = 250
  216. case "500-999": tableAccount?.businessSize = 500
  217. case "1000+": tableAccount?.businessSize = 1000
  218. default: break
  219. }
  220. let roleRow = self.form.formRow(withTag: "userrole")!.value as! String
  221. switch roleRow {
  222. case NSLocalizedString("_user_owner_", comment: ""): tableAccount?.role = "owner"
  223. case NSLocalizedString("_user_employee_", comment: ""): tableAccount?.role = "employee"
  224. case NSLocalizedString("_user_contractor_", comment: ""): tableAccount?.role = "contractor"
  225. default: break
  226. }
  227. let businesstypeArray = self.form.formRow(withTag: "userbusinesstype")!.value
  228. tableAccount?.businessType = (businesstypeArray as! [String]).joined(separator: ",")
  229. OCNetworking.sharedManager()?.putHCUserProfile(withAccount: appDelegate.activeAccount, serverUrl: appDelegate.activeUrl, address: tableAccount?.address, businesssize: String(describing: 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
  230. if errorCode == 0 && account == self.appDelegate.activeAccount {
  231. _ = NCManageDatabase.sharedInstance.updateAccount(self.tableAccount!)
  232. self.navigationController?.popViewController(animated: true)
  233. self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
  234. } else if errorCode != 0 {
  235. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  236. }
  237. NCUtility.sharedInstance.stopActivityIndicator()
  238. })
  239. }
  240. }