mirror of
https://github.com/openlibrecommunity/olcng.git
synced 2026-07-03 14:05:17 +02:00
fix(service,ui): add state synchronization and improve service state tracking
This commit is contained in:
@@ -355,6 +355,7 @@ object V2RayServiceManager {
|
||||
return try {
|
||||
Log.w(AppConfig.TAG, "StartCore-Manager: Core shutdown callback, attempting restart")
|
||||
val service = serviceControl.getService()
|
||||
MessageUtil.sendMsg2UI(service, AppConfig.MSG_STATE_NOT_RUNNING, "")
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
kotlinx.coroutines.delay(1000L)
|
||||
val ctx = service.applicationContext
|
||||
|
||||
@@ -205,20 +205,28 @@ class MainActivity : HelperBaseActivity(), NavigationView.OnNavigationItemSelect
|
||||
}
|
||||
isFabOperationInProgress = true
|
||||
|
||||
val actuallyRunning = V2RayServiceManager.isRunning()
|
||||
if (mainViewModel.isRunning.value != actuallyRunning) {
|
||||
Log.w(AppConfig.TAG, "FAB: UI state mismatch, syncing: vm=${mainViewModel.isRunning.value}, actual=$actuallyRunning")
|
||||
mainViewModel.isRunning.value = actuallyRunning
|
||||
isFabOperationInProgress = false
|
||||
return
|
||||
}
|
||||
|
||||
applyRunningState(isLoading = true, isRunning = false)
|
||||
|
||||
lifecycleScope.launch {
|
||||
try {
|
||||
if (mainViewModel.isRunning.value == true) {
|
||||
Log.d(AppConfig.TAG, "FAB: stopping service, isRunning=${mainViewModel.isRunning.value}")
|
||||
if (actuallyRunning) {
|
||||
Log.d(AppConfig.TAG, "FAB: stopping service")
|
||||
V2RayServiceManager.stopVService(this@MainActivity)
|
||||
} else {
|
||||
Log.d(AppConfig.TAG, "FAB: starting service, isRunning=${mainViewModel.isRunning.value}")
|
||||
Log.d(AppConfig.TAG, "FAB: starting service")
|
||||
startV2RayWithPermission()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(AppConfig.TAG, "FAB: error", e)
|
||||
applyRunningState(isLoading = false, isRunning = mainViewModel.isRunning.value ?: false)
|
||||
applyRunningState(isLoading = false, isRunning = V2RayServiceManager.isRunning())
|
||||
} finally {
|
||||
isFabOperationInProgress = false
|
||||
}
|
||||
@@ -367,6 +375,12 @@ class MainActivity : HelperBaseActivity(), NavigationView.OnNavigationItemSelect
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
val actuallyRunning = V2RayServiceManager.isRunning()
|
||||
if (mainViewModel.isRunning.value != actuallyRunning) {
|
||||
Log.w(AppConfig.TAG, "onResume: syncing state vm=${mainViewModel.isRunning.value}, actual=$actuallyRunning")
|
||||
mainViewModel.isRunning.value = actuallyRunning
|
||||
}
|
||||
MessageUtil.sendMsg2Service(this, AppConfig.MSG_REGISTER_CLIENT, "")
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
|
||||
Reference in New Issue
Block a user