Skip to content

Commit

Permalink
refactor: use and inject repositories instead of the entire db
Browse files Browse the repository at this point in the history
  • Loading branch information
abdallahmehiz committed Nov 11, 2024
1 parent 6864fc8 commit 9ed2d30
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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()
}
}
7 changes: 6 additions & 1 deletion app/src/main/java/live/mehiz/mpvkt/di/DatabaseModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -16,5 +20,6 @@ val DatabaseModule = module {
.build()
}

singleOf(::CustomButtonRepositoryImpl)
singleOf(::CustomButtonRepositoryImpl).bind(CustomButtonRepository::class)
singleOf(::PlaybackStateRepositoryImpl).bind(PlaybackStateRepository::class)
}
Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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>(CustomButtonDialog.None)
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }
Expand Down Expand Up @@ -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()) {
Expand All @@ -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
}
Expand Down

0 comments on commit 9ed2d30

Please sign in to comment.