Watch local manga directories for changes

This commit is contained in:
Koitharu
2022-08-10 15:15:22 +03:00
parent 0077dc2f1c
commit ad1d247694
5 changed files with 22 additions and 26 deletions

View File

@@ -191,6 +191,7 @@ class DownloadService : BaseService() {
var isRunning: Boolean = false var isRunning: Boolean = false
private set private set
@Deprecated("Use LocalMangaRepository.watchReadableDirs instead")
const val ACTION_DOWNLOAD_COMPLETE = "${BuildConfig.APPLICATION_ID}.action.ACTION_DOWNLOAD_COMPLETE" const val ACTION_DOWNLOAD_COMPLETE = "${BuildConfig.APPLICATION_ID}.action.ACTION_DOWNLOAD_COMPLETE"
private const val ACTION_DOWNLOAD_CANCEL = "${BuildConfig.APPLICATION_ID}.action.ACTION_DOWNLOAD_CANCEL" private const val ACTION_DOWNLOAD_CANCEL = "${BuildConfig.APPLICATION_ID}.action.ACTION_DOWNLOAD_CANCEL"

View File

@@ -8,4 +8,4 @@ class TempFileFilter : FilenameFilter {
override fun accept(dir: File, name: String): Boolean { override fun accept(dir: File, name: String): Boolean {
return name.endsWith(".tmp", ignoreCase = true) return name.endsWith(".tmp", ignoreCase = true)
} }
} }

View File

@@ -15,6 +15,8 @@ import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filterNot
import org.koitharu.kotatsu.core.parser.MangaRepository import org.koitharu.kotatsu.core.parser.MangaRepository
import org.koitharu.kotatsu.local.data.CbzFilter import org.koitharu.kotatsu.local.data.CbzFilter
import org.koitharu.kotatsu.local.data.LocalStorageManager import org.koitharu.kotatsu.local.data.LocalStorageManager
@@ -219,6 +221,13 @@ class LocalMangaRepository @Inject constructor(private val storageManager: Local
} }
} }
suspend fun watchReadableDirs(): Flow<File> {
val filter = TempFileFilter()
val dirs = storageManager.getReadableDirs()
return storageManager.observe(dirs)
.filterNot { filter.accept(it, it.name) }
}
private fun CoroutineScope.getFromFileAsync( private fun CoroutineScope.getFromFileAsync(
file: File, file: File,
context: CoroutineContext, context: CoroutineContext,

View File

@@ -1,9 +1,5 @@
package org.koitharu.kotatsu.local.ui package org.koitharu.kotatsu.local.ui
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
@@ -16,7 +12,6 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import org.koitharu.kotatsu.R import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.base.ui.list.ListSelectionController import org.koitharu.kotatsu.base.ui.list.ListSelectionController
import org.koitharu.kotatsu.download.ui.service.DownloadService
import org.koitharu.kotatsu.list.ui.MangaListFragment import org.koitharu.kotatsu.list.ui.MangaListFragment
import org.koitharu.kotatsu.utils.ShareHelper import org.koitharu.kotatsu.utils.ShareHelper
import org.koitharu.kotatsu.utils.ext.addMenuProvider import org.koitharu.kotatsu.utils.ext.addMenuProvider
@@ -24,21 +19,6 @@ import org.koitharu.kotatsu.utils.ext.addMenuProvider
class LocalListFragment : MangaListFragment() { class LocalListFragment : MangaListFragment() {
override val viewModel by viewModels<LocalListViewModel>() override val viewModel by viewModels<LocalListViewModel>()
private val downloadReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action == DownloadService.ACTION_DOWNLOAD_COMPLETE) {
viewModel.onRefresh()
}
}
}
override fun onAttach(context: Context) {
super.onAttach(context)
context.registerReceiver(
downloadReceiver,
IntentFilter(DownloadService.ACTION_DOWNLOAD_COMPLETE),
)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
@@ -46,11 +26,6 @@ class LocalListFragment : MangaListFragment() {
viewModel.onMangaRemoved.observe(viewLifecycleOwner) { onItemRemoved() } viewModel.onMangaRemoved.observe(viewLifecycleOwner) { onItemRemoved() }
} }
override fun onDetach() {
requireContext().unregisterReceiver(downloadReceiver)
super.onDetach()
}
override fun onEmptyActionClick() { override fun onEmptyActionClick() {
ImportDialogFragment.show(childFragmentManager) ImportDialogFragment.show(childFragmentManager)
} }

View File

@@ -6,6 +6,7 @@ import java.io.IOException
import javax.inject.Inject import javax.inject.Inject
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@@ -59,6 +60,7 @@ class LocalListViewModel @Inject constructor(
init { init {
onRefresh() onRefresh()
cleanup() cleanup()
watchDirectories()
} }
override fun onRefresh() { override fun onRefresh() {
@@ -108,4 +110,13 @@ class LocalListViewModel @Inject constructor(
} }
} }
} }
private fun watchDirectories() {
viewModelScope.launch(Dispatchers.Default) {
repository.watchReadableDirs()
.collectLatest {
doRefresh()
}
}
}
} }