|
| 1 | +@* The one thing a downloaded app has to be able to say: there is a newer one. |
| 2 | +
|
| 3 | + There is no auto-update and there will not be — the Windows build is unsigned, and a program that |
| 4 | + fetches and runs code on its own is the behaviour this project tells teachers to be suspicious of. |
| 5 | + So this reports a number and links to the notes. The person decides. |
| 6 | +
|
| 7 | + It asks before it checks. That is the whole reason this is a strip and not a silent background |
| 8 | + task: it would be the first network call the app makes without being asked, and a tool whose case |
| 9 | + rests on "nothing leaves your machine" does not get to make an exception quietly, even one that |
| 10 | + sends no text and no identifier. Asked once, remembered, changeable on /download. |
| 11 | +
|
| 12 | + Nothing renders in a browser tab, which is always whatever was last deployed. *@ |
| 13 | +@inherits LocalizedComponent |
| 14 | +@inject IUpdateCheck Updates |
| 15 | +@inject UpdatePreference Preference |
| 16 | +@inject HostCapabilities Host |
| 17 | + |
| 18 | +@if (_state is State.Asking) |
| 19 | +{ |
| 20 | + <div class="update-strip ask" role="status"> |
| 21 | + <div> |
| 22 | + <strong>@L["update.ask"]</strong> |
| 23 | + <span class="update-detail">@L["update.ask.detail"]</span> |
| 24 | + </div> |
| 25 | + <div class="update-actions"> |
| 26 | + <button class="ghost small" @onclick="@(() => Answer(true))">@L["update.yes"]</button> |
| 27 | + <button class="ghost small" @onclick="@(() => Answer(false))">@L["update.no"]</button> |
| 28 | + </div> |
| 29 | + </div> |
| 30 | +} |
| 31 | +else if (_state is State.Available && _found is { Latest: { } latest, Url: { } url }) |
| 32 | +{ |
| 33 | + <div class="update-strip found" role="status"> |
| 34 | + <div> |
| 35 | + <strong>@L.F("update.available", latest)</strong> |
| 36 | + @* Only when there is a build number to name. A host that cannot say which version it is |
| 37 | + has nothing useful to put in that sentence, and "you are running —" is worse than |
| 38 | + saying less. *@ |
| 39 | + @if (Host.Version is { } running) |
| 40 | + { |
| 41 | + <span class="update-detail">@L.F("update.available.detail", running)</span> |
| 42 | + } |
| 43 | + </div> |
| 44 | + <div class="update-actions"> |
| 45 | + <a class="ghost small" href="@url" target="_blank" rel="noopener">@L["update.see"]</a> |
| 46 | + <button class="ghost small" @onclick="Dismiss" title="@L["common.close"]">@L["update.dismiss"]</button> |
| 47 | + </div> |
| 48 | + </div> |
| 49 | +} |
| 50 | + |
| 51 | +@code { |
| 52 | + private enum State { Idle, Asking, Available } |
| 53 | + |
| 54 | + private State _state = State.Idle; |
| 55 | + private UpdateStatus? _found; |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// After the first render, never during it: reading the preference is a JavaScript call, and the |
| 59 | + /// check that may follow is a network one. Neither belongs in the path that puts the page on |
| 60 | + /// screen — a page that waits on GitHub before drawing has made somebody's essay wait on GitHub. |
| 61 | + /// </summary> |
| 62 | + protected override async Task OnAfterRenderAsync(bool firstRender) |
| 63 | + { |
| 64 | + if (!firstRender || !Updates.IsAvailable) return; |
| 65 | + |
| 66 | + var consent = await Preference.ConsentAsync(); |
| 67 | + if (consent is null) |
| 68 | + { |
| 69 | + _state = State.Asking; |
| 70 | + StateHasChanged(); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + if (consent is true) await Check(); |
| 75 | + } |
| 76 | + |
| 77 | + private async Task Answer(bool agreed) |
| 78 | + { |
| 79 | + await Preference.SetConsentAsync(agreed); |
| 80 | + _state = State.Idle; |
| 81 | + |
| 82 | + // Checking straight away, so saying yes does something visible rather than promising to do |
| 83 | + // something tomorrow. |
| 84 | + if (agreed) await Check(); |
| 85 | + else StateHasChanged(); |
| 86 | + } |
| 87 | + |
| 88 | + private async Task Check() |
| 89 | + { |
| 90 | + var today = DateOnly.FromDateTime(DateTime.Now); |
| 91 | + if (!await Preference.DueAsync(today)) return; |
| 92 | + |
| 93 | + var status = await Updates.CheckAsync(); |
| 94 | + await Preference.MarkCheckedAsync(today); |
| 95 | + |
| 96 | + // A failed check and an up-to-date build are the same answer here, and both are silence. |
| 97 | + if (!status.IsNewer || status.Latest is null) return; |
| 98 | + |
| 99 | + // Told once per version, not once per launch. |
| 100 | + if (await Preference.DismissedAsync() == status.Latest) return; |
| 101 | + |
| 102 | + _found = status; |
| 103 | + _state = State.Available; |
| 104 | + StateHasChanged(); |
| 105 | + } |
| 106 | + |
| 107 | + private async Task Dismiss() |
| 108 | + { |
| 109 | + if (_found?.Latest is { } latest) await Preference.DismissAsync(latest); |
| 110 | + _state = State.Idle; |
| 111 | + } |
| 112 | +} |
0 commit comments