From 029ff34d9177ae9eabe1955771334f86699c395c Mon Sep 17 00:00:00 2001
From: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
Date: Fri, 10 Jul 2020 16:48:31 -0400
Subject: [PATCH] settings: add peerDiscovery

Change-Id: Ifab215af940a8f1969ec4d81a984e287c5a0d5f0
Gitlab: #79
---
 Ring/Ring/Constants/Generated/Strings.swift   |  2 +
 .../Features/Me/Me/MeViewController.swift     | 26 ++++++++++++-
 Ring/Ring/Features/Me/Me/MeViewModel.swift    | 19 ++++++++++
 .../Resources/en.lproj/Localizable.strings    |  1 +
 Ring/Ring/Services/AccountsService.swift      | 38 ++++++-------------
 5 files changed, 58 insertions(+), 28 deletions(-)

diff --git a/Ring/Ring/Constants/Generated/Strings.swift b/Ring/Ring/Constants/Generated/Strings.swift
index 4ffa5f6bc..c7c61167d 100644
--- a/Ring/Ring/Constants/Generated/Strings.swift
+++ b/Ring/Ring/Constants/Generated/Strings.swift
@@ -115,6 +115,8 @@ internal enum L10n {
     internal static let other = L10n.tr("Localizable", "accountPage.other")
     /// Enter account password
     internal static let passwordPlaceholder = L10n.tr("Localizable", "accountPage.passwordPlaceholder")
+    /// Auto connect on local network
+    internal static let peerDiscovery = L10n.tr("Localizable", "accountPage.peerDiscovery")
     /// Provide proxy address
     internal static let proxyAddressAlert = L10n.tr("Localizable", "accountPage.proxyAddressAlert")
     /// In order to receive notifications, please enable proxy
diff --git a/Ring/Ring/Features/Me/Me/MeViewController.swift b/Ring/Ring/Features/Me/Me/MeViewController.swift
index 1719a21ed..f30cc0584 100644
--- a/Ring/Ring/Features/Me/Me/MeViewController.swift
+++ b/Ring/Ring/Features/Me/Me/MeViewController.swift
@@ -456,6 +456,28 @@ class MeViewController: EditProfileViewController, StoryboardBased, ViewModelBas
                             self?.viewModel.enableNotifications(enable: value)
                         }).disposed(by: cell.disposeBag)
                     return cell
+                case .peerDiscovery:
+                    let cell = DisposableCell()
+                    cell.backgroundColor = UIColor.jamiBackgroundColor
+                    cell.textLabel?.text = L10n.AccountPage.peerDiscovery
+                    let switchView = UISwitch()
+                    cell.selectionStyle = .none
+                    cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
+                    cell.accessoryView = switchView
+                    self.viewModel.peerDiscoveryEnabled
+                        .asObservable()
+                        .startWith(self.viewModel.peerDiscoveryEnabled.value)
+                        .observeOn(MainScheduler.instance)
+                        .bind(to: switchView.rx.value)
+                        .disposed(by: cell.disposeBag)
+                    switchView.rx
+                        .isOn.changed
+                        .debounce(0.2, scheduler: MainScheduler.instance)
+                        .distinctUntilChanged().asObservable()
+                        .subscribe(onNext: {[weak self] enable in
+                            self?.viewModel.enablePeerDiscovery(enable: enable)
+                        }).disposed(by: cell.disposeBag)
+                    return cell
                 case .sipUserName(let value):
                     let cell = self
                         .configureSipCredentialsCell(cellType: .sipUserName(value: value),
@@ -493,8 +515,8 @@ class MeViewController: EditProfileViewController, StoryboardBased, ViewModelBas
                     state.asObservable()
                         .observeOn(MainScheduler.instance)
                         .subscribe(onNext: { (status) in
-                                                   cell.detailTextLabel?.text = status
-                                               }).disposed(by: cell.disposeBag)
+                            cell.detailTextLabel?.text = status
+                        }).disposed(by: cell.disposeBag)
                     return cell
                 case .boothMode:
                     let cell = DisposableCell(style: .subtitle, reuseIdentifier: self.jamiIDCell)
