Update gomobile usage
This commit is contained in:
@@ -36,9 +36,9 @@ class Application : Application() {
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
AppLifecycleObserver.register()
|
||||
AppLifecycleObserver.register(this)
|
||||
|
||||
Seq.setContext(this)
|
||||
// Seq.setContext(this)
|
||||
Libbox.setLocale(Locale.getDefault().toLanguageTag().replace("-", "_"))
|
||||
HookStatusClient.register(this)
|
||||
PrivilegeSettingsClient.register(this)
|
||||
|
||||
@@ -292,7 +292,7 @@ class BoxService(private val service: Service, private val platformInterface: Pl
|
||||
closeService()
|
||||
commandServer.apply {
|
||||
close()
|
||||
Seq.destroyRef(refnum)
|
||||
// Seq.destroyRef(refnum)
|
||||
}
|
||||
Settings.startedByUser = false
|
||||
withContext(Dispatchers.Main) {
|
||||
|
||||
@@ -59,16 +59,25 @@ class ConnectionsViewModel :
|
||||
|
||||
override fun createInitialState() = ConnectionsUiState()
|
||||
|
||||
private data class ConnectionState(
|
||||
val foreground: Boolean,
|
||||
val screenOn: Boolean,
|
||||
val visibleCount: Int,
|
||||
val status: Status,
|
||||
)
|
||||
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
combine(
|
||||
AppLifecycleObserver.isForeground,
|
||||
AppLifecycleObserver.isScreenOn,
|
||||
_visibleCount,
|
||||
_serviceStatus,
|
||||
) { foreground, visibleCount, status ->
|
||||
Triple(foreground, visibleCount, status)
|
||||
}.collect { (foreground, visibleCount, status) ->
|
||||
val shouldConnect = foreground && visibleCount > 0 && status == Status.Started
|
||||
) { foreground, screenOn, visibleCount, status ->
|
||||
ConnectionState(foreground, screenOn, visibleCount, status)
|
||||
}.collect { state ->
|
||||
val shouldConnect = state.foreground && state.screenOn &&
|
||||
state.visibleCount > 0 && state.status == Status.Started
|
||||
if (shouldConnect) {
|
||||
updateState { copy(isLoading = true) }
|
||||
commandClient.connect()
|
||||
@@ -190,7 +199,7 @@ class ConnectionsViewModel :
|
||||
val generation = connectionsGeneration.get()
|
||||
val snapshot = connectionsMutex.withLock {
|
||||
if (connectionsStore == null) {
|
||||
connectionsStore = Libbox.newConnections()
|
||||
connectionsStore = Connections()
|
||||
}
|
||||
val store = connectionsStore ?: return@withLock null
|
||||
store.applyEvents(events)
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
package io.nekohasekai.sfa.utils
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.os.PowerManager
|
||||
import androidx.core.content.getSystemService
|
||||
import androidx.lifecycle.DefaultLifecycleObserver
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import androidx.lifecycle.ProcessLifecycleOwner
|
||||
@@ -11,8 +17,31 @@ object AppLifecycleObserver : DefaultLifecycleObserver {
|
||||
private val _isForeground = MutableStateFlow(true)
|
||||
val isForeground: StateFlow<Boolean> = _isForeground.asStateFlow()
|
||||
|
||||
fun register() {
|
||||
private val _isScreenOn = MutableStateFlow(true)
|
||||
val isScreenOn: StateFlow<Boolean> = _isScreenOn.asStateFlow()
|
||||
|
||||
private val screenReceiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
when (intent.action) {
|
||||
Intent.ACTION_SCREEN_ON -> _isScreenOn.value = true
|
||||
Intent.ACTION_SCREEN_OFF -> _isScreenOn.value = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun register(context: Context) {
|
||||
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
|
||||
|
||||
val powerManager = context.getSystemService<PowerManager>()!!
|
||||
_isScreenOn.value = powerManager.isInteractive
|
||||
|
||||
context.registerReceiver(
|
||||
screenReceiver,
|
||||
IntentFilter().apply {
|
||||
addAction(Intent.ACTION_SCREEN_ON)
|
||||
addAction(Intent.ACTION_SCREEN_OFF)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
override fun onStart(owner: LifecycleOwner) {
|
||||
|
||||
@@ -109,7 +109,7 @@ open class CommandClient(
|
||||
runCatching {
|
||||
disconnect()
|
||||
}
|
||||
Seq.destroyRef(refnum)
|
||||
// Seq.destroyRef(refnum)
|
||||
}
|
||||
commandClient = null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user