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

Fixed CardBrowser Heading Language Change #17497

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
75 changes: 41 additions & 34 deletions AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -506,40 +506,6 @@ open class CardBrowser :
}
}

fun setupColumnSpinners() {
// Create a spinner for column 1
findViewById<Spinner>(R.id.browser_column1_spinner).apply {
adapter = ArrayAdapter(
this@CardBrowser,
android.R.layout.simple_spinner_item,
viewModel.column1Candidates.map { it.getLabel(viewModel.cardsOrNotes) }
).apply {
setDropDownViewResource(R.layout.spinner_custom_layout)
}
setSelection(COLUMN1_KEYS.indexOf(viewModel.column1))
onItemSelectedListener = BasicItemSelectedListener { pos, _ ->
viewModel.setColumn1(COLUMN1_KEYS[pos])
}
}

// Setup the column 2 heading as a spinner so that users can easily change the column type
findViewById<Spinner>(R.id.browser_column2_spinner).apply {
adapter = ArrayAdapter(
this@CardBrowser,
android.R.layout.simple_spinner_item,
viewModel.column2Candidates.map { it.getLabel(viewModel.cardsOrNotes) }
).apply {
// The custom layout for the adapter is used to prevent the overlapping of various interactive components on the screen
setDropDownViewResource(R.layout.spinner_custom_layout)
}
setSelection(COLUMN2_KEYS.indexOf(viewModel.column2))
// Create a new list adapter with updated column map any time the user changes the column
onItemSelectedListener = BasicItemSelectedListener { pos, _ ->
viewModel.setColumn2(COLUMN2_KEYS[pos])
}
}
}

fun initCompletedChanged(completed: Boolean) {
if (!completed) return

Expand Down Expand Up @@ -576,6 +542,41 @@ open class CardBrowser :
viewModel.flowOfCardsOrNotes.launchCollectionInLifecycleScope(::cardsOrNotesChanged)
}

private fun setupColumnSpinners() {
Copy link
Member

Choose a reason for hiding this comment

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

as below, revert + react to the ViewModel change

// Create a spinner for column 1
findViewById<Spinner>(R.id.browser_column1_spinner).apply {
adapter = ArrayAdapter(
this@CardBrowser,
android.R.layout.simple_spinner_item,
viewModel.column1Candidates.map { it.getLabel(viewModel.cardsOrNotes) }
).apply {
setDropDownViewResource(R.layout.spinner_custom_layout)
}
Timber.i("Language:${viewModel.column1Candidates.map { it.getLabel(viewModel.cardsOrNotes) }}")
setSelection(COLUMN1_KEYS.indexOf(viewModel.column1))
onItemSelectedListener = BasicItemSelectedListener { pos, _ ->
viewModel.setColumn1(COLUMN1_KEYS[pos])
}
}

// Setup the column 2 heading as a spinner so that users can easily change the column type
findViewById<Spinner>(R.id.browser_column2_spinner).apply {
adapter = ArrayAdapter(
this@CardBrowser,
android.R.layout.simple_spinner_item,
viewModel.column2Candidates.map { it.getLabel(viewModel.cardsOrNotes) }
).apply {
// The custom layout for the adapter is used to prevent the overlapping of various interactive components on the screen
setDropDownViewResource(R.layout.spinner_custom_layout)
}
setSelection(COLUMN2_KEYS.indexOf(viewModel.column2))
// Create a new list adapter with updated column map any time the user changes the column
onItemSelectedListener = BasicItemSelectedListener { pos, _ ->
viewModel.setColumn2(COLUMN2_KEYS[pos])
}
}
}

// Finish initializing the activity after the collection has been correctly loaded
override fun onCollectionLoaded(col: Collection) {
super.onCollectionLoaded(col)
Expand Down Expand Up @@ -918,6 +919,12 @@ open class CardBrowser :

override fun onResume() {
david-allison marked this conversation as resolved.
Show resolved Hide resolved
super.onResume()
if (viewModel.flowOfInitCompleted.value) {
soCallmeAdityaKumar marked this conversation as resolved.
Show resolved Hide resolved
launchCatchingTask {
viewModel.setUpColumns()
david-allison marked this conversation as resolved.
Show resolved Hide resolved
setupColumnSpinners()
david-allison marked this conversation as resolved.
Show resolved Hide resolved
}
}
selectNavigationItem(R.id.nav_browser)
updateNumCardsToRender()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,7 @@ class CardBrowserViewModel(
val cardsOrNotes = withCol { CardsOrNotes.fromCollection(this@withCol) }
flowOfCardsOrNotes.update { cardsOrNotes }

val allColumns = withCol { allBrowserColumns() }.associateBy { it.key }
column1Candidates = CardBrowserColumn.COLUMN1_KEYS.map { allColumns[it.ankiColumnKey]!! }
column2Candidates = CardBrowserColumn.COLUMN2_KEYS.map { allColumns[it.ankiColumnKey]!! }

setUpColumns()
Copy link
Member

Choose a reason for hiding this comment

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

Rename: two methods one after the other with the same name

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

setupColumns(cardsOrNotes)

withCol {
Expand All @@ -331,6 +328,11 @@ class CardBrowserViewModel(
}
}

suspend fun setUpColumns() {
val allColumns = withCol { allBrowserColumns() }.associateBy { it.key }
column1Candidates = CardBrowserColumn.COLUMN1_KEYS.map { allColumns[it.ankiColumnKey]!! }
column2Candidates = CardBrowserColumn.COLUMN2_KEYS.map { allColumns[it.ankiColumnKey]!! }
}
private suspend fun setupColumns(cardsOrNotes: CardsOrNotes) {
Timber.d("loading columns columns for %s mode", cardsOrNotes)
val columns = BrowserColumnCollection.load(sharedPrefs(), cardsOrNotes)
Expand Down