public async Task <Entry> RestoreAsync() { if (IsLocalMode) { return(await _fallbackPersister.RestoreAsync()); } try { var client = GetClient(); var username = _authManager.AuthData.Username; var entryDate = Entry.GetEntryDate(); var url = string.Format("/api/entries/find/{0}/{1}", username, entryDate); var response = await client.GetAsync(url); if (response.IsSuccessStatusCode) { var serialized = await response.Content.ReadAsStringAsync(); var entries = JsonConvert.DeserializeObject <Entry[]>(serialized); return(entries.OrderBy(x => x.EntryDate).LastOrDefault()); } else { IsLocalMode = true; } } catch (Exception e) { Debug.WriteLine(e.Message); IsLocalMode = true; } if (IsLocalMode) { return(await _fallbackPersister.RestoreAsync()); } return(await Task.FromResult <Entry>(null)); }
private async Task ChangeStateAsync(Func <IWatcherState> action) { var entry = await _persister.RestoreAsync(); var interval = Settings.RefreshInterval; if (entry != null && entry.SessionId != Session.SessionId && (DateTime.UtcNow - entry.Timestamp) < interval) { await _dialog.AlertAsync("Alert", "Syncing in progress... Please try again later."); return; } State = action(); RaiseAllCanExecute(); RaiseAllPropertyChanged(); var username = _authManager.AuthData.Username; entry = await Entry.CreateAsync(username, State.StateId, State.Data); await _persister.PersistAsync(entry); }