Skip to content

Commit 04eaba0

Browse files
david-allisonlukstbit
authored andcommitted
refactor: use BrowserColumnCellBinding
Issue 11116
1 parent 413caff commit 04eaba0

File tree

3 files changed

+82
-14
lines changed

3 files changed

+82
-14
lines changed

AnkiDroid/src/main/java/com/ichi2/anki/browser/BrowserMultiColumnAdapter.kt

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import android.view.ViewGroup
2929
import android.widget.CheckBox
3030
import android.widget.TextView
3131
import androidx.annotation.ColorInt
32-
import androidx.annotation.IdRes
3332
import androidx.annotation.VisibleForTesting
3433
import androidx.appcompat.widget.ThemeUtils
3534
import androidx.core.graphics.drawable.toDrawable
@@ -40,6 +39,8 @@ import com.ichi2.anki.AnkiDroidApp.Companion.sharedPrefs
4039
import com.ichi2.anki.Flag
4140
import com.ichi2.anki.R
4241
import com.ichi2.anki.common.annotations.NeedsTest
42+
import com.ichi2.anki.common.utils.ext.replaceWith
43+
import com.ichi2.anki.databinding.BrowserColumnCellBinding
4344
import com.ichi2.anki.databinding.CardItemBrowserBinding
4445
import com.ichi2.anki.utils.android.darkenColor
4546
import com.ichi2.anki.utils.android.lightenColorAbsolute
@@ -86,23 +87,15 @@ class BrowserMultiColumnAdapter(
8687
field = value
8788
// remove the past set of columns
8889
binding.root.removeChildren { it !is CheckBox }
89-
columnViews.clear()
9090

9191
val layoutInflater = LayoutInflater.from(context)
9292

93-
// inflates and returns the inflated view
94-
fun inflate(
95-
@IdRes id: Int,
96-
) = layoutInflater.inflate(id, binding.root, false).apply {
97-
binding.root.addView(this)
98-
}
99-
10093
// recreate the columns and the dividers
101-
(1..value).map { index ->
102-
inflate(R.layout.browser_column_cell).apply {
103-
columnViews.add(this as TextView)
104-
}
105-
}
94+
columnViews.replaceWith(
95+
(1..value).map { index ->
96+
BrowserColumnCellBinding.inflate(layoutInflater, binding.root, true).root
97+
},
98+
)
10699

107100
columnViews.forEach { it.setupTextSize() }
108101
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2025 David Allison <davidallisongithub@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify it under
5+
* the terms of the GNU General Public License as published by the Free Software
6+
* Foundation; either version 3 of the License, or (at your option) any later
7+
* version.
8+
*
9+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
10+
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
11+
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License along with
14+
* this program. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
17+
package com.ichi2.anki.common.utils.ext
18+
19+
/**
20+
* Replaces the contents of the list with [items].
21+
*
22+
* @param items the new contents of the list
23+
*/
24+
fun <T> MutableList<T>.replaceWith(items: Collection<T>) {
25+
clear()
26+
addAll(items)
27+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2025 David Allison <davidallisongithub@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify it under
5+
* the terms of the GNU General Public License as published by the Free Software
6+
* Foundation; either version 3 of the License, or (at your option) any later
7+
* version.
8+
*
9+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
10+
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
11+
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License along with
14+
* this program. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
17+
package com.ichi2.anki.common.utils.ext
18+
19+
import org.hamcrest.MatcherAssert.assertThat
20+
import org.hamcrest.Matchers.contains
21+
import org.hamcrest.Matchers.empty
22+
import org.junit.Test
23+
24+
class MutableListTest {
25+
@Test
26+
fun `replaceWith replaces contents`() {
27+
val list = mutableListOf(1, 2, 3)
28+
list.replaceWith(listOf(4, 5))
29+
30+
assertThat(list, contains(4, 5))
31+
}
32+
33+
@Test
34+
fun `replaceWith works on empty list`() {
35+
val list = mutableListOf<String>()
36+
list.replaceWith(listOf("a", "b"))
37+
38+
assertThat(list, contains("a", "b"))
39+
}
40+
41+
@Test
42+
fun `replaceWith works with empty input`() {
43+
val list = mutableListOf(1, 2, 3)
44+
list.replaceWith(emptyList())
45+
46+
assertThat(list, empty())
47+
}
48+
}

0 commit comments

Comments
 (0)