refactorisation & last config date @GCM regisitering

This commit is contained in:
2017-02-06 14:32:17 +01:00
parent 00c548e1de
commit f618f6d6dc
9 changed files with 84 additions and 83 deletions

View File

@ -16,6 +16,8 @@
// along with yavsc. If not, see <http://www.gnu.org/licenses/>. // along with yavsc. If not, see <http://www.gnu.org/licenses/>.
// //
using System;
namespace Yavsc.Models.Identity namespace Yavsc.Models.Identity
{ {
public interface IGCMDeclaration public interface IGCMDeclaration
@ -25,7 +27,7 @@ namespace Yavsc.Models.Identity
string Model { get; set; } string Model { get; set; }
string Platform { get; set; } string Platform { get; set; }
string Version { get; set; } string Version { get; set; }
DateTime LatestActivityUpdate { get; set; }
} }
public interface IGoogleCloudMobileDeclaration: IGCMDeclaration public interface IGoogleCloudMobileDeclaration: IGCMDeclaration

View File

@ -32,7 +32,6 @@ using XLabs.Platform.Services.Email;
using XLabs.Platform.Services.Media; using XLabs.Platform.Services.Media;
using XLabs.Serialization; using XLabs.Serialization;
using XLabs.Serialization.JsonNET; using XLabs.Serialization.JsonNET;
using Yavsc.Helpers;
using Yavsc.Models.Identity; using Yavsc.Models.Identity;
namespace ZicMoove.Droid 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<TAnswer>(string method, object arg)
{
using (var m =
new SimpleJsonPostMethod(
method, "Bearer " +
MainSettings.CurrentUser.YavscTokens.AccessToken
))
{
return m.Invoke<TAnswer>(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<T>() public T Resolve<T>()
{ {

View File

@ -352,7 +352,6 @@
<ItemGroup> <ItemGroup>
<Compile Include="Accounts\YavscAccountAuthenticator.cs" /> <Compile Include="Accounts\YavscAccountAuthenticator.cs" />
<Compile Include="Helpers\Settings.cs" /> <Compile Include="Helpers\Settings.cs" />
<Compile Include="Helpers\SimpleJsonPostMethod.cs" />
<Compile Include="Helpers\YavscHelpers.cs" /> <Compile Include="Helpers\YavscHelpers.cs" />
<Compile Include="Html.cs" /> <Compile Include="Html.cs" />
<Compile Include="Interfaces\IGCMessageHandler.cs" /> <Compile Include="Interfaces\IGCMessageHandler.cs" />

View File

@ -32,6 +32,10 @@ namespace ZicMoove
using System.Collections.Generic; using System.Collections.Generic;
using Model.Social; using Model.Social;
using Settings; using Settings;
using Helpers;
using Model.Auth;
using Plugin.DeviceInfo;
using Yavsc.Models.Identity;
public partial class App : Application // superclass new in 1.3 public partial class App : Application // superclass new in 1.3
{ {
@ -67,12 +71,10 @@ namespace ZicMoove
app.Startup += OnStartup; app.Startup += OnStartup;
app.Suspended += OnSuspended; app.Suspended += OnSuspended;
MainSettings.UserChanged += MainSettings_UserChanged; MainSettings.UserChanged += MainSettings_UserChanged;
CrossConnectivity.Current.ConnectivityChanged += (conSender, args) =>
{ App.IsConnected = args.IsConnected; };
SetupHubConnection(); SetupHubConnection();
MainSettings_UserChanged(this, null); MainSettings_UserChanged(this, null);
StartConnexion();
StartConnexion();
} }
// omg // omg
@ -334,22 +336,7 @@ namespace ZicMoove
public static INavigationService NavigationService { protected set; get; } public static INavigationService NavigationService { protected set; get; }
public static bool isConnected; public static bool IsConnected { get { return CrossConnectivity.Current.IsConnected; } }
public static bool IsConnected { get { return isConnected; }
private set
{
if (isConnected != value)
{
isConnected = value;
if (isConnected)
{
// TODO Start all cloud related stuff
StartConnexion();
}
}
}
}
private static HubConnection chatHubConnection = null; private static HubConnection chatHubConnection = null;
public static HubConnection ChatHubConnection public static HubConnection ChatHubConnection
{ {
@ -453,11 +440,57 @@ namespace ZicMoove
public static void PostDeviceInfo() public static void PostDeviceInfo()
{ {
var info = PlatformSpecificInstance.GetDeviceInfo(); var info = GetDeviceInfo();
if (!string.IsNullOrWhiteSpace(info.GCMRegistrationId)) 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<TAnswer> InvokeApi<TAnswer>(string method, object arg)
{
using (var m =
new SimpleJsonPostMethod(
method, "Bearer " +
MainSettings.CurrentUser.YavscTokens.AccessToken
))
{
return await m.Invoke<TAnswer>(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 var page = new BookQueryPage

View File

@ -48,7 +48,6 @@
BookQueries = new RemoteEntityRO<BookQuery, long>("bookquery", q => q.Id); BookQueries = new RemoteEntityRO<BookQuery, long>("bookquery", q => q.Id);
Estimates = new EstimateEntity(); Estimates = new EstimateEntity();
Blogspot = new RemoteEntity<Blog, long>("blog", x=>x.Id); Blogspot = new RemoteEntity<Blog, long>("blog", x=>x.Id);
Contacts = new LocalEntity<ClientProviderInfo, string>(c => c.UserId); Contacts = new LocalEntity<ClientProviderInfo, string>(c => c.UserId);
AppState = new LocalEntity<PageState, int>(s => s.Position); AppState = new LocalEntity<PageState, int>(s => s.Position);
EstimationCache = new LocalEntity<EditEstimateViewModel, long>(e => e.Query.Id); EstimationCache = new LocalEntity<EditEstimateViewModel, long>(e => e.Query.Id);
@ -57,6 +56,7 @@
RemoteFiles = new RemoteFilesEntity (); RemoteFiles = new RemoteFilesEntity ();
BlackList = new RemoteEntity<BlackListed, long>("blacklist",u => u.Id); BlackList = new RemoteEntity<BlackListed, long>("blacklist",u => u.Id);
ChatUsers = new ChatUserCollection(); ChatUsers = new ChatUserCollection();
Activities = new RemoteEntityRO<Activity,string>("activity",a=>a.Code);
PrivateMessages.Load(); PrivateMessages.Load();
BookQueries.Load(); BookQueries.Load();
Estimates.Load(); Estimates.Load();

View File

@ -27,7 +27,7 @@ using System;
using System.Diagnostics; using System.Diagnostics;
using ZicMoove; using ZicMoove;
namespace Yavsc.Helpers namespace ZicMoove.Helpers
{ {
/// <summary> /// <summary>
/// Simple json post method. /// Simple json post method.
@ -46,10 +46,9 @@ namespace Yavsc.Helpers
request.Method = "POST"; request.Method = "POST";
request.Accept = "application/json"; request.Accept = "application/json";
request.ContentType = "application/json"; request.ContentType = "application/json";
request.SendChunked = true;
request.TransferEncoding = "UTF-8"; if (authorizationHeader != null)
if (authorizationHeader!=null) request.Headers["Authorization"] = authorizationHeader;
request.Headers.Add($"Authorization: {authorizationHeader}");
} }
public static string BasePath { get; private set; } = Constants.YavscApiUrl; public static string BasePath { get; private set; } = Constants.YavscApiUrl;
@ -63,21 +62,20 @@ namespace Yavsc.Helpers
/// Invoke the specified query. /// Invoke the specified query.
/// </summary> /// </summary>
/// <param name="query">Query.</param> /// <param name="query">Query.</param>
public TAnswer Invoke<TAnswer>(object query) public async Task<TAnswer> Invoke<TAnswer>(object query)
{ {
using (Stream streamQuery = request.GetRequestStream()) { using (Stream streamQuery = await request.GetRequestStreamAsync()) {
using (StreamWriter writer = new StreamWriter(streamQuery)) { using (StreamWriter writer = new StreamWriter(streamQuery)) {
writer.Write (JsonConvert.SerializeObject(query)); writer.Write (JsonConvert.SerializeObject(query));
}} }}
TAnswer ans = default (TAnswer); TAnswer ans = default (TAnswer);
using (WebResponse response = request.GetResponse ()) { using (WebResponse response = await request.GetResponseAsync ()) {
using (Stream responseStream = response.GetResponseStream ()) { using (Stream responseStream = response.GetResponseStream ()) {
using (StreamReader rdr = new StreamReader (responseStream)) { using (StreamReader rdr = new StreamReader (responseStream)) {
ans = (TAnswer) JsonConvert.DeserializeObject<TAnswer> (rdr.ReadToEnd ()); ans = (TAnswer) JsonConvert.DeserializeObject<TAnswer> (rdr.ReadToEnd ());
} }
} }
response.Close();
} }
return ans; return ans;
} }
@ -88,31 +86,30 @@ namespace Yavsc.Helpers
try try
{ {
using (Stream streamQuery = request.GetRequestStream()) using (Stream streamQuery = await request.GetRequestStreamAsync())
{ {
using (StreamWriter writer = new StreamWriter(streamQuery)) using (StreamWriter writer = new StreamWriter(streamQuery))
{ {
writer.Write(JsonConvert.SerializeObject(query)); writer.Write(JsonConvert.SerializeObject(query));
} }
} }
using (WebResponse response = request.GetResponse()) using (WebResponse response = await request.GetResponseAsync())
{ {
using (Stream stream = response.GetResponseStream()) using (Stream stream = response.GetResponseStream())
{ {
if (stream.Length > 0) if (stream.Length > 0)
jsonDoc = await Task.Run(() => JsonObject.Load(stream)); jsonDoc = await Task.Run(() => JsonObject.Load(stream));
} }
response.Close();
} }
} }
catch (WebException ex) catch (WebException ex)
{ {
// TODO err logging // 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) 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; return jsonDoc;

View File

@ -17,12 +17,6 @@ namespace ZicMoove.Interfaces
void AddAccount(); void AddAccount();
void RevokeAccount(string userName); void RevokeAccount(string userName);
IGCMDeclaration GetDeviceInfo();
TAnswer InvokeApi<TAnswer>(string method, object arg);
object InvokeApi(string method, object arg);
} }
} }

View File

@ -15,6 +15,9 @@ namespace ZicMoove.Model.Auth
public string GCMRegistrationId public string GCMRegistrationId
{ get; set; } { get; set; }
public DateTime LatestActivityUpdate
{ get; set; }
public string Model public string Model
{ get; set; } { get; set; }

View File

@ -67,6 +67,7 @@
<Compile Include="Data\ApiCallFailedException.cs" /> <Compile Include="Data\ApiCallFailedException.cs" />
<Compile Include="Data\EstimateEntity.cs" /> <Compile Include="Data\EstimateEntity.cs" />
<Compile Include="Data\NonCrUD\RemoteFiles.cs" /> <Compile Include="Data\NonCrUD\RemoteFiles.cs" />
<Compile Include="Helpers\SimpleJsonPostMethod.cs" />
<Compile Include="Model\Access\BlackListed.cs" /> <Compile Include="Model\Access\BlackListed.cs" />
<Compile Include="Model\Auth\GCRegIdDeclaration.cs" /> <Compile Include="Model\Auth\GCRegIdDeclaration.cs" />
<Compile Include="Model\Booking\MusicalPreference.cs" /> <Compile Include="Model\Booking\MusicalPreference.cs" />
@ -301,6 +302,12 @@
<Reference Include="Plugin.Connectivity.Abstractions"> <Reference Include="Plugin.Connectivity.Abstractions">
<HintPath>..\..\packages\Xam.Plugin.Connectivity.2.2.12\lib\MonoAndroid10\Plugin.Connectivity.Abstractions.dll</HintPath> <HintPath>..\..\packages\Xam.Plugin.Connectivity.2.2.12\lib\MonoAndroid10\Plugin.Connectivity.Abstractions.dll</HintPath>
</Reference> </Reference>
<Reference Include="Plugin.DeviceInfo">
<HintPath>..\..\packages\Xam.Plugin.DeviceInfo.2.0.2\lib\portable-net45+wp8+wpa81+win8+MonoAndroid10+MonoTouch10+Xamarin.iOS10+UAP10\Plugin.DeviceInfo.dll</HintPath>
</Reference>
<Reference Include="Plugin.DeviceInfo.Abstractions">
<HintPath>..\..\packages\Xam.Plugin.DeviceInfo.2.0.2\lib\portable-net45+wp8+wpa81+win8+MonoAndroid10+MonoTouch10+Xamarin.iOS10+UAP10\Plugin.DeviceInfo.Abstractions.dll</HintPath>
</Reference>
<Reference Include="Plugin.Geolocator"> <Reference Include="Plugin.Geolocator">
<HintPath>..\..\packages\Xam.Plugin.Geolocator.3.0.4\lib\portable-net45+wp8+wpa81+win8+MonoAndroid10+MonoTouch10+Xamarin.iOS10+UAP10\Plugin.Geolocator.dll</HintPath> <HintPath>..\..\packages\Xam.Plugin.Geolocator.3.0.4\lib\portable-net45+wp8+wpa81+win8+MonoAndroid10+MonoTouch10+Xamarin.iOS10+UAP10\Plugin.Geolocator.dll</HintPath>
</Reference> </Reference>
@ -329,6 +336,9 @@
<HintPath>..\..\packages\SQLite.Net-PCL.3.1.1\lib\portable-win8+net45+wp8+wpa81+MonoAndroid1+MonoTouch1\SQLite.Net.dll</HintPath> <HintPath>..\..\packages\SQLite.Net-PCL.3.1.1\lib\portable-win8+net45+wp8+wpa81+MonoAndroid1+MonoTouch1\SQLite.Net.dll</HintPath>
<Private>True</Private> <Private>True</Private>
</Reference> </Reference>
<Reference Include="System.Json">
<HintPath>..\..\..\..\..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\Framework\MonoAndroid\v1.0\System.Json.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http.Extensions, Version=1.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="System.Net.Http.Extensions, Version=1.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Net.Http.2.2.29\lib\portable-net45+win8+wpa81\System.Net.Http.Extensions.dll</HintPath> <HintPath>..\..\packages\Microsoft.Net.Http.2.2.29\lib\portable-net45+win8+wpa81\System.Net.Http.Extensions.dll</HintPath>
<Private>True</Private> <Private>True</Private>