HCEditProfile.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 backButton : UIBarButtonItem!
  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. let 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. // Replace the default back button
  178. self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
  179. self.navigationItem.setHidesBackButton(true, animated: false)
  180. self.backButton = UIBarButtonItem(title: NSLocalizedString("_manage_account_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(goBack))
  181. self.navigationItem.leftBarButtonItem = backButton
  182. self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  183. initializeForm()
  184. }
  185. @objc func goBack() {
  186. NCUtility.sharedInstance.startActivityIndicator(view: self.view, bottom: 0)
  187. let displayname = self.form.formRow(withTag: "userfullname")!.value as! String
  188. let address = self.form.formRow(withTag: "useraddress")!.value as! String
  189. let city = self.form.formRow(withTag: "usercity")!.value as! String
  190. let zip = self.form.formRow(withTag: "userzip")!.value as! String
  191. var country = ""
  192. let countryNameRow = self.form.formRow(withTag: "usercountry")!.value as? String
  193. for localeCode in NSLocale.isoCountryCodes {
  194. let countryName = (Locale.current as NSLocale).displayName(forKey: .countryCode, value: localeCode) ?? ""
  195. if countryNameRow == countryName {
  196. country = localeCode
  197. break
  198. }
  199. }
  200. let phone = self.form.formRow(withTag: "userphone")!.value as! String
  201. let email = self.form.formRow(withTag: "useremail")!.value as! String
  202. let website = self.form.formRow(withTag: "userweb")!.value as! String
  203. let twitter = self.form.formRow(withTag: "usertwitter")!.value as! String
  204. let company = self.form.formRow(withTag: "usercompany")!.value as! String
  205. let businesssize = self.form.formRow(withTag: "userbusinesssize")!.value as! String
  206. let role_ = self.form.formRow(withTag: "userrole")!.value as! String
  207. let businesstypeArray = self.form.formRow(withTag: "userbusinesstype")!.value
  208. let businesstype = (businesstypeArray as! [String]).joined(separator: ",")
  209. 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
  210. if errorCode == 0 && account == self.appDelegate.activeAccount {
  211. self.navigationController?.popViewController(animated: true)
  212. self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
  213. } else if errorCode != 0 {
  214. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  215. }
  216. NCUtility.sharedInstance.stopActivityIndicator()
  217. })
  218. }
  219. }