diff --git a/Ring/Ring/Features/Me/Me/MeViewModel.swift b/Ring/Ring/Features/Me/Me/MeViewModel.swift
index dec3a49ca..30dde46b0 100644
--- a/Ring/Ring/Features/Me/Me/MeViewModel.swift
+++ b/Ring/Ring/Features/Me/Me/MeViewModel.swift
@@ -55,6 +55,7 @@ enum SettingsSection: SectionModelType {
         case enableAccount
         case changePassword
         case boothMode
+        case peerDiscovery
     }
 
     var items: [SectionRow] {
@@ -212,6 +213,7 @@ class MeViewModel: ViewModel, Stateable {
     lazy var otherJamiSettings: Observable<SettingsSection> = {
         return Observable
             .just(SettingsSection.accountSettings( items: [.sectionHeader(title: L10n.AccountPage.other),
+                                                           .peerDiscovery,
                                                            .blockedList,
                                                            .accountState(state: self.accountStatus),
                                                            .enableAccount,
@@ -693,6 +695,16 @@ class MeViewModel: ViewModel, Stateable {
         return Variable<Bool>(true)
     }()
 
+    lazy var peerDiscoveryEnabled: Variable<Bool> = {
+        if let account = self.accountService.currentAccount,
+            let details = account.details {
+            let enable = details.get(withConfigKeyModel:
+                ConfigKeyModel.init(withKey: .dhtPeerDiscovery)).boolValue
+            return Variable<Bool>(enable)
+        }
+        return Variable<Bool>(true)
+    }()
+
     func enableAccount(enable: Bool) {
         if self.accountEnabled.value == enable {return}
         guard let account = self.accountService.currentAccount else {return}
@@ -700,6 +712,13 @@ class MeViewModel: ViewModel, Stateable {
         accountEnabled.value = enable
     }
 
+    func enablePeerDiscovery(enable: Bool) {
+        guard self.peerDiscoveryEnabled.value != enable,
+            let account = self.accountService.currentAccount else { return }
+        self.accountService.enablePeerDiscovery(enable: enable, accountId: account.id)
+        peerDiscoveryEnabled.value = enable
+    }
+
     // MARK: Sip Credentials
     let sipUsername = Variable<String>("")
     let sipPassword = Variable<String>("")
diff --git a/Ring/Ring/Resources/en.lproj/Localizable.strings b/Ring/Ring/Resources/en.lproj/Localizable.strings
index 62ac32878..331546d49 100644
--- a/Ring/Ring/Resources/en.lproj/Localizable.strings
+++ b/Ring/Ring/Resources/en.lproj/Localizable.strings
@@ -218,6 +218,7 @@
 "accountPage.boothModeExplanation" = "In booth mode conversation history not saved and jami functionality limited by making outgoing calls. When you enable booth mode all your conversations will be removed.";
 "accountPage.noBoothMode" = "To enable Booth mode you need to create account password first.";
 "accountPage.boothModeAlertMessage" = "After enabling booth mode all your conversations will be removed.";
+"accountPage.peerDiscovery" = "Auto connect on local network";
 
 //Account
 "account.sipUsername" = "User Name";
diff --git a/Ring/Ring/Services/AccountsService.swift b/Ring/Ring/Services/AccountsService.swift
index 949e6c67a..e5169c916 100644
--- a/Ring/Ring/Services/AccountsService.swift
+++ b/Ring/Ring/Services/AccountsService.swift
@@ -528,13 +528,7 @@ class AccountsService: AccountAdapterDelegate {
         if details
             .get(withConfigKeyModel: ConfigKeyModel(withKey: ConfigKey.ringtonePath)) == filename &&
             details
-                .get(withConfigKeyModel: ConfigKeyModel(withKey: ConfigKey.ringtoneEnabled)) == "false" &&
-            details
-                .get(withConfigKeyModel: ConfigKeyModel(withKey: ConfigKey.dhtPeerDiscovery)) == "false" &&
-            details
-                .get(withConfigKeyModel: ConfigKeyModel(withKey: ConfigKey.accountPeerDiscovery)) == "false" &&
-            details
-                .get(withConfigKeyModel: ConfigKeyModel(withKey: ConfigKey.accountPublish)) == "false" {
+                .get(withConfigKeyModel: ConfigKeyModel(withKey: ConfigKey.ringtoneEnabled)) == "false" {
             return
         }
         details
@@ -543,15 +537,6 @@ class AccountsService: AccountAdapterDelegate {
         details
             .set(withConfigKeyModel: ConfigKeyModel(withKey: ConfigKey.ringtoneEnabled),
                  withValue: "false")
-        details
-        .set(withConfigKeyModel: ConfigKeyModel(withKey: ConfigKey.dhtPeerDiscovery),
-             withValue: "false")
-        details
-            .set(withConfigKeyModel: ConfigKeyModel(withKey: ConfigKey.accountPeerDiscovery),
-                 withValue: "false")
-        details
-            .set(withConfigKeyModel: ConfigKeyModel(withKey: ConfigKey.accountPublish),
-                 withValue: "false")
         setAccountDetails(forAccountId: accountId, withDetails: details)
     }
 
@@ -709,14 +694,8 @@ class AccountsService: AccountAdapterDelegate {
         }
         accountDetails!.updateValue("oversip", forKey: ConfigKey.accountDTMFType.rawValue)
         accountDetails!.updateValue("true", forKey: ConfigKey.videoEnabled.rawValue)
-        accountDetails!.updateValue("default.wav", forKey: ConfigKey.ringtonePath.rawValue)
         accountDetails!.updateValue(accountType, forKey: ConfigKey.accountType.rawValue)
         accountDetails!.updateValue("true", forKey: ConfigKey.accountUpnpEnabled.rawValue)
-        if accountType == AccountType.ring.rawValue {
-            accountDetails!.updateValue("false", forKey: ConfigKey.dhtPeerDiscovery.rawValue)
-            accountDetails!.updateValue("false", forKey: ConfigKey.accountPeerDiscovery.rawValue)
-            accountDetails!.updateValue("false", forKey: ConfigKey.accountPublish.rawValue)
-        }
         return accountDetails!
     }
 
@@ -969,11 +948,18 @@ class AccountsService: AccountAdapterDelegate {
     }
 
     func enableAccount(enable: Bool, accountId: String) {
+        self.switchAccountPropertyTo(state: enable, accountId: accountId, property: ConfigKeyModel(withKey: ConfigKey.accountEnable));
+    }
+
+    func enablePeerDiscovery(enable: Bool, accountId: String) {
+        self.switchAccountPropertyTo(state: enable, accountId: accountId, property: ConfigKeyModel(withKey: ConfigKey.dhtPeerDiscovery));
+    }
+
+    func switchAccountPropertyTo(state: Bool, accountId: String, property: ConfigKeyModel) {
         let accountDetails = self.getAccountDetails(fromAccountId: accountId)
-        if accountDetails.get(withConfigKeyModel: ConfigKeyModel(withKey: ConfigKey.accountEnable)) != enable.toString() {
-            accountDetails.set(withConfigKeyModel: ConfigKeyModel(withKey: ConfigKey.accountEnable), withValue: enable.toString())
-            self.setAccountDetails(forAccountId: accountId, withDetails: accountDetails)
-        }
+        guard accountDetails.get(withConfigKeyModel: property) != state.toString() else { return }
+        accountDetails.set(withConfigKeyModel: property, withValue: state.toString())
+        self.setAccountDetails(forAccountId: accountId, withDetails: accountDetails)
     }
 
     // MARK: - observable account data
-- 
GitLab