new: add logging abstraction with Kermit integration
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
package fr.ajaury.gwenedeg.core.logging
|
||||
|
||||
import co.touchlab.kermit.Logger as Kermit
|
||||
|
||||
/**
|
||||
* Kermit-backed implementation (adapter) of the [Logger] port.
|
||||
*/
|
||||
class KermitLogger(
|
||||
private val tag: String = "Gwenedeg",
|
||||
) : Logger {
|
||||
override fun debug(message: String, throwable: Throwable?) {
|
||||
Kermit.d(throwable = throwable, tag = tag) { message }
|
||||
}
|
||||
|
||||
override fun info(message: String, throwable: Throwable?) {
|
||||
Kermit.i(throwable = throwable, tag = tag) { message }
|
||||
}
|
||||
|
||||
override fun warning(message: String, throwable: Throwable?) {
|
||||
Kermit.w(throwable = throwable, tag = tag) { message }
|
||||
}
|
||||
|
||||
override fun error(message: String, throwable: Throwable?) {
|
||||
Kermit.e(throwable = throwable, tag = tag) { message }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package fr.ajaury.gwenedeg.core.logging
|
||||
|
||||
/**
|
||||
* Logging abstraction so the rest of the app depends on this interface
|
||||
* rather than on a concrete logging library.
|
||||
*/
|
||||
interface Logger {
|
||||
fun debug(
|
||||
message: String,
|
||||
throwable: Throwable? = null,
|
||||
)
|
||||
|
||||
fun info(
|
||||
message: String,
|
||||
throwable: Throwable? = null,
|
||||
)
|
||||
|
||||
fun warning(
|
||||
message: String,
|
||||
throwable: Throwable? = null,
|
||||
)
|
||||
|
||||
fun error(
|
||||
message: String,
|
||||
throwable: Throwable? = null,
|
||||
)
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package fr.ajaury.gwenedeg.di
|
||||
|
||||
import fr.ajaury.gwenedeg.core.logging.KermitLogger
|
||||
import fr.ajaury.gwenedeg.core.logging.Logger
|
||||
import fr.ajaury.gwenedeg.player.ui.viewmodel.PlayerViewModel
|
||||
import fr.ajaury.gwenedeg.records.data.InMemoryRecordRepository
|
||||
import fr.ajaury.gwenedeg.records.domain.RecordRepository
|
||||
@@ -10,6 +12,7 @@ import org.koin.core.module.dsl.viewModelOf
|
||||
import org.koin.dsl.module
|
||||
|
||||
val sharedModule = module {
|
||||
single<Logger> { KermitLogger() }
|
||||
factoryOf(::InMemoryRecordRepository) { bind<RecordRepository>() }
|
||||
viewModelOf(::RecordsViewModel)
|
||||
viewModelOf(::PlayerViewModel)
|
||||
|
||||
+9
-3
@@ -2,6 +2,7 @@ package fr.ajaury.gwenedeg.player.ui.viewmodel
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import fr.ajaury.gwenedeg.core.logging.Logger
|
||||
import fr.ajaury.gwenedeg.player.AudioPlayer
|
||||
import fr.ajaury.gwenedeg.player.AudioSessionManager
|
||||
import fr.ajaury.gwenedeg.player.PlaybackState
|
||||
@@ -16,6 +17,7 @@ import kotlin.time.Duration.Companion.seconds
|
||||
class PlayerViewModel(
|
||||
private val audioPlayer: AudioPlayer,
|
||||
private val audioSessionManager: AudioSessionManager,
|
||||
private val logger: Logger,
|
||||
) : ViewModel() {
|
||||
private var filePath: String? = null
|
||||
|
||||
@@ -32,9 +34,13 @@ class PlayerViewModel(
|
||||
}
|
||||
|
||||
fun play(filePath: String) {
|
||||
this.filePath = filePath
|
||||
audioPlayer.load(uri = Res.getUri(filePath))
|
||||
audioPlayer.play()
|
||||
try {
|
||||
this.filePath = filePath
|
||||
audioPlayer.load(uri = Res.getUri(filePath))
|
||||
audioPlayer.play()
|
||||
} catch (exception: Exception) {
|
||||
logger.error(message = "Failed to play audio: $filePath", throwable = exception)
|
||||
}
|
||||
}
|
||||
|
||||
fun performMainPlayAction() {
|
||||
|
||||
Reference in New Issue
Block a user