Skip to content

Commit

Permalink
#173 missed payments (#176) (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisK00 authored May 30, 2024
1 parent 6417eb2 commit 811fd24
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:
jobs:
build:

runs-on: windows-2022
runs-on: windows-latest

steps:
- uses: actions/setup-java@v2
Expand All @@ -25,7 +25,7 @@ jobs:
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x

- name: Restore workload
run: dotnet workload restore

Expand Down
7 changes: 5 additions & 2 deletions docs/roadmap.drawio
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<mxfile host="65bd71144e">
<diagram id="0sBSfLOzs08FeBCRcvm6" name="2023">
<mxGraphModel dx="1850" dy="485" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0">
<mxGraphModel dx="1624" dy="576" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0">
<root>
<mxCell id="0"/>
<mxCell id="1" parent="0"/>
Expand Down Expand Up @@ -163,9 +163,12 @@
<mxCell id="61" value="" style="sketch=0;html=1;aspect=fixed;strokeColor=none;shadow=0;align=center;verticalAlign=top;fillColor=#2D9C5E;shape=mxgraph.gcp2.check" parent="1" vertex="1">
<mxGeometry x="292.5" y="304" width="25" height="20" as="geometry"/>
</mxCell>
<mxCell id="62" value="" style="sketch=0;html=1;aspect=fixed;strokeColor=none;shadow=0;align=center;verticalAlign=top;fillColor=#2D9C5E;shape=mxgraph.gcp2.check" vertex="1" parent="1">
<mxCell id="62" value="" style="sketch=0;html=1;aspect=fixed;strokeColor=none;shadow=0;align=center;verticalAlign=top;fillColor=#2D9C5E;shape=mxgraph.gcp2.check" parent="1" vertex="1">
<mxGeometry x="260" y="250" width="25" height="20" as="geometry"/>
</mxCell>
<mxCell id="63" value="" style="sketch=0;html=1;aspect=fixed;strokeColor=none;shadow=0;align=center;verticalAlign=top;fillColor=#2D9C5E;shape=mxgraph.gcp2.check" vertex="1" parent="1">
<mxGeometry x="433" y="263.5" width="25" height="20" as="geometry"/>
</mxCell>
</root>
</mxGraphModel>
</diagram>
Expand Down
Binary file modified docs/roadmap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 25 additions & 7 deletions subtrack.MAUI/Services/Android/NotifyDueSubscriptionsJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ public async Task Run(JobInfo jobInfo, CancellationToken cancellationToken)

if (lastSubscriptionReminderTimeStamp.Value?.Date != today)
{
await NotifyDueSubscriptionsJob.RunInternal(serviceProvider, today);
var subscriptionService = serviceProvider.GetRequiredService<ISubscriptionService>();
var subscriptionsCalculator = serviceProvider.GetRequiredService<ISubscriptionsCalculator>();

var subscriptions = await subscriptionService.GetAllAsync();
await NotifyUpcomingPayments(subscriptions, today, subscriptionsCalculator);
await NotifyMissedPaymentsAsync(subscriptions, subscriptionsCalculator, today);

lastSubscriptionReminderTimeStamp.Value = now;
await settingsService.UpdateAsync(lastSubscriptionReminderTimeStamp);
Expand All @@ -62,12 +67,8 @@ public async Task Run(JobInfo jobInfo, CancellationToken cancellationToken)
}
}

private static async Task RunInternal(IServiceProvider serviceProvider, DateTime today)
private static async Task NotifyUpcomingPayments(IEnumerable<Subscription> subscriptions, DateTime today, ISubscriptionsCalculator subscriptionsCalculator)
{
var subscriptionService = serviceProvider.GetRequiredService<ISubscriptionService>();
var subscriptionsCalculator = serviceProvider.GetRequiredService<ISubscriptionsCalculator>();

var subscriptions = await subscriptionService.GetAllAsync();
var subscriptionsWithNotificationsEnabled = subscriptions.Where(x => x.NotificationDays.HasValue).ToList();
if (!subscriptionsWithNotificationsEnabled.Any())
{
Expand All @@ -76,7 +77,7 @@ private static async Task RunInternal(IServiceProvider serviceProvider, DateTime

await NotificationsUtil.EnsureNotificationsAreEnabled();

var notificationGroup = today.Day.ToString();
var notificationGroup = $"upcoming {today.Day}";
subscriptionsWithNotificationsEnabled.ForEach(sub =>
{
var dueDate = subscriptionsCalculator.GetNextPaymentDate(sub);
Expand All @@ -90,6 +91,23 @@ private static async Task RunInternal(IServiceProvider serviceProvider, DateTime
});
}

private static async Task NotifyMissedPaymentsAsync(IEnumerable<Subscription> subscriptions, ISubscriptionsCalculator subscriptionsCalculator, DateTime today)
{
if (!await NotificationsUtil.HasEnabledNotifications())
{
return;
}

foreach (var sub in subscriptions)
{
var dueDate = subscriptionsCalculator.GetNextPaymentDate(sub);
if (dueDate.IsPastDate(today))
{
SendNotification(sub.Id, $"Missed payment for {sub.Name}", $"missed {today.Day}");
}
}
}

private static string GetDueDaysText(int dueDays)
{
return dueDays switch
Expand Down
3 changes: 2 additions & 1 deletion subtrack.MAUI/Utilities/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ public static class DateTimeExtensions
{
public static TimeSpan TimeRemainingFromToday(this DateTime date, IDateProvider dateTimeProvider) => date.Subtract(dateTimeProvider.Today);

public static bool IsPastDate(this DateTime date, IDateProvider dateTimeProvider) => date < dateTimeProvider.Today;
public static bool IsPastDate(this DateTime date, IDateProvider dateTimeProvider) => IsPastDate(date, dateTimeProvider.Today);
public static bool IsPastDate(this DateTime date, DateTime today) => date < today;

public static DateTime AddWeeks(this DateTime date, int weeks) => date.AddDays(weeks * 7);

Expand Down
2 changes: 2 additions & 0 deletions subtrack.MAUI/Utilities/NotificationsUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public static async Task EnsureNotificationsAreEnabled()
await LocalNotificationCenter.Current.RequestNotificationPermission();
}
}

public static async Task<bool> HasEnabledNotifications() => await LocalNotificationCenter.Current.AreNotificationsEnabled();
}

0 comments on commit 811fd24

Please sign in to comment.