Skip to content

Commit

Permalink
revert: Use pachliAccountId in MainActivity (#1267)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nikclayton authored Feb 7, 2025
1 parent 54e1978 commit c67fb26
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
27 changes: 18 additions & 9 deletions app/src/main/java/app/pachli/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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 -> {
Expand All @@ -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)
Expand Down Expand Up @@ -545,7 +552,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
}
KeyEvent.KEYCODE_SEARCH -> {
startActivityWithDefaultTransition(
SearchActivityIntent(this, intent.pachliAccountId),
SearchActivityIntent(this, pachliAccountId),
)
return true
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
}
}
Expand Down Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class DraftHelper @Inject constructor(
statusId = statusId,
)

draftDao.insertOrReplace(draft)
draftDao.upsert(draft)
Timber.d("saved draft to db")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class DraftsViewModel @Inject constructor(

fun restoreDraft(draft: DraftEntity) {
viewModelScope.launch {
draftDao.insertOrReplace(draft)
draftDao.upsert(draft)
deletedDrafts.remove(draft)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c67fb26

Please sign in to comment.