Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import xyz.mpv.rex.domain.media.model.Video
import xyz.mpv.rex.preferences.BrowserPreferences
import xyz.mpv.rex.preferences.FoldersPreferences
import xyz.mpv.rex.utils.media.MediaFormatter
import xyz.mpv.rex.utils.storage.isPathExcluded
import xyz.mpv.rex.utils.storage.FileFilterUtils
import xyz.mpv.rex.utils.storage.FileTypeUtils
import xyz.mpv.rex.utils.storage.StorageVolumeUtils
Expand Down Expand Up @@ -84,11 +85,10 @@ class HybridMediaIndexRepository(
suspend fun purgeExcludedFolders(excludedPaths: Set<String> = foldersPreferences.blacklistedFolders.get()) = withContext(Dispatchers.IO) {
if (excludedPaths.isEmpty()) return@withContext
Log.d(TAG, "Purging excluded folders from Room index: $excludedPaths")
excludedPaths.forEach { rawPath ->
val normalized = normalizePath(File(rawPath))
val escaped = normalized.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
dao.deleteByPathPrefix(normalized, "$escaped/%")
}
val locations = dao.getAvailableMedia(includeNoMedia = true)
.filter { isPathExcluded(it.location, excludedPaths) || isPathExcluded(it.parentIdentity, excludedPaths) }
.map { it.location }
locations.chunked(400).forEach { dao.deleteByLocations(it) }
}

suspend fun removeEntries(paths: List<String>) = withContext(Dispatchers.IO) {
Expand Down Expand Up @@ -318,13 +318,20 @@ class HybridMediaIndexRepository(
}


private suspend fun visibleMedia(includeNoMedia: Boolean): List<HybridMediaEntity> {
val blacklist = foldersPreferences.blacklistedFolders.get()
return dao.getAvailableMedia(includeNoMedia).filter { item ->
!isPathExcluded(item.location, blacklist) && !isPathExcluded(item.parentIdentity, blacklist)
}
}

suspend fun getFlatFolders(
playbackStates: List<PlaybackStateEntity>,
thresholdDays: Int,
watchedThreshold: Int,
includeNoMedia: Boolean = browserPreferences.includeNoMediaContent.get(),
): List<MediaFolder> = withContext(Dispatchers.IO) {
val items = dao.getAvailableMedia(includeNoMedia)
val items = visibleMedia(includeNoMedia)
val stateByIdentity = playbackStates.associateBy { it.mediaTitle }
val thresholdMillis = thresholdDays * 24L * 60L * 60L * 1000L
val now = System.currentTimeMillis()
Expand Down Expand Up @@ -381,7 +388,7 @@ class HybridMediaIndexRepository(
includeNoMedia: Boolean = browserPreferences.includeNoMediaContent.get(),
): List<MediaFolder> = withContext(Dispatchers.IO) {
val tree = buildFileTree(
dao.getAvailableMedia(includeNoMedia),
visibleMedia(includeNoMedia),
playbackStates,
thresholdDays,
watchedThreshold,
Expand All @@ -399,7 +406,7 @@ class HybridMediaIndexRepository(
includeNoMedia: Boolean = browserPreferences.includeNoMediaContent.get(),
): MediaFolder? = withContext(Dispatchers.IO) {
buildFileTree(
dao.getAvailableMedia(includeNoMedia),
visibleMedia(includeNoMedia),
playbackStates,
thresholdDays,
watchedThreshold,
Expand All @@ -410,7 +417,7 @@ class HybridMediaIndexRepository(
parentIdentity: String,
includeNoMedia: Boolean = browserPreferences.includeNoMediaContent.get(),
): List<Video> = withContext(Dispatchers.IO) {
dao.getAvailableMedia(includeNoMedia)
visibleMedia(includeNoMedia)
.asSequence()
.filter { it.parentIdentity == parentIdentity }
.map { it.toVideo() }
Expand All @@ -435,7 +442,10 @@ class HybridMediaIndexRepository(
val prefix = "$parentPath/%"
dao.searchMediaInPath(query, parentPath, prefix, includeNoMedia)
}
entities.map { it.toVideo() }
val blacklist = foldersPreferences.blacklistedFolders.get()
entities.filter { item ->
!isPathExcluded(item.location, blacklist) && !isPathExcluded(item.parentIdentity, blacklist)
}.map { it.toVideo() }
}

suspend fun clear() {
Expand Down Expand Up @@ -507,7 +517,7 @@ class HybridMediaIndexRepository(

val includeNoMedia = browserPreferences.includeNoMediaContent.get()
val blacklist = foldersPreferences.blacklistedFolders.get()
.mapTo(mutableSetOf()) { normalizePath(File(it)) }
.toSet()
val batch = mutableListOf<HybridMediaEntity>()
var count = 0

Expand All @@ -522,7 +532,7 @@ class HybridMediaIndexRepository(
currentCoroutineContext().ensureActive()
if (depth > MAX_DEPTH) return
val normalizedFolder = normalizePath(folder)
if (blacklist.any { normalizedFolder == it || normalizedFolder.startsWith("$it/") }) {
if (isPathExcluded(normalizedFolder, blacklist)) {
Log.d(TAG, "Skipping excluded directory tree: $normalizedFolder")
return
}
Expand Down Expand Up @@ -602,11 +612,7 @@ class HybridMediaIndexRepository(
currentCoroutineContext().ensureActive()
if (depth > MAX_DEPTH) return
val folderUriStr = folder.uri.toString()
if (blacklist.any { excluded ->
folderUriStr == excluded || folderUriStr.startsWith("$excluded/") ||
(excluded.startsWith("/") && Uri.decode(folderUriStr).contains(excluded))
}
) {
if (isPathExcluded(folderUriStr, blacklist)) {
Log.d(TAG, "Skipping excluded SAF directory tree: $folderUriStr")
return
}
Expand Down Expand Up @@ -669,7 +675,7 @@ class HybridMediaIndexRepository(
alreadyScanned: Int,
): Int {
val blacklist = foldersPreferences.blacklistedFolders.get()
.mapTo(mutableSetOf()) { normalizePath(File(it)) }
.toSet()
val batch = mutableListOf<HybridMediaEntity>()
var count = 0

Expand Down Expand Up @@ -716,7 +722,7 @@ class HybridMediaIndexRepository(
val contentUri = Uri.withAppendedPath(uri, id.toString()).toString()
val location = file?.let(::normalizePath) ?: contentUri
val parent = file?.parentFile?.let(::normalizePath) ?: contentUri.substringBeforeLast('/')
if (blacklist.any { location == it || location.startsWith("$it/") || parent == it || parent.startsWith("$it/") }) {
if (isPathExcluded(location, blacklist) || isPathExcluded(parent, blacklist)) {
Log.d(TAG, "Skipping excluded MediaStore item: $location")
continue
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import xyz.mpv.rex.utils.media.MediaLibraryEvents
import xyz.mpv.rex.utils.media.MetadataRetrieval
import xyz.mpv.rex.utils.storage.FileFilterUtils
import xyz.mpv.rex.utils.storage.FileTypeUtils
import xyz.mpv.rex.utils.storage.isPathExcluded
import java.io.File
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -119,7 +120,7 @@ class FolderListViewModel(
browserPreferences.includeNoMediaContent.changes(),
) { folders, blacklist, showAudio, includeNoMedia ->
folders.filter { folder ->
folder.path !in blacklist &&
!isPathExcluded(folder.path, blacklist) &&
folder.activeCount(showAudio) > 0 &&
(includeNoMedia || !FileFilterUtils.isWithinNoMediaBoundary(File(folder.path)))
}
Expand Down Expand Up @@ -276,7 +277,7 @@ class FolderListViewModel(
val showAudio = browserPreferences.showAudioFiles.get()
val includeNoMedia = browserPreferences.includeNoMediaContent.get()
val filteredFolders = folders.filter { folder ->
folder.path !in blacklist &&
!isPathExcluded(folder.path, blacklist) &&
folder.activeCount(showAudio) > 0 &&
(includeNoMedia || !FileFilterUtils.isWithinNoMediaBoundary(File(folder.path)))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import xyz.mpv.rex.utils.storage.isPathExcluded
import kotlinx.coroutines.flow.merge
import kotlinx.coroutines.flow.map
import java.io.File

enum class SearchScope {
Expand Down Expand Up @@ -96,6 +99,14 @@ class SearchViewModel(
private var searchJob: Job? = null

init {
viewModelScope.launch {
merge(
MediaLibraryEvents.changes,
foldersPreferences.blacklistedFolders.changes().map { Unit },
).debounce(150L).collectLatest {
executeSearch(searchQuery.value, searchScope.value)
}
}
viewModelScope.launch {
searchQuery
.debounce(150L)
Expand Down Expand Up @@ -142,7 +153,7 @@ class SearchViewModel(
// 1. Query videos from Hybrid DB
val videos = hybridMediaIndex.searchMedia(query, targetPath, includeNoMedia)
.filter { isAudioEnabled || !it.isAudio }
.filter { it.path !in blacklist && !blacklist.any { b -> it.path.startsWith("$b/") } }
.filter { !isPathExcluded(it.path, blacklist) }

// Pre-apply cached metadata to ensure valid durations on frame 1
val cachedVideos = MetadataRetrieval.applyCachedMetadata(videos, browserPreferences, metadataCache)
Expand All @@ -162,7 +173,7 @@ class SearchViewModel(
if (targetPath == null) {
val folders = MediaFileRepository.getAllVideoFolders(getApplication())
folders.filter { it.name.contains(query, ignoreCase = true) || it.path.contains(query, ignoreCase = true) }
.filter { it.path !in blacklist && it.activeCount(isAudioEnabled) > 0 }
.filter { !isPathExcluded(it.path, blacklist) && it.activeCount(isAudioEnabled) > 0 }
.forEach { folder ->
results.add(
FileSystemItem.Folder(
Expand Down
Loading
Loading