|
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; |
5 | 3 | using PocketDDD.BlazorClient.Services; |
6 | | -using PocketDDD.Shared.API.RequestDTOs; |
7 | 4 |
|
8 | 5 | namespace PocketDDD.BlazorClient.Features.Sync.Store; |
9 | 6 |
|
10 | 7 | public class SyncEffects |
11 | 8 | { |
12 | | - private readonly IState<SyncState> _state; |
| 9 | + private readonly SyncService _syncService; |
13 | 10 | private readonly LocalStorageContext _localStorage; |
14 | | - private readonly IPocketDDDApiService _pocketDDDAPI; |
15 | 11 |
|
16 | | - public SyncEffects(IState<SyncState> state, IDispatcher dispatcher, LocalStorageContext localStorage, IPocketDDDApiService pocketDDDAPI) |
| 12 | + public SyncEffects(SyncService syncService, LocalStorageContext localStorage) |
17 | 13 | { |
18 | | - _state = state; |
| 14 | + _syncService = syncService; |
19 | 15 | _localStorage = localStorage; |
20 | | - _pocketDDDAPI = pocketDDDAPI; |
21 | 16 |
|
22 | 17 | _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)); |
36 | 19 |
|
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)); |
117 | 22 | } |
118 | 23 |
|
119 | 24 | [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(); |
146 | 27 | } |
0 commit comments