manga "working" :D

This commit is contained in:
Finnley Somdahl 2023-10-20 01:44:36 -05:00
parent 57a584a820
commit 41b90e3a39
32 changed files with 1179 additions and 409 deletions

View file

@ -28,10 +28,18 @@ import ani.dantotsu.snackString
import ani.dantotsu.tryWithSuspend
import ani.dantotsu.currContext
import ani.dantotsu.R
import ani.dantotsu.parsers.AnimeSources
import ani.dantotsu.parsers.AniyomiAdapter
import ani.dantotsu.parsers.DynamicMangaParser
import ani.dantotsu.parsers.HAnimeSources
import ani.dantotsu.parsers.HMangaSources
import ani.dantotsu.parsers.MangaSources
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
import eu.kanade.tachiyomi.source.online.HttpSource
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
class MediaDetailsViewModel : ViewModel() {
val scrolledToTop = MutableLiveData(true)
@ -41,15 +49,34 @@ class MediaDetailsViewModel : ViewModel() {
}
fun loadSelected(media: Media): Selected {
return loadData<Selected>("${media.id}-select") ?: Selected().let {
it.source = if (media.isAdult) 0 else when (media.anime != null) {
true -> loadData("settings_def_anime_source") ?: 0
else -> loadData("settings_def_manga_source") ?: 0
val data = loadData<Selected>("${media.id}-select") ?: Selected().let {
it.source = if (media.isAdult) "" else when (media.anime != null) {
true -> loadData("settings_def_anime_source") ?: ""
else -> loadData("settings_def_manga_source") ?: ""
}
it.preferDub = loadData("settings_prefer_dub") ?: false
it.sourceIndex = loadSelectedStringLocation(it.source)
saveSelected(media.id, it)
it
}
if (media.anime != null) {
val sources = if (media.isAdult) HAnimeSources else AnimeSources
data.sourceIndex = sources.list.indexOfFirst { it.name == data.source }
} else {
val sources = if (media.isAdult) HMangaSources else MangaSources
data.sourceIndex = sources.list.indexOfFirst { it.name == data.source }
}
if (data.sourceIndex == -1) {
data.sourceIndex = 0
}
return data
}
fun loadSelectedStringLocation(sourceName: String): Int {
//find the location of the source in the list
var location = watchSources?.list?.indexOfFirst { it.name == sourceName } ?: 0
if (location == -1) {location = 0}
return location
}
var continueMedia: Boolean? = null
@ -167,7 +194,8 @@ class MediaDetailsViewModel : ViewModel() {
val server = selected.server ?: return false
val link = ep.link ?: return false
ep.extractors = mutableListOf(watchSources?.get(selected.source)?.let {
ep.extractors = mutableListOf(watchSources?.get(loadSelectedStringLocation(selected.source))?.let {
selected.sourceIndex = loadSelectedStringLocation(selected.source)
if (!post && !it.allowsPreloading) null
else ep.sEpisode?.let { it1 ->
it.loadSingleVideoServer(server, link, ep.extra,
@ -238,7 +266,7 @@ class MediaDetailsViewModel : ViewModel() {
suspend fun loadMangaChapterImages(chapter: MangaChapter, selected: Selected, post: Boolean = true): Boolean {
return tryWithSuspend(true) {
chapter.addImages(
mangaReadSources?.get(selected.source)?.loadImages(chapter.link, chapter.sChapter) ?: return@tryWithSuspend false
mangaReadSources?.get(loadSelectedStringLocation(selected.source))?.loadImages(chapter.link, chapter.sChapter) ?: return@tryWithSuspend false
)
if (post) mangaChapter.postValue(chapter)
true
@ -261,7 +289,7 @@ class MediaDetailsViewModel : ViewModel() {
}
suspend fun autoSearchNovels(media: Media) {
val source = novelSources[media.selected?.source ?: 0]
val source = novelSources[loadSelectedStringLocation(media.selected?.source?:"")]
tryWithSuspend(post = true) {
if (source != null) {
novelResponses.postValue(source.sortedSearch(media))

View file

@ -7,7 +7,8 @@ data class Selected(
var recyclerStyle: Int? = null,
var recyclerReversed: Boolean = false,
var chip: Int = 0,
var source: Int = 0,
var source: String = "",
var sourceIndex: Int = 0,
var preferDub: Boolean = false,
var server: String? = null,
var video: Int = 0,

View file

@ -57,7 +57,7 @@ class SourceSearchDialogFragment : BottomSheetDialogFragment() {
binding.searchRecyclerView.visibility = View.GONE
binding.searchProgress.visibility = View.VISIBLE
i = media!!.selected!!.source
i = media!!.selected!!.sourceIndex
val source = if (media!!.anime != null) {
(if (!media!!.isAdult) AnimeSources else HAnimeSources)[i!!]

View file

@ -68,7 +68,7 @@ class AnimeWatchAdapter(
}
//Source Selection
val source = media.selected!!.source.let { if (it >= watchSources.names.size) 0 else it }
val source = media.selected!!.sourceIndex.let { if (it >= watchSources.names.size) 0 else it }
if (watchSources.names.isNotEmpty() && source in 0 until watchSources.names.size) {
binding.animeSource.setText(watchSources.names[source])
watchSources[source].apply {

View file

@ -130,7 +130,7 @@ class AnimeWatchFragment : Fragment() {
async { model.loadKitsuEpisodes(media) },
async { model.loadFillerEpisodes(media) }
)
model.loadEpisodes(media, media.selected!!.source)
model.loadEpisodes(media, media.selected!!.sourceIndex)
}
loaded = true
} else {
@ -140,7 +140,7 @@ class AnimeWatchFragment : Fragment() {
}
model.getEpisodes().observe(viewLifecycleOwner) { loadedEpisodes ->
if (loadedEpisodes != null) {
val episodes = loadedEpisodes[media.selected!!.source]
val episodes = loadedEpisodes[media.selected!!.sourceIndex]
if (episodes != null) {
episodes.forEach { (i, episode) ->
if (media.anime?.fillerEpisodes != null) {
@ -206,8 +206,8 @@ class AnimeWatchFragment : Fragment() {
media.anime?.episodes = null
reload()
val selected = model.loadSelected(media)
model.watchSources?.get(selected.source)?.showUserTextListener = null
selected.source = i
model.watchSources?.get(selected.sourceIndex)?.showUserTextListener = null
selected.sourceIndex = i
selected.server = null
model.saveSelected(media.id, selected, requireActivity())
media.selected = selected
@ -216,11 +216,11 @@ class AnimeWatchFragment : Fragment() {
fun onDubClicked(checked: Boolean) {
val selected = model.loadSelected(media)
model.watchSources?.get(selected.source)?.selectDub = checked
model.watchSources?.get(selected.sourceIndex)?.selectDub = checked
selected.preferDub = checked
model.saveSelected(media.id, selected, requireActivity())
media.selected = selected
lifecycleScope.launch(Dispatchers.IO) { model.forceLoadEpisode(media, selected.source) }
lifecycleScope.launch(Dispatchers.IO) { model.forceLoadEpisode(media, selected.sourceIndex) }
}
fun loadEpisodes(i: Int) {

View file

@ -817,7 +817,7 @@ class ExoplayerView : AppCompatActivity(), Player.Listener {
}
model.watchSources = if (media.isAdult) HAnimeSources else AnimeSources
serverInfo.text = model.watchSources!!.names.getOrNull(media.selected!!.source) ?: model.watchSources!!.names[0]
serverInfo.text = model.watchSources!!.names.getOrNull(media.selected!!.sourceIndex) ?: model.watchSources!!.names[0]
model.epChanged.observe(this) {
epChanging = !it
@ -1353,7 +1353,7 @@ class ExoplayerView : AppCompatActivity(), Player.Listener {
if (media.selected!!.server != null)
model.loadEpisodeSingleVideo(ep, selected, false)
else
model.loadEpisodeVideos(ep, selected.source, false)
model.loadEpisodeVideos(ep, selected.sourceIndex, false)
}
}
}

View file

@ -135,7 +135,7 @@ class SelectorDialogFragment : BottomSheetDialogFragment() {
}
}
scope.launch(Dispatchers.IO) {
model.loadEpisodeVideos(ep, media!!.selected!!.source)
model.loadEpisodeVideos(ep, media!!.selected!!.sourceIndex)
withContext(Dispatchers.Main){
binding.selectorProgressBar.visibility = View.GONE
}

View file

@ -0,0 +1,64 @@
package ani.dantotsu.media.manga
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.LruCache
import eu.kanade.tachiyomi.source.model.Page
import eu.kanade.tachiyomi.source.online.HttpSource
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
data class ImageData(
val page: Page,
val source: HttpSource,
){
suspend fun fetchAndProcessImage(page: Page, httpSource: HttpSource): Bitmap? {
return withContext(Dispatchers.IO) {
try {
// Fetch the image
val response = httpSource.getImage(page)
// Convert the Response to an InputStream
val inputStream = response.body?.byteStream()
// Convert InputStream to Bitmap
val bitmap = BitmapFactory.decodeStream(inputStream)
inputStream?.close()
return@withContext bitmap
} catch (e: Exception) {
// Handle any exceptions
println("An error occurred: ${e.message}")
return@withContext null
}
}
}
}
class MangaCache() {
private val maxMemory = (Runtime.getRuntime().maxMemory() / 1024 / 2).toInt()
private val cache = LruCache<String, ImageData>(maxMemory)
@Synchronized
fun put(key: String, imageDate: ImageData) {
cache.put(key, imageDate)
}
@Synchronized
fun get(key: String): ImageData? = cache.get(key)
@Synchronized
fun remove(key: String) {
cache.remove(key)
}
@Synchronized
fun clear() {
cache.evictAll()
}
fun size(): Int = cache.size()
}

View file

@ -49,7 +49,7 @@ class MangaReadAdapter(
}
//Source Selection
val source = media.selected!!.source.let { if (it >= mangaReadSources.names.size) 0 else it }
val source = media.selected!!.sourceIndex.let { if (it >= mangaReadSources.names.size) 0 else it }
if (mangaReadSources.names.isNotEmpty() && source in 0 until mangaReadSources.names.size) {
binding.animeSource.setText(mangaReadSources.names[source])

View file

@ -121,7 +121,7 @@ open class MangaReadFragment : Fragment() {
binding.animeSourceRecycler.adapter = ConcatAdapter(headerAdapter, chapterAdapter)
lifecycleScope.launch(Dispatchers.IO) {
model.loadMangaChapters(media, media.selected!!.source)
model.loadMangaChapters(media, media.selected!!.sourceIndex)
}
loaded = true
} else {
@ -136,7 +136,7 @@ open class MangaReadFragment : Fragment() {
model.getMangaChapters().observe(viewLifecycleOwner) { loadedChapters ->
if (loadedChapters != null) {
val chapters = loadedChapters[media.selected!!.source]
val chapters = loadedChapters[media.selected!!.sourceIndex]
if (chapters != null) {
media.manga?.chapters = chapters
@ -177,8 +177,8 @@ open class MangaReadFragment : Fragment() {
media.manga?.chapters = null
reload()
val selected = model.loadSelected(media)
model.mangaReadSources?.get(selected.source)?.showUserTextListener = null
selected.source = i
model.mangaReadSources?.get(selected.sourceIndex)?.showUserTextListener = null
selected.sourceIndex = i
selected.server = null
model.saveSelected(media.id, selected, requireActivity())
media.selected = selected

View file

@ -14,15 +14,21 @@ import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView
import ani.dantotsu.*
import ani.dantotsu.media.manga.MangaChapter
import ani.dantotsu.parsers.DynamicMangaParser
import ani.dantotsu.settings.CurrentReaderSettings
import com.alexvasilkov.gestures.views.GestureFrameLayout
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
import eu.kanade.tachiyomi.source.model.Page
import eu.kanade.tachiyomi.source.online.HttpSource
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import ani.dantotsu.media.manga.MangaCache
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
abstract class BaseImageAdapter(
val activity: MangaReaderActivity,
@ -44,10 +50,33 @@ abstract class BaseImageAdapter(
if (settings.layout != CurrentReaderSettings.Layouts.PAGED) {
if (settings.padding) {
when (settings.direction) {
CurrentReaderSettings.Directions.TOP_TO_BOTTOM -> view.setPadding(0, 0, 0, 16f.px)
CurrentReaderSettings.Directions.LEFT_TO_RIGHT -> view.setPadding(0, 0, 16f.px, 0)
CurrentReaderSettings.Directions.BOTTOM_TO_TOP -> view.setPadding(0, 16f.px, 0, 0)
CurrentReaderSettings.Directions.RIGHT_TO_LEFT -> view.setPadding(16f.px, 0, 0, 0)
CurrentReaderSettings.Directions.TOP_TO_BOTTOM -> view.setPadding(
0,
0,
0,
16f.px
)
CurrentReaderSettings.Directions.LEFT_TO_RIGHT -> view.setPadding(
0,
0,
16f.px,
0
)
CurrentReaderSettings.Directions.BOTTOM_TO_TOP -> view.setPadding(
0,
16f.px,
0,
0
)
CurrentReaderSettings.Directions.RIGHT_TO_LEFT -> view.setPadding(
16f.px,
0,
0,
0
)
}
}
view.updateLayoutParams {
@ -87,7 +116,7 @@ abstract class BaseImageAdapter(
abstract suspend fun loadImage(position: Int, parent: View): Boolean
companion object {
suspend fun Context.loadBitmap(link: FileUrl, transforms: List<BitmapTransformation>): Bitmap? {
/*suspend fun Context.loadBitmap(link: FileUrl, transforms: List<BitmapTransformation>): Bitmap? {
return tryWithSuspend {
withContext(Dispatchers.IO) {
Glide.with(this@loadBitmap)
@ -113,6 +142,43 @@ abstract class BaseImageAdapter(
.get()
}
}
}*/
suspend fun Context.loadBitmap(link: FileUrl, transforms: List<BitmapTransformation>): Bitmap? {
return tryWithSuspend {
val mangaCache = uy.kohesive.injekt.Injekt.get<MangaCache>()
withContext(Dispatchers.IO) {
Glide.with(this@loadBitmap)
.asBitmap()
.let {
if (link.url.startsWith("file://")) {
it.load(link.url)
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)
} else {
println("bitmap from cache")
println(link.url)
println(mangaCache.get(link.url))
println("cache size: ${mangaCache.size()}")
mangaCache.get(link.url)?.let { imageData ->
val bitmap = imageData.fetchAndProcessImage(imageData.page, imageData.source)
it.load(bitmap)
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)
}
}
}
?.let {
if (transforms.isNotEmpty()) {
it.transform(*transforms.toTypedArray())
} else {
it
}
}
?.submit()
?.get()
}
}
}
fun mergeBitmap(bitmap1: Bitmap, bitmap2: Bitmap, scale: Boolean = false): Bitmap {
@ -133,4 +199,8 @@ abstract class BaseImageAdapter(
}
}
}
interface ImageFetcher {
suspend fun fetchImage(page: Page): Bitmap?
}

View file

@ -30,6 +30,7 @@ import ani.dantotsu.connections.updateProgress
import ani.dantotsu.databinding.ActivityMangaReaderBinding
import ani.dantotsu.media.Media
import ani.dantotsu.media.MediaDetailsViewModel
import ani.dantotsu.media.manga.MangaCache
import ani.dantotsu.media.manga.MangaChapter
import ani.dantotsu.others.ImageViewDialog
import ani.dantotsu.others.getSerialized
@ -47,12 +48,16 @@ import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.util.*
import kotlin.math.min
import kotlin.properties.Delegates
@SuppressLint("SetTextI18n")
class MangaReaderActivity : AppCompatActivity() {
private val mangaCache = Injekt.get<MangaCache>()
private lateinit var binding: ActivityMangaReaderBinding
private val model: MediaDetailsViewModel by viewModels()
private val scope = lifecycleScope
@ -106,6 +111,7 @@ class MangaReaderActivity : AppCompatActivity() {
}
override fun onDestroy() {
mangaCache.clear()
rpc?.close()
super.onDestroy()
}
@ -174,7 +180,7 @@ class MangaReaderActivity : AppCompatActivity() {
model.mangaReadSources = if (media.isAdult) HMangaSources else MangaSources
binding.mangaReaderSource.visibility = if (settings.showSource) View.VISIBLE else View.GONE
binding.mangaReaderSource.text = model.mangaReadSources!!.names[media.selected!!.source]
binding.mangaReaderSource.text = model.mangaReadSources!!.names[media.selected!!.sourceIndex]
binding.mangaReaderTitle.text = media.userPreferredName
@ -205,6 +211,7 @@ class MangaReaderActivity : AppCompatActivity() {
//Chapter Change
fun change(index: Int) {
mangaCache.clear()
saveData("${media.id}_${chaptersArr[currentChapterIndex]}", currentChapterPage, this)
ChapterLoaderDialog.newInstance(chapters[chaptersArr[index]]!!).show(supportFragmentManager, "dialog")
}
@ -258,7 +265,7 @@ class MangaReaderActivity : AppCompatActivity() {
type = RPC.Type.WATCHING
activityName = media.userPreferredName
details = chap.title?.takeIf { it.isNotEmpty() } ?: getString(R.string.chapter_num, chap.number)
state = "Chapter : ${chap.number}/${media.manga?.totalChapters ?: "??"}"
state = "${chap.number}/${media.manga?.totalChapters ?: "??"}"
media.cover?.let { cover ->
largeImage = RPC.Link(media.userPreferredName, cover)
}
@ -691,7 +698,7 @@ class MangaReaderActivity : AppCompatActivity() {
}
fun getTransformation(mangaImage: MangaImage): BitmapTransformation? {
return model.loadTransformation(mangaImage, media.selected!!.source)
return model.loadTransformation(mangaImage, media.selected!!.sourceIndex)
}
fun onImageLongClicked(

View file

@ -35,7 +35,7 @@ class NovelReadAdapter(
fun search(): Boolean {
val query = binding.searchBarText.text.toString()
val source = media.selected!!.source.let { if (it >= novelReadSources.names.size) 0 else it }
val source = media.selected!!.sourceIndex.let { if (it >= novelReadSources.names.size) 0 else it }
fragment.source = source
binding.searchBarText.clearFocus()
@ -44,7 +44,7 @@ class NovelReadAdapter(
return true
}
val source = media.selected!!.source.let { if (it >= novelReadSources.names.size) 0 else it }
val source = media.selected!!.sourceIndex.let { if (it >= novelReadSources.names.size) 0 else it }
if (novelReadSources.names.isNotEmpty() && source in 0 until novelReadSources.names.size) {
binding.animeSource.setText(novelReadSources.names[source], false)
}

View file

@ -67,7 +67,7 @@ class NovelReadFragment : Fragment() {
binding.animeSourceRecycler.adapter = ConcatAdapter(headerAdapter, novelResponseAdapter)
loaded = true
Handler(Looper.getMainLooper()).postDelayed({
search(searchQuery, sel?.source ?: 0, auto = sel?.server == null)
search(searchQuery, sel?.sourceIndex ?: 0, auto = sel?.server == null)
}, 100)
}
}
@ -103,7 +103,7 @@ class NovelReadFragment : Fragment() {
fun onSourceChange(i: Int) {
val selected = model.loadSelected(media)
selected.source = i
selected.sourceIndex = i
source = i
selected.server = null
model.saveSelected(media.id, selected, requireActivity())