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

Move all database queries off the ui thread & add a ViewModel for MainActivity #4786

Open
wants to merge 29 commits into
base: develop
Choose a base branch
from

Conversation

connyduck
Copy link
Collaborator

@connyduck connyduck commented Dec 3, 2024

  • Move all database queries off the ui thread - this is a massive performance improvement
  • ViewModel for MainActivity - this makes MainActivity smaller and network requests won't be retried when rotating the screen
  • removes the Push Notification Migration feature. We had it long enough, all users who want push notifications should be migrated by now
  • AccountEntity is now immutable
  • converted BaseActivity to Kotlin
  • The header image of Accounts is now cached as well

@@ -46,7 +46,6 @@ object StorageModule {
fun providesDatabase(@ApplicationContext appContext: Context, converters: Converters): AppDatabase {
return Room.databaseBuilder(appContext, AppDatabase::class.java, "tuskyDB")
.addTypeConverter(converters)
.allowMainThreadQueries()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

🥳

@connyduck connyduck marked this pull request as ready for review January 6, 2025 10:35
@connyduck connyduck requested review from charlag, Tak and Lakoja January 6, 2025 10:35
@@ -0,0 +1,259 @@
/* Copyright 2024 Tusky Contributors
Copy link
Collaborator

Choose a reason for hiding this comment

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

We still have the year numbers?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, why not. I think it is interesting to see when a file was created.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Well, it was sort of created in 2018, I guess. The original that is. :-)

Meaning: I think it doesn't really help.

(Just an observation.)

app/src/main/java/com/keylesspalace/tusky/MainActivity.kt Outdated Show resolved Hide resolved
app/src/main/java/com/keylesspalace/tusky/MainActivity.kt Outdated Show resolved Hide resolved
@@ -75,7 +77,7 @@ class NotificationPreferencesFragment : PreferenceFragmentCompat() {
isIconSpaceReserved = false
isChecked = activeAccount.notificationsFollowRequested
setOnPreferenceChangeListener { _, newValue ->
updateAccount { it.notificationsFollowRequested = newValue as Boolean }
updateAccount { copy(notificationsFollowRequested = newValue as Boolean) }
Copy link
Collaborator

Choose a reason for hiding this comment

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

What does "copy" do?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Create a new instance with all the same attributes except those specified. It is a feature of every data class in Kotlin.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, never seen it (which doesn't mean much).

But I am looking at it for half an hour now and have a sort of comprehension question. :-)

I see that AccountEntity now is read-only. And this "copy + changer" is a way to deal with that fact. Meaning it can be changed.

Question: What is the advantage now?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Before: You get an AccountEntity, and then you need to check if it changed. Everyone needs the same instance, and on every update it must be written to the database or it gets inconsistent.
Now: You subscribe to a flow that will notify you if an AccountEntity changed. Changed in the database, that is. If someone just "changed" it by making a copy, nobody gets notified unless the copy is written to the database.

Less room for error, more stuff happening automatically if you will.

@Lakoja
Copy link
Collaborator

Lakoja commented Jan 12, 2025

Maybe the commits can all be squashed?

(Most of the messages are not really helpful.)

# Conflicts:
#	app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationsViewModel.kt
#	app/src/main/java/com/keylesspalace/tusky/components/systemnotifications/NotificationFetcher.kt
#	app/src/main/java/com/keylesspalace/tusky/components/timeline/viewmodel/CachedTimelineViewModel.kt
@@ -109,8 +109,6 @@ class NotificationFetcher @Inject constructor(

// NOTE having multiple summary notifications this here should still collapse them in only one occurrence
notificationManagerCompat.notify(newNotifications)

accountManager.saveAccount(account)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Todo: Find what this line did

@connyduck
Copy link
Collaborator Author

Maybe the commits can all be squashed?

(Most of the messages are not really helpful.)

The whole branch will be squashed-and-merged. I'll try to improve my commit messages 😅

Copy link
Collaborator

@Tak Tak 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, I'll start running it

@@ -587,6 +517,10 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
startActivity(composeIntent)
}

override fun finish() {
super.finish()
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Needed this to easily set a breakpoint for debugging, will remove, thx

val accountsFlow: StateFlow<List<AccountEntity>> = runBlocking {
accountDao.allAccounts()
.stateIn(CoroutineScope(applicationScope.coroutineContext + Dispatchers.IO))
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

We do a lot of searching the list by id, would it be nontrivial to make this a map of id to account?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes that should be easy with associate. But actually the "a lot of searching the list by id" is a mistake, this should only happen once in AccountManager and everything else should use that code, let me change this.

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.

3 participants