-
Notifications
You must be signed in to change notification settings - Fork 169
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
db refactor #973
db refactor #973
Conversation
yate
commented
Jul 5, 2023
•
edited
Loading
edited
- Reorganize db into different packages (entity, dao, repository)
- Make all queries async, remove need for allowMainThreadQueries() (Fixes Remove the suppression for DB queries on UI thread check. #949)
- Refactor the way the viewModels were getting initialized with repositories based on this codelab https://developer.android.com/codelabs/basic-android-kotlin-compose-persisting-data-room#8
- 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
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.
Seems fine to me.
@@ -165,17 +165,14 @@ class MainActivity : AppCompatActivity() { | |||
) { | |||
LoginActivity( | |||
navController = navController, | |||
accountViewModel = accountViewModel, |
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
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.
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) { |
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.
Seems fine to me.
@@ -52,7 +53,7 @@ data class MetaDataRes(val title: String?, val loading: Boolean) | |||
|
|||
@Composable | |||
fun CreatePostActivity( | |||
accountViewModel: AccountViewModel, | |||
accountViewModel: AccountViewModel = viewModel(factory = AccountSettingsViewModelFactory.Factory), |
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.
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.
@@ -133,7 +133,7 @@ class MainActivity : AppCompatActivity() { | |||
appSettings.usePrivateTabs, | |||
) | |||
|
|||
ShowChangelog(appSettingsViewModel = appSettingsViewModel) | |||
ShowChangelog() |
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.
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. |
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.
Rather have this one merged before the others that modify appsettings
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.
K I just tested this locally, and it works great. Thx!