merge default IOS-1719
authorbuff <andreas@pep-project.org>
Mon, 05 Aug 2019 16:17:29 +0200
branchIOS-1719
changeset 9538ed541a9548f3
parent 9525 b41657adf180
parent 9536 1f84134b02d9
child 9541 606bbce2e550
merge default
     1.1 --- a/pEpForiOS/AppDelegate.swift	Fri Aug 02 19:24:32 2019 +0200
     1.2 +++ b/pEpForiOS/AppDelegate.swift	Mon Aug 05 16:17:29 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/ViewModel/SettingsCellViewModel.swift	Fri Aug 02 19:24:32 2019 +0200
     2.2 +++ b/pEpForiOS/UI/Settings/ViewModel/SettingsCellViewModel.swift	Mon Aug 05 16:17:29 2019 +0200
     2.3 @@ -89,6 +89,24 @@
     2.4      }
     2.5  
     2.6      func delete() {
     2.7 -        account?.delete()
     2.8 +        guard let acc = account else {
     2.9 +            Log.shared.errorAndCrash(message: "Account lost")
    2.10 +            return
    2.11 +        }
    2.12 +
    2.13 +        let oldAddress = acc.user.address
    2.14 +        acc.delete()
    2.15 +
    2.16 +        if AppSettings.defaultAccount == oldAddress {
    2.17 +            let newDefaultAccount = Account.all().first
    2.18 +            guard let newDefaultAddress = newDefaultAccount?.user.address else {
    2.19 +                return
    2.20 +                //no more accounts, no default account
    2.21 +            }
    2.22 +            AppSettings.defaultAccount = newDefaultAddress
    2.23 +        }
    2.24 +
    2.25      }
    2.26 +
    2.27 +
    2.28  }
     3.1 --- a/pEpForiOSTests/Models/Settings/SettingsViewModelTest.swift	Fri Aug 02 19:24:32 2019 +0200
     3.2 +++ b/pEpForiOSTests/Models/Settings/SettingsViewModelTest.swift	Mon Aug 05 16:17:29 2019 +0200
     3.3 @@ -16,6 +16,11 @@
     3.4      var keySyncDeviceGroupServiceMoc: KeySyncDeviceGroupServiceMoc!
     3.5      var messageModelServiceMoc: MessageModelServiceMoc!
     3.6  
     3.7 +    func givenThereAreTwoAccounts() {
     3.8 +        _ = SecretTestData().createWorkingCdAccount(number: 1, context: moc)
     3.9 +        moc.saveAndLogErrors()
    3.10 +    }
    3.11 +
    3.12  
    3.13      //Number of sections corresponding to SettingsSectionViewModel.SectionType count
    3.14      let sections = 4
    3.15 @@ -54,9 +59,25 @@
    3.16          XCTAssertTrue(thereIsOneLessAccount)
    3.17      }
    3.18  
    3.19 -    func givenThereAreTwoAccounts() {
    3.20 -        _ = SecretTestData().createWorkingCdAccount(number: 1, context: moc)
    3.21 -        moc.saveAndLogErrors()
    3.22 +    func testDeleteAccountWithMoreThanOneAccountUpdatesDefaultAccount() {
    3.23 +
    3.24 +        givenThereAreTwoAccounts()
    3.25 +        setupViewModel()
    3.26 +
    3.27 +        let firstAccountPosition = (0,0)
    3.28 +        let secondAccountPosition = (0,0)
    3.29 +        let defaultAddress = (settingsVM[0][0] as? SettingsCellViewModel)?.account?.user.address
    3.30 +
    3.31 +        AppSettings.defaultAccount = defaultAddress
    3.32 +        XCTAssertEqual(AppSettings.defaultAccount, defaultAddress)
    3.33 +
    3.34 +        settingsVM.delete(section: firstAccountPosition.0, cell: firstAccountPosition.1)
    3.35 +
    3.36 +        XCTAssertNotEqual(AppSettings.defaultAccount, defaultAddress)
    3.37 +        XCTAssertNotNil(AppSettings.defaultAccount)
    3.38 +        let newDefaultAddress = (settingsVM[secondAccountPosition.0][secondAccountPosition.1] as? SettingsCellViewModel)?.account?.user.address
    3.39 +        XCTAssertEqual(AppSettings.defaultAccount, newDefaultAddress)
    3.40 +
    3.41      }
    3.42  
    3.43      func testLeaveDeviceGroupPressed() {