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