Skip to content

Commit 079d0d7

Browse files
committed
Refactor sync into service
1 parent 5bd4f1a commit 079d0d7

4 files changed

Lines changed: 144 additions & 135 deletions

File tree

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
using Fluxor;
2+
using PocketDDD.BlazorClient.Features.Sync.Store;
3+
using PocketDDD.BlazorClient.Services;
4+
using PocketDDD.Shared.API.RequestDTOs;
5+
6+
namespace PocketDDD.BlazorClient.Features.Sync.Services;
7+
8+
public class SyncService
9+
{
10+
private readonly IDispatcher _dispatcher;
11+
private readonly LocalStorageContext _localStorage;
12+
private readonly IPocketDDDApiService _pocketDDDAPI;
13+
14+
public SyncService(IDispatcher dispatcher, LocalStorageContext localStorage, IPocketDDDApiService pocketDDDAPI)
15+
{
16+
_dispatcher = dispatcher;
17+
_localStorage = localStorage;
18+
_pocketDDDAPI = pocketDDDAPI;
19+
}
20+
21+
public async Task SyncAll()
22+
{
23+
var syncEvent = SyncEvent();
24+
var syncEventFeedback = SyncEventFeedback();
25+
var syncSessionFeedback = SyncSessionFeedback();
26+
27+
await Task.WhenAll(syncEvent, syncEventFeedback, syncSessionFeedback);
28+
}
29+
30+
public async Task SyncEvent()
31+
{
32+
_dispatcher.Dispatch(new SetSyncingEventAction(true));
33+
34+
try
35+
{
36+
var eventData = await _localStorage.EventData.GetAsync();
37+
var eventDataVersion = eventData?.Version ?? 0;
38+
39+
var newEventData = await _pocketDDDAPI.FetchLatestEventData(new EventDataUpdateRequestDTO { Version = eventDataVersion });
40+
41+
if (newEventData is not null)
42+
await _localStorage.EventData.SetAsync(newEventData);
43+
}
44+
catch
45+
{
46+
// ignored
47+
}
48+
finally
49+
{
50+
_dispatcher.Dispatch(new SetSyncingEventAction(false));
51+
}
52+
}
53+
54+
public async Task SyncEventFeedback()
55+
{
56+
try
57+
{
58+
var syncItems = await _localStorage.EventFeedbackSync.GetAllSyncItemsAsync();
59+
await SyncEventFeedbackItems(syncItems);
60+
}
61+
catch
62+
{
63+
// ignored
64+
}
65+
}
66+
67+
public async Task SyncEventFeedbackItems(IList<SubmitEventFeedbackDTO> syncItems)
68+
{
69+
_dispatcher.Dispatch(new SetSyncingEventFeedbackAction(true));
70+
71+
try
72+
{
73+
foreach (var item in syncItems)
74+
{
75+
try
76+
{
77+
var result = await _pocketDDDAPI.SubmitClientEventFeedback(item);
78+
await _localStorage.EventFeedbackSync.RemoveSyncItemAsync(result.ClientId);
79+
await _localStorage.EventScore.SetAsync(result.Score);
80+
}
81+
catch
82+
{
83+
// ignored
84+
}
85+
}
86+
}
87+
finally
88+
{
89+
_dispatcher.Dispatch(new SetSyncingEventFeedbackAction(false));
90+
}
91+
}
92+
93+
public async Task SyncSessionFeedback()
94+
{
95+
try
96+
{
97+
var syncItems = await _localStorage.SessionFeedbackSync.GetAllSyncItemsAsync();
98+
await SyncSessionFeedbackItems(syncItems);
99+
}
100+
catch
101+
{
102+
// ignored
103+
}
104+
}
105+
106+
public async Task SyncSessionFeedbackItems(IList<SubmitSessionFeedbackDTO> syncItems)
107+
{
108+
_dispatcher.Dispatch(new SetSyncingSessionFeedbackAction(true));
109+
110+
try
111+
{
112+
foreach (var item in syncItems)
113+
{
114+
try
115+
{
116+
var result = await _pocketDDDAPI.SubmitClientSessionFeedback(item);
117+
await _localStorage.SessionFeedbackSync.RemoveSyncItemAsync(result.ClientId);
118+
await _localStorage.EventScore.SetAsync(result.Score);
119+
}
120+
catch
121+
{
122+
// ignored
123+
}
124+
}
125+
}
126+
finally
127+
{
128+
_dispatcher.Dispatch(new SetSyncingSessionFeedbackAction(false));
129+
}
130+
}
131+
132+
}

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33
namespace PocketDDD.BlazorClient.Features.Sync.Store;
44

