Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
edgiardina committed Jun 9, 2024
1 parent 2f7a15e commit f448c30
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Models/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static string CalendarRankingSystem
set => Preferences.Set("CalendarRankingSystem", value);
}

public static int LastCalendarIdSeen
public static long LastCalendarIdSeen
{
get => Preferences.Get("LastCalendarIdSeen", 0);
set => Preferences.Set("LastCalendarIdSeen", value);
Expand Down
35 changes: 29 additions & 6 deletions Services/NotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
using PinballApi.Models.WPPR.v1.Calendar;
using Microsoft.Extensions.Logging;
using CommunityToolkit.Maui.ApplicationModel;
using Microsoft.Maui.Devices.Sensors;
using System.Net;
using PinballApi.Interfaces;
using PinballApi.Models.WPPR.Universal;
using PinballApi.Models.WPPR.v2.Calendar;

namespace Ifpa.Services
{
Expand All @@ -16,16 +21,21 @@ public class NotificationService
private readonly ILogger<NotificationService> logger;
private readonly IBadge badge;

public NotificationService(PinballRankingApiV1 pinballRankingApi, BlogPostService blogPostService, INotificationManager notificationManager, IBadge badge, ILogger<NotificationService> logger)
private readonly IGeocoding Geocoding;

public NotificationService(PinballRankingApiV1 pinballRankingApi, PinballRankingApi universalPinballRankingApi, IGeocoding geocoding, BlogPostService blogPostService, INotificationManager notificationManager, IBadge badge, ILogger<NotificationService> logger)
{
PinballRankingApi = pinballRankingApi;
UniversalPinballRankingApi = universalPinballRankingApi;
Geocoding = geocoding;

BlogPostService = blogPostService;
this.notificationManager = notificationManager;
this.logger = logger;
this.badge = badge;
}
private PinballRankingApiV1 PinballRankingApi { get; set; }
private PinballRankingApi UniversalPinballRankingApi { get; set; }

public static string NewTournamentNotificationTitle = Strings.NotificationService_NewTournamentNotificationTitle;
protected readonly string NewTournamentNotificationDescription = Strings.NotificationService_NewTournamentNotificationDescription;
Expand Down Expand Up @@ -166,17 +176,30 @@ public async Task NotifyIfNewCalendarEntry()
{
try
{
var items = await PinballRankingApi.GetCalendarSearch(Settings.LastCalendarLocation, Settings.LastCalendarDistance, DistanceUnit.Miles);
var geoLocation = await Geocoding.GetLocationsAsync(Settings.LastCalendarLocation);

var longitude = geoLocation.FirstOrDefault()?.Longitude;
var latitude = geoLocation.FirstOrDefault()?.Latitude;
var rankingSystem = (RankingSystem?)(Settings.CalendarRankingSystem == "All" ? null : Enum.Parse(typeof(RankingSystem), Settings.CalendarRankingSystem));

var items = await UniversalPinballRankingApi.TournamentSearch(latitude,
longitude,
Settings.LastCalendarDistance,
DistanceType.Miles,
startDate: DateTime.Now,
endDate: DateTime.Now.AddYears(1),
rankingSystem: rankingSystem,
totalReturn: 500);

var newestCalendarItemId = items.Calendar.Max(n => n.CalendarId);
var newestCalendarItemId = items.Tournaments.Max(n => n.TournamentId);

if(newestCalendarItemId > Settings.LastCalendarIdSeen)
{
foreach (var calendarItem in items.Calendar.Where(n => n.CalendarId > Settings.LastCalendarIdSeen))
foreach (var calendarItem in items.Tournaments.Where(n => n.TournamentId > Settings.LastCalendarIdSeen))
{
await SendNotification(NewTournamentOnCalendarTitle,
string.Format(NewTournamentOnCalendarDescription, calendarItem.TournamentName, calendarItem.StartDate.ToShortDateString()),
$"///calendar/calendar-detail?calendarId={calendarItem.CalendarId}");
string.Format(NewTournamentOnCalendarDescription, calendarItem.TournamentName, calendarItem.EventStartDate.DateTime.ToShortDateString()),
$"///calendar/calendar-detail?tournamentId={calendarItem.TournamentId}");
}

Settings.LastCalendarIdSeen = newestCalendarItemId;
Expand Down
4 changes: 4 additions & 0 deletions Views/CalendarPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
</Grid>
<CollectionView x:Name="TournamentListView"
Grid.Row="1"
IsVisible="{Binding IsBusy, Converter={StaticResource invertedBoolConverter}}"
ItemsSource="{Binding Tournaments}"
SelectionMode="Single"
SelectionChanged="TournamentListView_SelectionChanged">
Expand Down Expand Up @@ -105,6 +106,9 @@
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<ActivityIndicator Grid.Row="2"
IsVisible="{Binding IsBusy}"
IsRunning="{Binding IsBusy}" />
</Grid>
<calendar:Calendar x:Name="myCal"
SelectedDate="{Binding Source={x:Static sys:DateTime.Now}}"
Expand Down

0 comments on commit f448c30

Please sign in to comment.