public int GetStreak(State state) { int count = 0; var expiration = GetExpiration().AddDays(-1); var times = state.Data.CompletionTimes; var start = times.Count - 1; if (HasCompletedToday(state)) { start--; count++; } for (int i = start; i >= 0; --i) { expiration = expiration.AddDays(-1); if (times[i] > expiration) { count++; } else { break; } } return count; }
public void MarkCompletion(State state) { if (HasCompletedToday(state)) { return; } state.Data.CompletionTimes.Add(DateTime.UtcNow); }
private void UpdateTile(TimeSpan timeLeft, Logic logic, State state) { var updater = TileUpdateManager.CreateTileUpdaterForApplication(); updater.Clear(); if (logic.HasCompletedToday(state)) { return; } XmlDocument tileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileSquare150x150Text01); tileXml.GetElementsByTagName("text")[0].InnerText = string.Format("You have {0} hour{1}!", timeLeft.Hours, timeLeft.Hours == 1 ? "" : "s"); updater.Update(new TileNotification(tileXml)); }
private async Task UpdateToast(TimeSpan timeLeft, Logic logic, State state) { if (logic.ShouldNotify(state)) { if (timeLeft.Hours >= 2) { SendNotification(string.Format("You have {0} hours!", timeLeft.Hours)); } else { SendNotification(string.Format("You only have {0:hh\\:mm}!", timeLeft)); } logic.OnNotify(state); await state.Save(); } }
public bool ShouldNotify(State state) { if (HasCompletedToday(state)) { return false; } var expires = GetExpiration(); var now = DateTime.UtcNow; var timeLeft = expires - now; var timeSince = now - state.Data.LastNotification; if (timeLeft.Hours > 9) { return false; } if (timeLeft.Hours >= 4 && timeSince.Hours < 3) { return false; } if (timeLeft.Hours >= 2 && timeSince.Hours < 1) { return false; } return true; }
public void OnNotify(State state) { state.Data.LastNotification = DateTime.UtcNow; }
public bool HasCompletedToday(State state) { var times = state.Data.CompletionTimes; var expiration = GetExpiration().AddDays(-1); return times.Any() && times.Last() > expiration; }