revert: Use pachliAccountId in MainActivity (#1267)

It won't be -1, which intent.pachliAccountId might be. Some of the
functions called expect it not to be -1, and crash if it is.
This commit is contained in:
Nik Clayton 2025-02-07 22:17:59 +01:00 committed by GitHub
parent 54e1978626
commit c67fb26c0b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 24 additions and 17 deletions

View File

@ -175,6 +175,7 @@ import dagger.hilt.android.AndroidEntryPoint
import de.c1710.filemojicompat_ui.helpers.EMOJI_PREFERENCE
import javax.inject.Inject
import kotlin.math.max
import kotlin.properties.Delegates
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.distinctUntilChangedBy
@ -254,6 +255,12 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
*/
private val listDrawerItems = mutableListOf<PrimaryDrawerItem>()
/**
* Version of [intent.pachliAccountId] where `-1` has been resolved to the
* actual active account value.
*/
private var pachliAccountId by Delegates.notNull<Long>()
/** Mutex to protect modifications to the drawer's items. */
private val drawerMutex = Mutex()
@ -493,7 +500,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
super.onMenuItemSelected(menuItem)
return when (menuItem.itemId) {
R.id.action_search -> {
startActivity(SearchActivityIntent(this@MainActivity, intent.pachliAccountId))
startActivity(SearchActivityIntent(this@MainActivity, pachliAccountId))
true
}
R.id.action_remove_tab -> {
@ -502,7 +509,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
true
}
R.id.action_tab_preferences -> {
startActivity(TabPreferenceActivityIntent(this, intent.pachliAccountId))
startActivity(TabPreferenceActivityIntent(this, pachliAccountId))
true
}
else -> super.onOptionsItemSelected(menuItem)
@ -545,7 +552,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
}
KeyEvent.KEYCODE_SEARCH -> {
startActivityWithDefaultTransition(
SearchActivityIntent(this, intent.pachliAccountId),
SearchActivityIntent(this, pachliAccountId),
)
return true
}
@ -555,7 +562,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
when (keyCode) {
KeyEvent.KEYCODE_N -> {
// open compose activity by pressing SHIFT + N (or CTRL + N)
val composeIntent = ComposeActivityIntent(applicationContext, intent.pachliAccountId)
val composeIntent = ComposeActivityIntent(applicationContext, pachliAccountId)
startActivity(composeIntent)
return true
}
@ -700,9 +707,11 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
}
uiResult.onSuccess { uiSuccess ->
when (uiSuccess) {
is UiSuccess.RefreshAccount -> { /* do nothing */ }
is UiSuccess.RefreshAccount -> {
/* do nothing */
}
is UiSuccess.SetActiveAccount -> { /* nothing */ }
is UiSuccess.SetActiveAccount -> pachliAccountId = uiSuccess.accountEntity.id
}
}
}
@ -1117,13 +1126,13 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
0 -> {
Timber.d("Clearing home timeline cache")
lifecycleScope.launch {
developerToolsUseCase.clearHomeTimelineCache(intent.pachliAccountId)
developerToolsUseCase.clearHomeTimelineCache(pachliAccountId)
}
}
1 -> {
Timber.d("Removing most recent 40 statuses")
lifecycleScope.launch {
developerToolsUseCase.deleteFirstKStatuses(intent.pachliAccountId, 40)
developerToolsUseCase.deleteFirstKStatuses(pachliAccountId, 40)
}
}
}
@ -1276,7 +1285,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
private fun refreshComposeButtonState(tabViewData: TabViewData) {
tabViewData.composeIntent?.let { intent ->
binding.composeButton.setOnClickListener {
startActivity(intent(applicationContext, this.intent.pachliAccountId))
startActivity(intent(applicationContext, pachliAccountId))
}
binding.composeButton.show()
} ?: binding.composeButton.hide()

View File

@ -129,7 +129,7 @@ class DraftHelper @Inject constructor(
statusId = statusId,
)
draftDao.insertOrReplace(draft)
draftDao.upsert(draft)
Timber.d("saved draft to db")
}

View File

@ -56,7 +56,7 @@ class DraftsViewModel @Inject constructor(
fun restoreDraft(draft: DraftEntity) {
viewModelScope.launch {
draftDao.insertOrReplace(draft)
draftDao.upsert(draft)
deletedDrafts.remove(draft)
}
}

View File

@ -20,16 +20,14 @@ package app.pachli.core.database.dao
import androidx.lifecycle.LiveData
import androidx.paging.PagingSource
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Upsert
import app.pachli.core.database.model.DraftEntity
@Dao
interface DraftDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertOrReplace(draft: DraftEntity)
@Upsert
suspend fun upsert(draft: DraftEntity)
@Query(
"""

View File

@ -283,7 +283,7 @@ class AccountEntityForeignKeyTest {
language = null,
statusId = null,
)
draftDao.insertOrReplace(draft)
draftDao.upsert(draft)
// Check everything is as expected.
assertThat(draftDao.loadDrafts(pachliAccountId)).containsExactly(draft)