forked from SteveSandersonMS/CarChecker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyncStatus.razor
67 lines (60 loc) · 1.76 KB
/
SyncStatus.razor
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
@inject LocalVehiclesStore LocalVehiclesStore
@inject IStringLocalizer<App> Localize
@if (isSynchronizing)
{
<span>@Localize["Updating..."]</span>
}
else if (lastSyncFailed)
{
<span>@Localize["Could not sync. Unsent edits:"] @outstandingLocalEdits</span>
<a class="toolbar-item toolbar-item-end toolbar-item-bg" @onclick="Synchronize">@Localize["Retry"]</a>
}
else
{
<span>@Localize["Last updated:"] @GetLastUpdatedText()</span>
<a class="toolbar-item toolbar-item-end" @onclick="Synchronize">@Localize["Update"]</a>
}
@code {
bool isSynchronizing;
DateTime? lastUpdated;
int outstandingLocalEdits;
bool lastSyncFailed;
protected override async Task OnInitializedAsync()
{
await Synchronize();
}
async Task Synchronize()
{
isSynchronizing = true;
lastSyncFailed = false;
try
{
await LocalVehiclesStore.SynchronizeAsync();
}
catch (Exception ex)
{
lastSyncFailed = true;
Console.WriteLine(ex);
}
finally
{
// Even if we weren't able to reach the server, we can update status based on local data
lastUpdated = await LocalVehiclesStore.GetLastUpdateDateAsync();
outstandingLocalEdits = (await LocalVehiclesStore.GetOutstandingLocalEditsAsync()).Length;
}
isSynchronizing = false;
}
string GetLastUpdatedText()
{
if (lastUpdated.HasValue)
{
return lastUpdated.Value.Date == DateTime.Now.Date
? lastUpdated.Value.ToLocalTime().ToShortTimeString()
: lastUpdated.Value.ToLocalTime().ToShortDateString();
}
else
{
return Localize["Never"];
}
}
}