mirror of
https://github.com/wgtunnel/android.git
synced 2026-07-03 14:07:49 +02:00
fix: logger start and clear
This commit is contained in:
@@ -18,6 +18,7 @@ import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.distinctUntilChangedBy
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import timber.log.Timber
|
||||
@@ -59,8 +60,15 @@ class WireGuardAutoTunnel : Application(), Configuration.Provider {
|
||||
|
||||
applicationScope.launch(ioDispatcher) {
|
||||
launch {
|
||||
val monitoringSettings = monitoringRepository.getMonitoringSettings()
|
||||
if (monitoringSettings.isLocalLogsEnabled) logReader.start()
|
||||
monitoringRepository.flow
|
||||
.distinctUntilChangedBy { it.isLocalLogsEnabled }
|
||||
.collect { settings ->
|
||||
if (settings.isLocalLogsEnabled) {
|
||||
logReader.start()
|
||||
} else {
|
||||
logReader.stop()
|
||||
}
|
||||
}
|
||||
}
|
||||
launch { notificationMonitor.handleApplicationNotifications() }
|
||||
}
|
||||
|
||||
+25
-38
@@ -6,7 +6,6 @@ import com.zaneschepke.logcatter.LogReader
|
||||
import com.zaneschepke.wireguardautotunnel.BuildConfig
|
||||
import com.zaneschepke.wireguardautotunnel.R
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.GlobalEffectRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.repository.MonitoringSettingsRepository
|
||||
import com.zaneschepke.wireguardautotunnel.domain.sideeffect.GlobalSideEffect
|
||||
import com.zaneschepke.wireguardautotunnel.ui.state.LoggerUiState
|
||||
import com.zaneschepke.wireguardautotunnel.util.Constants
|
||||
@@ -15,8 +14,6 @@ import com.zaneschepke.wireguardautotunnel.util.StringValue
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.*
|
||||
import org.orbitmvi.orbit.ContainerHost
|
||||
import org.orbitmvi.orbit.viewmodel.container
|
||||
import timber.log.Timber
|
||||
@@ -26,7 +23,6 @@ class LoggerViewModel
|
||||
@Inject
|
||||
constructor(
|
||||
private val logReader: LogReader,
|
||||
private val monitoringRepository: MonitoringSettingsRepository,
|
||||
private val fileUtils: FileUtils,
|
||||
private val globalEffectRepository: GlobalEffectRepository,
|
||||
) : ContainerHost<LoggerUiState, Nothing>, ViewModel() {
|
||||
@@ -38,33 +34,17 @@ constructor(
|
||||
buildSettings = { repeatOnSubscribedStopTimeout = 5000L },
|
||||
) {
|
||||
intent {
|
||||
monitoringRepository.flow
|
||||
.onEach { reduce { state.copy(monitoringSettings = it) } }
|
||||
.distinctUntilChangedBy { it.isLocalLogsEnabled }
|
||||
.onEach { settings ->
|
||||
if (settings.isLocalLogsEnabled) {
|
||||
logReader.start()
|
||||
} else {
|
||||
logReader.stop()
|
||||
logReader.deleteAndClearLogs()
|
||||
reduce { state.copy(messages = emptyList()) }
|
||||
}
|
||||
}
|
||||
.flatMapLatest { settings ->
|
||||
if (settings.isLocalLogsEnabled) logReader.bufferedLogs else emptyFlow()
|
||||
}
|
||||
.catch { e -> Timber.e(e) }
|
||||
.collect { logMessage ->
|
||||
reduce {
|
||||
state.copy(
|
||||
messages =
|
||||
state.messages.toMutableList().apply {
|
||||
if (size >= MAX_LOG_SIZE) removeAt(0)
|
||||
add(logMessage)
|
||||
}
|
||||
)
|
||||
}
|
||||
logReader.bufferedLogs.collect { logMessage ->
|
||||
reduce {
|
||||
state.copy(
|
||||
messages =
|
||||
state.messages.toMutableList().apply {
|
||||
if (size >= MAX_LOG_SIZE) removeAt(0)
|
||||
add(logMessage)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,17 +71,24 @@ constructor(
|
||||
}
|
||||
Unit
|
||||
}
|
||||
result.onSuccess { file ->
|
||||
logReader.zipLogFiles(file.absolutePath)
|
||||
fileUtils.exportFile(file, uri, FileUtils.ZIP_FILE_MIME_TYPE).onFailure(onFailure)
|
||||
}
|
||||
result.onFailure(onFailure)
|
||||
result.fold(
|
||||
onSuccess = { file ->
|
||||
try {
|
||||
logReader.zipLogFiles(file.absolutePath)
|
||||
fileUtils
|
||||
.exportFile(file, uri, FileUtils.ZIP_FILE_MIME_TYPE)
|
||||
.onFailure(onFailure)
|
||||
} finally {
|
||||
if (file.exists()) file.delete()
|
||||
}
|
||||
},
|
||||
onFailure = onFailure,
|
||||
)
|
||||
}
|
||||
|
||||
fun deleteLogs() = intent {
|
||||
monitoringRepository.upsert(state.monitoringSettings.copy(isLocalLogsEnabled = false))
|
||||
delay(1_000L)
|
||||
monitoringRepository.upsert(state.monitoringSettings.copy(isLocalLogsEnabled = true))
|
||||
reduce { state.copy(messages = emptyList()) }
|
||||
logReader.deleteAndClearLogs()
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
+6
-2
@@ -320,8 +320,12 @@ constructor(
|
||||
fileUtils
|
||||
.createNewShareFile(shareFileName)
|
||||
.onSuccess {
|
||||
fileUtils.zipAll(it, files).onFailure(onFailure)
|
||||
fileUtils.exportFile(it, uri, FileUtils.ZIP_FILE_MIME_TYPE).onFailure(onFailure)
|
||||
try {
|
||||
fileUtils.zipAll(it, files).onFailure(onFailure)
|
||||
fileUtils.exportFile(it, uri, FileUtils.ZIP_FILE_MIME_TYPE).onFailure(onFailure)
|
||||
} finally {
|
||||
if (it.exists()) it.delete()
|
||||
}
|
||||
postSideEffect(
|
||||
GlobalSideEffect.Snackbar(StringValue.StringResource(R.string.export_success))
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user