1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-06-26 10:36:21 +02:00
bitwarden-mobile/src/App/Pages/BaseContentPage.cs

142 lines
4.1 KiB
C#
Raw Normal View History

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