public async Task GetTrainingSessions() { if (Connectivity.NetworkAccess == NetworkAccess.None) { return; } var syncRequestUrl = $"{Constants.WebServerBaseUrl}{syncRequestBase}"; try { // create the sync request var userId = Preferences.Get(Constants.UserIdPreference, "Matt"); var syncRequest = new SyncRequest { FromVersion = 0, UserId = userId }; // perform the request var request = new HttpRequestMessage(HttpMethod.Post, syncRequestUrl); request.Content = new StringContent(JsonConvert.SerializeObject(syncRequest), Encoding.UTF8, "application/json"); var response = await client.SendAsync(request); // pull out the info var syncResultJson = await response.Content.ReadAsStringAsync(); var syncResult = JsonConvert.DeserializeObject <DataSyncResult>(syncResultJson); // save training sessions locally var localData = DependencyService.Get <ILocalDataService>(DependencyFetchTarget.GlobalInstance); localData.SaveSessionFromWeb(syncResult.TrainingData); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); } }
public async Task GetTrainingSessions() { if (Connectivity.NetworkAccess == NetworkAccess.None) { await Shell.Current.DisplayAlert("No Internet", "You do not have a connection to the internet", "OK"); return; } var syncRequestUrl = $"{Constants.WebServerBaseUrl}{syncRequestBase}"; try { // Get the latest version sync'd var syncPoint = (int)await dataService.GetLatestSyncVersion(); // create the sync request var syncRequest = new SyncRequest { FromVersion = syncPoint, UserId = userName }; // perform the request var request = new HttpRequestMessage(HttpMethod.Post, syncRequestUrl); request.Content = new StringContent(JsonConvert.SerializeObject(syncRequest), Encoding.UTF8, "application/json"); var response = await client.SendAsync(request); // pull out the info var syncResultJson = await response.Content.ReadAsStringAsync(); var syncResult = JsonConvert.DeserializeObject <DataSyncResult>(syncResultJson); await dataService.SaveSessionsFromWeb(syncResult.Metadata.Sync, syncResult.TrainingData); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); } }