Skip to content
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

Merged
merged 17 commits into from
Jul 12, 2023
Merged

db refactor #973

merged 17 commits into from
Jul 12, 2023

Conversation

yate
Copy link
Contributor

@yate yate commented Jul 5, 2023

yate added 3 commits July 4, 2023 23:36
- reorganize db into different packages
- make all queries async, remove need for allowMainThreadQueries()
- refactor the way the viewModels were getting initialized
@dessalines
Copy link
Member

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

val account = getCurrentAccount(accountViewModel)

LaunchedEffect(account) {
if (account == null || account.id != -1) {
Copy link
Contributor Author

@yate yate Jul 5, 2023

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

Copy link
Member

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,
Copy link
Contributor Author

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")
Copy link
Contributor Author

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

@yate
Copy link
Contributor Author

yate commented Jul 5, 2023

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.

@yate yate marked this pull request as ready for review July 5, 2023 22:09
yate added 4 commits July 7, 2023 21:30
# 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
Copy link
Member

@dessalines dessalines left a 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) {
Copy link
Member

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),
Copy link
Member

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()
Copy link
Member

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.

yate added 3 commits July 11, 2023 21:13
# 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
@yate yate requested a review from MV-GH as a code owner July 12, 2023 03:14
@yate
Copy link
Contributor Author

yate commented Jul 12, 2023

@dessalines Thanks, I removed the ViewModel parameter defaults.

Copy link
Collaborator

@MV-GH MV-GH left a 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

Copy link
Member

@dessalines dessalines left a 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!

@dessalines dessalines merged commit d545c2c into LemmyNet:main Jul 12, 2023
@yate yate deleted the db-refactor branch July 12, 2023 17:13
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.

Remove the suppression for DB queries on UI thread check.
3 participants