55
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);
126

137
public record SetSyncingEventAction(bool Syncing);
148
public record SetSyncingEventFeedbackAction(bool Syncing);
Lines changed: 10 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,146 +1,27 @@
1-
using System.ComponentModel.DataAnnotations;
2-
using Fluxor;
3-
using PocketDDD.BlazorClient.Features.EventScore.Store;
4-
using PocketDDD.BlazorClient.Features.Sync.Store;
1+
using Fluxor;
2+
using PocketDDD.BlazorClient.Features.Sync.Services;
53
using PocketDDD.BlazorClient.Services;
6-
using PocketDDD.Shared.API.RequestDTOs;
74

85
namespace PocketDDD.BlazorClient.Features.Sync.Store;
96

107
public class SyncEffects
118
{
12-
private readonly IState<SyncState> _state;
9+
private readonly SyncService _syncService;
1310
private readonly LocalStorageContext _localStorage;
14-
private readonly IPocketDDDApiService _pocketDDDAPI;
1511

16-
public SyncEffects(IState<SyncState> state, IDispatcher dispatcher, LocalStorageContext localStorage, IPocketDDDApiService pocketDDDAPI)
12+
public SyncEffects(SyncService syncService, LocalStorageContext localStorage)
1713
{
18-
_state = state;
14+
_syncService = syncService;
1915
_localStorage = localStorage;
20-
_pocketDDDAPI = pocketDDDAPI;
2116

2217
_localStorage.EventFeedbackSync.SubscribeToChanges(
23-
items => dispatcher.Dispatch(new SyncEventFeedbackItemsAction(items)));
24-
_localStorage.SessionFeedbackSync.SubscribeToChanges(
25-
items => dispatcher.Dispatch(new SyncSessionFeedbackItemsAction(items)));
26-
}
27-
28-
[EffectMethod]
29-
public Task OnSync(SyncAction action, IDispatcher dispatcher)
30-
{
31-
dispatcher.Dispatch(new SyncEventAction());
32-
dispatcher.Dispatch(new SyncEventFeedbackAction());
33-
dispatcher.Dispatch(new SyncSessionFeedbackAction());
34-
return Task.CompletedTask;
35-
}
18+
async items => await _syncService.SyncEventFeedbackItems(items));
3619

