merge default IOS-1737
authorbuff <andreas@pep-project.org>
Mon, 05 Aug 2019 17:01:12 +0200
branchIOS-1737
changeset 9545e9c0999557db
parent 9535 cfc972409286
parent 9544 f91483e3e50c
child 9552 1e69af4783c7
merge default
     1.1 --- a/pEpForiOS/AppDelegate.swift	Mon Aug 05 15:16:02 2019 +0200
     1.2 +++ b/pEpForiOS/AppDelegate.swift	Mon Aug 05 17:01:12 2019 +0200
     1.3 @@ -80,9 +80,8 @@
     1.4      private func gracefullyShutdownServices() {
     1.5          guard syncUserActionsAndCleanupbackgroundTaskId == UIBackgroundTaskIdentifier.invalid
     1.6              else {
     1.7 -                Log.shared.warn(
     1.8 -                    "Will not start background sync, pending %d",
     1.9 -                    syncUserActionsAndCleanupbackgroundTaskId.rawValue)
    1.10 +                Log.shared.errorAndCrash("Will not start background sync, pending %d",
    1.11 +                                         syncUserActionsAndCleanupbackgroundTaskId.rawValue)
    1.12                  return
    1.13          }
    1.14          syncUserActionsAndCleanupbackgroundTaskId =
     2.1 --- a/pEpForiOS/UI/Settings/Setting/OnOffSettings/ViewModel/EnableKeySyncViewModel.swift	Mon Aug 05 15:16:02 2019 +0200
     2.2 +++ b/pEpForiOS/UI/Settings/Setting/OnOffSettings/ViewModel/EnableKeySyncViewModel.swift	Mon Aug 05 17:01:12 2019 +0200
     2.3 @@ -16,7 +16,7 @@
     2.4      var cellIdentifier = "switchOptionCell"
     2.5      private let messageModelService: MessageModelServiceProtocol
     2.6  
     2.7 -    private(set) var title = NSLocalizedString("Key Sync Enable",
     2.8 +    private(set) var title = NSLocalizedString("Enable Key Sync",
     2.9                                                 comment: "enable key sync with other devices in the group")
    2.10  
    2.11      init(_ messageModelService: MessageModelServiceProtocol) {
     3.1 --- a/pEpForiOS/UI/Settings/ViewModel/SettingsCellViewModel.swift	Mon Aug 05 15:16:02 2019 +0200
     3.2 +++ b/pEpForiOS/UI/Settings/ViewModel/SettingsCellViewModel.swift	Mon Aug 05 17:01:12 2019 +0200
     3.3 @@ -89,6 +89,24 @@
     3.4      }
     3.5  
     3.6      func delete() {
     3.7 -        account?.delete()
     3.8 +        guard let acc = account else {
     3.9 +            Log.shared.errorAndCrash(message: "Account lost")
    3.10 +            return
    3.11 +        }
    3.12 +
    3.13 +        let oldAddress = acc.user.address
    3.14 +        acc.delete()
    3.15 +
    3.16 +        if AppSettings.defaultAccount == oldAddress {
    3.17 +            let newDefaultAccount = Account.all().first
    3.18 +            guard let newDefaultAddress = newDefaultAccount?.user.address else {
    3.19 +                return
    3.20 +                //no more accounts, no default account
    3.21 +            }
    3.22 +            AppSettings.defaultAccount = newDefaultAddress
    3.23 +        }
    3.24 +
    3.25      }
    3.26 +
    3.27 +
    3.28  }
     4.1 --- a/pEpForiOS/UI/Settings/ViewModel/SettingsSectionViewModel.swift	Mon Aug 05 15:16:02 2019 +0200
     4.2 +++ b/pEpForiOS/UI/Settings/ViewModel/SettingsSectionViewModel.swift	Mon Aug 05 17:01:12 2019 +0200
     4.3 @@ -59,10 +59,12 @@
     4.4          }
     4.5      }
     4.6  
     4.7 -    private func generateKeySyncCells(_ messageModelService: MessageModelServiceProtocol) {
     4.8 -        cells.append(EnableKeySyncViewModel(messageModelService))
     4.9 -        if isInDeviceGroup() {
    4.10 -            cells.append(SettingsActionCellViewModel(type: .leaveKeySyncGroup))
    4.11 +    func removeLeaveDeviceGroupCell() {
    4.12 +        cells.removeAll { cell in
    4.13 +            guard let actionCell = cell as? SettingsActionCellViewModel else {
    4.14 +                return false
    4.15 +            }
    4.16 +            return actionCell.type == .leaveKeySyncGroup
    4.17          }
    4.18      }
    4.19  
    4.20 @@ -113,4 +115,11 @@
    4.21          }
    4.22          return keySyncDeviceGroupService.deviceGroupState == .grouped
    4.23      }
    4.24 +
    4.25 +    private func generateKeySyncCells(_ messageModelService: MessageModelServiceProtocol) {
    4.26 +        cells.append(EnableKeySyncViewModel(messageModelService))
    4.27 +        if isInDeviceGroup() {
    4.28 +            cells.append(SettingsActionCellViewModel(type: .leaveKeySyncGroup))
    4.29 +        }
    4.30 +    }
    4.31  }
     5.1 --- a/pEpForiOS/UI/Settings/ViewModel/SettingsViewModel.swift	Mon Aug 05 15:16:02 2019 +0200
     5.2 +++ b/pEpForiOS/UI/Settings/ViewModel/SettingsViewModel.swift	Mon Aug 05 17:01:12 2019 +0200
     5.3 @@ -45,6 +45,7 @@
     5.4          }
     5.5          do {
     5.6              try keySyncDeviceGroupService.leaveDeviceGroup()
     5.7 +            removeLeaveDeviceGroupCell()
     5.8          } catch {
     5.9              Log.shared.errorAndCrash("%@", error.localizedDescription)
    5.10              return error
    5.11 @@ -80,4 +81,13 @@
    5.12      private func sectionIsValid(section: Int) -> Bool {
    5.13          return section >= 0 && section < sections.count
    5.14      }
    5.15 +
    5.16 +    private func removeLeaveDeviceGroupCell() {
    5.17 +        for section in sections {
    5.18 +            guard section.type == .keySync else {
    5.19 +                continue
    5.20 +            }
    5.21 +            section.removeLeaveDeviceGroupCell()
    5.22 +        }
    5.23 +    }
    5.24  }
     6.1 --- a/pEpForiOSTests/Models/Settings/SettingsViewModelTest.swift	Mon Aug 05 15:16:02 2019 +0200
     6.2 +++ b/pEpForiOSTests/Models/Settings/SettingsViewModelTest.swift	Mon Aug 05 17:01:12 2019 +0200
     6.3 @@ -16,6 +16,11 @@
     6.4      var keySyncDeviceGroupServiceMoc: KeySyncDeviceGroupServiceMoc!
     6.5      var messageModelServiceMoc: MessageModelServiceMoc!
     6.6  
     6.7 +    func givenThereAreTwoAccounts() {
     6.8 +        _ = SecretTestData().createWorkingCdAccount(number: 1, context: moc)
     6.9 +        moc.saveAndLogErrors()
    6.10 +    }
    6.11 +
    6.12  
    6.13      //Number of sections corresponding to SettingsSectionViewModel.SectionType count
    6.14      let sections = 4
    6.15 @@ -54,9 +59,25 @@
    6.16          XCTAssertTrue(thereIsOneLessAccount)
    6.17      }
    6.18  
    6.19 -    func givenThereAreTwoAccounts() {
    6.20 -        _ = SecretTestData().createWorkingCdAccount(number: 1, context: moc)
    6.21 -        moc.saveAndLogErrors()
    6.22 +    func testDeleteAccountWithMoreThanOneAccountUpdatesDefaultAccount() {
    6.23 +
    6.24 +        givenThereAreTwoAccounts()
    6.25 +        setupViewModel()
    6.26 +
    6.27 +        let firstAccountPosition = (0,0)
    6.28 +        let secondAccountPosition = (0,0)
    6.29 +        let defaultAddress = (settingsVM[0][0] as? SettingsCellViewModel)?.account?.user.address
    6.30 +
    6.31 +        AppSettings.defaultAccount = defaultAddress
    6.32 +        XCTAssertEqual(AppSettings.defaultAccount, defaultAddress)
    6.33 +
    6.34 +        settingsVM.delete(section: firstAccountPosition.0, cell: firstAccountPosition.1)
    6.35 +
    6.36 +        XCTAssertNotEqual(AppSettings.defaultAccount, defaultAddress)
    6.37 +        XCTAssertNotNil(AppSettings.defaultAccount)
    6.38 +        let newDefaultAddress = (settingsVM[secondAccountPosition.0][secondAccountPosition.1] as? SettingsCellViewModel)?.account?.user.address
    6.39 +        XCTAssertEqual(AppSettings.defaultAccount, newDefaultAddress)
    6.40 +
    6.41      }
    6.42  
    6.43      func testLeaveDeviceGroupPressed() {
    6.44 @@ -68,6 +89,11 @@
    6.45  
    6.46          // THEN
    6.47          XCTAssertTrue(keySyncDeviceGroupServiceMoc.didCallLeaveDeviceGroup)
    6.48 +        guard let section = keySyncSection() else { return }
    6.49 +        for cell in section.cells {
    6.50 +            guard let cell = cell as? SettingsActionCellViewModel else { continue }
    6.51 +            XCTAssertFalse(cell.type == .leaveKeySyncGroup)
    6.52 +        }
    6.53      }
    6.54  
    6.55      func testKeySyncEnabledSetTrue() {
    6.56 @@ -75,12 +101,13 @@
    6.57          setupViewModel()
    6.58  
    6.59          // WHEN
    6.60 -        for section in settingsVM.sections {
    6.61 -            guard section.type == SettingsSectionViewModel.SectionType.keySync else { continue }
    6.62 -            for cell in section.cells {
    6.63 -                guard let cell = cell as? EnableKeySyncViewModel else { continue }
    6.64 -                cell.setSwitch(value: true)
    6.65 -            }
    6.66 +        guard let section = keySyncSection() else {
    6.67 +            XCTFail()
    6.68 +            return
    6.69 +        }
    6.70 +        for cell in section.cells {
    6.71 +            guard let cell = cell as? EnableKeySyncViewModel else { continue }
    6.72 +            cell.setSwitch(value: true)
    6.73          }
    6.74  
    6.75          // THEN
    6.76 @@ -112,4 +139,12 @@
    6.77          keySyncDeviceGroupServiceMoc = KeySyncDeviceGroupServiceMoc()
    6.78          settingsVM = SettingsViewModel(messageModelServiceMoc, keySyncDeviceGroupServiceMoc)
    6.79      }
    6.80 +
    6.81 +    private func keySyncSection() -> SettingsSectionViewModel? {
    6.82 +        for section in settingsVM.sections {
    6.83 +            guard section.type == SettingsSectionViewModel.SectionType.keySync else { continue }
    6.84 +            return section
    6.85 +        }
    6.86 +        return nil
    6.87 +    }
    6.88  }