From 728182de6c8e8c5db723719a6eb347c46ceed9d4 Mon Sep 17 00:00:00 2001 From: Federico Maccaroni Date: Thu, 15 Dec 2022 15:21:29 -0300 Subject: [PATCH] [EC-844] Improve Apple Watch states (#2246) * EC-844 improve need login / need setup states on the watch * EC-844 Fix naming of things and moved constant to the proper place to maintain format. Also removed UpdateLastUserShouldConnectToWatchAsync from the interface of the StateService given that it's not used outside the service and made it private --- src/App/Services/BaseWatchDeviceService.cs | 13 ++++++++----- src/Core/Abstractions/IStateService.cs | 1 + src/Core/Constants.cs | 5 +++++ src/Core/Services/StateService.cs | 17 +++++++++++++++++ .../ViewModels/CipherListViewModel.swift | 6 ------ 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/App/Services/BaseWatchDeviceService.cs b/src/App/Services/BaseWatchDeviceService.cs index f2ffc0090..defd04a35 100644 --- a/src/App/Services/BaseWatchDeviceService.cs +++ b/src/App/Services/BaseWatchDeviceService.cs @@ -91,16 +91,19 @@ namespace Bit.App.Services private async Task GetStateAsync(string userId, bool shouldConnectToWatch) { + if (await _stateService.GetLastUserShouldConnectToWatchAsync() + && + (userId is null || !await _stateService.IsAuthenticatedAsync())) + { + // if the last user had "Connect to Watch" enabled and there's no user authenticated + return WatchState.NeedLogin; + } + if (!shouldConnectToWatch) { return WatchState.NeedSetup; } - if (!await _stateService.IsAuthenticatedAsync() || userId is null) - { - return WatchState.NeedLogin; - } - //if (await _vaultTimeoutService.IsLockedAsync() || // await _vaultTimeoutService.ShouldLockAsync()) //{ diff --git a/src/Core/Abstractions/IStateService.cs b/src/Core/Abstractions/IStateService.cs index 5f9b33e2d..f849c8178 100644 --- a/src/Core/Abstractions/IStateService.cs +++ b/src/Core/Abstractions/IStateService.cs @@ -162,5 +162,6 @@ namespace Bit.Core.Abstractions Task SetUsernameGenerationOptionsAsync(UsernameGenerationOptions value, string userId = null); Task GetShouldConnectToWatchAsync(string userId = null); Task SetShouldConnectToWatchAsync(bool shouldConnect, string userId = null); + Task GetLastUserShouldConnectToWatchAsync(); } } diff --git a/src/Core/Constants.cs b/src/Core/Constants.cs index 4547ec091..9c0a4893c 100644 --- a/src/Core/Constants.cs +++ b/src/Core/Constants.cs @@ -36,6 +36,11 @@ public const string NotificationData = "notificationData"; public const string NotificationDataType = "Type"; public const string PasswordlessLoginRequestKey = "passwordlessLoginRequest"; + /// + /// This key is used to store the value of "ShouldConnectToWatch" of the last user that had logged in + /// which is used to handle Apple Watch state logic + /// + public const string LastUserShouldConnectToWatchKey = "lastUserShouldConnectToWatch"; public const int SelectFileRequestCode = 42; public const int SelectFilePermissionRequestCode = 43; public const int SaveFileRequestCode = 44; diff --git a/src/Core/Services/StateService.cs b/src/Core/Services/StateService.cs index 9b7371f55..1390cf351 100644 --- a/src/Core/Services/StateService.cs +++ b/src/Core/Services/StateService.cs @@ -84,6 +84,8 @@ namespace Bit.Core.Services // Update pre-auth settings based on now-active user await SetRememberedOrgIdentifierAsync(await GetRememberedOrgIdentifierAsync()); await SetPreAuthEnvironmentUrlsAsync(await GetEnvironmentUrlsAsync()); + + await SetLastUserShouldConnectToWatchAsync(); } public async Task CheckExtensionActiveUserAndSwitchIfNeededAsync() @@ -1708,6 +1710,21 @@ namespace Bit.Core.Services ReconcileOptions(new StorageOptions { UserId = userId }, await GetDefaultStorageOptionsAsync()); var key = Constants.ShouldConnectToWatchKey(reconciledOptions.UserId); await SetValueAsync(key, shouldConnect, reconciledOptions); + await SetLastUserShouldConnectToWatchAsync(shouldConnect); + } + + public async Task GetLastUserShouldConnectToWatchAsync() + { + var options = await GetDefaultStorageOptionsAsync(); + var key = Constants.LastUserShouldConnectToWatchKey; + return await GetValueAsync(key, options) ?? false; + } + + private async Task SetLastUserShouldConnectToWatchAsync(bool? shouldConnect = null) + { + var options = await GetDefaultStorageOptionsAsync(); + var key = Constants.LastUserShouldConnectToWatchKey; + await SetValueAsync(key, shouldConnect ?? await GetShouldConnectToWatchAsync(), options); } } } diff --git a/src/watchOS/bitwarden/bitwarden WatchKit Extension/ViewModels/CipherListViewModel.swift b/src/watchOS/bitwarden/bitwarden WatchKit Extension/ViewModels/CipherListViewModel.swift index 0cafc784e..13402ecc4 100644 --- a/src/watchOS/bitwarden/bitwarden WatchKit Extension/ViewModels/CipherListViewModel.swift +++ b/src/watchOS/bitwarden/bitwarden WatchKit Extension/ViewModels/CipherListViewModel.swift @@ -51,12 +51,6 @@ class CipherListViewModel : ObservableObject { user = StateService.shared.getUser() - if user == nil && !watchConnectivityManager.isSessionActivated { - currentState = .needSetup - showingSheet = true - return - } - currentState = state ?? StateService.shared.currentState showingSheet = currentState != .valid