Skip to content

Commit d6a2084

Browse files
committed
Improve syncing
1 parent facf654 commit d6a2084

4 files changed

Lines changed: 104 additions & 48 deletions

File tree

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
using PocketDDD.Shared.API.RequestDTOs;
2-
using PocketDDD.Shared.API.ResponseDTOs;
32

43
namespace PocketDDD.BlazorClient.Features.Sync.Store;
54

6-
public record SyncAction();
7-
public record SyncEventFeedbackItemsAction(IList<SubmitEventFeedbackDTO> syncItems);
8-
public record SyncSessionFeedbackItemsAction(IList<SubmitSessionFeedbackDTO> syncItems);
9-
public record SyncCompletedAction();
5+
public record SyncAction;
6+
public record SyncEventAction;
7+
public record SyncEventFeedbackAction;
8+
public record SyncSessionFeedbackAction;
9+
10+
public record SyncEventFeedbackItemsAction(IList<SubmitEventFeedbackDTO> SyncItems);
11+
public record SyncSessionFeedbackItemsAction(IList<SubmitSessionFeedbackDTO> SyncItems);
12+
13+
public record SetSyncingEventAction(bool Syncing);
14+
public record SetSyncingEventFeedbackAction(bool Syncing);
15+
public record SetSyncingSessionFeedbackAction(bool Syncing);
Lines changed: 80 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.ComponentModel.DataAnnotations;
22
using Fluxor;
33
using PocketDDD.BlazorClient.Features.EventScore.Store;
4+
using PocketDDD.BlazorClient.Features.Sync.Store;
45
using PocketDDD.BlazorClient.Services;
56
using PocketDDD.Shared.API.RequestDTOs;
67

@@ -27,76 +28,118 @@ public SyncEffects(IState<SyncState> state, IDispatcher dispatcher, LocalStorage
2728
[EffectMethod]
2829
public async Task OnSync(SyncAction action, IDispatcher dispatcher)
2930
{
31+
dispatcher.Dispatch(new SyncEventAction());
32+
dispatcher.Dispatch(new SyncEventFeedbackAction());
33+
dispatcher.Dispatch(new SyncSessionFeedbackAction());
34+
}
35+
36+
[EffectMethod]
37+
public async Task OnSyncEvent(SyncEventAction action, IDispatcher dispatcher)
38+
{
39+
dispatcher.Dispatch(new SetSyncingEventAction(true));
40+
3041
try
3142
{
3243
var eventData = await _localStorage.EventData.GetAsync();
3344
var eventDataVersion = eventData?.Version ?? 0;
3445

35-
try
36-
{
37-
var newEventData = await _pocketDDDAPI.FetchLatestEventData(new EventDataUpdateRequestDTO { Version = eventDataVersion });
46+
var newEventData = await _pocketDDDAPI.FetchLatestEventData(new EventDataUpdateRequestDTO { Version = eventDataVersion });
3847

39-
if (newEventData is not null)
40-
{
41-
await _localStorage.EventData.SetAsync(newEventData);
42-
}
43-
}
44-
catch
45-
{
46-
// ignored
47-
}
48+
if (newEventData is not null)
49+
await _localStorage.EventData.SetAsync(newEventData);
50+
}
51+
catch
52+
{
53+
// ignored
54+
}
55+
finally
56+
{
57+
dispatcher.Dispatch(new SetSyncingEventAction(false));
58+
}
59+
}
4860

61+
[EffectMethod]
62+
public async Task OnSyncEventFeedback(SyncEventFeedbackAction action, IDispatcher dispatcher)
63+
{
64+
try
65+
{
4966
var eventFeedbackItems = await _localStorage.EventFeedbackSync.GetAllSyncItemsAsync();
50-
var sessionFeedbackItems = await _localStorage.SessionFeedbackSync.GetAllSyncItemsAsync();
51-
5267
dispatcher.Dispatch(new SyncEventFeedbackItemsAction(eventFeedbackItems));
53-
dispatcher.Dispatch(new SyncSessionFeedbackItemsAction(sessionFeedbackItems));
5468
}
5569
catch
5670
{
5771
// ignored
5872
}
59-
finally
73+
}
74+
75+
[EffectMethod]
76+
public async Task OnSyncSessionFeedback(SyncSessionFeedbackAction action, IDispatcher dispatcher)
77+
{
78+
try
79+
{
80+
var sessionFeedbackItems = await _localStorage.SessionFeedbackSync.GetAllSyncItemsAsync();
81+
dispatcher.Dispatch(new SyncSessionFeedbackItemsAction(sessionFeedbackItems));
82+
}
83+
catch
6084
{
61-
dispatcher.Dispatch(new SyncCompletedAction());
85+
// ignored
6286
}
6387
}
6488

89+
6590
[EffectMethod]
6691
public async Task OnSyncEventFeedbackItems(SyncEventFeedbackItemsAction action, IDispatcher dispatcher)
6792
{
68-
var syncItems = action.syncItems;
69-
foreach (var item in syncItems)
93+
dispatcher.Dispatch(new SetSyncingEventFeedbackAction(true));
94+
95+
try
7096
{
71-
try
72-
{
73-
var result = await _pocketDDDAPI.SubmitClientEventFeedback(item);
74-
await _localStorage.EventFeedbackSync.RemoveSyncItemAsync(result.ClientId);
75-
dispatcher.Dispatch(new EventScoreUpdatedAction(result.Score));
76-
}
77-
catch
97+
var syncItems = action.SyncItems;
98+
foreach (var item in syncItems)
7899
{
79-
// ignored
100+
try
101+
{
102+
var result = await _pocketDDDAPI.SubmitClientEventFeedback(item);
103+
await _localStorage.EventFeedbackSync.RemoveSyncItemAsync(result.ClientId);
104+
dispatcher.Dispatch(new EventScoreUpdatedAction(result.Score));
105+
}
106+
catch
107+
{
108+
// ignored
109+
}
80110
}
81111
}
112+
finally
113+
{
114+
dispatcher.Dispatch(new SetSyncingEventFeedbackAction(false));
115+
}
82116
}
83117

84118
[EffectMethod]
85119
public async Task OnSyncSessionFeedbackItems(SyncSessionFeedbackItemsAction action, IDispatcher dispatcher)
86120
{
87-
var syncItems = action.syncItems;
88-
foreach (var item in syncItems)
121+
dispatcher.Dispatch(new SetSyncingSessionFeedbackAction(true));
122+
123+
try
89124
{
90-
try
91-
{
92-
var result = await _pocketDDDAPI.SubmitClientSessionFeedback(item);
93-
await _localStorage.SessionFeedbackSync.RemoveSyncItemAsync(result.ClientId);
94-
dispatcher.Dispatch(new EventScoreUpdatedAction(result.Score));
95-
}
96-
catch
125+
var syncItems = action.SyncItems;
126+
foreach (var item in syncItems)
97127
{
98-
// ignored
128+
try
129+
{
130+
var result = await _pocketDDDAPI.SubmitClientSessionFeedback(item);
131+
await _localStorage.SessionFeedbackSync.RemoveSyncItemAsync(result.ClientId);
132+
dispatcher.Dispatch(new EventScoreUpdatedAction(result.Score));
133+
}
134+
catch
135+
{
136+
// ignored
137+
}
99138
}
100139
}
140+
finally
141+
{
142+
dispatcher.Dispatch(new SetSyncingSessionFeedbackAction(false));
143+
}
101144
}
102145
}

