Skip to content

Commit

Permalink
Refactor: Rename Directory model to Folder model (#483)
Browse files Browse the repository at this point in the history
* refactor: get sorted video use case to use repository function instead of filtering

* refactor: rename Directory model to Folder model

* lint: run KtlintFormat
  • Loading branch information
anilbeesetti authored Aug 2, 2023
1 parent 7686a02 commit 6a4e947
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package dev.anilbeesetti.nextplayer.core.data.mappers

import dev.anilbeesetti.nextplayer.core.common.Utils
import dev.anilbeesetti.nextplayer.core.database.entities.DirectoryEntity
import dev.anilbeesetti.nextplayer.core.model.Directory
import dev.anilbeesetti.nextplayer.core.model.Folder

fun DirectoryEntity.toDirectory() = Directory(
fun DirectoryEntity.toFolder() = Folder(
name = name,
path = path,
mediaSize = size,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import androidx.activity.result.IntentSenderRequest
import dagger.hilt.android.qualifiers.ApplicationContext
import dev.anilbeesetti.nextplayer.core.common.di.ApplicationScope
import dev.anilbeesetti.nextplayer.core.common.extensions.deleteFiles
import dev.anilbeesetti.nextplayer.core.data.mappers.toDirectory
import dev.anilbeesetti.nextplayer.core.data.mappers.toFolder
import dev.anilbeesetti.nextplayer.core.data.mappers.toVideo
import dev.anilbeesetti.nextplayer.core.data.mappers.toVideoState
import dev.anilbeesetti.nextplayer.core.data.models.VideoState
import dev.anilbeesetti.nextplayer.core.database.dao.DirectoryDao
import dev.anilbeesetti.nextplayer.core.database.dao.MediumDao
import dev.anilbeesetti.nextplayer.core.database.entities.DirectoryEntity
import dev.anilbeesetti.nextplayer.core.database.entities.MediumEntity
import dev.anilbeesetti.nextplayer.core.model.Directory
import dev.anilbeesetti.nextplayer.core.model.Folder
import dev.anilbeesetti.nextplayer.core.model.Video
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
Expand All @@ -36,8 +36,12 @@ class LocalMediaRepository @Inject constructor(
return mediumDao.getAll().map { it.map(MediumEntity::toVideo) }
}

override fun getDirectoriesFlow(): Flow<List<Directory>> {
return directoryDao.getAll().map { it.map(DirectoryEntity::toDirectory) }
override fun getVideosFlowFromFolderPath(folderPath: String): Flow<List<Video>> {
return mediumDao.getAllFromDirectory(folderPath).map { it.map(MediumEntity::toVideo) }
}

override fun getFoldersFlow(): Flow<List<Folder>> {
return directoryDao.getAll().map { it.map(DirectoryEntity::toFolder) }
}

override suspend fun getVideoState(path: String): VideoState? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dev.anilbeesetti.nextplayer.core.data.repository
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.IntentSenderRequest
import dev.anilbeesetti.nextplayer.core.data.models.VideoState
import dev.anilbeesetti.nextplayer.core.model.Directory
import dev.anilbeesetti.nextplayer.core.model.Folder
import dev.anilbeesetti.nextplayer.core.model.Video
import kotlinx.coroutines.flow.Flow

Expand All @@ -14,7 +14,18 @@ interface MediaRepository {
*/
fun getVideosFlow(): Flow<List<Video>>

fun getDirectoriesFlow(): Flow<List<Directory>>
/**
* Get list of [Video]s as flow for a specific directory
* @param folderPath the path the directory from which the videos should be returned
* @return flow of list of video items
*/
fun getVideosFlowFromFolderPath(folderPath: String): Flow<List<Video>>

/**
* Get list of [Folder]s as flow
* @return flow of list of folder items
*/
fun getFoldersFlow(): Flow<List<Folder>>

/**
* Save video state
Expand All @@ -25,19 +36,8 @@ interface MediaRepository {
* -1 to disable track
* null to not change track
*/
suspend fun saveVideoState(
path: String,
position: Long,
audioTrackIndex: Int?,
subtitleTrackIndex: Int?,
playbackSpeed: Float?
)
suspend fun saveVideoState(path: String, position: Long, audioTrackIndex: Int?, subtitleTrackIndex: Int?, playbackSpeed: Float?)

/**
* Get video state
* @param path path of the video
* @return [VideoState] of the video
*/
suspend fun getVideoState(path: String): VideoState?

suspend fun deleteVideos(videoUris: List<String>, intentSenderLauncher: ActivityResultLauncher<IntentSenderRequest>)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@ import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.IntentSenderRequest
import dev.anilbeesetti.nextplayer.core.data.models.VideoState
import dev.anilbeesetti.nextplayer.core.data.repository.MediaRepository
import dev.anilbeesetti.nextplayer.core.model.Directory
import dev.anilbeesetti.nextplayer.core.model.Folder
import dev.anilbeesetti.nextplayer.core.model.Video
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf

class FakeMediaRepository : MediaRepository {

val videos = mutableListOf<Video>()
val directories = mutableListOf<Directory>()
val directories = mutableListOf<Folder>()

override fun getVideosFlow(): Flow<List<Video>> {
return flowOf(videos)
}

override fun getDirectoriesFlow(): Flow<List<Directory>> {
override fun getVideosFlowFromFolderPath(folderPath: String): Flow<List<Video>> {
return flowOf(videos)
}

override fun getFoldersFlow(): Flow<List<Folder>> {
return flowOf(directories)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package dev.anilbeesetti.nextplayer.core.domain

import dev.anilbeesetti.nextplayer.core.common.Dispatcher
import dev.anilbeesetti.nextplayer.core.common.NextDispatchers
import dev.anilbeesetti.nextplayer.core.data.repository.LocalMediaRepository
import dev.anilbeesetti.nextplayer.core.data.repository.MediaRepository
import dev.anilbeesetti.nextplayer.core.data.repository.PreferencesRepository
import dev.anilbeesetti.nextplayer.core.model.Directory
import dev.anilbeesetti.nextplayer.core.model.Folder
import dev.anilbeesetti.nextplayer.core.model.SortBy
import dev.anilbeesetti.nextplayer.core.model.SortOrder
import javax.inject.Inject
Expand All @@ -13,19 +13,19 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flowOn

class GetSortedDirectoriesUseCase @Inject constructor(
private val videoRepository: LocalMediaRepository,
class GetSortedFoldersUseCase @Inject constructor(
private val mediaRepository: MediaRepository,
private val preferencesRepository: PreferencesRepository,
@Dispatcher(NextDispatchers.Default) private val defaultDispatcher: CoroutineDispatcher
) {

operator fun invoke(): Flow<List<Directory>> {
operator fun invoke(): Flow<List<Folder>> {
return combine(
videoRepository.getDirectoriesFlow(),
mediaRepository.getFoldersFlow(),
preferencesRepository.applicationPreferences
) { directories, preferences ->
) { folders, preferences ->

val nonExcludedDirectories = directories.filterNot {
val nonExcludedDirectories = folders.filterNot {
it.path in preferences.excludeFolders
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,39 @@ class GetSortedVideosUseCase @Inject constructor(
) {

operator fun invoke(folderPath: String? = null): Flow<List<Video>> {
val videosFlow = if (folderPath != null) {
mediaRepository.getVideosFlowFromFolderPath(folderPath)
} else {
mediaRepository.getVideosFlow()
}

return combine(
mediaRepository.getVideosFlow(),
videosFlow,
preferencesRepository.applicationPreferences
) { videoItems, preferences ->

val filteredVideos = videoItems.filter {
if (preferences.groupVideosByFolder) {
folderPath == null || it.parentPath == folderPath
} else {
true
}
}.filterNot {
val nonExcludedVideos = videoItems.filterNot {
it.parentPath in preferences.excludeFolders
}

when (preferences.sortOrder) {
SortOrder.ASCENDING -> {
when (preferences.sortBy) {
SortBy.TITLE -> filteredVideos.sortedBy { it.displayName.lowercase() }
SortBy.LENGTH -> filteredVideos.sortedBy { it.duration }
SortBy.PATH -> filteredVideos.sortedBy { it.path.lowercase() }
SortBy.SIZE -> filteredVideos.sortedBy { it.size }
SortBy.DATE -> filteredVideos.sortedBy { it.dateModified }
SortBy.TITLE -> nonExcludedVideos.sortedBy { it.displayName.lowercase() }
SortBy.LENGTH -> nonExcludedVideos.sortedBy { it.duration }
SortBy.PATH -> nonExcludedVideos.sortedBy { it.path.lowercase() }
SortBy.SIZE -> nonExcludedVideos.sortedBy { it.size }
SortBy.DATE -> nonExcludedVideos.sortedBy { it.dateModified }
}
}

SortOrder.DESCENDING -> {
when (preferences.sortBy) {
SortBy.TITLE -> filteredVideos.sortedByDescending { it.displayName.lowercase() }
SortBy.LENGTH -> filteredVideos.sortedByDescending { it.duration }
SortBy.PATH -> filteredVideos.sortedByDescending { it.path.lowercase() }
SortBy.SIZE -> filteredVideos.sortedByDescending { it.size }
SortBy.DATE -> filteredVideos.sortedByDescending { it.dateModified }
SortBy.TITLE -> nonExcludedVideos.sortedByDescending { it.displayName.lowercase() }
SortBy.LENGTH -> nonExcludedVideos.sortedByDescending { it.duration }
SortBy.PATH -> nonExcludedVideos.sortedByDescending { it.path.lowercase() }
SortBy.SIZE -> nonExcludedVideos.sortedByDescending { it.size }
SortBy.DATE -> nonExcludedVideos.sortedByDescending { it.dateModified }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.anilbeesetti.nextplayer.core.model

data class Directory(
data class Folder(
val name: String,
val path: String,
val mediaSize: Long,
Expand All @@ -10,7 +10,7 @@ data class Directory(
) {

companion object {
val sample = Directory(
val sample = Folder(
name = "Folder 1",
path = "/storage/emulated/0/DCIM/Camera/Live Photos",
mediaSize = 1024,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import dev.anilbeesetti.nextplayer.core.data.repository.MediaRepository
import dev.anilbeesetti.nextplayer.core.data.repository.PreferencesRepository
import dev.anilbeesetti.nextplayer.core.model.ApplicationPreferences
import dev.anilbeesetti.nextplayer.core.model.Directory
import dev.anilbeesetti.nextplayer.core.model.Folder
import javax.inject.Inject
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.map
Expand All @@ -19,7 +19,7 @@ class MediaLibraryPreferencesViewModel @Inject constructor(
private val preferencesRepository: PreferencesRepository
) : ViewModel() {

val uiState = mediaRepository.getDirectoriesFlow()
val uiState = mediaRepository.getFoldersFlow()
.map { FolderPreferencesUiState.Success(it) }
.stateIn(
scope = viewModelScope,
Expand Down Expand Up @@ -52,5 +52,5 @@ class MediaLibraryPreferencesViewModel @Inject constructor(
sealed interface FolderPreferencesUiState {
object Loading : FolderPreferencesUiState

data class Success(val directories: List<Directory>) : FolderPreferencesUiState
data class Success(val directories: List<Folder>) : FolderPreferencesUiState
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.onParent
import dev.anilbeesetti.nextplayer.core.model.ApplicationPreferences
import dev.anilbeesetti.nextplayer.core.model.Directory
import dev.anilbeesetti.nextplayer.core.model.Folder
import dev.anilbeesetti.nextplayer.core.model.Video
import dev.anilbeesetti.nextplayer.core.ui.R
import dev.anilbeesetti.nextplayer.feature.videopicker.screens.FoldersState
Expand Down Expand Up @@ -84,7 +84,7 @@ class MediaPickerScreenTest {

/**
* This test is to check if the folder items are displayed
* when the [FoldersState.Success] is passed with list of [Directory],
* when the [FoldersState.Success] is passed with list of [Folder],
* along with [ApplicationPreferences].groupVideosByFolder = true
*/
@Test
Expand Down Expand Up @@ -206,14 +206,14 @@ val videoItemsTestData = listOf(
)

val foldersTestData = listOf(
Directory(
Folder(
name = "Folder 1",
path = "/storage/emulated/0/DCIM/Camera/Folder 1",
mediaCount = 1,
mediaSize = 1000,
dateModified = 1000
),
Directory(
Folder(
name = "Folder 2",
path = "/storage/emulated/0/DCIM/Camera/Folder 2",
mediaCount = 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.offset
import dev.anilbeesetti.nextplayer.core.model.Directory
import dev.anilbeesetti.nextplayer.core.model.Folder
import dev.anilbeesetti.nextplayer.core.ui.R
import dev.anilbeesetti.nextplayer.core.ui.designsystem.NextIcons
import dev.anilbeesetti.nextplayer.core.ui.preview.DayNightPreview
Expand All @@ -29,7 +29,7 @@ import dev.anilbeesetti.nextplayer.core.ui.theme.NextPlayerTheme
@OptIn(ExperimentalLayoutApi::class)
@Composable
fun FolderItem(
directory: Directory,
folder: Folder,
modifier: Modifier = Modifier
) {
ListItem(
Expand All @@ -47,15 +47,15 @@ fun FolderItem(
},
headlineContent = {
Text(
text = directory.name,
text = folder.name,
maxLines = 2,
style = MaterialTheme.typography.titleMedium,
overflow = TextOverflow.Ellipsis
)
},
supportingContent = {
Text(
text = directory.path,
text = folder.path,
maxLines = 2,
style = MaterialTheme.typography.bodySmall,
overflow = TextOverflow.Ellipsis,
Expand All @@ -67,15 +67,15 @@ fun FolderItem(
verticalAlignment = Alignment.CenterVertically
) {
InfoChip(
text = "${directory.mediaCount} ${
text = "${folder.mediaCount} ${
stringResource(
id = R.string.video.takeIf { directory.mediaCount == 1 } ?: R.string.videos
id = R.string.video.takeIf { folder.mediaCount == 1 } ?: R.string.videos
)
}",
modifier = Modifier.padding(vertical = 5.dp)
)
InfoChip(
text = directory.formattedMediaSize,
text = folder.formattedMediaSize,
modifier = Modifier.padding(vertical = 5.dp)
)
}
Expand All @@ -88,7 +88,7 @@ fun FolderItem(
@Composable
fun FolderItemPreview() {
NextPlayerTheme {
FolderItem(directory = Directory.sample)
FolderItem(folder = Folder.sample)
}
}

Expand Down
Loading

0 comments on commit 6a4e947

Please sign in to comment.