-
Notifications
You must be signed in to change notification settings - Fork 10
/
App.xaml.cs
72 lines (65 loc) · 2.61 KB
/
App.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System.Text.RegularExpressions;
using Fluxor;
using LiftLog.Lib.Models;
using LiftLog.Ui.Services;
using LiftLog.Ui.Store.App;
using LiftLog.Ui.Store.CurrentSession;
using LiftLog.Ui.Store.Feed;
using LiftLog.Ui.Store.Program;
using LiftLog.Ui.Store.Settings;
using MaterialColorUtilities.Maui;
using Microsoft.AspNetCore.Components;
namespace LiftLog.Maui;
public partial class App : Application
{
private readonly Fluxor.IDispatcher dispatcher;
private readonly IState<CurrentSessionState> currentSessionState;
public App(Fluxor.IDispatcher dispatcher, IState<CurrentSessionState> currentSessionState)
{
InitializeComponent();
MainPage = new MainPage();
this.dispatcher = dispatcher;
this.currentSessionState = currentSessionState;
IMaterialColorService.Current.Initialize(Resources);
}
protected override Window CreateWindow(IActivationState? activationState)
{
Window window = base.CreateWindow(activationState);
window.Activated += (sender, args) =>
{
var session = currentSessionState.Value.WorkoutSession;
// Complete a finished session if it has been more than half an hour since the last set
// And there are no more sets to complete
// OR
// It has been more than 12 hours since the last set
if (session?.LastExercise?.LastRecordedSet?.Set is not null)
{
var lastSet = session.LastExercise.LastRecordedSet.Set;
var lastSetTime = session.Date.ToDateTime(
lastSet.CompletionTime,
DateTimeKind.Local
);
var timeSinceLastSet = DateTime.Now - lastSetTime;
if (
(timeSinceLastSet > TimeSpan.FromMinutes(30) && session.IsComplete)
|| (timeSinceLastSet > TimeSpan.FromHours(12))
)
{
dispatcher.Dispatch(
new PersistCurrentSessionAction(SessionTarget.WorkoutSession)
);
dispatcher.Dispatch(new AddUnpublishedSessionIdAction(session.Id));
dispatcher.Dispatch(
new SetCurrentSessionAction(SessionTarget.WorkoutSession, null)
);
dispatcher.Dispatch(
new NavigateAction("/", IfCurrentPathMatches: SessionPathRegex())
);
}
}
};
return window;
}
[GeneratedRegex(".*/session")]
private static partial Regex SessionPathRegex();
}