diff --git a/app/src/main/java/live/mehiz/mpvkt/database/repository/PlaybackStateRepositoryImpl.kt b/app/src/main/java/live/mehiz/mpvkt/database/repository/PlaybackStateRepositoryImpl.kt new file mode 100644 index 0000000..fcc6a15 --- /dev/null +++ b/app/src/main/java/live/mehiz/mpvkt/database/repository/PlaybackStateRepositoryImpl.kt @@ -0,0 +1,21 @@ +package live.mehiz.mpvkt.database.repository + +import live.mehiz.mpvkt.database.MpvKtDatabase +import live.mehiz.mpvkt.database.entities.PlaybackStateEntity +import live.mehiz.mpvkt.domain.playbackstate.repository.PlaybackStateRepository + +class PlaybackStateRepositoryImpl( + private val database: MpvKtDatabase +) : PlaybackStateRepository { + override suspend fun upsert(playbackState: PlaybackStateEntity) { + database.videoDataDao().upsert(playbackState) + } + + override suspend fun getVideoDataByTitle(mediaTitle: String): PlaybackStateEntity? { + return database.videoDataDao().getVideoDataByTitle(mediaTitle) + } + + override suspend fun clearAllPlaybackStates() { + database.videoDataDao().clearAllPlaybackStates() + } +} diff --git a/app/src/main/java/live/mehiz/mpvkt/di/DatabaseModule.kt b/app/src/main/java/live/mehiz/mpvkt/di/DatabaseModule.kt index 96bf091..b48671a 100644 --- a/app/src/main/java/live/mehiz/mpvkt/di/DatabaseModule.kt +++ b/app/src/main/java/live/mehiz/mpvkt/di/DatabaseModule.kt @@ -4,8 +4,12 @@ import androidx.room.Room import live.mehiz.mpvkt.database.Migrations import live.mehiz.mpvkt.database.MpvKtDatabase import live.mehiz.mpvkt.database.repository.CustomButtonRepositoryImpl +import live.mehiz.mpvkt.database.repository.PlaybackStateRepositoryImpl +import live.mehiz.mpvkt.domain.custombuttons.repository.CustomButtonRepository +import live.mehiz.mpvkt.domain.playbackstate.repository.PlaybackStateRepository import org.koin.android.ext.koin.androidContext import org.koin.core.module.dsl.singleOf +import org.koin.dsl.bind import org.koin.dsl.module val DatabaseModule = module { @@ -16,5 +20,6 @@ val DatabaseModule = module { .build() } - singleOf(::CustomButtonRepositoryImpl) + singleOf(::CustomButtonRepositoryImpl).bind(CustomButtonRepository::class) + singleOf(::PlaybackStateRepositoryImpl).bind(PlaybackStateRepository::class) } diff --git a/app/src/main/java/live/mehiz/mpvkt/domain/playbackstate/repository/PlaybackStateRepository.kt b/app/src/main/java/live/mehiz/mpvkt/domain/playbackstate/repository/PlaybackStateRepository.kt new file mode 100644 index 0000000..84947e7 --- /dev/null +++ b/app/src/main/java/live/mehiz/mpvkt/domain/playbackstate/repository/PlaybackStateRepository.kt @@ -0,0 +1,12 @@ +package live.mehiz.mpvkt.domain.playbackstate.repository + +import live.mehiz.mpvkt.database.entities.PlaybackStateEntity + +interface PlaybackStateRepository { + + suspend fun upsert(playbackState: PlaybackStateEntity) + + suspend fun getVideoDataByTitle(mediaTitle: String): PlaybackStateEntity? + + suspend fun clearAllPlaybackStates() +} diff --git a/app/src/main/java/live/mehiz/mpvkt/ui/custombuttons/CustomButtonsScreenViewModel.kt b/app/src/main/java/live/mehiz/mpvkt/ui/custombuttons/CustomButtonsScreenViewModel.kt index ece95e9..719bc51 100644 --- a/app/src/main/java/live/mehiz/mpvkt/ui/custombuttons/CustomButtonsScreenViewModel.kt +++ b/app/src/main/java/live/mehiz/mpvkt/ui/custombuttons/CustomButtonsScreenViewModel.kt @@ -11,11 +11,11 @@ import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import live.mehiz.mpvkt.database.entities.CustomButtonEntity -import live.mehiz.mpvkt.database.repository.CustomButtonRepositoryImpl +import live.mehiz.mpvkt.domain.custombuttons.repository.CustomButtonRepository import live.mehiz.mpvkt.preferences.PlayerPreferences class CustomButtonsScreenViewModel( - private val customButtonsRepository: CustomButtonRepositoryImpl, + private val customButtonsRepository: CustomButtonRepository, private val playerPreferences: PlayerPreferences ) : ViewModel() { private val _dialog = MutableStateFlow(CustomButtonDialog.None) diff --git a/app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerActivity.kt b/app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerActivity.kt index c32a5ad..dcaa071 100644 --- a/app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerActivity.kt +++ b/app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerActivity.kt @@ -42,10 +42,10 @@ import `is`.xyz.mpv.Utils import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch -import live.mehiz.mpvkt.database.MpvKtDatabase import live.mehiz.mpvkt.database.entities.CustomButtonEntity import live.mehiz.mpvkt.database.entities.PlaybackStateEntity import live.mehiz.mpvkt.databinding.PlayerLayoutBinding +import live.mehiz.mpvkt.domain.playbackstate.repository.PlaybackStateRepository import live.mehiz.mpvkt.preferences.AdvancedPreferences import live.mehiz.mpvkt.preferences.AudioPreferences import live.mehiz.mpvkt.preferences.GesturePreferences @@ -68,7 +68,7 @@ class PlayerActivity : AppCompatActivity() { private val viewModelModule: Module by lazy { module { viewModel { viewModel } } } private val binding by lazy { PlayerLayoutBinding.inflate(layoutInflater) } private val playerObserver by lazy { PlayerObserver(this) } - private val mpvKtDatabase: MpvKtDatabase by inject() + private val playbackStateRepository: PlaybackStateRepository by inject() val player by lazy { binding.player } val windowInsetsController by lazy { WindowCompat.getInsetsController(window, window.decorView) } val audioManager by lazy { getSystemService(Context.AUDIO_SERVICE) as AudioManager } @@ -550,9 +550,9 @@ class PlayerActivity : AppCompatActivity() { private fun saveVideoPlaybackState(mediaTitle: String) { if (mediaTitle.isBlank()) return lifecycleScope.launch(Dispatchers.IO) { - val oldState = mpvKtDatabase.videoDataDao().getVideoDataByTitle(fileName) + val oldState = playbackStateRepository.getVideoDataByTitle(fileName) Log.d(TAG, "Saving playback state") - mpvKtDatabase.videoDataDao().upsert( + playbackStateRepository.upsert( PlaybackStateEntity( mediaTitle = mediaTitle, lastPosition = if (playerPreferences.savePositionOnQuit.get()) { @@ -575,7 +575,7 @@ class PlayerActivity : AppCompatActivity() { private suspend fun loadVideoPlaybackState(mediaTitle: String) { if (mediaTitle.isBlank()) return - val state = mpvKtDatabase.videoDataDao().getVideoDataByTitle(mediaTitle) + val state = playbackStateRepository.getVideoDataByTitle(mediaTitle) val getDelay: (Int, Int?) -> Double = { preferenceDelay, stateDelay -> (stateDelay ?: preferenceDelay) / 1000.0 }