borepin/Borepin/Borepin/PageModel/StartPageModel.cs
2022-05-23 18:11:05 +02:00

100 lines
3.6 KiB
C#

using Borepin.Base;
using Borepin.Service;
using Borepin.Service.Storage;
using FabAccessAPI;
using Prism.AppModel;
using Prism.Navigation;
using Prism.Services;
using System.Collections.Generic;
using Xamarin.Forms;
namespace Borepin.PageModel
{
public class StartPageModel : ConnectionModelBase, IPageLifecycleAware
{
#region Private Fields
private readonly ILoginStorageService _LoginStorageService;
#endregion
#region Constructors
public StartPageModel(INavigationService navigationService, IPageDialogService pageDialogService, IAPIService apiService, ILoginStorageService loginStorageService) : base(navigationService, pageDialogService, apiService)
{
_LoginStorageService = loginStorageService;
}
#endregion
#region Fields
private bool _IsConnecting;
public bool IsConnecting
{
get => _IsConnecting;
set => SetProperty(ref _IsConnecting, value);
}
#endregion
#region IPageLifecycleAware
public async void OnAppearing()
{
IList<ConnectionData> connectionData_List = await _LoginStorageService.GetList().ConfigureAwait(false);
ConnectionData connectionData_Default = await _LoginStorageService.GetDefault().ConfigureAwait(false);
if (connectionData_List.Count == 0)
{
Device.BeginInvokeOnMainThread(async () =>
{
INavigationResult result = await _NavigationService.NavigateAsync("/MainPage/NavigationPage/SetUpProcess_WelcomePage").ConfigureAwait(false);
if (result.Exception != null)
{
Log.Fatal(result.Exception, "Navigating failed");
}
});
}
else if(connectionData_Default != null)
{
IsBusy = false;
IsConnecting = true;
try
{
await _API.Connect(connectionData_Default).ConfigureAwait(false);
Device.BeginInvokeOnMainThread(async () =>
{
INavigationResult result = await _NavigationService.NavigateAsync("/MainPage/NavigationPage/MachineListPage").ConfigureAwait(false);
if (result.Exception != null)
{
Log.Fatal(result.Exception, "Navigating failed");
}
});
}
catch
{
Device.BeginInvokeOnMainThread(async () =>
{
INavigationResult result = await _NavigationService.NavigateAsync("/MainPage/NavigationPage/ServerListPage").ConfigureAwait(false);
if (result.Exception != null)
{
Log.Fatal(result.Exception, "Navigating failed");
}
});
}
}
else
{
Device.BeginInvokeOnMainThread(async () =>
{
INavigationResult result = await _NavigationService.NavigateAsync("/MainPage/NavigationPage/ServerListPage").ConfigureAwait(false);
if (result.Exception != null)
{
Log.Fatal(result.Exception, "Navigating failed");
}
});
}
}
public void OnDisappearing()
{
}
#endregion
}
}