[WIP] Downloads

This commit is contained in:
tom5079
2021-06-08 18:24:52 +09:00
parent ff0df0d9cc
commit 760194bde8
10 changed files with 127 additions and 18 deletions

View File

@@ -151,4 +151,5 @@ val sourceModule = DI.Module(name = "source") {
}
bind { factory { source: String -> History(di, source) } }
bind { singleton { Downloads(di) } }
}

View File

@@ -0,0 +1,103 @@
/*
* Pupil, Hitomi.la viewer for Android
* Copyright (C) 2021 tom5079
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package xyz.quaver.pupil.sources
import android.app.Application
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import org.kodein.di.DI
import org.kodein.di.DIAware
import org.kodein.di.instance
import xyz.quaver.floatingsearchview.suggestions.model.SearchSuggestion
import xyz.quaver.io.FileX
import xyz.quaver.io.util.getChild
import xyz.quaver.pupil.R
import xyz.quaver.pupil.util.DownloadManager
import kotlin.math.max
import kotlin.math.min
class Downloads(override val di: DI) : Source<DefaultSortMode, SearchSuggestion>(), DIAware {
override val name: String
get() = "Downloads"
override val iconResID: Int
get() = R.drawable.ic_download
override val preferenceID: Int
get() = -1
override val availableSortMode: Array<DefaultSortMode> = DefaultSortMode.values()
private val downloadManager: DownloadManager by instance()
private val applicationContext: Application by instance()
override suspend fun search(query: String, range: IntRange, sortMode: Enum<*>): Pair<Channel<ItemInfo>, Int> {
val downloads = downloadManager.downloads.toList()
val channel = Channel<ItemInfo>()
val sanitizedRange = max(0, range.first) .. min(range.last, downloads.size - 1)
CoroutineScope(Dispatchers.IO).launch {
downloads.slice(sanitizedRange).map { (_, folderName) ->
transform(downloadManager.downloadFolder.getChild(folderName))
}.forEach {
channel.send(it)
}
channel.close()
}
return Pair(channel, downloads.size)
}
override suspend fun suggestion(query: String): List<SearchSuggestion> {
return emptyList()
}
override suspend fun images(itemID: String): List<String> {
TODO("Not yet implemented")
}
override suspend fun info(itemID: String): ItemInfo {
TODO("Not yet implemented")
}
companion object {
private fun firstImage(folder: FileX): String? =
folder.list { _, name ->
name.takeLastWhile { it != '.' } !in listOf("jpg", "png", "gif", "webp")
}?.firstOrNull()
fun transform(folder: FileX): ItemInfo =
kotlin.runCatching {
Json.decodeFromString<ItemInfo>(folder.getChild(".metadata").readText())
}.getOrNull() ?:
ItemInfo(
"Downloads",
"",
folder.name,
firstImage(folder) ?: "",
""
)
}
}

View File

@@ -239,14 +239,12 @@ class MainActivity :
binding.navView.setNavigationItemSelectedListener(this)
with (binding.contents.cancelFab) {
setImageResource(R.drawable.cancel)
setOnClickListener {
}
}
with (binding.contents.jumpFab) {
setImageResource(R.drawable.ic_jump)
setOnClickListener {
val perPage = Preferences["per_page", "25"].toInt()
val editText = EditText(context)
@@ -269,12 +267,10 @@ class MainActivity :
}
with (binding.contents.randomFab) {
setImageResource(R.drawable.shuffle_variant)
setOnClickListener {
setImageDrawable(CircularProgressDrawable(context))
model.random { runOnUiThread {
setImageResource(R.drawable.shuffle_variant)
GalleryDialogFragment(model.source.value!!.name, it.id).apply {
onChipClickedHandler.add {
model.setQueryAndSearch(it.toQuery())
@@ -286,7 +282,6 @@ class MainActivity :
}
with (binding.contents.idFab) {
setImageResource(R.drawable.numeric)
setOnClickListener {
val editText = EditText(context).apply {
inputType = InputType.TYPE_CLASS_NUMBER
@@ -469,6 +464,7 @@ class MainActivity :
when(item.itemId) {
R.id.main_drawer_home -> model.setModeAndReset(MainViewModel.MainMode.SEARCH)
R.id.main_drawer_history -> model.setModeAndReset(MainViewModel.MainMode.HISTORY)
R.id.main_drawer_downloads -> model.setModeAndReset(MainViewModel.MainMode.DOWNLOADS)
R.id.main_drawer_help -> startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.help))))
R.id.main_drawer_github -> startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.github))))
R.id.main_drawer_homepage -> startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.home_page))))

View File

@@ -79,7 +79,7 @@ class DownloadLocationDialogFragment : DialogFragment(), DIAware {
}
}
} else {
val downloadFolder = DownloadManager.getInstance(context ?: return@registerForActivityResult).downloadFolder.canonicalPath
val downloadFolder = downloadManager.downloadFolder.canonicalPath
val key = entries.keys.firstOrNull { it?.canonicalPath == downloadFolder }
if (key == null)
entries[key]!!.locationAvailable.text = downloadFolder

View File

@@ -28,6 +28,7 @@ import org.kodein.di.direct
import org.kodein.di.instance
import xyz.quaver.floatingsearchview.suggestions.model.SearchSuggestion
import xyz.quaver.pupil.sources.AnySource
import xyz.quaver.pupil.sources.Downloads
import xyz.quaver.pupil.sources.History
import xyz.quaver.pupil.sources.ItemInfo
import xyz.quaver.pupil.util.Preferences
@@ -106,6 +107,7 @@ class MainViewModel(app: Application) : AndroidViewModel(app), DIAware {
sourceFactory = when (mode) {
MainMode.SEARCH -> defaultSourceFactory
MainMode.HISTORY -> { { direct.instance<String, History>(arg = it) } }
MainMode.DOWNLOADS -> { { direct.instance<Downloads>() } }
else -> return
}

View File

@@ -27,6 +27,7 @@ import kotlinx.coroutines.launch
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import okhttp3.internal.toImmutableMap
import org.kodein.di.DIAware
import org.kodein.di.android.closestDI
import xyz.quaver.io.FileX
@@ -72,6 +73,9 @@ class DownloadManager constructor(context: Context) : ContextWrapper(context), D
return downloadFolderMapInstance ?: mutableMapOf()
}
val downloads: Map<String, String>
get() = downloadFolderMap.toImmutableMap()
@Synchronized
fun getDownloadFolder(source: String, itemID: String): FileX? =
downloadFolderMap["$source-$itemID"]?.let { downloadFolder.getChild(it) }