android: Move logging into it's own repository class.
Logs will be maintained and appended even when the main UI is not bound to the service. This also lets us log without filling a Handler with a bunch of messages we might just throw away anyway.
This commit is contained in:
@@ -37,12 +37,14 @@ import com.google.accompanist.permissions.ExperimentalPermissionsApi
|
||||
import com.google.accompanist.permissions.isGranted
|
||||
import com.google.accompanist.permissions.rememberPermissionState
|
||||
import com.google.accompanist.permissions.shouldShowRationale
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import org.musicpd.Main
|
||||
import org.musicpd.R
|
||||
|
||||
@AndroidEntryPoint
|
||||
class SettingsActivity : ComponentActivity() {
|
||||
|
||||
private val settingsViewModel: SettingsViewModel by viewModels()
|
||||
@@ -72,10 +74,6 @@ class SettingsActivity : ComponentActivity() {
|
||||
settingsViewModel.updateStatus(error, false)
|
||||
connectClient()
|
||||
}
|
||||
|
||||
override fun onLog(priority: Int, msg: String) {
|
||||
settingsViewModel.addLogItem(priority, msg)
|
||||
}
|
||||
})
|
||||
|
||||
settingsViewModel.setClient(client)
|
||||
@@ -138,7 +136,7 @@ fun SettingsContainer(settingsViewModel: SettingsViewModel = viewModel()) {
|
||||
settingsViewModel.setPauseOnHeadphonesDisconnect(newValue)
|
||||
}
|
||||
)
|
||||
LogView(settingsViewModel.logItemFLow.collectAsStateWithLifecycle())
|
||||
LogView(settingsViewModel.getLogs().collectAsStateWithLifecycle())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,23 @@
|
||||
package org.musicpd.ui
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import androidx.lifecycle.ViewModel
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import org.musicpd.Main
|
||||
import org.musicpd.Preferences
|
||||
import org.musicpd.data.LoggingRepository
|
||||
import javax.inject.Inject
|
||||
|
||||
private const val MAX_LOGS = 500
|
||||
|
||||
class SettingsViewModel : ViewModel() {
|
||||
|
||||
@HiltViewModel
|
||||
class SettingsViewModel @Inject constructor(
|
||||
private var loggingRepository: LoggingRepository
|
||||
) : ViewModel() {
|
||||
private var mClient: Main.Client? = null
|
||||
|
||||
private val _logItemFLow = MutableStateFlow(listOf<String>())
|
||||
val logItemFLow: StateFlow<List<String>> = _logItemFLow
|
||||
|
||||
data class StatusUiState(
|
||||
val statusMessage: String = "",
|
||||
val running: Boolean = false
|
||||
@@ -26,21 +26,8 @@ class SettingsViewModel : ViewModel() {
|
||||
private val _statusUIState = MutableStateFlow(StatusUiState())
|
||||
val statusUIState: StateFlow<StatusUiState> = _statusUIState.asStateFlow()
|
||||
|
||||
fun addLogItem(priority: Int, message: String) {
|
||||
if (_logItemFLow.value.size > MAX_LOGS) {
|
||||
_logItemFLow.value = _logItemFLow.value.drop(1)
|
||||
}
|
||||
|
||||
val priorityString: String = when (priority) {
|
||||
Log.DEBUG -> "D"
|
||||
Log.ERROR -> "E"
|
||||
Log.INFO -> "I"
|
||||
Log.VERBOSE -> "V"
|
||||
Log.WARN -> "W"
|
||||
else -> ""
|
||||
}
|
||||
|
||||
_logItemFLow.value = _logItemFLow.value + ("$priorityString/$message")
|
||||
fun getLogs(): StateFlow<List<String>> {
|
||||
return loggingRepository.logItemFLow
|
||||
}
|
||||
|
||||
fun updateStatus(message: String, running: Boolean) {
|
||||
|
||||
Reference in New Issue
Block a user