yavsc/BookAStar/BookAStar/App.xaml.cs

337 lines
11 KiB
C#
Raw Normal View History

2016-10-14 16:21:03 +02:00
using System;
using Xamarin.Forms;
2016-09-27 02:35:45 +02:00
using XLabs.Forms.Mvvm;
2016-09-29 04:54:14 +02:00
using XLabs.Forms.Pages;
2016-09-27 02:35:45 +02:00
using XLabs.Forms.Services;
using XLabs.Ioc;
using XLabs.Platform.Mvvm;
using XLabs.Platform.Services;
2016-10-03 18:32:37 +02:00
using XLabs.Settings;
2016-10-05 01:20:06 +02:00
using XLabs;
using XLabs.Enums;
2016-10-02 19:27:32 +02:00
namespace BookAStar
{
2016-10-25 23:20:26 +02:00
using System.Threading.Tasks;
using Data;
2016-10-14 16:21:03 +02:00
using Interfaces;
using Model;
using Model.UI;
2016-10-14 16:21:03 +02:00
using Pages;
2016-10-25 23:20:26 +02:00
using Plugin.Connectivity;
2016-10-14 16:21:03 +02:00
using ViewModels;
2016-10-25 23:20:26 +02:00
using Microsoft.AspNet.SignalR.Client;
2016-10-26 20:29:46 +02:00
using Model.Social.Messaging;
public partial class App : Application // superclass new in 1.3
{
2016-09-21 04:02:40 +02:00
public static IPlatform PlatformSpecificInstance { get; set; }
public static string AppName { get; set; }
2016-09-29 04:54:14 +02:00
2016-10-04 02:46:24 +02:00
2016-10-29 02:49:45 +02:00
[Obsolete("Instead using this, use new static properties.")]
public static App CurrentApp { get { return Current as App; } }
2016-10-14 16:21:03 +02:00
2016-10-04 02:46:24 +02:00
public static bool MasterPresented
{
2016-10-02 19:27:32 +02:00
get
{ return CurrentApp.masterDetail.IsPresented; }
internal set
{ CurrentApp.masterDetail.IsPresented = value; }
}
public void Init()
2016-09-25 00:44:30 +02:00
{
2016-09-27 02:35:45 +02:00
var app = Resolver.Resolve<IXFormsApp>();
2016-10-04 02:46:24 +02:00
2016-09-27 02:35:45 +02:00
if (app == null)
2016-09-25 00:44:30 +02:00
{
2016-09-27 02:35:45 +02:00
return;
}
Configure(app);
2016-10-05 01:20:06 +02:00
app.Closing += OnClosing;
app.Error += OnError;
app.Initialize += OnInitialize;
app.Resumed += OnAppResumed;
app.Rotation += OnRotation;
app.Startup += OnStartup;
app.Suspended += OnSuspended;
2016-10-26 20:29:46 +02:00
MainSettings.UserChanged += MainSettings_UserChanged;
CrossConnectivity.Current.ConnectivityChanged += (conSender, args) =>
{
App.IsConnected = args.IsConnected;
};
SetupHubConnection();
if (CrossConnectivity.Current.IsConnected)
StartHubConnection();
2016-09-25 00:44:30 +02:00
}
2016-10-05 01:20:06 +02:00
// omg
private void OnError(object sender, EventArgs e)
{
}
// Called on rotation after OnSuspended
private void OnClosing(object sender, EventArgs e)
{
}
2016-10-26 20:29:46 +02:00
// FIXME Never called.
2016-10-05 01:20:06 +02:00
private void OnInitialize(object sender, EventArgs e)
{
2016-10-26 20:29:46 +02:00
2016-10-05 01:20:06 +02:00
}
// called on app startup, not on rotation
private void OnStartup(object sender, EventArgs e)
{
// TODO special startup pages as
2016-10-05 01:20:06 +02:00
// notification details or wizard setup page
}
// Called on rotation
private void OnSuspended(object sender, EventArgs e)
{
2016-10-14 16:21:03 +02:00
// TODO save the navigation stack
int position = 0;
2016-10-18 15:51:56 +02:00
foreach (Page page in MainPage.Navigation.NavigationStack)
{
DataManager.Current.AppState.Add(
new PageState
{
Position = position++,
PageType = page.GetType().FullName,
BindingContext = page.BindingContext
});
}
DataManager.Current.AppState.SaveCollection();
2016-10-05 01:20:06 +02:00
}
// called on app startup, after OnStartup, not on rotation
private void OnAppResumed(object sender, EventArgs e)
{
2016-10-14 16:21:03 +02:00
// TODO restore the navigation stack
2016-10-05 01:20:06 +02:00
base.OnResume();
foreach (var pageState in DataManager.Current.AppState)
{
var pageType = Type.GetType(pageState.PageType);
NavigationService.NavigateTo(
pageType, true, pageState.BindingContext);
}
DataManager.Current.AppState.Clear();
DataManager.Current.AppState.SaveCollection();
2016-10-05 01:20:06 +02:00
}
2016-10-14 16:21:03 +02:00
// FIXME Not called?
2016-10-05 01:20:06 +02:00
private void OnRotation(object sender, EventArgs<Orientation> e)
{
2016-10-14 16:21:03 +02:00
2016-10-05 01:20:06 +02:00
}
2016-10-03 18:32:37 +02:00
public static GenericConfigSettingsMgr ConfigManager { protected set; get; }
private void Configure(IXFormsApp app)
2016-09-29 04:54:14 +02:00
{
ViewFactory.EnableCache = true;
2016-10-10 12:58:14 +02:00
ViewFactory.Register<ChatPage, ChatViewModel>(
2016-10-30 02:47:58 +02:00
r=> new ChatViewModel { ChatUser = MainSettings.UserName }
2016-10-10 12:58:14 +02:00
);
2016-09-29 04:54:14 +02:00
ViewFactory.Register<DashboardPage, DashboardViewModel>(
resolver => new DashboardViewModel());
ViewFactory.Register<BookQueryPage, BookQueryViewModel>();
2016-10-02 19:27:32 +02:00
ViewFactory.Register<BookQueriesPage, BookQueriesViewModel>();
2016-09-29 04:54:14 +02:00
ViewFactory.Register<EditBillingLinePage, BillingLineViewModel>();
2016-10-11 15:19:51 +02:00
ViewFactory.Register<EditEstimatePage, EditEstimateViewModel>();
2016-10-03 18:32:37 +02:00
ConfigManager = new XLabs.Settings.GenericConfigSettingsMgr(s =>
2016-10-04 02:46:24 +02:00
MainSettings.AppSettings.GetValueOrDefault<string>(s, MainSettings.SettingsDefault), null);
2016-10-03 18:32:37 +02:00
}
2016-10-04 02:46:24 +02:00
2016-09-29 04:54:14 +02:00
ExtendedMasterDetailPage masterDetail;
2016-09-25 00:44:30 +02:00
2016-09-18 00:11:35 +02:00
public App(IPlatform instance)
{
2016-10-05 01:20:06 +02:00
// This declaration became obsolete by introduction
// of the XLabs App that
// refers this instance with
// its application context property
// and is obtained using the `Resolver`
2016-09-27 02:35:45 +02:00
PlatformSpecificInstance = instance;
2016-10-05 01:20:06 +02:00
// Xaml
2016-09-25 00:44:30 +02:00
InitializeComponent();
2016-10-05 01:20:06 +02:00
// Static properties construction
2016-09-27 02:35:45 +02:00
Init();
2016-10-05 01:20:06 +02:00
// Builds the Main page
2016-09-29 04:54:14 +02:00
BuildMainPage();
2016-10-05 01:20:06 +02:00
2016-09-27 02:35:45 +02:00
}
2016-10-03 18:32:37 +02:00
2016-09-29 04:54:14 +02:00
BookQueriesPage bQueriesPage;
AccountChooserPage accChooserPage;
HomePage home;
2016-09-25 00:44:30 +02:00
2016-09-29 04:54:14 +02:00
private void BuildMainPage()
2016-09-27 02:35:45 +02:00
{
2016-09-29 04:54:14 +02:00
accChooserPage = new AccountChooserPage();
bQueriesPage = new BookQueriesPage
2016-09-25 00:44:30 +02:00
{
Title = "Demandes",
2016-10-02 19:27:32 +02:00
Icon = "icon.png",
BindingContext = new BookQueriesViewModel()
2016-09-25 00:44:30 +02:00
};
2016-09-27 02:35:45 +02:00
home = new HomePage() { Title = "Accueil", Icon = "icon.png" };
2016-09-27 02:35:45 +02:00
2016-09-29 04:54:14 +02:00
// var mainPage = new NavigationPage(bQueriesPage);
2016-10-04 02:46:24 +02:00
masterDetail = new ExtendedMasterDetailPage()
{
Title = "MainPage"
2016-09-27 02:35:45 +02:00
};
2016-10-04 02:46:24 +02:00
masterDetail.Master = new DashboardPage
{
2016-09-29 04:54:14 +02:00
Title = "Bookingstar",
2016-10-04 02:46:24 +02:00
BindingContext = new DashboardViewModel()
};
2016-09-27 02:35:45 +02:00
2016-10-02 19:27:32 +02:00
// masterDetail.Detail = home;
2016-10-02 19:27:32 +02:00
masterDetail.Detail = new NavigationPage(home);
2016-09-18 00:11:35 +02:00
ToolbarItem tiSetts = new ToolbarItem()
{
2016-09-27 02:35:45 +02:00
Text = "Paramètres",
2016-10-07 23:13:34 +02:00
Icon = "ic_corp_icon.png",
Command = new Command(
() => { NavigationService.NavigateTo<AccountChooserPage>(); }
)
};
2016-09-18 00:11:35 +02:00
2016-09-27 02:35:45 +02:00
ToolbarItem tiHome = new ToolbarItem()
2016-09-18 00:11:35 +02:00
{
2016-09-27 02:35:45 +02:00
Text = "Accueil",
Icon = "icon.png"
2016-09-18 00:11:35 +02:00
};
2016-10-10 12:58:14 +02:00
ToolbarItem tiPubChat= new ToolbarItem()
{
Text = "Chat",
Icon = "chat_icon_s.png"
};
tiPubChat.Clicked += TiPubChat_Clicked;
2016-10-05 18:45:45 +02:00
masterDetail.ToolbarItems.Add(tiHome);
masterDetail.ToolbarItems.Add(tiSetts);
2016-10-10 12:58:14 +02:00
masterDetail.ToolbarItems.Add(tiPubChat);
this.MainPage = masterDetail;
2016-10-05 18:45:45 +02:00
NavigationService = new NavigationService(masterDetail.Detail.Navigation);
2016-09-27 02:35:45 +02:00
}
2016-10-10 12:58:14 +02:00
private void TiPubChat_Clicked(object sender, EventArgs e)
{
NavigationService.NavigateTo<ChatPage>();
}
public static INavigationService NavigationService { protected set; get; }
2016-10-25 23:20:26 +02:00
public static bool isConnected;
public static bool IsConnected { get { return isConnected; }
private set
{
if (isConnected != value)
{
isConnected = value;
if (isConnected)
{
// TODO Start all cloud related stuff
2016-10-26 05:49:28 +02:00
CurrentApp.StartHubConnection();
2016-10-25 23:20:26 +02:00
}
2016-10-26 20:29:46 +02:00
2016-10-25 23:20:26 +02:00
}
}
}
2016-10-29 02:49:45 +02:00
private static HubConnection chatHubConnection = null;
public static HubConnection ChatHubConnection
2016-10-26 20:29:46 +02:00
{
get
{
return chatHubConnection;
}
}
2016-10-25 23:20:26 +02:00
// Start the Hub connection
2016-10-26 05:49:28 +02:00
public async void StartHubConnection ()
2016-10-25 23:20:26 +02:00
{
2016-10-26 20:29:46 +02:00
await chatHubConnection.Start();
}
public void SetupHubConnection()
{
2016-10-25 23:20:26 +02:00
chatHubConnection = new HubConnection(Constants.SignalRHubsUrl);
2016-10-26 20:29:46 +02:00
chatHubConnection.Error += ChatHubConnection_Error;
2016-10-25 23:20:26 +02:00
chatHubProxy = chatHubConnection.CreateHubProxy("ChatHub");
chatHubProxy.On<string, string>("addPV", (n, m) => {
2016-10-26 05:49:28 +02:00
DataManager.Current.PrivateMessages.Add(
2016-10-27 23:31:02 +02:00
new ChatMessage
2016-10-26 05:49:28 +02:00
{
Message = m,
SenderId = n,
Date = DateTime.Now
}
);
2016-10-25 23:20:26 +02:00
});
MainSettings_UserChanged(this, null);
2016-10-25 23:20:26 +02:00
}
2016-10-26 20:29:46 +02:00
private void MainSettings_UserChanged(object sender, EventArgs e)
{
if (MainSettings.CurrentUser == null)
2016-10-26 20:29:46 +02:00
{
chatHubConnection.Headers.Clear();
}
else
{
var token = MainSettings.CurrentUser.YavscTokens.AccessToken;
chatHubConnection.Headers.Add(
"Authorization", $"Bearer {token}");
2016-10-26 20:29:46 +02:00
}
}
private void ChatHubConnection_Error(Exception obj)
{
// TODO log in debug binaries
}
2016-10-30 02:47:58 +02:00
private static IHubProxy chatHubProxy = null;
public static IHubProxy ChatHubProxy
2016-10-26 20:29:46 +02:00
{
get
{
return chatHubProxy;
}
}
2016-10-25 23:20:26 +02:00
public static Task<bool> HasSomeCloud {
get
{
return CrossConnectivity.Current.IsReachable(Constants.YavscHomeUrl, Constants.CloudTimeout);
}
}
public void PostDeviceInfo()
{
2016-10-26 20:29:46 +02:00
var info = PlatformSpecificInstance.GetDeviceInfo();
if (!string.IsNullOrWhiteSpace(info.GCMRegistrationId))
PlatformSpecificInstance.InvokeApi("gcm/register", info);
}
2016-10-04 02:46:24 +02:00
2016-09-29 04:54:14 +02:00
public static void ShowBookQuery (BookQueryData query)
2016-09-21 04:02:40 +02:00
{
2016-10-04 02:46:24 +02:00
var page = ViewFactory.CreatePage<BookQueryViewModel
, BookQueryPage>((b, p) => p.BindingContext = new BookQueryViewModel(query));
App.Current.MainPage.Navigation.PushAsync(page as Page);
2016-09-21 04:02:40 +02:00
}
}
}