2022-02-23 20:40:17 +03:00
|
|
|
|
using System;
|
2019-05-01 17:11:49 +03:00
|
|
|
|
using System.Threading.Tasks;
|
2020-12-14 23:29:30 +03:00
|
|
|
|
using Bit.App.Abstractions;
|
2022-02-23 20:40:17 +03:00
|
|
|
|
using Bit.App.Controls;
|
2021-10-08 15:47:40 +03:00
|
|
|
|
using Bit.App.Utilities;
|
2022-02-23 20:40:17 +03:00
|
|
|
|
using Bit.Core.Abstractions;
|
|
|
|
|
using Bit.Core.Utilities;
|
2019-05-01 17:11:49 +03:00
|
|
|
|
using Xamarin.Forms;
|
2019-10-01 03:38:22 +03:00
|
|
|
|
using Xamarin.Forms.PlatformConfiguration;
|
|
|
|
|
using Xamarin.Forms.PlatformConfiguration.iOSSpecific;
|
2019-05-01 17:11:49 +03:00
|
|
|
|
|
|
|
|
|
namespace Bit.App.Pages
|
|
|
|
|
{
|
|
|
|
|
public class BaseContentPage : ContentPage
|
|
|
|
|
{
|
2022-02-23 20:40:17 +03:00
|
|
|
|
private IStateService _stateService;
|
2020-12-14 23:29:30 +03:00
|
|
|
|
private IDeviceActionService _deviceActionService;
|
2019-05-16 19:30:13 +03:00
|
|
|
|
|
2019-07-08 19:06:37 +03:00
|
|
|
|
protected int ShowModalAnimationDelay = 400;
|
|
|
|
|
protected int ShowPageAnimationDelay = 100;
|
2019-05-01 22:29:57 +03:00
|
|
|
|
|
2019-10-01 03:38:22 +03:00
|
|
|
|
public BaseContentPage()
|
|
|
|
|
{
|
|
|
|
|
if (Device.RuntimePlatform == Device.iOS)
|
|
|
|
|
{
|
2021-10-22 17:06:17 +03:00
|
|
|
|
On<iOS>().SetUseSafeArea(true);
|
2019-10-01 03:38:22 +03:00
|
|
|
|
On<iOS>().SetModalPresentationStyle(UIModalPresentationStyle.FullScreen);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 05:49:57 +03:00
|
|
|
|
public DateTime? LastPageAction { get; set; }
|
|
|
|
|
|
2022-01-24 23:20:48 +03:00
|
|
|
|
public bool IsThemeDirty { get; set; }
|
|
|
|
|
|
2022-02-23 20:40:17 +03:00
|
|
|
|
protected async override void OnAppearing()
|
2019-05-16 19:30:13 +03:00
|
|
|
|
{
|
|
|
|
|
base.OnAppearing();
|
2022-04-26 18:21:17 +03:00
|
|
|
|
|
2022-01-24 23:20:48 +03:00
|
|
|
|
if (IsThemeDirty)
|
|
|
|
|
{
|
|
|
|
|
UpdateOnThemeChanged();
|
|
|
|
|
}
|
2022-04-26 18:21:17 +03:00
|
|
|
|
|
2022-02-23 20:40:17 +03:00
|
|
|
|
await SaveActivityAsync();
|
2019-05-16 19:30:13 +03:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 06:07:47 +03:00
|
|
|
|
public bool DoOnce(Action action = null, int milliseconds = 1000)
|
|
|
|
|
{
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (LastPageAction.HasValue && (DateTime.UtcNow - LastPageAction.Value).TotalMilliseconds < milliseconds)
|
2019-05-07 06:07:47 +03:00
|
|
|
|
{
|
|
|
|
|
// Last action occurred recently.
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
LastPageAction = DateTime.UtcNow;
|
|
|
|
|
action?.Invoke();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 20:40:17 +03:00
|
|
|
|
public virtual Task UpdateOnThemeChanged()
|
|
|
|
|
{
|
|
|
|
|
IsThemeDirty = false;
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 15:33:17 +03:00
|
|
|
|
protected void SetActivityIndicator(ContentView targetView = null)
|
2019-05-01 17:11:49 +03:00
|
|
|
|
{
|
2019-05-08 15:33:17 +03:00
|
|
|
|
var indicator = new ActivityIndicator
|
2019-05-01 17:11:49 +03:00
|
|
|
|
{
|
|
|
|
|
IsRunning = true,
|
|
|
|
|
VerticalOptions = LayoutOptions.CenterAndExpand,
|
2021-10-08 15:47:40 +03:00
|
|
|
|
HorizontalOptions = LayoutOptions.Center,
|
|
|
|
|
Color = ThemeManager.GetResourceColor("PrimaryColor"),
|
2019-05-01 17:11:49 +03:00
|
|
|
|
};
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (targetView != null)
|
2019-05-08 15:33:17 +03:00
|
|
|
|
{
|
|
|
|
|
targetView.Content = indicator;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Content = indicator;
|
|
|
|
|
}
|
2019-05-01 17:11:49 +03:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 15:33:17 +03:00
|
|
|
|
protected async Task LoadOnAppearedAsync(View sourceView, bool fromModal, Func<Task> workFunction,
|
|
|
|
|
ContentView targetView = null)
|
2019-05-01 17:11:49 +03:00
|
|
|
|
{
|
2019-05-01 17:20:05 +03:00
|
|
|
|
async Task DoWorkAsync()
|
2019-05-01 17:11:49 +03:00
|
|
|
|
{
|
|
|
|
|
await workFunction.Invoke();
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (sourceView != null)
|
2019-05-01 17:11:49 +03:00
|
|
|
|
{
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (targetView != null)
|
2019-05-08 15:33:17 +03:00
|
|
|
|
{
|
|
|
|
|
targetView.Content = sourceView;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Content = sourceView;
|
|
|
|
|
}
|
2019-05-01 17:11:49 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (Device.RuntimePlatform == Device.iOS)
|
2019-05-01 17:11:49 +03:00
|
|
|
|
{
|
2019-05-01 17:20:05 +03:00
|
|
|
|
await DoWorkAsync();
|
2019-05-01 17:11:49 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
await Task.Run(async () =>
|
|
|
|
|
{
|
2019-07-08 19:06:37 +03:00
|
|
|
|
await Task.Delay(fromModal ? ShowModalAnimationDelay : ShowPageAnimationDelay);
|
2019-05-01 17:20:05 +03:00
|
|
|
|
Device.BeginInvokeOnMainThread(async () => await DoWorkAsync());
|
2019-05-01 17:11:49 +03:00
|
|
|
|
});
|
|
|
|
|
}
|
2019-05-01 22:29:57 +03:00
|
|
|
|
|
2019-05-07 05:35:42 +03:00
|
|
|
|
protected void RequestFocus(InputView input)
|
2019-05-01 22:29:57 +03:00
|
|
|
|
{
|
|
|
|
|
Task.Run(async () =>
|
|
|
|
|
{
|
2019-07-08 19:06:37 +03:00
|
|
|
|
await Task.Delay(ShowModalAnimationDelay);
|
2019-05-07 05:35:42 +03:00
|
|
|
|
Device.BeginInvokeOnMainThread(() => input.Focus());
|
2019-05-01 22:29:57 +03:00
|
|
|
|
});
|
|
|
|
|
}
|
2019-05-16 19:30:13 +03:00
|
|
|
|
|
2022-02-23 20:40:17 +03:00
|
|
|
|
protected async Task<bool> ShowAccountSwitcherAsync()
|
|
|
|
|
{
|
|
|
|
|
return await _stateService.GetActiveUserIdAsync() != null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async Task<AvatarImageSource> GetAvatarImageSourceAsync(bool useCurrentActiveAccount = true)
|
|
|
|
|
{
|
|
|
|
|
if (useCurrentActiveAccount)
|
|
|
|
|
{
|
|
|
|
|
return new AvatarImageSource(await _stateService.GetNameAsync(), await _stateService.GetEmailAsync());
|
|
|
|
|
}
|
|
|
|
|
return new AvatarImageSource();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 23:29:30 +03:00
|
|
|
|
private void SetServices()
|
2019-05-16 19:30:13 +03:00
|
|
|
|
{
|
2022-02-23 20:40:17 +03:00
|
|
|
|
if (_stateService == null)
|
2019-05-16 19:30:13 +03:00
|
|
|
|
{
|
2022-02-23 20:40:17 +03:00
|
|
|
|
_stateService = ServiceContainer.Resolve<IStateService>("stateService");
|
2019-05-16 19:30:13 +03:00
|
|
|
|
}
|
2020-12-14 23:29:30 +03:00
|
|
|
|
if (_deviceActionService == null)
|
|
|
|
|
{
|
|
|
|
|
_deviceActionService = ServiceContainer.Resolve<IDeviceActionService>("deviceActionService");
|
|
|
|
|
}
|
2019-05-16 19:30:13 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 20:40:17 +03:00
|
|
|
|
private async Task SaveActivityAsync()
|
2019-05-16 19:30:13 +03:00
|
|
|
|
{
|
2020-12-14 23:29:30 +03:00
|
|
|
|
SetServices();
|
2022-02-23 20:40:17 +03:00
|
|
|
|
await _stateService.SetLastActiveTimeAsync(_deviceActionService.GetActiveTime());
|
2022-01-24 23:20:48 +03:00
|
|
|
|
}
|
2019-05-01 17:11:49 +03:00
|
|
|
|
}
|
|
|
|
|
}
|