PocketDDD.BlazorClient/PocketDDD.BlazorClient/Features/Sync/Store/SyncReducer.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ namespace PocketDDD.BlazorClient.Features.Sync.Store;
77
public static class SyncReducer
88
{
99
[ReducerMethod]
10-
public static SyncState OnSync(SyncState state, SyncAction action) =>
11-
state with { IsSyncing = true };
10+
public static SyncState OnSetSyncingEvent(SyncState state, SetSyncingEventAction action) =>
11+
state with { IsSyncingEvent = action.Syncing };
1212

1313
[ReducerMethod]
14-
public static SyncState OnSyncCompleted(SyncState state, SyncCompletedAction action) =>
15-
state with { IsSyncing = false };
14+
public static SyncState OnSetSyncingEventFeedback(SyncState state, SetSyncingEventFeedbackAction action) =>
15+
state with { IsSyncingEventFeedback = action.Syncing };
16+
17+
[ReducerMethod]
18+
public static SyncState OnSetSyncingSessionFeedbackAction(SyncState state, SetSyncingSessionFeedbackAction action) =>
19+
state with { IsSyncingSessionFeedback = action.Syncing };
1620

1721
[ReducerMethod]
1822
public static SyncState OnSetEventScore(SyncState state, SetEventScoreAction action) =>
@@ -21,5 +25,4 @@ public static SyncState OnSetEventScore(SyncState state, SetEventScoreAction act
2125
[ReducerMethod]
2226
public static SyncState OnSetCurrentUser(SyncState state, SetCurrentUserAction action) =>
2327
state with { LoggedInUser = action.User };
24-
2528
}

PocketDDD.BlazorClient/PocketDDD.BlazorClient/Features/Sync/Store/SyncState.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@ public record SyncState
1414
public int OutstandingSyncItems =>
1515
OutstandingEventFeedbackSyncCount + OutstandingSessionFeedbackSyncCount;
1616

17-
public bool IsSyncing { get; init; } = false;
17+
public bool IsSyncingEvent { get; init; } = false;
18+
public bool IsSyncingEventFeedback { get; init; } = false;
19+
public bool IsSyncingSessionFeedback { get; init; } = false;
20+
21+
public bool IsSyncing => IsSyncingEvent || IsSyncingEventFeedback || IsSyncingSessionFeedback;
1822
}

0 commit comments

Comments
 (0)