fix: Prevent NPE in AccountViewModel.setAccountInfo#2175
Merged
nikclayton merged 3 commits intopachli:mainfrom Mar 19, 2026
Merged
fix: Prevent NPE in AccountViewModel.setAccountInfo#2175nikclayton merged 3 commits intopachli:mainfrom
nikclayton merged 3 commits intopachli:mainfrom
Conversation
Instead of the implementation copied into that module. Rename `Loadable.get()` to `getOrNull()` to be consistent with similarly named methods in other packages.
Previous code could crash using:
```kotlin
private val activeAccount = accountManager.activeAccount!!
...
fun setAccountInfo(accountId: String) {
this.accountId = accountId
// NPE on next line
this.isSelf = activeAccount.accountId == accountId
reload(false)
}
```
Fix this by starting to refactor `AccountViewModel` to expose all of
this information as flows with correct dependencies between them.
Not all observables are converted to flows, only the minimum needed to
fix this problem. Future work will convert the other observables and
update the `AccountViewModel` design.
Concrete changes:
- `AccountActivity.loadedAccount` is gone, `AccountViewModel.accountData`
is created and collected. This emits a
`Result<Loadable.Loaded<Account>, GetAccountError>` to handle failure
cases.
- Functions that need the account (like `updateAccountAvatar()`) now
take the account as a parameter, instead of reading it from a nullable
`loadedAccount`.
- Similar changes for `AccountViewModel.isSelf`. This is now a stateflow
allowing code to react to it changing while also using the current value
in code that can't react.
- Create `AccountRepository` and related classes to start holding code
that interacts with accounts. Make this injectable with Hilt.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Previous code could crash using:
Fix this by starting to refactor
AccountViewModelto expose all ofthis information as flows with correct dependencies between them.
Not all observables are converted to flows, only the minimum needed to
fix this problem. Future work will convert the other observables and
update the
AccountViewModeldesign.Concrete changes:
AccountActivity.loadedAccountis gone,AccountViewModel.accountDatais created and collected. This emits a
Result<Loadable.Loaded<Account>, GetAccountError>to handle failurecases.
updateAccountAvatar()) nowtake the account as a parameter, instead of reading it from a nullable
loadedAccount.AccountViewModel.isSelf. This is now a stateflowallowing code to react to it changing while also using the current value
in code that can't react.
AccountRepositoryand related classes to start holding codethat interacts with accounts. Make this injectable with Hilt.