HCEditProfile.swift 17 KB

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