@@ -5,6 +5,8 @@ import androidx.lifecycle.ViewModel
55import androidx.lifecycle.viewModelScope
66import dev.bartuzen.qbitcontroller.data.ServerManager
77import dev.bartuzen.qbitcontroller.data.repositories.AddTorrentRepository
8+ import dev.bartuzen.qbitcontroller.model.QBittorrentVersion
9+ import dev.bartuzen.qbitcontroller.network.RequestManager
810import dev.bartuzen.qbitcontroller.network.RequestResult
911import dev.bartuzen.qbitcontroller.utils.getSerializableStateFlow
1012import io.github.vinceglb.filekit.PlatformFile
@@ -14,6 +16,7 @@ import kotlinx.coroutines.CancellationException
1416import kotlinx.coroutines.Job
1517import kotlinx.coroutines.async
1618import kotlinx.coroutines.channels.Channel
19+ import kotlinx.coroutines.delay
1720import kotlinx.coroutines.flow.MutableStateFlow
1821import kotlinx.coroutines.flow.asStateFlow
1922import kotlinx.coroutines.flow.collectLatest
@@ -29,6 +32,7 @@ class AddTorrentViewModel(
2932 private val savedStateHandle : SavedStateHandle ,
3033 private val repository : AddTorrentRepository ,
3134 private val serverManager : ServerManager ,
35+ private val requestManager : RequestManager ,
3236) : ViewModel() {
3337 private val eventChannel = Channel <Event >()
3438 val eventFlow = eventChannel.receiveAsFlow()
@@ -224,35 +228,60 @@ class AddTorrentViewModel(
224228
225229 fun searchDirectories (serverId : Int , path : String ) {
226230 searchDirectoriesJob?.cancel()
227- if (path.isBlank() || ! path.startsWith(" /" )) {
231+
232+ if (path.isBlank()) {
228233 _directorySuggestions .value = emptyList()
229234 return
230235 }
231236
237+ val version = requestManager.getQBittorrentVersion(serverId)
238+ if (version < QBittorrentVersion (5 , 0 , 0 )) {
239+ return
240+ }
241+
232242 searchDirectoriesJob = viewModelScope.launch {
233- kotlinx.coroutines. delay(300 )
243+ delay(300 )
234244
235245 val suggestions = ArrayList <String >()
236246
237- when (val result = repository.getDirectoryContent(serverId, path)) {
238- is RequestResult .Success -> {
239- suggestions.addAll(result.data)
247+ val pathDeferred = async {
248+ when (val result = repository.getDirectoryContent(serverId, path)) {
249+ is RequestResult .Success -> {
250+ result.data
251+ }
252+ is RequestResult .Error -> {
253+ emptyList()
254+ }
240255 }
241- else -> {}
242256 }
243257
244- // if path doesn't ends with a slash, we need to also check the parent directory for suggestions
245- // e.g. for /downloads/m, check if there's a directory's name under /downloads starts with "m"
246- if (! path.endsWith(" /" )) {
247- val lastSeparatorIndex = path.lastIndexOf(' /' )
248- val parent = path.take(lastSeparatorIndex + 1 )
249- when (val result = repository.getDirectoryContent(serverId, parent)) {
250- is RequestResult .Success -> {
251- suggestions.addAll(result.data)
258+ val parentDeferred = async {
259+ // if path doesn't ends with a slash, we need to also check the parent directory for suggestions
260+ // e.g. for /downloads/m, check if there's a directory's name under /downloads starts with "m"
261+ if (! path.endsWith(" /" ) && ! path.endsWith(" \\ " )) {
262+ val separator = if (path.contains(' /' )) ' /' else ' \\ '
263+ val lastSeparatorIndex = path.lastIndexOf(separator)
264+ if (lastSeparatorIndex != - 1 ) {
265+ val parent = path.take(lastSeparatorIndex + 1 )
266+ when (val result = repository.getDirectoryContent(serverId, parent)) {
267+ is RequestResult .Success -> {
268+ result.data
269+ }
270+ is RequestResult .Error -> {
271+ emptyList()
272+ }
273+ }
274+ } else {
275+ emptyList()
252276 }
253- else -> {}
277+ } else {
278+ emptyList()
254279 }
255280 }
281+
282+ suggestions.addAll(pathDeferred.await())
283+ suggestions.addAll(parentDeferred.await())
284+
256285 _directorySuggestions .value = suggestions
257286 .filter { it.startsWith(path, ignoreCase = true ) }
258287 .sorted()
0 commit comments