Bitwarden-app-android-iphon.../src/App/Pages/Settings/OptionsPageViewModel.cs

197 lines
7.5 KiB
C#
Raw Normal View History

2019-05-29 15:08:47 +02:00
using Bit.App.Abstractions;
using Bit.App.Resources;
2019-05-29 20:23:55 +02:00
using Bit.App.Utilities;
2019-05-29 15:36:57 +02:00
using Bit.Core;
2019-05-29 15:08:47 +02:00
using Bit.Core.Abstractions;
2019-05-29 20:11:15 +02:00
using Bit.Core.Enums;
2019-05-29 15:08:47 +02:00
using Bit.Core.Utilities;
2019-05-29 20:11:15 +02:00
using System.Collections.Generic;
2019-05-29 15:08:47 +02:00
using System.Threading.Tasks;
2019-05-29 21:50:20 +02:00
using Xamarin.Forms;
2019-05-29 15:08:47 +02:00
namespace Bit.App.Pages
{
public class OptionsPageViewModel : BaseViewModel
{
private readonly IDeviceActionService _deviceActionService;
private readonly IPlatformUtilsService _platformUtilsService;
2019-05-29 15:36:57 +02:00
private readonly IStorageService _storageService;
private readonly ITotpService _totpService;
private readonly IStateService _stateService;
2019-05-29 21:50:20 +02:00
private readonly IMessagingService _messagingService;
2019-05-29 15:36:57 +02:00
private bool _disableFavicon;
private bool _disableAutoTotpCopy;
2019-05-29 20:11:15 +02:00
private int _clearClipboardSelectedIndex;
private int _themeSelectedIndex;
private int _uriMatchSelectedIndex;
2019-05-29 20:23:55 +02:00
private bool _inited;
2019-05-29 20:11:15 +02:00
2019-05-29 15:08:47 +02:00
public OptionsPageViewModel()
{
_deviceActionService = ServiceContainer.Resolve<IDeviceActionService>("deviceActionService");
_platformUtilsService = ServiceContainer.Resolve<IPlatformUtilsService>("platformUtilsService");
2019-05-29 15:36:57 +02:00
_storageService = ServiceContainer.Resolve<IStorageService>("storageService");
_totpService = ServiceContainer.Resolve<ITotpService>("totpService");
_stateService = ServiceContainer.Resolve<IStateService>("stateService");
2019-05-29 21:50:20 +02:00
_messagingService = ServiceContainer.Resolve<IMessagingService>("messagingService");
2019-05-29 15:08:47 +02:00
PageTitle = AppResources.Options;
2019-05-29 20:11:15 +02:00
ClearClipboardOptions = new List<KeyValuePair<int?, string>>
{
new KeyValuePair<int?, string>(null, AppResources.Never),
new KeyValuePair<int?, string>(10, AppResources.TenSeconds),
new KeyValuePair<int?, string>(20, AppResources.TwentySeconds),
new KeyValuePair<int?, string>(30, AppResources.ThirtySeconds),
new KeyValuePair<int?, string>(60, AppResources.OneMinute),
new KeyValuePair<int?, string>(120, AppResources.TwoMinutes),
new KeyValuePair<int?, string>(300, AppResources.FiveMinutes),
};
ThemeOptions = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>(null, AppResources.Default),
new KeyValuePair<string, string>("light", AppResources.Light),
new KeyValuePair<string, string>("dark", AppResources.Dark),
};
UriMatchOptions = new List<KeyValuePair<UriMatchType?, string>>
{
new KeyValuePair<UriMatchType?, string>(UriMatchType.Domain, AppResources.BaseDomain),
new KeyValuePair<UriMatchType?, string>(UriMatchType.Host, AppResources.Host),
new KeyValuePair<UriMatchType?, string>(UriMatchType.StartsWith, AppResources.StartsWith),
new KeyValuePair<UriMatchType?, string>(UriMatchType.RegularExpression, AppResources.RegEx),
new KeyValuePair<UriMatchType?, string>(UriMatchType.Exact, AppResources.Exact),
new KeyValuePair<UriMatchType?, string>(UriMatchType.Never, AppResources.Never),
};
}
public List<KeyValuePair<int?, string>> ClearClipboardOptions { get; set; }
public List<KeyValuePair<string, string>> ThemeOptions { get; set; }
public List<KeyValuePair<UriMatchType?, string>> UriMatchOptions { get; set; }
public int ClearClipboardSelectedIndex
{
get => _clearClipboardSelectedIndex;
set
{
if(SetProperty(ref _clearClipboardSelectedIndex, value))
{
var task = SaveClipboardChangedAsync();
}
}
}
public int ThemeSelectedIndex
{
get => _themeSelectedIndex;
set
{
if(SetProperty(ref _themeSelectedIndex, value))
{
var task = SaveThemeAsync();
}
}
}
public int UriMatchSelectedIndex
{
get => _uriMatchSelectedIndex;
set
{
if(SetProperty(ref _uriMatchSelectedIndex, value))
{
var task = SaveDefaultUriAsync();
}
}
2019-05-29 15:08:47 +02:00
}
2019-05-29 15:36:57 +02:00
public bool DisableFavicon
{
get => _disableFavicon;
set
{
if(SetProperty(ref _disableFavicon, value))
{
var task = UpdateDisableFaviconAsync();
}
}
}
public bool DisableAutoTotpCopy
{
get => _disableAutoTotpCopy;
set
{
if(SetProperty(ref _disableAutoTotpCopy, value))
{
var task = UpdateAutoTotpCopyAsync();
}
}
}
public async Task InitAsync()
{
DisableAutoTotpCopy = !(await _totpService.IsAutoCopyEnabledAsync());
DisableFavicon = await _storageService.GetAsync<bool>(Constants.DisableFaviconKey);
2019-05-29 20:11:15 +02:00
var theme = await _storageService.GetAsync<string>(Constants.ThemeKey);
ThemeSelectedIndex = ThemeOptions.FindIndex(k => k.Key == theme);
var defaultUriMatch = await _storageService.GetAsync<int?>(Constants.DefaultUriMatch);
UriMatchSelectedIndex = defaultUriMatch == null ? 0 :
UriMatchOptions.FindIndex(k => (int?)k.Key == defaultUriMatch);
var clearClipboard = await _storageService.GetAsync<int?>(Constants.ClearClipboardKey);
ClearClipboardSelectedIndex = ClearClipboardOptions.FindIndex(k => k.Key == clearClipboard);
2019-05-29 20:23:55 +02:00
_inited = true;
2019-05-29 15:36:57 +02:00
}
private async Task UpdateAutoTotpCopyAsync()
{
2019-05-29 20:23:55 +02:00
if(_inited)
{
await _storageService.SaveAsync(Constants.DisableAutoTotpCopyKey, DisableAutoTotpCopy);
}
2019-05-29 15:36:57 +02:00
}
private async Task UpdateDisableFaviconAsync()
{
2019-05-29 20:23:55 +02:00
if(_inited)
{
await _storageService.SaveAsync(Constants.DisableFaviconKey, DisableFavicon);
await _stateService.SaveAsync(Constants.DisableFaviconKey, DisableFavicon);
}
2019-05-29 15:36:57 +02:00
}
2019-05-29 20:11:15 +02:00
private async Task SaveClipboardChangedAsync()
{
2019-05-29 20:23:55 +02:00
if(_inited && ClearClipboardSelectedIndex > -1)
2019-05-29 20:11:15 +02:00
{
await _storageService.SaveAsync(Constants.ClearClipboardKey,
ClearClipboardOptions[ClearClipboardSelectedIndex].Key);
}
}
private async Task SaveThemeAsync()
{
2019-05-29 20:23:55 +02:00
if(_inited && ThemeSelectedIndex > -1)
2019-05-29 20:11:15 +02:00
{
2019-05-29 20:23:55 +02:00
var theme = ThemeOptions[ThemeSelectedIndex].Key;
await _storageService.SaveAsync(Constants.ThemeKey, theme);
2019-05-29 21:50:20 +02:00
if(Device.RuntimePlatform == Device.Android)
{
2019-05-30 05:02:30 +02:00
await _deviceActionService.ShowLoadingAsync(AppResources.Restarting);
await Task.Delay(1000);
2019-05-29 21:50:20 +02:00
}
_messagingService.Send("updatedTheme", theme);
2019-05-29 20:11:15 +02:00
}
}
private async Task SaveDefaultUriAsync()
{
2019-05-29 20:23:55 +02:00
if(_inited && UriMatchSelectedIndex > -1)
2019-05-29 20:11:15 +02:00
{
2019-05-30 14:45:39 +02:00
await _storageService.SaveAsync(Constants.DefaultUriMatch,
(int?)UriMatchOptions[UriMatchSelectedIndex].Key);
2019-05-29 20:11:15 +02:00
}
}
2019-05-29 15:08:47 +02:00
}
}