Skip to content

Restore last opened room on app launch - #1097

Closed
longunmin wants to merge 1 commit into
commetchat:mainfrom
longunmin:restore-last-room
Closed

longunmin wants to merge 1 commit into
commetchat:mainfrom
longunmin:restore-last-room

Conversation

@longunmin

Copy link
Copy Markdown

Summary

Open the app back on the last room you were reading.

Commet currently always lands on the room list when cold-started. Most users who
open a chat app do it to continue a conversation they were in a moment ago —
especially on mobile where a phone-call, a doze, or a background-kill has closed
the app. This PR persists the last room you opened in each account and reopens
it on the next launch, mirroring the existing deep-link restore path.

This follows up on #778, which was scoped to spaces. The behavior here
covers the general (non-space) room-list case.

How it works

main_page.dart already handles "restore a room on launch" when the app is
opened via a deep link (see the block starting at
if (client == null && widget.initialRoom != null)). This PR reuses the same
pattern for the no-deep-link case and adds the persistence it needs:

  • preferences.dart

    • New _rememberedRooms preference — a Map<clientIdentifier, roomIdentifier>
      stored as JSON via SharedPreferences, matching the existing
      storeRoomsListCache pattern.
    • getRememberedRoom(clientId) / setRememberedRoom(clientId, roomId).
      An empty roomId removes the entry (logout-friendly hook, even though we
      don't wire the logout cleanup in this PR — the restore path already guards
      against a stale id, so a left-behind entry simply won't restore anything).
  • main_page.dart

    • In selectRoom(...): record room.client.identifier → room.identifier so
      the most recently opened room wins on next launch.
    • After the existing deep-link restore block in initState: if the user
      didn't come in via a deep link, and no room is currently selected, fetch
      the remembered room id for this client and selectRoom(...) it. We guard
      on client.isLoggedIn() and on currentRoom == null to keep the deep-link
      path and any manual navigation from being clobbered.

Multi-account: keys are per client.identifier, so logged-in accounts each get
their own remembered room. The app's existing single-"active" account model
means only one is visible on the front screen at a time, so no disambiguation
UI is needed.

What's not in this PR

Testing

Not compiled/flutter analyzed (no Flutter toolchain on the box that produced
this). The code is small, mirrors existing patterns in preferences.dart and
the initialRoom restore block in main_page.dart, and should pass CI as-is.
Manual test plan once CI is green:

  1. Log in to tcmatrix.duckdns.org → open Room A → force-stop the app → relaunch →
    expect to land in Room A.
  2. Switch to Room B in a different Space → force-stop → relaunch → expect to land
    in Room B (most-recently-opened wins).
  3. Open the app via the existing deep-link path (or a URL into a room) →
    expect to land in the deep-link room, not the remembered one.
  4. Log out, log back in → no crash, no phantom room restore.
  5. Multi-account: log in with account A (Room A), log in with account B (Room B),
    log out of A → relaunch → should land in B's last room (A is gone).

Files changed

  • commet/lib/config/preferences.dart (+30)
  • commet/lib/ui/pages/main/main_page.dart (+16)

Caveats / known limitations

  • "Last room" is per-account-global, not per-space. If you were in Room A of
    Space X, closed the app, and last touched an in-app Space Y screen, you'll
    still come back to A. That's the right default — but flagging it so we can
    follow up on [Feature] Return to last visited room when switching spaces #778 separately.
  • Doesn't persist "last room per view mode" (e.g. if you last saw the pinned /
    unread filter view, we ignore that). Trivial follow-up if wanted.

/cc @commetchat maintain — happy to adjust to fit house style / split into
smaller commits / add a unit test for getRememberedRoom/setRememberedRoom
whichever is preferred.

Persists the most recently selected room per account as a JSON
clientId->roomId map in preferences (following the existing
storeRoomsListCache pattern), records it in MainPage.selectRoom, and
re-opens it in initState when launched without a deep-link target.

Follow-up to commetchat#778 (spaces restore). Deep-link open-into-room remains
tracked under commetchat#1070 (MSC4550) and is out of scope here.
@Airyzz

Airyzz commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

closing as this seems AI generated. Please stick to the contribution guide

@Airyzz Airyzz closed this Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants