Fix path indicator on SettingsActivity

This commit is contained in:
Andrew 2024-06-28 18:43:00 +09:30
parent 8cc7e65abf
commit 0ebbd35031

View File

@ -26,6 +26,7 @@ import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
@ -39,6 +40,13 @@ import androidx.lifecycle.lifecycleScope
import androidx.localbroadcastmanager.content.LocalBroadcastManager import androidx.localbroadcastmanager.content.LocalBroadcastManager
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.startWith
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import network.loki.messenger.BuildConfig import network.loki.messenger.BuildConfig
@ -408,11 +416,7 @@ class SettingsActivity : PassphraseRequiredActionBarActivity() {
Spacer(modifier = Modifier.height(LocalDimensions.current.itemSpacing)) Spacer(modifier = Modifier.height(LocalDimensions.current.itemSpacing))
var hasPaths by remember { val hasPaths by hasPaths().collectAsState(initial = false)
mutableStateOf(false)
}
CheckPaths { hasPaths = it }
Cell { Cell {
Column { Column {
@ -441,31 +445,16 @@ class SettingsActivity : PassphraseRequiredActionBarActivity() {
} }
} }
} }
}
@Composable private fun Context.hasPaths(): Flow<Boolean> = LocalBroadcastManager.getInstance(this).hasPaths()
fun CheckPaths(setHasPaths: (Boolean) -> Unit) { private fun LocalBroadcastManager.hasPaths(): Flow<Boolean> = callbackFlow {
val context = LocalContext.current val receiver: BroadcastReceiver = object : BroadcastReceiver() {
val manager = LocalBroadcastManager.getInstance(context) override fun onReceive(context: Context, intent: Intent) { trySend(Unit) }
fun update() {
lifecycleScope.launch {
val paths = withContext(Dispatchers.IO) { OnionRequestAPI.paths }
setHasPaths(paths.isNotEmpty())
}
}
fun addReceiver(action: String): BroadcastReceiver = createReceiver { update() }.also { manager.registerReceiver(it, IntentFilter(action)) }
val receivers = listOf("buildingPaths", "pathsBuilt").map(::addReceiver)
DisposableEffect(Unit) {
onDispose {
receivers.forEach(manager::unregisterReceiver)
}
}
} }
}
fun createReceiver(update: () -> Unit) = object : BroadcastReceiver() { registerReceiver(receiver, IntentFilter("buildingPaths"))
override fun onReceive(context: Context, intent: Intent) { update() } registerReceiver(receiver, IntentFilter("pathsBuilt"))
}
awaitClose { unregisterReceiver(receiver) }
}.onStart { emit(Unit) }.map { OnionRequestAPI.paths.isNotEmpty() }