37-
[EffectMethod]
38-
public async Task OnSyncEvent(SyncEventAction action, IDispatcher dispatcher)
39-
{
40-
dispatcher.Dispatch(new SetSyncingEventAction(true));
41-
42-
try
43-
{
44-
var eventData = await _localStorage.EventData.GetAsync();
45-
var eventDataVersion = eventData?.Version ?? 0;
46-
47-
var newEventData = await _pocketDDDAPI.FetchLatestEventData(new EventDataUpdateRequestDTO { Version = eventDataVersion });
48-
49-
if (newEventData is not null)
50-
await _localStorage.EventData.SetAsync(newEventData);
51-
}
52-
catch
53-
{
54-
// ignored
55-
}
56-
finally
57-
{
58-
dispatcher.Dispatch(new SetSyncingEventAction(false));
59-
}
60-
}
61-
62-
[EffectMethod]
63-
public async Task OnSyncEventFeedback(SyncEventFeedbackAction action, IDispatcher dispatcher)
64-
{
65-
try
66-
{
67-
var eventFeedbackItems = await _localStorage.EventFeedbackSync.GetAllSyncItemsAsync();
68-
dispatcher.Dispatch(new SyncEventFeedbackItemsAction(eventFeedbackItems));
69-
}
70-
catch
71-
{
72-
// ignored
73-
}
74-
}
75-
76-
[EffectMethod]
77-
public async Task OnSyncSessionFeedback(SyncSessionFeedbackAction action, IDispatcher dispatcher)
78-
{
79-
try
80-
{
81-
var sessionFeedbackItems = await _localStorage.SessionFeedbackSync.GetAllSyncItemsAsync();
82-
dispatcher.Dispatch(new SyncSessionFeedbackItemsAction(sessionFeedbackItems));
83-
}
84-
catch
85-
{
86-
// ignored
87-
}
88-
}
89-
90-
91-
[EffectMethod]
92-
public async Task OnSyncEventFeedbackItems(SyncEventFeedbackItemsAction action, IDispatcher dispatcher)
93-
{
94-
dispatcher.Dispatch(new SetSyncingEventFeedbackAction(true));
95-
96-
try
97-
{
98-
var syncItems = action.SyncItems;
99-
foreach (var item in syncItems)
100-
{
101-
try
102-
{
103-
var result = await _pocketDDDAPI.SubmitClientEventFeedback(item);
104-
await _localStorage.EventFeedbackSync.RemoveSyncItemAsync(result.ClientId);
105-
await _localStorage.EventScore.SetAsync(result.Score);
106-
}
107-
catch
108-
{
109-
// ignored
110-
}
111-
}
112-
}
113-
finally
114-
{
115-
dispatcher.Dispatch(new SetSyncingEventFeedbackAction(false));
116-
}
20+
_localStorage.SessionFeedbackSync.SubscribeToChanges(
21+
async items => await _syncService.SyncSessionFeedbackItems(items));
11722
}
11823

11924
[EffectMethod]
120-
public async Task OnSyncSessionFeedbackItems(SyncSessionFeedbackItemsAction action, IDispatcher dispatcher)
121-
{
122-
dispatcher.Dispatch(new SetSyncingSessionFeedbackAction(true));
123-
124-
try
125-
{
126-
var syncItems = action.SyncItems;
127-
foreach (var item in syncItems)
128-
{
129-
try
130-
{
131-
var result = await _pocketDDDAPI.SubmitClientSessionFeedback(item);
132-
await _localStorage.SessionFeedbackSync.RemoveSyncItemAsync(result.ClientId);
133-
await _localStorage.EventScore.SetAsync(result.Score);
134-
}
135-
catch
136-
{
137-
// ignored
138-
}
139-
}
140-
}
141-
finally
142-
{
143-
dispatcher.Dispatch(new SetSyncingSessionFeedbackAction(false));
144-
}
145-
}
25+
public Task OnSync(SyncAction action, IDispatcher dispatcher) =>
26+
_syncService.SyncAll();
14627
}

PocketDDD.BlazorClient/PocketDDD.BlazorClient/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
55
using MudBlazor.Services;
66
using PocketDDD.BlazorClient;
7+
using PocketDDD.BlazorClient.Features.Sync.Services;
78
using PocketDDD.BlazorClient.Services;
89

910
var builder = WebAssemblyHostBuilder.CreateDefault(args);
@@ -21,5 +22,6 @@
2122
builder.Services.AddScoped<IPocketDDDApiService, FakePocketDDDApiService>();
2223

2324
builder.Services.AddScoped<LocalStorageContext>();
25+
builder.Services.AddScoped<SyncService>();
2426

2527
await builder.Build().RunAsync();

0 commit comments

Comments
 (0)