Wait for size to be submitted

This commit is contained in:
Mygod
2020-05-29 21:39:32 -04:00
parent a40a07b76e
commit d328215764
2 changed files with 9 additions and 8 deletions

View File

@@ -41,10 +41,7 @@ import be.mygod.vpnhotspot.util.SpanFormatter
import be.mygod.vpnhotspot.util.toPluralInt import be.mygod.vpnhotspot.util.toPluralInt
import be.mygod.vpnhotspot.widget.SmartSnackbar import be.mygod.vpnhotspot.widget.SmartSnackbar
import kotlinx.android.parcel.Parcelize import kotlinx.android.parcel.Parcelize
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.text.NumberFormat import java.text.NumberFormat
class ClientsFragment : Fragment() { class ClientsFragment : Fragment() {
@@ -169,10 +166,12 @@ class ClientsFragment : Fragment() {
} }
private inner class ClientAdapter : ListAdapter<Client, ClientViewHolder>(Client) { private inner class ClientAdapter : ListAdapter<Client, ClientViewHolder>(Client) {
var size = 0 var size = CompletableDeferred(0)
override fun submitList(list: MutableList<Client>?) { override fun submitList(list: MutableList<Client>?) {
super.submitList(list) { size = list?.size ?: 0 } val deferred = CompletableDeferred<Int>()
size = deferred
super.submitList(list) { deferred.complete(list?.size ?: 0) }
binding.swipeRefresher.isRefreshing = false binding.swipeRefresher.isRefreshing = false
} }
@@ -231,7 +230,9 @@ class ClientsFragment : Fragment() {
override fun onStart() { override fun onStart() {
// icon might be changed due to TetherType changes // icon might be changed due to TetherType changes
if (BuildCompat.isAtLeastR()) TetherType.listener[this] = { adapter.notifyItemRangeChanged(0, adapter.size) } if (BuildCompat.isAtLeastR()) TetherType.listener[this] = {
lifecycleScope.launchWhenStarted { adapter.notifyItemRangeChanged(0, adapter.size.await()) }
}
super.onStart() super.onStart()
// we just put these two thing together as this is the only place we need to use this event for now // we just put these two thing together as this is the only place we need to use this event for now
TrafficRecorder.foregroundListeners[this] = adapter::updateTraffic TrafficRecorder.foregroundListeners[this] = adapter::updateTraffic

View File

@@ -66,7 +66,7 @@ class TetheringFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClick
} }
private var enabledIfaces = emptyList<String>() private var enabledIfaces = emptyList<String>()
private var listDeferred = CompletableDeferred<List<Manager>>().apply { complete(emptyList()) } private var listDeferred = CompletableDeferred<List<Manager>>(emptyList())
private fun updateEnabledTypes() { private fun updateEnabledTypes() {
this@TetheringFragment.enabledTypes = enabledIfaces.map { TetherType.ofInterface(it) }.toSet() this@TetheringFragment.enabledTypes = enabledIfaces.map { TetherType.ofInterface(it) }.toSet()
} }