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 @@ -89,7 +89,7 @@ val appModule = module {
viewModel { (serverId: Int, hash: String) -> TorrentPeersViewModel(serverId, hash, get(), get(), get()) }
viewModel { (serverId: Int, hash: String) -> TorrentWebSeedsViewModel(serverId, hash, get(), get()) }

viewModel { (initialServerId: Int?) -> AddTorrentViewModel(initialServerId, get(), get(), get()) }
viewModel { (initialServerId: Int?) -> AddTorrentViewModel(initialServerId, get(), get(), get(), get()) }

viewModel { (serverId: Int) -> RssFeedsViewModel(serverId, get()) }
viewModel { (serverId: Int, feedPath: List<String>, uid: String?) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
Expand Down Expand Up @@ -880,7 +882,7 @@ fun AddTorrentScreen(
var savePathExpanded by remember { mutableStateOf(false) }

// Hide suggestions when IME is folded
val density = androidx.compose.ui.platform.LocalDensity.current
val density = LocalDensity.current
val isImeVisible = WindowInsets.ime.getBottom(density) > 0
LaunchedEffect(isImeVisible) {
if (!isImeVisible) {
Expand Down Expand Up @@ -955,7 +957,7 @@ fun AddTorrentScreen(
onClick = {
savePath = TextFieldValue(
text = suggestion,
selection = androidx.compose.ui.text.TextRange(suggestion.length),
selection = TextRange(suggestion.length),
)
savePathExpanded = true
// Trigger search again for the new path to load its subdirectories
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dev.bartuzen.qbitcontroller.data.ServerManager
import dev.bartuzen.qbitcontroller.data.repositories.AddTorrentRepository
import dev.bartuzen.qbitcontroller.model.QBittorrentVersion
import dev.bartuzen.qbitcontroller.network.RequestManager
import dev.bartuzen.qbitcontroller.network.RequestResult
import dev.bartuzen.qbitcontroller.utils.getSerializableStateFlow
import io.github.vinceglb.filekit.PlatformFile
Expand All @@ -14,6 +16,7 @@ import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.collectLatest
Expand All @@ -29,6 +32,7 @@ class AddTorrentViewModel(
private val savedStateHandle: SavedStateHandle,
private val repository: AddTorrentRepository,
private val serverManager: ServerManager,
private val requestManager: RequestManager,
) : ViewModel() {
private val eventChannel = Channel<Event>()
val eventFlow = eventChannel.receiveAsFlow()
Expand Down Expand Up @@ -224,35 +228,60 @@ class AddTorrentViewModel(

fun searchDirectories(serverId: Int, path: String) {
searchDirectoriesJob?.cancel()
if (path.isBlank() || !path.startsWith("/")) {

if (path.isBlank()) {
_directorySuggestions.value = emptyList()
return
}

val version = requestManager.getQBittorrentVersion(serverId)
if (version < QBittorrentVersion(5, 0, 0)) {
return
}

searchDirectoriesJob = viewModelScope.launch {
kotlinx.coroutines.delay(300)
delay(300)

val suggestions = ArrayList<String>()

when (val result = repository.getDirectoryContent(serverId, path)) {
is RequestResult.Success -> {
suggestions.addAll(result.data)
val pathDeferred = async {
when (val result = repository.getDirectoryContent(serverId, path)) {
is RequestResult.Success -> {
result.data
}
is RequestResult.Error -> {
emptyList()
}
}
else -> {}
}

// if path doesn't ends with a slash, we need to also check the parent directory for suggestions
// e.g. for /downloads/m, check if there's a directory's name under /downloads starts with "m"
if (!path.endsWith("/")) {
val lastSeparatorIndex = path.lastIndexOf('/')
val parent = path.take(lastSeparatorIndex + 1)
when (val result = repository.getDirectoryContent(serverId, parent)) {
is RequestResult.Success -> {
suggestions.addAll(result.data)
val parentDeferred = async {
// if path doesn't ends with a slash, we need to also check the parent directory for suggestions
// e.g. for /downloads/m, check if there's a directory's name under /downloads starts with "m"
if (!path.endsWith("/") && !path.endsWith("\\")) {
val separator = if (path.contains('/')) '/' else '\\'
val lastSeparatorIndex = path.lastIndexOf(separator)
if (lastSeparatorIndex != -1) {
val parent = path.take(lastSeparatorIndex + 1)
when (val result = repository.getDirectoryContent(serverId, parent)) {
is RequestResult.Success -> {
result.data
}
is RequestResult.Error -> {
emptyList()
}
}
} else {
emptyList()
}
else -> {}
} else {
emptyList()
}
}

suggestions.addAll(pathDeferred.await())
suggestions.addAll(parentDeferred.await())

_directorySuggestions.value = suggestions
.filter { it.startsWith(path, ignoreCase = true) }
.sorted()
Expand Down