Skip to content

Commit

Permalink
Fix groups data
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Apr 7, 2024
1 parent 7549225 commit 15083b4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 21 deletions.
33 changes: 33 additions & 0 deletions app/src/main/java/io/nekohasekai/sfa/ui/dashboard/Groups.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,41 @@
package io.nekohasekai.sfa.ui.dashboard

import io.nekohasekai.libbox.OutboundGroup
import io.nekohasekai.libbox.OutboundGroupItem
import io.nekohasekai.libbox.OutboundGroupItemIterator

data class Group(
val tag: String,
val type: String,
val selectable: Boolean,
var selected: String,
var isExpand: Boolean,
var items: List<GroupItem>,
) {
constructor(item: OutboundGroup) : this(
item.tag,
item.type,
item.selectable,
item.selected,
item.isExpand,
item.items.toList().map { GroupItem(it) },
)
}

data class GroupItem(
val tag: String,
val type: String,
val urlTestTime: Long,
val urlTestDelay: Int,
) {
constructor(item: OutboundGroupItem) : this(
item.tag,
item.type,
item.urlTestTime,
item.urlTestDelay,
)
}

internal fun OutboundGroupItemIterator.toList(): List<OutboundGroupItem> {
val list = mutableListOf<OutboundGroupItem>()
while (hasNext()) {
Expand Down
36 changes: 15 additions & 21 deletions app/src/main/java/io/nekohasekai/sfa/ui/dashboard/GroupsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import androidx.recyclerview.widget.SimpleItemAnimator
import com.google.android.material.textfield.MaterialAutoCompleteTextView
import io.nekohasekai.libbox.Libbox
import io.nekohasekai.libbox.OutboundGroup
import io.nekohasekai.libbox.OutboundGroupItem
import io.nekohasekai.sfa.R
import io.nekohasekai.sfa.constant.Status
import io.nekohasekai.sfa.databinding.FragmentDashboardGroupsBinding
Expand Down Expand Up @@ -99,18 +98,18 @@ class GroupsFragment : Fragment(), CommandClient.Handler {
val adapter = adapter ?: return
activity?.runOnUiThread {
updateDisplayed(newGroups.isNotEmpty())
adapter.setGroups(newGroups)
adapter.setGroups(newGroups.map(::Group))
}
}

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

private lateinit var groups: MutableList<OutboundGroup>
private lateinit var groups: MutableList<Group>

@SuppressLint("NotifyDataSetChanged")
fun setGroups(newGroups: MutableList<OutboundGroup>) {
fun setGroups(newGroups: List<Group>) {
if (!::groups.isInitialized || groups.size != newGroups.size) {
groups = newGroups
groups = newGroups.toMutableList()
notifyDataSetChanged()
} else {
newGroups.forEachIndexed { index, group ->
Expand All @@ -119,7 +118,6 @@ class GroupsFragment : Fragment(), CommandClient.Handler {
notifyItemChanged(index)
}
}

}
}

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

private lateinit var group: OutboundGroup
private lateinit var items: MutableList<OutboundGroupItem>
private lateinit var group: Group
private lateinit var items: List<GroupItem>
private lateinit var adapter: ItemAdapter
private lateinit var textWatcher: TextWatcher

@OptIn(DelicateCoroutinesApi::class)
@SuppressLint("NotifyDataSetChanged")
fun bind(group: OutboundGroup) {
fun bind(group: Group) {
this.group = group
binding.groupName.text = group.tag
binding.groupType.text = Libbox.proxyDisplayType(group.type)
Expand All @@ -170,13 +168,9 @@ class GroupsFragment : Fragment(), CommandClient.Handler {
}
}
}
items = mutableListOf()
val itemIterator = group.items
while (itemIterator.hasNext()) {
items.add(itemIterator.next())
}
items = group.items
if (!::adapter.isInitialized) {
adapter = ItemAdapter(this, group, items)
adapter = ItemAdapter(this, group, items.toMutableList())
binding.itemList.adapter = adapter
(binding.itemList.itemAnimator as SimpleItemAnimator).supportsChangeAnimations =
false
Expand Down Expand Up @@ -231,7 +225,7 @@ class GroupsFragment : Fragment(), CommandClient.Handler {
}
}

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

private class ItemAdapter(
val groupView: GroupView,
var group: OutboundGroup,
private var items: MutableList<OutboundGroupItem> = mutableListOf()
var group: Group,
private var items: MutableList<GroupItem> = mutableListOf()
) :
RecyclerView.Adapter<ItemGroupView>() {

@SuppressLint("NotifyDataSetChanged")
fun setItems(newItems: MutableList<OutboundGroupItem>) {
fun setItems(newItems: List<GroupItem>) {
if (items.size != newItems.size) {
items = newItems
items = newItems.toMutableList()
notifyDataSetChanged()
} else {
newItems.forEachIndexed { index, item ->
Expand Down Expand Up @@ -285,7 +279,7 @@ class GroupsFragment : Fragment(), CommandClient.Handler {
RecyclerView.ViewHolder(binding.root) {

@OptIn(DelicateCoroutinesApi::class)
fun bind(groupView: GroupView, group: OutboundGroup, item: OutboundGroupItem) {
fun bind(groupView: GroupView, group: Group, item: GroupItem) {
if (group.selectable) {
binding.itemCard.setOnClickListener {
binding.selectedView.isVisible = true
Expand Down

0 comments on commit 15083b4

Please sign in to comment.