async Task <bool> syncResults(Athlete athlete, SyncResult syncRes) { var resultsOnMobile = repository.GetResults(athlete.AthleteID, true); syncRes.ResultsCountBeforeSync = resultsOnMobile.Count; syncRes.Info += "Starting sync"; var syncedResults = await webservice.SyncResults(resultsOnMobile); if (syncedResults == null) { syncRes.Result = SyncResultEnum.Failed; syncRes.TimeCompleted = DateTime.Now; return(false); } syncRes.ResultsCountAfterSync = syncedResults.Count; if (dbgSyncFlag) { dbgPrint(resultsOnMobile, "resultsOnMobile"); dbgPrint(syncedResults, "syncedResults"); } // any changes? bool hasChanges = false; if (resultsOnMobile.Count != syncedResults.Count) { hasChanges = true; } else { foreach (var result in syncedResults) { if ((from i in resultsOnMobile where i.IsDifferent(result) == false select i).Count() == 0) { hasChanges = true; } } } if (hasChanges == false) { syncRes.Result = SyncResultEnum.Ok; syncRes.TimeCompleted = DateTime.Now; return(true); } repository.UpdateAllAthleteResults(athlete.AthleteID, syncedResults); if (dbgSyncFlag) { var resultsOnMobile2 = repository.GetResults(athlete.AthleteID, true); dbgPrint(resultsOnMobile2, "resultsOnMobile After update"); } return(true); }
async Task <bool> syncScores(SyncResult syncRes) { var scoresOnMobile = repository.GetScores(true); // note: do not sync unfinished scores var scoresOnMobile_Unfinished = (from i in scoresOnMobile where i.IsUnfinished || i.AthleteBID == 0 select i).ToList(); var scoresOnMobile_Finished = (from i in scoresOnMobile where i.IsUnfinished == false && i.AthleteBID != 0 select i).ToList(); syncRes.ScoresCountBeforeSync = scoresOnMobile_Finished.Count; syncRes.Info += "Starting scores sync"; var syncedScores = await webservice.SyncScores(scoresOnMobile_Finished); if (syncedScores == null) { syncRes.Result = SyncResultEnum.Failed; syncRes.TimeCompleted = DateTime.Now; return(false); } syncRes.ScoresCountAfterSync = syncedScores.Count; // any changes? bool hasChanges = false; if (scoresOnMobile_Finished.Count != syncedScores.Count) { hasChanges = true; } else { foreach (var result in syncedScores) { if ((from i in scoresOnMobile_Finished where i.IsDifferent(result, true) == false select i).Count() == 0) { hasChanges = true; } } } if (hasChanges == false) { syncRes.Result = SyncResultEnum.Ok; syncRes.TimeCompleted = DateTime.Now; return(true); } foreach (var score in scoresOnMobile_Unfinished) { syncedScores.Add(score); } repository.UpdateAllScores(syncedScores); return(true); }
public void StartAsync() { if (App.LoginAndRegistrationLogic.RegistrationStatus != RegistrationStatusEnum.Registered) { return; } Task.Run(async delegate { Device.BeginInvokeOnMainThread(() => { if (this.StatusChanged != null) { this.StatusChanged(this, new SyncStatus() { Completed = false, Current = 0, TotalCount = 0 }); } }); SyncResult results = await sync(); this.SyncResults.Add(results); Device.BeginInvokeOnMainThread(() => { if (this.StatusChanged != null) { this.StatusChanged(this, new SyncStatus() { Error = results.Result == SyncResultEnum.Failed, Completed = true, Current = 0, TotalCount = 0 }); } }); }); }
protected override async Task <SyncResult> sync() { if (Config.App != MobileAppEnum.SnookerForVenues) { throw new Exception("App = " + Config.App.ToString()); } SyncResult syncRes = new SyncResult(); syncRes.TimeStarted = DateTime.Now; syncRes.Info = "test\r\n"; try { /// scores /// onStatusChanged(false, 1, 4); System.Threading.Thread.Sleep(500); // load scores from local db List <Score> localScores = App.Repository.GetScores(false); // send these scores to the web service List <Score> persistedScores = await App.WebService.PersistScoresFVO(localScores); onStatusChanged(false, 2, 4); System.Threading.Thread.Sleep(500); // set all persisted scores as 'deleted' foreach (var score in persistedScores) { var localScore = localScores.Where(i => i.Guid == score.Guid).FirstOrDefault(); if (localScore == null) { continue; // this shouldn't be happening } App.Repository.SetIsDeletedOnScore(localScore.ScoreID, true); } onStatusChanged(false, 3, 4); System.Threading.Thread.Sleep(500); /// results /// // load results from local db List <Result> localResults = App.Repository.GetResults(false); // send these results to the web service List <ResultWebModel> persistedResults = await App.WebService.PersistResultsFVO((from r in localResults select ResultWebModel.FromResult(r)).ToList()); onStatusChanged(false, 4, 4); System.Threading.Thread.Sleep(500); // set all persisted results as 'deleted' foreach (var result in persistedResults) { var localResult = localResults.Where(i => i.Guid == result.Guid).FirstOrDefault(); if (localResults == null) { continue; // this shouldn't be happening } App.Repository.SetIsDeletedOnResult(localResult.ResultID, true); } syncRes.Result = SyncResultEnum.Ok; syncRes.TimeCompleted = DateTime.Now; return(syncRes); } catch (Exception exc) { syncRes.Result = SyncResultEnum.Failed; syncRes.TimeCompleted = DateTime.Now; syncRes.Info += "Exception: " + TraceHelper.ExceptionToString(exc); return(syncRes); } }
protected override async Task <SyncResult> sync() { if (Config.App != MobileAppEnum.Snooker) { throw new Exception("App = " + Config.App.ToString()); } SyncResult syncRes = new SyncResult(); syncRes.TimeStarted = DateTime.Now; syncRes.Info = "test\r\n"; try { System.Threading.Thread.Sleep(100); /// Sync "My Athlete" record /// var athlete = repository.GetMyAthlete(); if (athlete.AthleteID == 0) { syncRes.Result = SyncResultEnum.Failed; syncRes.TimeCompleted = DateTime.Now; return(syncRes); } syncRes.Info += "AthleteID=" + athlete.AthleteID + "\r\n"; Console.WriteLine("sync: athlete.TimeModified=" + athlete.TimeModified.ToShortDateString() + " - " + athlete.TimeModified.ToLongTimeString()); var athleteFromWeb = await webservice.SyncMyAthlete(athlete); if (athleteFromWeb != null) { syncRes.Info += "Synced athlete record\r\n"; // non-null means that the web returned updated version of Athlete record athleteFromWeb.CopyTo(athlete, true); athlete.TimeModified = athleteFromWeb.TimeModified; repository.UpdateAthlete(athlete); } else { syncRes.Info += "Athlete record didn't change\r\n"; } /// Send all results of opponents /// await this.sendAllResultsByOpponents(); /// Sync "Results" and "Scores" /// if (await this.syncResults(athlete, syncRes) == false) { return(syncRes); } if (await this.syncScores(syncRes) == false) { return(syncRes); } syncRes.Result = SyncResultEnum.Ok; syncRes.TimeCompleted = DateTime.Now; return(syncRes); } catch (Exception exc) { syncRes.Result = SyncResultEnum.Failed; syncRes.TimeCompleted = DateTime.Now; syncRes.Info += "Exception: " + TraceHelper.ExceptionToString(exc); return(syncRes); } }