yavsc/BookAStar/BookAStar/App.xaml.cs

338 lines
11 KiB
C#

using System;
using Xamarin.Forms;
using XLabs.Forms.Mvvm;
using XLabs.Forms.Pages;
using XLabs.Forms.Services;
using XLabs.Ioc;
using XLabs.Platform.Mvvm;
using XLabs.Platform.Services;
using XLabs.Settings;
using XLabs;
using XLabs.Enums;
namespace BookAStar
{
using System.Threading.Tasks;
using Data;
using Interfaces;
using Model;
using Model.UI;
using Pages;
using Plugin.Connectivity;
using ViewModels;
using Microsoft.AspNet.SignalR.Client;
using Model.Social.Messaging;
public partial class App : Application // superclass new in 1.3
{
public static IPlatform PlatformSpecificInstance { get; set; }
public static string AppName { get; set; }
[Obsolete("Instead using this, use new static properties.")]
public static App CurrentApp { get { return Current as App; } }
public static bool MasterPresented
{
get
{ return CurrentApp.masterDetail.IsPresented; }
internal set
{ CurrentApp.masterDetail.IsPresented = value; }
}
public void Init()
{
var app = Resolver.Resolve<IXFormsApp>();
if (app == null)
{
return;
}
Configure(app);
app.Closing += OnClosing;
app.Error += OnError;
app.Initialize += OnInitialize;
app.Resumed += OnAppResumed;
app.Rotation += OnRotation;
app.Startup += OnStartup;
app.Suspended += OnSuspended;
MainSettings.UserChanged += MainSettings_UserChanged;
CrossConnectivity.Current.ConnectivityChanged += (conSender, args) =>
{
App.IsConnected = args.IsConnected;
};
SetupHubConnection();
if (CrossConnectivity.Current.IsConnected)
StartHubConnection();
}
// omg
private void OnError(object sender, EventArgs e)
{
}
// Called on rotation after OnSuspended
private void OnClosing(object sender, EventArgs e)
{
}
// FIXME Never called.
private void OnInitialize(object sender, EventArgs e)
{
}
// called on app startup, not on rotation
private void OnStartup(object sender, EventArgs e)
{
// TODO special startup pages as
// notification details or wizard setup page
}
// Called on rotation
private void OnSuspended(object sender, EventArgs e)
{
// TODO save the navigation stack
int position = 0;
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();
}
// called on app startup, after OnStartup, not on rotation
private void OnAppResumed(object sender, EventArgs e)
{
// TODO restore the navigation stack
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();
}
// FIXME Not called?
private void OnRotation(object sender, EventArgs<Orientation> e)
{
}
public static GenericConfigSettingsMgr ConfigManager { protected set; get; }
private void Configure(IXFormsApp app)
{
ViewFactory.EnableCache = true;
ViewFactory.Register<ChatPage, ChatViewModel>(
r=> new ChatViewModel { ChatUser = MainSettings.UserName }
);
ViewFactory.Register<DashboardPage, DashboardViewModel>(
resolver => new DashboardViewModel());
ViewFactory.Register<BookQueryPage, BookQueryViewModel>();
ViewFactory.Register<BookQueriesPage, BookQueriesViewModel>();
ViewFactory.Register<EditBillingLinePage, BillingLineViewModel>();
ViewFactory.Register<EditEstimatePage, EditEstimateViewModel>();
ConfigManager = new XLabs.Settings.GenericConfigSettingsMgr(s =>
MainSettings.AppSettings.GetValueOrDefault<string>(s, MainSettings.SettingsDefault), null);
}
ExtendedMasterDetailPage masterDetail;
public App(IPlatform instance)
{
// 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`
PlatformSpecificInstance = instance;
// Xaml
InitializeComponent();
// Static properties construction
Init();
// Builds the Main page
BuildMainPage();
}
BookQueriesPage bQueriesPage;
AccountChooserPage accChooserPage;
HomePage home;
private void BuildMainPage()
{
accChooserPage = new AccountChooserPage();
bQueriesPage = new BookQueriesPage
{
Title = "Demandes",
Icon = "icon.png",
BindingContext = new BookQueriesViewModel()
};
home = new HomePage() { Title = "Accueil", Icon = "icon.png" };
// var mainPage = new NavigationPage(bQueriesPage);
masterDetail = new ExtendedMasterDetailPage()
{
Title = "MainPage"
};
masterDetail.Master = new DashboardPage
{
Title = "Bookingstar",
BindingContext = new DashboardViewModel()
};
// masterDetail.Detail = home;
masterDetail.Detail = new NavigationPage(home);
ToolbarItem tiSetts = new ToolbarItem()
{
Text = "Paramètres",
Icon = "ic_corp_icon.png",
Command = new Command(
() => { NavigationService.NavigateTo<AccountChooserPage>(); }
)
};
ToolbarItem tiHome = new ToolbarItem()
{
Text = "Accueil",
Icon = "icon.png"
};
ToolbarItem tiPubChat= new ToolbarItem()
{
Text = "Chat",
Icon = "chat_icon_s.png"
};
tiPubChat.Clicked += TiPubChat_Clicked;
masterDetail.ToolbarItems.Add(tiHome);
masterDetail.ToolbarItems.Add(tiSetts);
masterDetail.ToolbarItems.Add(tiPubChat);
this.MainPage = masterDetail;
NavigationService = new NavigationService(masterDetail.Detail.Navigation);
}
private void TiPubChat_Clicked(object sender, EventArgs e)
{
NavigationService.NavigateTo<ChatPage>();
}
public static INavigationService NavigationService { protected set; get; }
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
CurrentApp.StartHubConnection();
}
}
}
}
private static HubConnection chatHubConnection = null;
public static HubConnection ChatHubConnection
{
get
{
return chatHubConnection;
}
}
// Start the Hub connection
public async void StartHubConnection ()
{
await chatHubConnection.Start();
}
public void SetupHubConnection()
{
chatHubConnection = new HubConnection(Constants.SignalRHubsUrl);
chatHubConnection.Error += ChatHubConnection_Error;
chatHubProxy = chatHubConnection.CreateHubProxy("ChatHub");
chatHubProxy.On<string, string>("addPV", (n, m) => {
DataManager.Current.PrivateMessages.Add(
new ChatMessage
{
Message = m,
SenderId = n,
Date = DateTime.Now
}
);
});
MainSettings_UserChanged(this, null);
}
private void MainSettings_UserChanged(object sender, EventArgs e)
{
if (MainSettings.CurrentUser == null)
{
chatHubConnection.Headers.Clear();
}
else
{
var token = MainSettings.CurrentUser.YavscTokens.AccessToken;
chatHubConnection.Headers.Add(
"Authorization", $"Bearer {token}");
}
}
private void ChatHubConnection_Error(Exception obj)
{
// TODO log in debug binaries
}
private static IHubProxy chatHubProxy = null;
public static IHubProxy ChatHubProxy
{
get
{
return chatHubProxy;
}
}
public static Task<bool> HasSomeCloud {
get
{
return CrossConnectivity.Current.IsReachable(Constants.YavscHomeUrl, Constants.CloudTimeout);
}
}
public void PostDeviceInfo()
{
var info = PlatformSpecificInstance.GetDeviceInfo();
if (!string.IsNullOrWhiteSpace(info.GCMRegistrationId))
PlatformSpecificInstance.InvokeApi("gcm/register", info);
}
public static void ShowBookQuery (BookQueryData query)
{
var page = ViewFactory.CreatePage<BookQueryViewModel
, BookQueryPage>((b, p) => p.BindingContext = new BookQueryViewModel(query));
App.Current.MainPage.Navigation.PushAsync(page as Page);
}
}
}