diff --git a/ZicMoove/ZicMoove/App.xaml.cs b/ZicMoove/ZicMoove/App.xaml.cs index 77fb061d..e4119172 100644 --- a/ZicMoove/ZicMoove/App.xaml.cs +++ b/ZicMoove/ZicMoove/App.xaml.cs @@ -36,6 +36,10 @@ namespace ZicMoove using Model.Auth; using Plugin.DeviceInfo; using Yavsc.Models.Identity; + using System.Json; + using Newtonsoft.Json; + using System.Net.Http; + using System.Text; public partial class App : Application // superclass new in 1.3 { @@ -196,7 +200,7 @@ namespace ZicMoove private static UserProfilePage userProfilePage; public static UserProfilePage UserProfilePage - { get { return userProfilePage; } } + { get { return userProfilePage; } } ChatPage chatPage; PinPage pinPage; @@ -231,15 +235,20 @@ namespace ZicMoove BindingContext = bookQueries }; - homePage = new HomePage() { + homePage = new HomePage() + { Title = "Accueil", - Icon = "icon.png" }; + Icon = "icon.png" + }; - homePage.BindingContext = new HomeViewModel { + homePage.BindingContext = new HomeViewModel + { BookQueries = bookQueries, - UserProfile = userprofile }; + UserProfile = userprofile + }; - userProfilePage = new UserProfilePage { + userProfilePage = new UserProfilePage + { Title = "Profile utilisateur", Icon = "ic_corp_icon.png", BindingContext = userprofile @@ -278,7 +287,7 @@ namespace ZicMoove () => { ShowPage(userProfilePage); - } ) + }) }; ToolbarItem tiHome = new ToolbarItem() @@ -286,12 +295,13 @@ namespace ZicMoove Text = "Accueil", Icon = "icon.png", Command = new Command( - () => { + () => + { ShowPage(homePage); }) }; - ToolbarItem tiPubChat= new ToolbarItem() + ToolbarItem tiPubChat = new ToolbarItem() { Text = "Chat", Icon = "chat_icon_s.png", @@ -311,10 +321,10 @@ namespace ZicMoove masterDetail.ToolbarItems.Add(tiSetts); masterDetail.ToolbarItems.Add(tiPubChat); this.MainPage = masterDetail; - + NavigationService = new NavigationService(Navigation); } - public static Task DisplayActionSheet(string title, string cancel, string destruction, string [] buttons) + public static Task DisplayActionSheet(string title, string cancel, string destruction, string[] buttons) { var currentPage = Navigation.NavigationStack.Last(); return currentPage.DisplayActionSheet(title, cancel, destruction, buttons); @@ -346,23 +356,23 @@ namespace ZicMoove } } // Start the Hub connection - public static async void StartConnexion () + public static async void StartConnexion() { if (CrossConnectivity.Current.IsConnected) try - { + { if (chatHubConnection.State == ConnectionState.Disconnected) - await chatHubConnection.Start(); - } - catch (WebException ) - { - // TODO use webex, set this cx down status somewhere, - // & display it & maybe try again later. - } - catch (Exception ) - { - // TODO use ex - } + await chatHubConnection.Start(); + } + catch (WebException) + { + // TODO use webex, set this cx down status somewhere, + // & display it & maybe try again later. + } + catch (Exception) + { + // TODO use ex + } } public void SetupHubConnection() @@ -373,7 +383,8 @@ namespace ZicMoove chatHubConnection.Error += ChatHubConnection_Error; chatHubProxy = chatHubConnection.CreateHubProxy("ChatHub"); - chatHubProxy.On("addPV", (n, m) => { + chatHubProxy.On("addPV", (n, m) => + { var msg = new ChatMessage { Message = m, @@ -388,12 +399,12 @@ namespace ZicMoove DataManager.Instance.ChatUsers.OnPrivateMessage(msg); }); } - public static void StopConnection() + public static void StopConnection() { try { if (chatHubConnection.State != ConnectionState.Disconnected) - chatHubConnection.Stop(); + chatHubConnection.Stop(); } catch (WebException) { @@ -431,24 +442,52 @@ namespace ZicMoove return chatHubProxy; } } - public static Task HasSomeCloud { + public static Task HasSomeCloud + { get { return CrossConnectivity.Current.IsReachable(Constants.YavscHomeUrl, Constants.CloudTimeout); } } - public static void PostDeviceInfo() + public static async Task PostDeviceInfo() { var info = GetDeviceInfo(); if (!string.IsNullOrWhiteSpace(info.GCMRegistrationId)) { if (MainSettings.CurrentUser != null) { - InvokeApi("gcm/register", info); - DataManager.Instance.Activities.Execute(null); + using (var client = UserHelpers.CreateJsonClient()) + { + try + { + var stringContent = JsonConvert.SerializeObject(info); + HttpContent content = new StringContent( + stringContent, Encoding.UTF8, "application/json" + ); + + using (var response = await client.PostAsync(Constants.MobileRegistrationUrl, content)) + { + if (response.IsSuccessStatusCode) + { + var rcontent = await response.Content.ReadAsStringAsync(); + var jvalue = JsonValue.Parse(rcontent); + if (jvalue != null) + if (jvalue.ContainsKey("UpdateActivities")) + if ((bool)jvalue["UpdateActivities"]) + { + DataManager.Instance.Activities.Execute(null); + DataManager.Instance.Activities.SaveEntity(); + } + } + } + } + catch (Exception) + { + // TODO err reporting + } + } } - } } public static IGCMDeclaration GetDeviceInfo() @@ -457,7 +496,7 @@ namespace ZicMoove DateTime? lupdate = DataManager.Instance.Activities.Count > 0 ? DataManager.Instance.Activities.Aggregate( (a, b) => a.DateModified > b.DateModified ? a : b - ).DateModified : (DateTime ?) null; + ).DateModified : (DateTime?)null; return new GCMRegIdDeclaration { @@ -470,31 +509,8 @@ namespace ZicMoove }; } - [Obsolete("Use RemoteEntity to manage entities from API")] - public async Task InvokeApi(string method, object arg) - { - using (var m = - new SimpleJsonPostMethod( - method, "Bearer " + - MainSettings.CurrentUser.YavscTokens.AccessToken - )) - { - return await m.Invoke(arg); - } - } - - public static object InvokeApi(string method, object arg) - { - using (var m = - new SimpleJsonPostMethod( - method, "Bearer " + - MainSettings.CurrentUser.YavscTokens.AccessToken - )) - { - return m.InvokeJson(arg); - } - } - public static void ShowBookQuery (BookQuery query) + + public static void ShowBookQuery(BookQuery query) { var page = new BookQueryPage { BindingContext = new BookQueryViewModel(query) }; diff --git a/ZicMoove/ZicMoove/Helpers/SimpleJsonPostMethod.cs b/ZicMoove/ZicMoove/Helpers/SimpleJsonPostMethod.cs deleted file mode 100644 index 3ead7852..00000000 --- a/ZicMoove/ZicMoove/Helpers/SimpleJsonPostMethod.cs +++ /dev/null @@ -1,118 +0,0 @@ -// -// PostJson.cs -// -// Author: -// Paul Schneider -// -// Copyright (c) 2015 Paul Schneider -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with this program. If not, see . -using System.Net; -using System.IO; -using System.Json; -using System.Threading.Tasks; -using Newtonsoft.Json; -using System; -using System.Diagnostics; -using ZicMoove; - -namespace ZicMoove.Helpers -{ - /// - /// Simple json post method. - /// - public class SimpleJsonPostMethod : IDisposable - { - private HttpWebRequest request=null; - - /// - /// Initializes a new instance of the Yavsc.Helpers.SimpleJsonPostMethod class. - /// - /// Path to method. - public SimpleJsonPostMethod (string pathToMethod, string authorizationHeader = null) - { - request = (HttpWebRequest) WebRequest.Create ($"{BasePath}/{pathToMethod}"); - request.Method = "POST"; - request.Accept = "application/json"; - request.ContentType = "application/json"; - - if (authorizationHeader != null) - request.Headers["Authorization"] = authorizationHeader; - } - - public static string BasePath { get; private set; } = Constants.YavscApiUrl; - - public void Dispose() - { - request.Abort(); - } - - /// - /// Invoke the specified query. - /// - /// Query. - public async Task Invoke(object query) - { - - using (Stream streamQuery = await request.GetRequestStreamAsync()) { - using (StreamWriter writer = new StreamWriter(streamQuery)) { - writer.Write (JsonConvert.SerializeObject(query)); - }} - TAnswer ans = default (TAnswer); - using (WebResponse response = await request.GetResponseAsync ()) { - using (Stream responseStream = response.GetResponseStream ()) { - using (StreamReader rdr = new StreamReader (responseStream)) { - ans = (TAnswer) JsonConvert.DeserializeObject (rdr.ReadToEnd ()); - } - } - } - return ans; - } - - public async Task InvokeJson(object query) - { - JsonValue jsonDoc = null; - - try - { - using (Stream streamQuery = await request.GetRequestStreamAsync()) - { - using (StreamWriter writer = new StreamWriter(streamQuery)) - { - writer.Write(JsonConvert.SerializeObject(query)); - } - } - using (WebResponse response = await request.GetResponseAsync()) - { - using (Stream stream = response.GetResponseStream()) - { - if (stream.Length > 0) - jsonDoc = await Task.Run(() => JsonObject.Load(stream)); - } - } - } - catch (WebException ex) - { - // TODO err logging - Debug.WriteLine($"Web request failed: {request.ToString()}\n" + ex.ToString()); - } - catch (Exception ex) - { - Debug.WriteLine($"Web request failed: {request.ToString()}\n" + ex.ToString()); - } - - return jsonDoc; - } - } -} diff --git a/ZicMoove/ZicMoove/Views/CommandFormView.cs b/ZicMoove/ZicMoove/Views/CommandFormView.cs new file mode 100644 index 00000000..38fbdd24 --- /dev/null +++ b/ZicMoove/ZicMoove/Views/CommandFormView.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection.Emit; +using System.Text; + +using Xamarin.Forms; +using ZicMoove.Model.Workflow; + +namespace ZicMoove.Views +{ + public class CommandFormView : ContentView + { + public static readonly BindableProperty CommandType = BindableProperty.Create( + "Type", + typeof(string), + typeof(CommandFormView) + ); + + public string Type + { + get + { + return GetValue(CommandType) as string; + } + set { + SetValue(CommandType, value); + } + } + + CommandForm Context { get { return BindingContext as CommandForm; } } + + public CommandFormView() + { + Content = new Label { Text = Context?.Title }; + + } + } +} diff --git a/ZicMoove/ZicMoove/ZicMoove.csproj b/ZicMoove/ZicMoove/ZicMoove.csproj index 1ff7c62f..12ae5cb5 100644 --- a/ZicMoove/ZicMoove/ZicMoove.csproj +++ b/ZicMoove/ZicMoove/ZicMoove.csproj @@ -67,7 +67,6 @@ - @@ -130,6 +129,7 @@ +