Update API usage

This commit is contained in:
世界 2023-08-24 23:25:35 +08:00
parent 1d578acb74
commit 065c62118e
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
4 changed files with 95 additions and 35 deletions

View file

@ -98,6 +98,9 @@ interface PlatformInterfaceWrapper : PlatformInterface {
return false
}
override fun clearDNSCache() {
}
private class InterfaceArray(private val iterator: Enumeration<NetworkInterface>) :
NetworkInterfaceIterator {

View file

@ -0,0 +1,11 @@
package io.nekohasekai.sfa.ktx
import io.nekohasekai.libbox.StringIterator
fun StringIterator.toList(): List<String> {
return mutableListOf<String>().apply {
while (hasNext()) {
add(next())
}
}
}

View file

@ -29,13 +29,15 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class OverviewFragment : Fragment(), CommandClient.Handler {
class OverviewFragment : Fragment() {
private val activity: MainActivity? get() = super.getActivity() as MainActivity?
private var _binding: FragmentDashboardOverviewBinding? = null
private val binding get() = _binding!!
private val commandClient =
CommandClient(lifecycleScope, CommandClient.ConnectionType.Status, this)
private val statusClient =
CommandClient(lifecycleScope, CommandClient.ConnectionType.Status, StatusClient())
private val clashModeClient =
CommandClient(lifecycleScope, CommandClient.ConnectionType.ClashMode, ClashModeClient())
private var _adapter: Adapter? = null
private val adapter get() = _adapter!!
@ -61,7 +63,8 @@ class OverviewFragment : Fragment(), CommandClient.Handler {
activity.serviceStatus.observe(viewLifecycleOwner) {
binding.statusContainer.isVisible = it == Status.Starting || it == Status.Started
if (it == Status.Started) {
commandClient.connect()
statusClient.connect()
// clashModeClient.connect()
}
}
ProfileManager.registerCallback(this::updateProfiles)
@ -71,46 +74,76 @@ class OverviewFragment : Fragment(), CommandClient.Handler {
super.onDestroyView()
_adapter = null
_binding = null
commandClient.disconnect()
statusClient.disconnect()
// clashModeClient.disconnect()
ProfileManager.unregisterCallback(this::updateProfiles)
}
override fun onPause() {
super.onPause()
statusClient.disconnect()
// clashModeClient.disconnect()
}
override fun onResume() {
super.onResume()
statusClient.connect()
// clashModeClient.connect()
}
private fun updateProfiles() {
_adapter?.reload()
}
override fun onConnected() {
val binding = _binding ?: return
lifecycleScope.launch(Dispatchers.Main) {
binding.memoryText.text = getString(R.string.loading)
binding.goroutinesText.text = getString(R.string.loading)
}
}
override fun onDisconnected() {
val binding = _binding ?: return
lifecycleScope.launch(Dispatchers.Main) {
binding.memoryText.text = getString(R.string.loading)
binding.goroutinesText.text = getString(R.string.loading)
}
}
inner class StatusClient : CommandClient.Handler {
override fun updateStatus(status: StatusMessage) {
val binding = _binding ?: return
lifecycleScope.launch(Dispatchers.Main) {
binding.memoryText.text = Libbox.formatBytes(status.memory)
binding.goroutinesText.text = status.goroutines.toString()
val trafficAvailable = status.trafficAvailable
binding.trafficContainer.isVisible = trafficAvailable
if (trafficAvailable) {
binding.inboundConnectionsText.text = status.connectionsIn.toString()
binding.outboundConnectionsText.text = status.connectionsOut.toString()
binding.uplinkText.text = Libbox.formatBytes(status.uplink) + "/s"
binding.downlinkText.text = Libbox.formatBytes(status.downlink) + "/s"
binding.uplinkTotalText.text = Libbox.formatBytes(status.uplinkTotal)
binding.downlinkTotalText.text = Libbox.formatBytes(status.downlinkTotal)
override fun onConnected() {
val binding = _binding ?: return
lifecycleScope.launch(Dispatchers.Main) {
binding.memoryText.text = getString(R.string.loading)
binding.goroutinesText.text = getString(R.string.loading)
}
}
override fun onDisconnected() {
val binding = _binding ?: return
lifecycleScope.launch(Dispatchers.Main) {
binding.memoryText.text = getString(R.string.loading)
binding.goroutinesText.text = getString(R.string.loading)
}
}
override fun updateStatus(status: StatusMessage) {
val binding = _binding ?: return
lifecycleScope.launch(Dispatchers.Main) {
binding.memoryText.text = Libbox.formatBytes(status.memory)
binding.goroutinesText.text = status.goroutines.toString()
val trafficAvailable = status.trafficAvailable
binding.trafficContainer.isVisible = trafficAvailable
if (trafficAvailable) {
binding.inboundConnectionsText.text = status.connectionsIn.toString()
binding.outboundConnectionsText.text = status.connectionsOut.toString()
binding.uplinkText.text = Libbox.formatBytes(status.uplink) + "/s"
binding.downlinkText.text = Libbox.formatBytes(status.downlink) + "/s"
binding.uplinkTotalText.text = Libbox.formatBytes(status.uplinkTotal)
binding.downlinkTotalText.text = Libbox.formatBytes(status.downlinkTotal)
}
}
}
}
inner class ClashModeClient : CommandClient.Handler {
override fun initializeClashMode(modeList: List<String>, currentMode: String) {
// TODO: initialize mode selector here
}
override fun updateClashMode(newMode: String) {
// TODO: update mode here
}
}
class Adapter(

View file

@ -8,20 +8,22 @@ import io.nekohasekai.libbox.Libbox
import io.nekohasekai.libbox.OutboundGroup
import io.nekohasekai.libbox.OutboundGroupIterator
import io.nekohasekai.libbox.StatusMessage
import io.nekohasekai.libbox.StringIterator
import io.nekohasekai.sfa.ktx.toList
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
class CommandClient(
open class CommandClient(
private val scope: CoroutineScope,
private val connectionType: ConnectionType,
private val handler: Handler
) {
enum class ConnectionType {
Status, Groups, Log
Status, Groups, Log, ClashMode
}
interface Handler {
@ -31,6 +33,8 @@ class CommandClient(
fun updateStatus(status: StatusMessage) {}
fun updateGroups(groups: List<OutboundGroup>) {}
fun appendLog(message: String) {}
fun initializeClashMode(modeList: List<String>, currentMode: String) {}
fun updateClashMode(newMode: String) {}
}
@ -44,6 +48,7 @@ class CommandClient(
ConnectionType.Status -> Libbox.CommandStatus
ConnectionType.Groups -> Libbox.CommandGroup
ConnectionType.Log -> Libbox.CommandLog
ConnectionType.ClashMode -> Libbox.CommandClashMode
}
options.statusInterval = 2 * 1000 * 1000 * 1000
val commandClient = CommandClient(clientHandler, options)
@ -115,6 +120,14 @@ class CommandClient(
handler.updateStatus(message)
}
override fun initializeClashMode(modeList: StringIterator, currentMode: String) {
handler.initializeClashMode(modeList.toList(), currentMode)
}
override fun updateClashMode(newMode: String) {
handler.updateClashMode(newMode)
}
}
}