-
Notifications
You must be signed in to change notification settings - Fork 890
Fix: Remember last selected account after client restart #8991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1334,7 +1334,6 @@ UserModel *UserModel::instance() | |
| UserModel::UserModel(QObject *parent) | ||
| : QAbstractListModel(parent) | ||
| { | ||
| // TODO: Remember selected user from last quit via settings file | ||
| if (AccountManager::instance()->accounts().size() > 0) { | ||
| buildUserList(); | ||
| } | ||
|
|
@@ -1349,8 +1348,34 @@ void UserModel::buildUserList() | |
| auto user = AccountManager::instance()->accounts().at(i); | ||
| addUser(user); | ||
| } | ||
| if (_init) { | ||
| _users.first()->setCurrentUser(true); | ||
| if (_init && !_users.isEmpty()) { | ||
| // Try to restore the last selected account | ||
| ConfigFile cfg; | ||
| const auto lastSelectedAccountId = cfg.lastSelectedAccount(); | ||
|
|
||
| if (!lastSelectedAccountId.isEmpty()) { | ||
| // Find the account by id (more stable than displayName) | ||
| int foundIndex = -1; | ||
| for (int i = 0; i < _users.size(); i++) { | ||
| if (_users[i]->account()->id() == lastSelectedAccountId) { | ||
| foundIndex = i; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (foundIndex >= 0 && foundIndex < _users.size()) { | ||
| setCurrentUserId(foundIndex); | ||
| _init = false; | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| // Fallback to first account if last selected account not found | ||
| setCurrentUserId(0); | ||
| _init = false; | ||
| } else if (_init && _users.isEmpty()) { | ||
| // No accounts available - ensure initialization is complete | ||
| // _currentUserId remains -1 (no account selected) | ||
| _init = false; | ||
| } | ||
| } | ||
|
|
@@ -1525,6 +1550,13 @@ void UserModel::setCurrentUserId(const int id) | |
| emit currentUserChanged(); | ||
| } else if (_currentUserId != id) { | ||
| _currentUserId = id; | ||
|
|
||
| // Save the last selected account identifier (but not during initialization) | ||
| if (!_init) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should save the last selected account id even during initialization. What if the last selected account was an account that has been deleted somehow? Then the newly selected account won't be saved to config. Not a practical issue, but for the sake of consistency, I would remove this condition. |
||
| ConfigFile cfg; | ||
| cfg.setLastSelectedAccount(_users[id]->account()->id()); | ||
| } | ||
|
|
||
| emit currentUserChanged(); | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Complexity could be reduced here a bit, but more importantly,
returnat the middle might not be optimal. If later we add some more functionality after the_initsetup, it's very easy to miss thatreturn.