refactoring logger
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
package ru.fincode.tsudesk.core.common.log
|
||||
|
||||
object Constants {
|
||||
const val LOG_DEBUG_TAG = "LOG_DEBUG_TAG"
|
||||
const val TAG = "DEBUG"
|
||||
}
|
||||
@@ -3,23 +3,21 @@ package ru.fincode.tsudesk.core.common.log
|
||||
import android.util.Log
|
||||
|
||||
private fun Any.moduleTag(): String {
|
||||
val module = this::class.java.`package`?.name?.substringAfterLast(".")?.uppercase() ?: "UNKNOWN"
|
||||
|
||||
return "${Constants.LOG_DEBUG_TAG}:$module"
|
||||
val pkg = this::class.java.`package`?.name.orEmpty()
|
||||
val parts = pkg.split('.')
|
||||
val module =
|
||||
if (parts.size >= 5 && parts[0] == "ru" && parts[1] == "fincode" && parts[2] == "tsudesk") {
|
||||
when (parts[3]) {
|
||||
"core", "feature" -> parts[4]
|
||||
else -> parts[3] // fallback
|
||||
}
|
||||
} else {
|
||||
parts.lastOrNull() ?: "unknown"
|
||||
}
|
||||
return "${Constants.TAG}:${module.uppercase()}"
|
||||
}
|
||||
|
||||
fun Any.logD(message: String) {
|
||||
Log.d(moduleTag(), message)
|
||||
}
|
||||
|
||||
fun Any.logD(message: String, throwable: Throwable) {
|
||||
Log.d(moduleTag(), message, throwable)
|
||||
}
|
||||
|
||||
fun Any.logE(message: String) {
|
||||
Log.e(moduleTag(), message)
|
||||
}
|
||||
|
||||
fun Any.logE(message: String, throwable: Throwable) {
|
||||
Log.e(moduleTag(), message, throwable)
|
||||
}
|
||||
fun Any.logD(message: String) = Log.d(moduleTag(), message)
|
||||
fun Any.logD(message: String, throwable: Throwable) = Log.d(moduleTag(), message, throwable)
|
||||
fun Any.logE(message: String) = Log.e(moduleTag(), message)
|
||||
fun Any.logE(message: String, throwable: Throwable) = Log.e(moduleTag(), message, throwable)
|
||||
|
||||
@@ -10,6 +10,7 @@ import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.onStart
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import ru.fincode.tsudesk.core.common.log.logD
|
||||
import ru.fincode.tsudesk.core.common.log.logE
|
||||
import ru.fincode.tsudesk.core.common.model.DataResult
|
||||
import ru.fincode.tsudesk.feature.schedule.domain.model.ScheduleType
|
||||
import ru.fincode.tsudesk.feature.schedule.domain.usecase.ObserveScheduleUseCase
|
||||
@@ -26,7 +27,7 @@ class ScheduleViewModel @Inject constructor(
|
||||
val state: StateFlow<ScheduleUiState> = observeScheduleUseCase(scheduleType).map { result ->
|
||||
when (result) {
|
||||
is DataResult.Data -> {
|
||||
logD("Data loaded from ${if (result.refreshedFromNetwork) "NETWORK" else "CACHE"}")
|
||||
logD("${if (result.refreshedFromNetwork) "NETWORK" else "CACHE"}: ${result.data ?: "null"}")
|
||||
ScheduleUiState(
|
||||
isLoading = false,
|
||||
data = result.data,
|
||||
@@ -35,7 +36,7 @@ class ScheduleViewModel @Inject constructor(
|
||||
}
|
||||
|
||||
is DataResult.Error -> {
|
||||
logD("Error loading schedule: ${result.error}")
|
||||
logE("Error loading schedule: ${result.error}")
|
||||
ScheduleUiState(isLoading = false, errorMessage = result.error.toString())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ fun SplashRoute(
|
||||
viewModel: SplashViewModel = hiltViewModel()
|
||||
) {
|
||||
val state = viewModel.state.collectAsStateWithLifecycle().value
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
viewModel.effects.collect { effect ->
|
||||
when (effect) {
|
||||
@@ -20,6 +19,5 @@ fun SplashRoute(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SplashScreen(state = state)
|
||||
}
|
||||
|
||||
@@ -7,40 +7,38 @@ import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharedFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asSharedFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import ru.fincode.tsudesk.core.common.model.DataResult
|
||||
import ru.fincode.tsudesk.core.config.domain.usecase.GetConfigUseCase
|
||||
import ru.fincode.tsudesk.feature.splash.presentation.model.SplashUiState
|
||||
import ru.fincode.tsudesk.feature.splash.presentation.model.SplashEffect
|
||||
import ru.fincode.tsudesk.feature.splash.presentation.model.SplashUiState
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
class SplashViewModel @Inject constructor(
|
||||
private val getConfigUseCase: GetConfigUseCase
|
||||
) : ViewModel() {
|
||||
|
||||
private val _state = MutableStateFlow(SplashUiState(isLoading = true))
|
||||
val state: StateFlow<SplashUiState> = _state
|
||||
|
||||
private val _effects = MutableSharedFlow<SplashEffect>(extraBufferCapacity = 1)
|
||||
val effects: SharedFlow<SplashEffect> = _effects
|
||||
val state: StateFlow<SplashUiState> = _state.asStateFlow()
|
||||
val effects: SharedFlow<SplashEffect> = _effects.asSharedFlow()
|
||||
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
when (val result = getConfigUseCase()) {
|
||||
is DataResult.Data -> {
|
||||
_state.value = SplashUiState(isLoading = false)
|
||||
_effects.tryEmit(SplashEffect.OpenMain)
|
||||
}
|
||||
|
||||
is DataResult.Error -> {
|
||||
_state.value = SplashUiState(
|
||||
isLoading = false,
|
||||
errorMessage = result.error.toString()
|
||||
)
|
||||
_effects.tryEmit(SplashEffect.OpenMain)
|
||||
}
|
||||
val errorMessage = when (val result = getConfigUseCase()) {
|
||||
is DataResult.Data -> null
|
||||
is DataResult.Error -> result.error.toString()
|
||||
}
|
||||
_state.update {
|
||||
it.copy(
|
||||
isLoading = false,
|
||||
errorMessage = errorMessage
|
||||
)
|
||||
}
|
||||
_effects.tryEmit(SplashEffect.OpenMain)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user