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

- 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
This commit is contained in:
Konrad Pozniak 2025-01-17 12:35:35 +01:00 committed by GitHub
commit 9e597800c9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
47 changed files with 2421 additions and 1127 deletions

View file

@ -3,7 +3,10 @@ package com.keylesspalace.tusky
import android.app.Activity
import android.app.NotificationManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import androidx.viewpager2.widget.ViewPager2
@ -12,13 +15,15 @@ import at.connyduck.calladapter.networkresult.NetworkResult
import com.keylesspalace.tusky.appstore.EventHub
import com.keylesspalace.tusky.components.accountlist.AccountListActivity
import com.keylesspalace.tusky.components.systemnotifications.NotificationHelper
import com.keylesspalace.tusky.db.AccountManager
import com.keylesspalace.tusky.db.entity.AccountEntity
import com.keylesspalace.tusky.entity.Account
import com.keylesspalace.tusky.entity.Notification
import com.keylesspalace.tusky.entity.TimelineAccount
import com.keylesspalace.tusky.network.MastodonApi
import com.keylesspalace.tusky.util.getSerializableExtraCompat
import java.util.Date
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.flow.MutableStateFlow
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
@ -128,17 +133,20 @@ class MainActivityTest {
private fun startMainActivity(intent: Intent): Activity {
val controller = Robolectric.buildActivity(MainActivity::class.java, intent)
val activity = controller.get()
activity.eventHub = EventHub()
activity.accountManager = mock {
val eventHub = EventHub()
activity.eventHub = eventHub
val accountManager: AccountManager = mock {
on { accounts } doReturn listOf(accountEntity)
on { accountsFlow } doReturn MutableStateFlow(listOf(accountEntity))
on { activeAccount } doReturn accountEntity
}
activity.draftsAlert = mock {}
activity.shareShortcutHelper = mock {}
activity.externalScope = TestScope()
activity.mastodonApi = mock {
activity.accountManager = accountManager
activity.draftsAlert = mock { }
val api: MastodonApi = mock {
onBlocking { accountVerifyCredentials() } doReturn NetworkResult.success(account)
onBlocking { announcements() } doReturn NetworkResult.success(emptyList())
}
activity.mastodonApi = api
activity.preferences = mock(defaultAnswer = {
when (it.method.returnType) {
String::class.java -> "test"
@ -146,6 +154,20 @@ class MainActivityTest {
else -> null
}
})
val viewModel = MainViewModel(
context = mock {
on { getSystemService(Context.NOTIFICATION_SERVICE) } doReturn mock<NotificationManager>()
},
api = api,
eventHub = eventHub,
accountManager = accountManager,
shareShortcutHelper = mock()
)
val testViewModelFactory = viewModelFactory {
initializer { viewModel }
}
activity.viewModelProviderFactory = testViewModelFactory
controller.create().start()
return activity
}