Add system proxy toggle

This commit is contained in:
世界
2023-09-03 22:55:19 +08:00
parent 1c8cc6b3cf
commit 03f9dc4883
8 changed files with 95 additions and 9 deletions

View File

@@ -65,12 +65,14 @@ class OverviewFragment : Fragment() {
binding.profileList.addItemDecoration(divider)
activity.serviceStatus.observe(viewLifecycleOwner) {
binding.statusContainer.isVisible = it == Status.Starting || it == Status.Started
if (it != Status.Started) {
if (it == Status.Stopped) {
binding.clashModeCard.isVisible = false
binding.systemProxyCard.isVisible = false
}
if (it == Status.Started) {
statusClient.connect()
clashModeClient.connect()
reloadSystemProxyStatus()
}
}
ProfileManager.registerCallback(this::updateProfiles)
@@ -89,6 +91,29 @@ class OverviewFragment : Fragment() {
adapter?.reload()
}
private fun reloadSystemProxyStatus() {
lifecycleScope.launch(Dispatchers.IO) {
val status = Libbox.newStandaloneCommandClient().systemProxyStatus
withContext(Dispatchers.Main) {
binding.systemProxyCard.isVisible = status.available
binding.systemProxyCard.isEnabled = true
binding.systemProxySwitch.setOnClickListener(null)
binding.systemProxySwitch.isChecked = status.enabled
binding.systemProxySwitch.setOnCheckedChangeListener { buttonView, isChecked ->
binding.systemProxyCard.isEnabled = false
lifecycleScope.launch(Dispatchers.IO) {
Settings.systemProxyEnabled = isChecked
runCatching {
Libbox.newStandaloneCommandClient().setSystemProxyEnabled(isChecked)
}.onFailure {
buttonView.context.errorDialogBuilder(it).show()
}
}
}
}
}
}
inner class StatusClient : CommandClient.Handler {
override fun onConnected() {