UserProfileTableViewController+Actions.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  3. // SPDX-License-Identifier: GPL-3.0-or-later
  4. //
  5. import Foundation
  6. extension UserProfileTableViewController {
  7. @objc func editButtonPressed() {
  8. if activeTextField != nil {
  9. self.waitingForModification = true
  10. activeTextField?.resignFirstResponder()
  11. return
  12. }
  13. if !isEditable {
  14. isEditable = true
  15. self.showDoneButton()
  16. } else {
  17. isEditable = false
  18. self.showEditButton()
  19. }
  20. self.refreshProfileTableView()
  21. }
  22. func addNewAccount() {
  23. self.dismiss(animated: true) {
  24. NCUserInterfaceController.sharedInstance().presentLoginViewController()
  25. }
  26. }
  27. func showLogoutConfirmationDialog() {
  28. let alertTitle = multiAccountEnabled.boolValue ? NSLocalizedString("Remove account", comment: "") : NSLocalizedString("Log out", comment: "")
  29. let alertMessageAccountRemove = NSLocalizedString("Do you really want to remove this account?", comment: "")
  30. let alertMessageAccountLogout = NSLocalizedString("Do you really want to log out from this account?", comment: "")
  31. let alertMessage = multiAccountEnabled.boolValue ? alertMessageAccountRemove : alertMessageAccountLogout
  32. let actionTitle = multiAccountEnabled.boolValue ? NSLocalizedString("Remove", comment: "") : NSLocalizedString("Log out", comment: "")
  33. let confirmDialog = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
  34. let confirmAction = UIAlertAction(title: actionTitle, style: .destructive) { _ in
  35. self.logout()
  36. }
  37. confirmDialog.addAction(confirmAction)
  38. let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .cancel, handler: nil)
  39. confirmDialog.addAction(cancelAction)
  40. self.present(confirmDialog, animated: true, completion: nil)
  41. }
  42. func logout() {
  43. let activeAccount = NCDatabaseManager.sharedInstance().activeAccount()
  44. NCSettingsController.sharedInstance().logoutAccount(withAccountId: activeAccount.accountId) { _ in
  45. NCUserInterfaceController.sharedInstance().presentConversationsList()
  46. NCConnectionController.sharedInstance().checkAppState()
  47. }
  48. }
  49. func presentSetPhoneNumberDialog() {
  50. let setPhoneNumberDialog = UIAlertController(title: NSLocalizedString("Phone number", comment: ""), message: nil, preferredStyle: .alert)
  51. let hasPhone = !account.phone.isEmpty
  52. setPhoneNumberDialog.addTextField { [self] textField in
  53. let regionCode = NSLocale.current.regionCode
  54. let countryCode = phoneUtil.getCountryCode(forRegion: regionCode)
  55. if let countryCode = countryCode {
  56. textField.text = "+\(countryCode)"
  57. }
  58. if hasPhone {
  59. textField.text = self.account.phone
  60. }
  61. let exampleNumber = try? self.phoneUtil.getExampleNumber(regionCode ?? "")
  62. if let exampleNumber = exampleNumber {
  63. textField.placeholder = try? self.phoneUtil.format(exampleNumber, numberFormat: NBEPhoneNumberFormat.INTERNATIONAL)
  64. textField.keyboardType = .phonePad
  65. textField.delegate = self
  66. textField.tag = self.kPhoneTextFieldTag
  67. }
  68. }
  69. setPhoneAction = UIAlertAction(title: NSLocalizedString("Set", comment: ""), style: .default, handler: { _ in
  70. let phoneNumber = setPhoneNumberDialog.textFields?[0].text
  71. if let phoneNumber = phoneNumber {
  72. self.setPhoneNumber(phoneNumber)
  73. }
  74. })
  75. setPhoneAction.isEnabled = false
  76. setPhoneNumberDialog.addAction(setPhoneAction)
  77. if hasPhone {
  78. let removeAction = UIAlertAction(title: NSLocalizedString("Remove", comment: ""), style: .destructive) { _ in
  79. self.setPhoneNumber("")
  80. }
  81. setPhoneNumberDialog.addAction(removeAction)
  82. }
  83. let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .cancel, handler: nil)
  84. setPhoneNumberDialog.addAction(cancelAction)
  85. self.present(setPhoneNumberDialog, animated: true, completion: nil)
  86. }
  87. func setPhoneNumber(_ phoneNumber: String) {
  88. self.setModifyingProfileUI()
  89. NCAPIController.sharedInstance().setUserProfileField(kUserProfilePhone, withValue: phoneNumber, for: account) { error, _ in
  90. if error != nil {
  91. self.showProfileModificationErrorForField(inTextField: self.kPhoneTextFieldTag, textField: nil)
  92. } else {
  93. self.refreshUserProfile()
  94. }
  95. self.removeModifyingProfileUI()
  96. }
  97. }
  98. }