Fix rename host to duplicate name

Fixes #140
This commit is contained in:
Davide De Rosa 2020-05-11 22:30:58 +02:00
parent 52e136e9c7
commit 452920823c
2 changed files with 26 additions and 2 deletions

View File

@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- In-app purchase unavailable for new providers. [#141](https://github.com/passepartoutvpn/passepartout-ios/issues/141)
- Hosts may be renamed to same name. [#140](https://github.com/passepartoutvpn/passepartout-ios/issues/140)
## 1.11.1 (2020-05-11)

View File

@ -248,17 +248,40 @@ class ServiceViewController: UIViewController, StrongTableHost {
guard let newTitle = alert.textFields?.first?.text else {
return
}
self.doRenameCurrentProfile(to: newTitle)
self.confirmRenameCurrentProfile(to: newTitle)
}
alert.addCancelAction(L10n.Core.Global.cancel)
pendingRenameAction?.isEnabled = false
present(alert, animated: true, completion: nil)
}
private func doRenameCurrentProfile(to newTitle: String) {
private func confirmRenameCurrentProfile(to newTitle: String) {
guard let profile = profile else {
return
}
if let existingProfile = service.hostProfile(withTitle: newTitle) {
let alert = UIAlertController.asAlert(L10n.Core.Service.Alerts.Rename.title, L10n.Core.Wizards.Host.Alerts.Existing.message)
alert.addPreferredAction(L10n.Core.Global.ok) {
self.doReplaceProfile(profile, to: newTitle, existingProfile: existingProfile)
}
alert.addCancelAction(L10n.Core.Global.cancel)
present(alert, animated: true, completion: nil)
return
}
doRenameProfile(profile, to: newTitle)
}
private func doReplaceProfile(_ profile: ConnectionProfile, to newTitle: String, existingProfile: ConnectionProfile) {
let wasActive = service.isActiveProfile(existingProfile)
service.removeProfile(ProfileKey(existingProfile))
service.renameProfile(profile, to: newTitle)
if wasActive {
service.activateProfile(profile)
}
setProfile(profile, reloadingViews: true)
}
private func doRenameProfile(_ profile: ConnectionProfile, to newTitle: String) {
service.renameProfile(profile, to: newTitle)
setProfile(profile, reloadingViews: false)
}