From f618f6d6dc784b707d1d6d7da17c9d2d08c23a58 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Mon, 6 Feb 2017 14:32:17 +0100 Subject: [PATCH] refactorisation & last config date @GCM regisitering --- YavscLib/IGoogleCloudMobileDeclaration.cs | 4 +- ZicMoove/ZicMoove.Droid/MainActivity.cs | 39 +--------- ZicMoove/ZicMoove.Droid/ZicMoove.Droid.csproj | 1 - ZicMoove/ZicMoove/App.xaml.cs | 77 +++++++++++++------ ZicMoove/ZicMoove/Data/DataManager.cs | 2 +- .../Helpers/SimpleJsonPostMethod.cs | 25 +++--- ZicMoove/ZicMoove/Interfaces/IPlatform.cs | 6 -- .../ZicMoove/Model/Auth/GCRegIdDeclaration.cs | 3 + ZicMoove/ZicMoove/ZicMoove.csproj | 10 +++ 9 files changed, 84 insertions(+), 83 deletions(-) rename ZicMoove/{ZicMoove.Droid => ZicMoove}/Helpers/SimpleJsonPostMethod.cs (80%) diff --git a/YavscLib/IGoogleCloudMobileDeclaration.cs b/YavscLib/IGoogleCloudMobileDeclaration.cs index 90f82c6b..b8b006b5 100644 --- a/YavscLib/IGoogleCloudMobileDeclaration.cs +++ b/YavscLib/IGoogleCloudMobileDeclaration.cs @@ -16,6 +16,8 @@ // along with yavsc. If not, see . // +using System; + namespace Yavsc.Models.Identity { public interface IGCMDeclaration @@ -25,7 +27,7 @@ namespace Yavsc.Models.Identity string Model { get; set; } string Platform { get; set; } string Version { get; set; } - + DateTime LatestActivityUpdate { get; set; } } public interface IGoogleCloudMobileDeclaration: IGCMDeclaration diff --git a/ZicMoove/ZicMoove.Droid/MainActivity.cs b/ZicMoove/ZicMoove.Droid/MainActivity.cs index 27f7f1f9..a47ae3ee 100644 --- a/ZicMoove/ZicMoove.Droid/MainActivity.cs +++ b/ZicMoove/ZicMoove.Droid/MainActivity.cs @@ -32,7 +32,6 @@ using XLabs.Platform.Services.Email; using XLabs.Platform.Services.Media; using XLabs.Serialization; using XLabs.Serialization.JsonNET; -using Yavsc.Helpers; using Yavsc.Models.Identity; namespace ZicMoove.Droid @@ -401,43 +400,7 @@ namespace ZicMoove.Droid - public IGCMDeclaration GetDeviceInfo() - { - var devinfo = CrossDeviceInfo.Current; - return new GCMRegIdDeclaration - { - DeviceId = devinfo.Id, - GCMRegistrationId = MainSettings.GoogleRegId, - Model = devinfo.Model, - Platform = devinfo.Platform.ToString(), - Version = devinfo.Version - }; - } - - [Obsolete("Use RemoteEntity to manage entities from API")] - public TAnswer InvokeApi(string method, object arg) - { - using (var m = - new SimpleJsonPostMethod( - method, "Bearer " + - MainSettings.CurrentUser.YavscTokens.AccessToken - )) - { - return m.Invoke(arg); - } - } - - public object InvokeApi(string method, object arg) - { - using (var m = - new SimpleJsonPostMethod( - method, "Bearer " + - MainSettings.CurrentUser.YavscTokens.AccessToken - )) - { - return m.InvokeJson(arg); - } - } + public T Resolve() { diff --git a/ZicMoove/ZicMoove.Droid/ZicMoove.Droid.csproj b/ZicMoove/ZicMoove.Droid/ZicMoove.Droid.csproj index 608b4ec3..1c08e1df 100644 --- a/ZicMoove/ZicMoove.Droid/ZicMoove.Droid.csproj +++ b/ZicMoove/ZicMoove.Droid/ZicMoove.Droid.csproj @@ -352,7 +352,6 @@ - diff --git a/ZicMoove/ZicMoove/App.xaml.cs b/ZicMoove/ZicMoove/App.xaml.cs index 3a83e21c..18dc82ae 100644 --- a/ZicMoove/ZicMoove/App.xaml.cs +++ b/ZicMoove/ZicMoove/App.xaml.cs @@ -32,6 +32,10 @@ namespace ZicMoove using System.Collections.Generic; using Model.Social; using Settings; + using Helpers; + using Model.Auth; + using Plugin.DeviceInfo; + using Yavsc.Models.Identity; public partial class App : Application // superclass new in 1.3 { @@ -67,12 +71,10 @@ namespace ZicMoove app.Startup += OnStartup; app.Suspended += OnSuspended; MainSettings.UserChanged += MainSettings_UserChanged; - CrossConnectivity.Current.ConnectivityChanged += (conSender, args) => - { App.IsConnected = args.IsConnected; }; + SetupHubConnection(); MainSettings_UserChanged(this, null); - - StartConnexion(); + StartConnexion(); } // omg @@ -334,22 +336,7 @@ namespace ZicMoove 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 - StartConnexion(); - } - - } - } - } + public static bool IsConnected { get { return CrossConnectivity.Current.IsConnected; } } private static HubConnection chatHubConnection = null; public static HubConnection ChatHubConnection { @@ -453,11 +440,57 @@ namespace ZicMoove public static void PostDeviceInfo() { - var info = PlatformSpecificInstance.GetDeviceInfo(); + var info = GetDeviceInfo(); if (!string.IsNullOrWhiteSpace(info.GCMRegistrationId)) - PlatformSpecificInstance.InvokeApi("gcm/register", info); + { + if (MainSettings.CurrentUser != null) + { + InvokeApi("gcm/register", info); + DataManager.Instance.Activities.Execute(null); + } + + } + } + public static IGCMDeclaration GetDeviceInfo() + { + var devinfo = CrossDeviceInfo.Current; + return new GCMRegIdDeclaration + { + DeviceId = devinfo.Id, + GCMRegistrationId = MainSettings.GoogleRegId, + Model = devinfo.Model, + Platform = devinfo.Platform.ToString(), + Version = devinfo.Version, + LatestActivityUpdate = DataManager.Instance.Activities.Aggregate( + (a,b)=> a.DateModified > b.DateModified ? a : b + ).DateModified + }; } + [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) { var page = new BookQueryPage diff --git a/ZicMoove/ZicMoove/Data/DataManager.cs b/ZicMoove/ZicMoove/Data/DataManager.cs index 0f8c338e..8e9352d5 100644 --- a/ZicMoove/ZicMoove/Data/DataManager.cs +++ b/ZicMoove/ZicMoove/Data/DataManager.cs @@ -48,7 +48,6 @@ BookQueries = new RemoteEntityRO("bookquery", q => q.Id); Estimates = new EstimateEntity(); Blogspot = new RemoteEntity("blog", x=>x.Id); - Contacts = new LocalEntity(c => c.UserId); AppState = new LocalEntity(s => s.Position); EstimationCache = new LocalEntity(e => e.Query.Id); @@ -57,6 +56,7 @@ RemoteFiles = new RemoteFilesEntity (); BlackList = new RemoteEntity("blacklist",u => u.Id); ChatUsers = new ChatUserCollection(); + Activities = new RemoteEntityRO("activity",a=>a.Code); PrivateMessages.Load(); BookQueries.Load(); Estimates.Load(); diff --git a/ZicMoove/ZicMoove.Droid/Helpers/SimpleJsonPostMethod.cs b/ZicMoove/ZicMoove/Helpers/SimpleJsonPostMethod.cs similarity index 80% rename from ZicMoove/ZicMoove.Droid/Helpers/SimpleJsonPostMethod.cs rename to ZicMoove/ZicMoove/Helpers/SimpleJsonPostMethod.cs index c562a0fc..3ead7852 100644 --- a/ZicMoove/ZicMoove.Droid/Helpers/SimpleJsonPostMethod.cs +++ b/ZicMoove/ZicMoove/Helpers/SimpleJsonPostMethod.cs @@ -27,7 +27,7 @@ using System; using System.Diagnostics; using ZicMoove; -namespace Yavsc.Helpers +namespace ZicMoove.Helpers { /// /// Simple json post method. @@ -46,10 +46,9 @@ namespace Yavsc.Helpers request.Method = "POST"; request.Accept = "application/json"; request.ContentType = "application/json"; - request.SendChunked = true; - request.TransferEncoding = "UTF-8"; - if (authorizationHeader!=null) - request.Headers.Add($"Authorization: {authorizationHeader}"); + + if (authorizationHeader != null) + request.Headers["Authorization"] = authorizationHeader; } public static string BasePath { get; private set; } = Constants.YavscApiUrl; @@ -63,21 +62,20 @@ namespace Yavsc.Helpers /// Invoke the specified query. /// /// Query. - public TAnswer Invoke(object query) + public async Task Invoke(object query) { - using (Stream streamQuery = request.GetRequestStream()) { + using (Stream streamQuery = await request.GetRequestStreamAsync()) { using (StreamWriter writer = new StreamWriter(streamQuery)) { writer.Write (JsonConvert.SerializeObject(query)); }} TAnswer ans = default (TAnswer); - using (WebResponse response = request.GetResponse ()) { + using (WebResponse response = await request.GetResponseAsync ()) { using (Stream responseStream = response.GetResponseStream ()) { using (StreamReader rdr = new StreamReader (responseStream)) { ans = (TAnswer) JsonConvert.DeserializeObject (rdr.ReadToEnd ()); } } - response.Close(); } return ans; } @@ -88,31 +86,30 @@ namespace Yavsc.Helpers try { - using (Stream streamQuery = request.GetRequestStream()) + using (Stream streamQuery = await request.GetRequestStreamAsync()) { using (StreamWriter writer = new StreamWriter(streamQuery)) { writer.Write(JsonConvert.SerializeObject(query)); } } - using (WebResponse response = request.GetResponse()) + using (WebResponse response = await request.GetResponseAsync()) { using (Stream stream = response.GetResponseStream()) { if (stream.Length > 0) jsonDoc = await Task.Run(() => JsonObject.Load(stream)); } - response.Close(); } } catch (WebException ex) { // TODO err logging - Debug.Print($"Web request failed: {request.ToString()}\n" + ex.ToString()); + Debug.WriteLine($"Web request failed: {request.ToString()}\n" + ex.ToString()); } catch (Exception ex) { - Debug.Print($"Web request failed: {request.ToString()}\n" + ex.ToString()); + Debug.WriteLine($"Web request failed: {request.ToString()}\n" + ex.ToString()); } return jsonDoc; diff --git a/ZicMoove/ZicMoove/Interfaces/IPlatform.cs b/ZicMoove/ZicMoove/Interfaces/IPlatform.cs index 0f81713f..04955a36 100644 --- a/ZicMoove/ZicMoove/Interfaces/IPlatform.cs +++ b/ZicMoove/ZicMoove/Interfaces/IPlatform.cs @@ -17,12 +17,6 @@ namespace ZicMoove.Interfaces void AddAccount(); void RevokeAccount(string userName); - - IGCMDeclaration GetDeviceInfo(); - - TAnswer InvokeApi(string method, object arg); - - object InvokeApi(string method, object arg); } } diff --git a/ZicMoove/ZicMoove/Model/Auth/GCRegIdDeclaration.cs b/ZicMoove/ZicMoove/Model/Auth/GCRegIdDeclaration.cs index 46743dc2..cb9c2163 100644 --- a/ZicMoove/ZicMoove/Model/Auth/GCRegIdDeclaration.cs +++ b/ZicMoove/ZicMoove/Model/Auth/GCRegIdDeclaration.cs @@ -15,6 +15,9 @@ namespace ZicMoove.Model.Auth public string GCMRegistrationId { get; set; } + public DateTime LatestActivityUpdate + { get; set; } + public string Model { get; set; } diff --git a/ZicMoove/ZicMoove/ZicMoove.csproj b/ZicMoove/ZicMoove/ZicMoove.csproj index 3e6d78b1..1ff7c62f 100644 --- a/ZicMoove/ZicMoove/ZicMoove.csproj +++ b/ZicMoove/ZicMoove/ZicMoove.csproj @@ -67,6 +67,7 @@ + @@ -301,6 +302,12 @@ ..\..\packages\Xam.Plugin.Connectivity.2.2.12\lib\MonoAndroid10\Plugin.Connectivity.Abstractions.dll + + ..\..\packages\Xam.Plugin.DeviceInfo.2.0.2\lib\portable-net45+wp8+wpa81+win8+MonoAndroid10+MonoTouch10+Xamarin.iOS10+UAP10\Plugin.DeviceInfo.dll + + + ..\..\packages\Xam.Plugin.DeviceInfo.2.0.2\lib\portable-net45+wp8+wpa81+win8+MonoAndroid10+MonoTouch10+Xamarin.iOS10+UAP10\Plugin.DeviceInfo.Abstractions.dll + ..\..\packages\Xam.Plugin.Geolocator.3.0.4\lib\portable-net45+wp8+wpa81+win8+MonoAndroid10+MonoTouch10+Xamarin.iOS10+UAP10\Plugin.Geolocator.dll @@ -329,6 +336,9 @@ ..\..\packages\SQLite.Net-PCL.3.1.1\lib\portable-win8+net45+wp8+wpa81+MonoAndroid1+MonoTouch1\SQLite.Net.dll True + + ..\..\..\..\..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\Framework\MonoAndroid\v1.0\System.Json.dll + ..\..\packages\Microsoft.Net.Http.2.2.29\lib\portable-net45+win8+wpa81\System.Net.Http.Extensions.dll True