Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.annotation.IdRes
import androidx.annotation.VisibleForTesting
import androidx.appcompat.widget.ThemeUtils
import androidx.core.graphics.drawable.toDrawable
Expand All @@ -40,6 +39,8 @@ import com.ichi2.anki.AnkiDroidApp.Companion.sharedPrefs
import com.ichi2.anki.Flag
import com.ichi2.anki.R
import com.ichi2.anki.common.annotations.NeedsTest
import com.ichi2.anki.common.utils.ext.replaceWith
import com.ichi2.anki.databinding.BrowserColumnCellBinding
import com.ichi2.anki.databinding.CardItemBrowserBinding
import com.ichi2.anki.utils.android.darkenColor
import com.ichi2.anki.utils.android.lightenColorAbsolute
Expand Down Expand Up @@ -86,23 +87,15 @@ class BrowserMultiColumnAdapter(
field = value
// remove the past set of columns
binding.root.removeChildren { it !is CheckBox }
columnViews.clear()

val layoutInflater = LayoutInflater.from(context)

// inflates and returns the inflated view
fun inflate(
@IdRes id: Int,
) = layoutInflater.inflate(id, binding.root, false).apply {
binding.root.addView(this)
}

// recreate the columns and the dividers
(1..value).map { index ->
inflate(R.layout.browser_column_cell).apply {
columnViews.add(this as TextView)
}
}
columnViews.replaceWith(
(1..value).map { index ->
BrowserColumnCellBinding.inflate(layoutInflater, binding.root, true).root
},
)

columnViews.forEach { it.setupTextSize() }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2025 David Allison <davidallisongithub@gmail.com>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.ichi2.anki.common.utils.ext

/**
* Replaces the contents of the list with [items].
*
* @param items the new contents of the list
*/
fun <T> MutableList<T>.replaceWith(items: Collection<T>) {
clear()
addAll(items)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2025 David Allison <davidallisongithub@gmail.com>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.ichi2.anki.common.utils.ext

import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.contains
import org.hamcrest.Matchers.empty
import org.junit.Test

class MutableListTest {
@Test
fun `replaceWith replaces contents`() {
val list = mutableListOf(1, 2, 3)
list.replaceWith(listOf(4, 5))

assertThat(list, contains(4, 5))
}

@Test
fun `replaceWith works on empty list`() {
val list = mutableListOf<String>()
list.replaceWith(listOf("a", "b"))

assertThat(list, contains("a", "b"))
}

@Test
fun `replaceWith works with empty input`() {
val list = mutableListOf(1, 2, 3)
list.replaceWith(emptyList())

assertThat(list, empty())
}
}
Loading