postIt: SettingsPage is a singleton, navigation is idempotent
Two related changes that close the loop on the SettingsPage
push semantics.
1. The SettingsPage used to be registered as Transient. Each
click on the Paramètres button resolved a fresh instance,
re-bound it to the Settings singleton, and pushed it onto
the navigation stack. Repeated clicks accumulated stacked
instances, each fully bound, and the user had to tap Back
N times to leave. The fix is to register the page as a
Singleton in the DI container. There is now one and only
one SettingsPage ContentPage for the lifetime of the app:
- its DataContext is wired once, at composition time
(just after the ViewLocator is added to DataTemplates),
not on every push;
- the OpenSettingsRequested handler is a pure navigation
concern, with no DI resolution and no rebinding;
- the in-memory Settings state is preserved across visits
(any in-flight edit stays in the same instance).
2. The OpenSettingsRequested handler is guarded so that if the
SettingsPage is already at the top of NavigationStack, the
push is a no-op. NavigationPage.PushAsync does not
deduplicate; without the guard, calling it twice with the
same instance pushes it a second time, and the user has to
tap Back twice to leave. The guard is a reference comparison
on NavigationStack[Count - 1] against the singleton
instance, which is correct precisely because the page is
a singleton.
doc/architecture/postit.md is updated to match: the DI table
reflects the new lifetime, and the 'Garde anti-empilement'
section is rewritten from 'to be implemented' to the actual
implementation, including the rationale for reference
comparison and the cross-dependency between the singleton
lifetime and the guard.
The Settings-singleton invariant (in the same doc) is
unchanged: Settings is still a singleton, and adding a
transient override would still be the bug it always was.
The new SettingsPage singleton sits alongside it cleanly.
Build: 0 errors. Tests: 3/3 SettingsLoadTests green.
This commit is contained in:
parent
120bae6f5c
commit
1733dababb
2 changed files with 68 additions and 24 deletions
|
|
@ -60,7 +60,18 @@ public partial class App : Application
|
|||
|
||||
// Vues
|
||||
services.AddTransient<MainPage>();
|
||||
services.AddTransient<SettingsPage>();
|
||||
// SettingsPage is a singleton: there must be one and only one
|
||||
// instance of the settings UI for the lifetime of the app.
|
||||
// This guarantees that (a) the bindings always reflect the
|
||||
// current in-memory Settings state, (b) the page already has
|
||||
// its DataContext wired up at composition-root time (see
|
||||
// below), and (c) the OpenSettingsRequested handler is a
|
||||
// pure push with a no-op-if-already-on-top guard, never a
|
||||
// re-resolution from DI. Transient would let the user
|
||||
// accumulate stale SettingsPage instances on the navigation
|
||||
// stack, each bound to a fresh SettingsViewModel and missing
|
||||
// any in-flight edits.
|
||||
services.AddSingleton<SettingsPage>();
|
||||
services.AddTransient<HomePage>();
|
||||
services.AddTransient<SignaturePage>();
|
||||
|
||||
|
|
@ -93,6 +104,17 @@ public partial class App : Application
|
|||
DataTemplates.Clear();
|
||||
DataTemplates.Add(new ViewLocator(provider));
|
||||
|
||||
// Wire the Settings singleton onto the SettingsPage singleton
|
||||
// once, at composition time. The page is registered as a
|
||||
// singleton (see above) precisely so this binding is stable
|
||||
// for the lifetime of the app: every push to / pop from the
|
||||
// navigation stack finds the same ContentPage with the same
|
||||
// DataContext, and the TwoWay bindings inside the page keep
|
||||
// mutating the same in-memory Settings instance that the rest
|
||||
// of the app reads (OidcClientOptions construction, etc.).
|
||||
provider.GetRequiredService<SettingsPage>().DataContext =
|
||||
provider.GetRequiredService<Settings>();
|
||||
|
||||
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
||||
{
|
||||
var homePage = provider.GetRequiredService<HomePage>();
|
||||
|
|
@ -130,18 +152,30 @@ public partial class App : Application
|
|||
};
|
||||
|
||||
// When the user clicks the "Paramètres" button on the
|
||||
// session banner, push the SettingsPage on top of the
|
||||
// current navigation stack. Resolved from DI so the
|
||||
// ViewLocator + service-locator dance stays out of the
|
||||
// VM, and bound to the same Settings singleton the rest
|
||||
// of the app is using (the one we Load()'d at startup).
|
||||
// Two-way bindings on the page mutate that singleton
|
||||
// in place; callers re-read on next access.
|
||||
// session banner, push the SettingsPage singleton on top
|
||||
// of the current navigation stack. The DataContext is
|
||||
// already wired at composition time (see the
|
||||
// provider.GetRequiredService<SettingsPage>().DataContext
|
||||
// assignment above), so this handler is a pure
|
||||
// navigation concern.
|
||||
//
|
||||
// Anti-empilement guard: if the SettingsPage is already
|
||||
// at the top of the stack, do nothing. NavigationPage's
|
||||
// PushAsync does not deduplicate; calling it twice with
|
||||
// the same instance would push it a second time and the
|
||||
// user would have to tap Back twice to leave. Reference
|
||||
// comparison is correct here because SettingsPage is a
|
||||
// singleton — there is exactly one instance to compare
|
||||
// against.
|
||||
sessionStatus.OpenSettingsRequested += () =>
|
||||
{
|
||||
var w = (MainWindow)((IClassicDesktopStyleApplicationLifetime)ApplicationLifetime!).MainWindow!;
|
||||
var settingsPage = provider.GetRequiredService<SettingsPage>();
|
||||
settingsPage.DataContext = provider.GetRequiredService<Settings>();
|
||||
var stack = w.NavRoot.NavigationStack;
|
||||
if (stack.Count > 0 && ReferenceEquals(stack[stack.Count - 1], settingsPage))
|
||||
{
|
||||
return;
|
||||
}
|
||||
_ = w.NavRoot.PushAsync(settingsPage);
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue