Skip to content

Commit 15083b4

Browse files
committed
Fix groups data
1 parent 7549225 commit 15083b4

File tree

2 files changed

+48
-21
lines changed

2 files changed

+48
-21
lines changed

app/src/main/java/io/nekohasekai/sfa/ui/dashboard/Groups.kt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,41 @@
11
package io.nekohasekai.sfa.ui.dashboard
22

3+
import io.nekohasekai.libbox.OutboundGroup
34
import io.nekohasekai.libbox.OutboundGroupItem
45
import io.nekohasekai.libbox.OutboundGroupItemIterator
56

7+
data class Group(
8+
val tag: String,
9+
val type: String,
10+
val selectable: Boolean,
11+
var selected: String,
12+
var isExpand: Boolean,
13+
var items: List<GroupItem>,
14+
) {
15+
constructor(item: OutboundGroup) : this(
16+
item.tag,
17+
item.type,
18+
item.selectable,
19+
item.selected,
20+
item.isExpand,
21+
item.items.toList().map { GroupItem(it) },
22+
)
23+
}
24+
25+
data class GroupItem(
26+
val tag: String,
27+
val type: String,
28+
val urlTestTime: Long,
29+
val urlTestDelay: Int,
30+
) {
31+
constructor(item: OutboundGroupItem) : this(
32+
item.tag,
33+
item.type,
34+
item.urlTestTime,
35+
item.urlTestDelay,
36+
)
37+
}
38+
639
internal fun OutboundGroupItemIterator.toList(): List<OutboundGroupItem> {
740
val list = mutableListOf<OutboundGroupItem>()
841
while (hasNext()) {

app/src/main/java/io/nekohasekai/sfa/ui/dashboard/GroupsFragment.kt

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import androidx.recyclerview.widget.SimpleItemAnimator
1818
import com.google.android.material.textfield.MaterialAutoCompleteTextView
1919
import io.nekohasekai.libbox.Libbox
2020
import io.nekohasekai.libbox.OutboundGroup
21-
import io.nekohasekai.libbox.OutboundGroupItem
2221
import io.nekohasekai.sfa.R
2322
import io.nekohasekai.sfa.constant.Status
2423
import io.nekohasekai.sfa.databinding.FragmentDashboardGroupsBinding
@@ -99,18 +98,18 @@ class GroupsFragment : Fragment(), CommandClient.Handler {
9998
val adapter = adapter ?: return
10099
activity?.runOnUiThread {
101100
updateDisplayed(newGroups.isNotEmpty())
102-
adapter.setGroups(newGroups)
101+
adapter.setGroups(newGroups.map(::Group))
103102
}
104103
}
105104

106105
private class Adapter : RecyclerView.Adapter<GroupView>() {
107106

108-
private lateinit var groups: MutableList<OutboundGroup>
107+
private lateinit var groups: MutableList<Group>
109108

110109
@SuppressLint("NotifyDataSetChanged")
111-
fun setGroups(newGroups: MutableList<OutboundGroup>) {
110+
fun setGroups(newGroups: List<Group>) {
112111
if (!::groups.isInitialized || groups.size != newGroups.size) {
113-
groups = newGroups
112+
groups = newGroups.toMutableList()
114113
notifyDataSetChanged()
115114
} else {
116115
newGroups.forEachIndexed { index, group ->
@@ -119,7 +118,6 @@ class GroupsFragment : Fragment(), CommandClient.Handler {
119118
notifyItemChanged(index)
120119
}
121120
}
122-
123121
}
124122
}
125123

@@ -148,14 +146,14 @@ class GroupsFragment : Fragment(), CommandClient.Handler {
148146
private class GroupView(val binding: ViewDashboardGroupBinding) :
149147
RecyclerView.ViewHolder(binding.root) {
150148

151-
private lateinit var group: OutboundGroup
152-
private lateinit var items: MutableList<OutboundGroupItem>
149+
private lateinit var group: Group
150+
private lateinit var items: List<GroupItem>
153151
private lateinit var adapter: ItemAdapter
154152
private lateinit var textWatcher: TextWatcher
155153

156154
@OptIn(DelicateCoroutinesApi::class)
157155
@SuppressLint("NotifyDataSetChanged")
158-
fun bind(group: OutboundGroup) {
156+
fun bind(group: Group) {
159157
this.group = group
160158
binding.groupName.text = group.tag
161159
binding.groupType.text = Libbox.proxyDisplayType(group.type)
@@ -170,13 +168,9 @@ class GroupsFragment : Fragment(), CommandClient.Handler {
170168
}
171169
}
172170
}
173-
items = mutableListOf()
174-
val itemIterator = group.items
175-
while (itemIterator.hasNext()) {
176-
items.add(itemIterator.next())
177-
}
171+
items = group.items
178172
if (!::adapter.isInitialized) {
179-
adapter = ItemAdapter(this, group, items)
173+
adapter = ItemAdapter(this, group, items.toMutableList())
180174
binding.itemList.adapter = adapter
181175
(binding.itemList.itemAnimator as SimpleItemAnimator).supportsChangeAnimations =
182176
false
@@ -231,7 +225,7 @@ class GroupsFragment : Fragment(), CommandClient.Handler {
231225
}
232226
}
233227

234-
fun updateSelected(group: OutboundGroup, itemTag: String) {
228+
fun updateSelected(group: Group, itemTag: String) {
235229
val oldSelected = items.indexOfFirst { it.tag == group.selected }
236230
group.selected = itemTag
237231
if (oldSelected != -1) {
@@ -242,15 +236,15 @@ class GroupsFragment : Fragment(), CommandClient.Handler {
242236

243237
private class ItemAdapter(
244238
val groupView: GroupView,
245-
var group: OutboundGroup,
246-
private var items: MutableList<OutboundGroupItem> = mutableListOf()
239+
var group: Group,
240+
private var items: MutableList<GroupItem> = mutableListOf()
247241
) :
248242
RecyclerView.Adapter<ItemGroupView>() {
249243

250244
@SuppressLint("NotifyDataSetChanged")
251-
fun setItems(newItems: MutableList<OutboundGroupItem>) {
245+
fun setItems(newItems: List<GroupItem>) {
252246
if (items.size != newItems.size) {
253-
items = newItems
247+
items = newItems.toMutableList()
254248
notifyDataSetChanged()
255249
} else {
256250
newItems.forEachIndexed { index, item ->
@@ -285,7 +279,7 @@ class GroupsFragment : Fragment(), CommandClient.Handler {
285279
RecyclerView.ViewHolder(binding.root) {
286280

287281
@OptIn(DelicateCoroutinesApi::class)
288-
fun bind(groupView: GroupView, group: OutboundGroup, item: OutboundGroupItem) {
282+
fun bind(groupView: GroupView, group: Group, item: GroupItem) {
289283
if (group.selectable) {
290284
binding.itemCard.setOnClickListener {
291285
binding.selectedView.isVisible = true

0 commit comments

Comments
 (0)