Update API usage
This commit is contained in:
@@ -98,6 +98,9 @@ interface PlatformInterfaceWrapper : PlatformInterface {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun clearDNSCache() {
|
||||||
|
}
|
||||||
|
|
||||||
private class InterfaceArray(private val iterator: Enumeration<NetworkInterface>) :
|
private class InterfaceArray(private val iterator: Enumeration<NetworkInterface>) :
|
||||||
NetworkInterfaceIterator {
|
NetworkInterfaceIterator {
|
||||||
|
|
||||||
|
|||||||
11
app/src/main/java/io/nekohasekai/sfa/ktx/Wrappers.kt
Normal file
11
app/src/main/java/io/nekohasekai/sfa/ktx/Wrappers.kt
Normal 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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -29,13 +29,15 @@ import kotlinx.coroutines.delay
|
|||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
class OverviewFragment : Fragment(), CommandClient.Handler {
|
class OverviewFragment : Fragment() {
|
||||||
|
|
||||||
private val activity: MainActivity? get() = super.getActivity() as MainActivity?
|
private val activity: MainActivity? get() = super.getActivity() as MainActivity?
|
||||||
private var _binding: FragmentDashboardOverviewBinding? = null
|
private var _binding: FragmentDashboardOverviewBinding? = null
|
||||||
private val binding get() = _binding!!
|
private val binding get() = _binding!!
|
||||||
private val commandClient =
|
private val statusClient =
|
||||||
CommandClient(lifecycleScope, CommandClient.ConnectionType.Status, this)
|
CommandClient(lifecycleScope, CommandClient.ConnectionType.Status, StatusClient())
|
||||||
|
private val clashModeClient =
|
||||||
|
CommandClient(lifecycleScope, CommandClient.ConnectionType.ClashMode, ClashModeClient())
|
||||||
|
|
||||||
private var _adapter: Adapter? = null
|
private var _adapter: Adapter? = null
|
||||||
private val adapter get() = _adapter!!
|
private val adapter get() = _adapter!!
|
||||||
@@ -61,7 +63,8 @@ class OverviewFragment : Fragment(), CommandClient.Handler {
|
|||||||
activity.serviceStatus.observe(viewLifecycleOwner) {
|
activity.serviceStatus.observe(viewLifecycleOwner) {
|
||||||
binding.statusContainer.isVisible = it == Status.Starting || it == Status.Started
|
binding.statusContainer.isVisible = it == Status.Starting || it == Status.Started
|
||||||
if (it == Status.Started) {
|
if (it == Status.Started) {
|
||||||
commandClient.connect()
|
statusClient.connect()
|
||||||
|
// clashModeClient.connect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ProfileManager.registerCallback(this::updateProfiles)
|
ProfileManager.registerCallback(this::updateProfiles)
|
||||||
@@ -71,46 +74,76 @@ class OverviewFragment : Fragment(), CommandClient.Handler {
|
|||||||
super.onDestroyView()
|
super.onDestroyView()
|
||||||
_adapter = null
|
_adapter = null
|
||||||
_binding = null
|
_binding = null
|
||||||
commandClient.disconnect()
|
statusClient.disconnect()
|
||||||
|
// clashModeClient.disconnect()
|
||||||
ProfileManager.unregisterCallback(this::updateProfiles)
|
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() {
|
private fun updateProfiles() {
|
||||||
_adapter?.reload()
|
_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() {
|
inner class StatusClient : CommandClient.Handler {
|
||||||
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) {
|
override fun onConnected() {
|
||||||
val binding = _binding ?: return
|
val binding = _binding ?: return
|
||||||
lifecycleScope.launch(Dispatchers.Main) {
|
lifecycleScope.launch(Dispatchers.Main) {
|
||||||
binding.memoryText.text = Libbox.formatBytes(status.memory)
|
binding.memoryText.text = getString(R.string.loading)
|
||||||
binding.goroutinesText.text = status.goroutines.toString()
|
binding.goroutinesText.text = getString(R.string.loading)
|
||||||
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 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(
|
class Adapter(
|
||||||
|
|||||||
@@ -8,20 +8,22 @@ import io.nekohasekai.libbox.Libbox
|
|||||||
import io.nekohasekai.libbox.OutboundGroup
|
import io.nekohasekai.libbox.OutboundGroup
|
||||||
import io.nekohasekai.libbox.OutboundGroupIterator
|
import io.nekohasekai.libbox.OutboundGroupIterator
|
||||||
import io.nekohasekai.libbox.StatusMessage
|
import io.nekohasekai.libbox.StatusMessage
|
||||||
|
import io.nekohasekai.libbox.StringIterator
|
||||||
|
import io.nekohasekai.sfa.ktx.toList
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.isActive
|
import kotlinx.coroutines.isActive
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
class CommandClient(
|
open class CommandClient(
|
||||||
private val scope: CoroutineScope,
|
private val scope: CoroutineScope,
|
||||||
private val connectionType: ConnectionType,
|
private val connectionType: ConnectionType,
|
||||||
private val handler: Handler
|
private val handler: Handler
|
||||||
) {
|
) {
|
||||||
|
|
||||||
enum class ConnectionType {
|
enum class ConnectionType {
|
||||||
Status, Groups, Log
|
Status, Groups, Log, ClashMode
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Handler {
|
interface Handler {
|
||||||
@@ -31,6 +33,8 @@ class CommandClient(
|
|||||||
fun updateStatus(status: StatusMessage) {}
|
fun updateStatus(status: StatusMessage) {}
|
||||||
fun updateGroups(groups: List<OutboundGroup>) {}
|
fun updateGroups(groups: List<OutboundGroup>) {}
|
||||||
fun appendLog(message: String) {}
|
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.Status -> Libbox.CommandStatus
|
||||||
ConnectionType.Groups -> Libbox.CommandGroup
|
ConnectionType.Groups -> Libbox.CommandGroup
|
||||||
ConnectionType.Log -> Libbox.CommandLog
|
ConnectionType.Log -> Libbox.CommandLog
|
||||||
|
ConnectionType.ClashMode -> Libbox.CommandClashMode
|
||||||
}
|
}
|
||||||
options.statusInterval = 2 * 1000 * 1000 * 1000
|
options.statusInterval = 2 * 1000 * 1000 * 1000
|
||||||
val commandClient = CommandClient(clientHandler, options)
|
val commandClient = CommandClient(clientHandler, options)
|
||||||
@@ -115,6 +120,14 @@ class CommandClient(
|
|||||||
handler.updateStatus(message)
|
handler.updateStatus(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun initializeClashMode(modeList: StringIterator, currentMode: String) {
|
||||||
|
handler.initializeClashMode(modeList.toList(), currentMode)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun updateClashMode(newMode: String) {
|
||||||
|
handler.updateClashMode(newMode)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user