2019-05-16 19:30:13 +03:00
|
|
|
|
using Bit.Core;
|
|
|
|
|
using Bit.Core.Abstractions;
|
|
|
|
|
using Bit.Core.Utilities;
|
|
|
|
|
using System;
|
2019-05-01 17:11:49 +03:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Xamarin.Forms;
|
|
|
|
|
|
|
|
|
|
namespace Bit.App.Pages
|
|
|
|
|
{
|
|
|
|
|
public class BaseContentPage : ContentPage
|
|
|
|
|
{
|
2019-05-16 19:30:13 +03:00
|
|
|
|
private IStorageService _storageService;
|
|
|
|
|
|
2019-05-01 22:29:57 +03:00
|
|
|
|
protected int AndroidShowModalAnimationDelay = 400;
|
2019-05-02 19:46:08 +03:00
|
|
|
|
protected int AndroidShowPageAnimationDelay = 100;
|
2019-05-01 22:29:57 +03:00
|
|
|
|
|
2019-05-07 05:49:57 +03:00
|
|
|
|
public DateTime? LastPageAction { get; set; }
|
|
|
|
|
|
2019-05-16 19:30:13 +03:00
|
|
|
|
protected override void OnAppearing()
|
|
|
|
|
{
|
|
|
|
|
base.OnAppearing();
|
|
|
|
|
SaveActivity();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnDisappearing()
|
|
|
|
|
{
|
|
|
|
|
base.OnDisappearing();
|
|
|
|
|
SaveActivity();
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 06:07:47 +03:00
|
|
|
|
public bool DoOnce(Action action = null, int milliseconds = 1000)
|
|
|
|
|
{
|
|
|
|
|
if(LastPageAction.HasValue && (DateTime.UtcNow - LastPageAction.Value).TotalMilliseconds < milliseconds)
|
|
|
|
|
{
|
|
|
|
|
// Last action occurred recently.
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
LastPageAction = DateTime.UtcNow;
|
|
|
|
|
action?.Invoke();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
|
|
|
|
HorizontalOptions = LayoutOptions.Center
|
|
|
|
|
};
|
2019-05-08 15:33:17 +03:00
|
|
|
|
if(targetView != null)
|
|
|
|
|
{
|
|
|
|
|
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();
|
2019-05-08 15:33:17 +03:00
|
|
|
|
if(sourceView != null)
|
2019-05-01 17:11:49 +03:00
|
|
|
|
{
|
2019-05-08 15:33:17 +03:00
|
|
|
|
if(targetView != null)
|
|
|
|
|
{
|
|
|
|
|
targetView.Content = sourceView;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Content = sourceView;
|
|
|
|
|
}
|
2019-05-01 17:11:49 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-02 19:46:08 +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-05-02 19:46:08 +03:00
|
|
|
|
await Task.Delay(fromModal ? AndroidShowModalAnimationDelay : AndroidShowPageAnimationDelay);
|
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
|
|
|
|
{
|
|
|
|
|
if(Device.RuntimePlatform == Device.iOS)
|
|
|
|
|
{
|
2019-05-07 05:35:42 +03:00
|
|
|
|
input.Focus();
|
2019-05-01 22:29:57 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(AndroidShowModalAnimationDelay);
|
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
|
|
|
|
|
|
|
|
|
private void SetStorageService()
|
|
|
|
|
{
|
|
|
|
|
if(_storageService == null)
|
|
|
|
|
{
|
|
|
|
|
_storageService = ServiceContainer.Resolve<IStorageService>("storageService");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SaveActivity()
|
|
|
|
|
{
|
|
|
|
|
SetStorageService();
|
|
|
|
|
_storageService.SaveAsync(Constants.LastActiveKey, DateTime.UtcNow);
|
|
|
|
|
}
|
2019-05-01 17:11:49 +03:00
|
|
|
|
}
|
|
|
|
|
}
|