Skip to content

db refactor#973

Merged
dessalines merged 17 commits into
LemmyNet:mainfrom
yate:db-refactor
Jul 12, 2023
Merged

db refactor#973
dessalines merged 17 commits into
LemmyNet:mainfrom
yate:db-refactor

Conversation

@yate

@yate yate commented Jul 5, 2023

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
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) {

@yate yate Jul 5, 2023

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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.

) {
LoginActivity(
navController = navController,
accountViewModel = accountViewModel,

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

Choose a reason for hiding this comment

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

@yate

yate commented Jul 5, 2023

Copy link
Copy Markdown
Contributor Author

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

@dessalines dessalines left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
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.

@Composable
fun CreatePostActivity(
accountViewModel: AccountViewModel,
accountViewModel: AccountViewModel = viewModel(factory = AccountSettingsViewModelFactory.Factory),

Copy link
Copy Markdown
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.

)

ShowChangelog(appSettingsViewModel = appSettingsViewModel)
ShowChangelog()

Copy link
Copy Markdown
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

yate commented Jul 12, 2023

Copy link
Copy Markdown
Contributor Author

@dessalines Thanks, I removed the ViewModel parameter defaults.

@MV-GH MV-GH left a comment

Copy link
Copy Markdown
Collaborator

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

@dessalines dessalines left a comment

Copy link
Copy Markdown
Member

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