db refactor#973
Conversation
- reorganize db into different packages - make all queries async, remove need for allowMainThreadQueries() - refactor the way the viewModels were getting initialized
|
I'm fairly busy, but ping me when you're ready for me to take a look. |
|
|
||
| fun CreationExtras.jerboaApplication(): JerboaApplication = | ||
| (this[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY] as JerboaApplication) | ||
|
|
There was a problem hiding this comment.
This is referenced in view model factories here https://github.com/dessalines/jerboa/pull/973/files#diff-41754fa505a06441b96271ba020ae2e014472578466e14a44fd7ce01a6c578f4R37, based on the compose room workshop again https://github.com/google-developer-training/basic-android-kotlin-compose-training-inventory-app/blob/room/app/src/main/java/com/example/inventory/ui/AppViewModelProvider.kt#L65
| val account = getCurrentAccount(accountViewModel) | ||
|
|
||
| LaunchedEffect(account) { | ||
| if (account == null || account.id != -1) { |
There was a problem hiding this comment.
There was an occasional race condition type issue where getCurrentAccount returns an initial value of null because it hasn't gotten the data from the database yet and there was no initial state set on the observeAsState method here https://github.com/dessalines/jerboa/pull/973/files#diff-697367ddff23d571ded643b597f312aeba4b29669b058e9edb03edf9bb030d92R14. It ended up making two calls to fetchInitialData. Once with the null initial value, then again with the actual data from the database. This caused two site data calls to go out, depending on which one returned first, the user data in the siteViewModel could set incorrectly set to the wrong value.
As a solution to this, I set an initial state value in observeAsState, so I know if the account id is -1, we haven't actually gotten any data yet from Room, so there is no need to call fetchInitialData. If there is no account in Room, it will return null and work like before.
Let me know if there's a better way to handle something like this
| ) { | ||
| LoginActivity( | ||
| navController = navController, | ||
| accountViewModel = accountViewModel, |
There was a problem hiding this comment.
I saw in the workshop, the viewModel parameters weren't passed in directly, they used the factory in the composable instead, like https://github.com/dessalines/jerboa/pull/973/files#diff-6ca2c69ae054a88358d16c98f47efd02bb16eae48a09295b4db9bd5e99ba8e76R25.
| @Query("SELECT * FROM account") | ||
| fun getAll(): LiveData<List<Account>> | ||
|
|
||
| @Query("SELECT * FROM account where current = 1 limit 1") |
There was a problem hiding this comment.
Made a query to get the current account instead of getting all and fetching the first here https://github.com/dessalines/jerboa/pull/973/files#diff-697367ddff23d571ded643b597f312aeba4b29669b058e9edb03edf9bb030d92L24
|
Thanks @dessalines, it should be ready. I added some initial comments. It's mostly large because of the reorganization of the db package, but I also changed the way the view models/factories were getting initialized which could all be split into a separate PR, or removed, if you prefer. |
# Conflicts: # app/src/main/java/com/jerboa/db/AppDB.kt
# Conflicts: # app/src/main/java/com/jerboa/Utils.kt # app/src/main/java/com/jerboa/ui/components/comment/reply/CommentReplyActivity.kt # app/src/main/java/com/jerboa/ui/components/community/list/CommunityListActivity.kt # app/src/main/java/com/jerboa/ui/components/inbox/InboxActivity.kt # app/src/main/java/com/jerboa/ui/components/login/LoginActivity.kt # app/src/main/java/com/jerboa/ui/components/privatemessage/PrivateMessageReplyActivity.kt # app/src/main/java/com/jerboa/ui/components/report/comment/CreateCommentReportActivity.kt # app/src/main/java/com/jerboa/ui/components/report/post/CreatePostReportActivity.kt # app/src/main/java/com/jerboa/ui/components/settings/account/AccountSettingsViewModel.kt
dessalines
left a comment
There was a problem hiding this comment.
Looks good, but please remove the defaults, and make the requirement explicit again.
| val account = getCurrentAccount(accountViewModel) | ||
|
|
||
| LaunchedEffect(account) { | ||
| if (account == null || account.id != -1) { |
| @Composable | ||
| fun CreatePostActivity( | ||
| accountViewModel: AccountViewModel, | ||
| accountViewModel: AccountViewModel = viewModel(factory = AccountSettingsViewModelFactory.Factory), |
There was a problem hiding this comment.
With regards to all of these, I think I'd much prefer them not having defaults, and filling this from MainActivity. Its much more explicit that way, and I've tried to avoid defaults, especially for viewmodels, wherever possible.
| ) | ||
|
|
||
| ShowChangelog(appSettingsViewModel = appSettingsViewModel) | ||
| ShowChangelog() |
There was a problem hiding this comment.
Stuff like this scares me, it seems like it has no actual dependencies now, when in reality the appSettings does need to be injected.
# Conflicts: # app/src/main/java/com/jerboa/db/AppDB.kt # app/src/main/java/com/jerboa/model/AccountSettingsViewModel.kt # app/src/main/java/com/jerboa/ui/components/home/Home.kt # app/src/main/java/com/jerboa/ui/components/home/HomeActivity.kt
- fix drawer not showing switch account/sign out
|
@dessalines Thanks, I removed the ViewModel parameter defaults. |
MV-GH
left a comment
There was a problem hiding this comment.
Rather have this one merged before the others that modify appsettings
dessalines
left a comment
There was a problem hiding this comment.
K I just tested this locally, and it works great. Thx!
Uh oh!
There was an error while loading. Please reload this page.