Fixes
This commit is contained in:
@@ -75,7 +75,10 @@ class ChaptersFragment : BaseFragment<FragmentChaptersBinding>(),
|
|||||||
|
|
||||||
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
|
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
|
||||||
super.onCreateOptionsMenu(menu, inflater)
|
super.onCreateOptionsMenu(menu, inflater)
|
||||||
inflater.inflate(R.menu.opt_chapters, menu)
|
// workaround: duplication after screen rotation
|
||||||
|
if (menu.findItem(R.id.action_reversed) == null) {
|
||||||
|
inflater.inflate(R.menu.opt_chapters, menu)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onPrepareOptionsMenu(menu: Menu) {
|
override fun onPrepareOptionsMenu(menu: Menu) {
|
||||||
|
|||||||
@@ -30,8 +30,7 @@ import org.koitharu.kotatsu.R
|
|||||||
import org.koitharu.kotatsu.base.domain.MangaIntent
|
import org.koitharu.kotatsu.base.domain.MangaIntent
|
||||||
import org.koitharu.kotatsu.base.ui.BaseActivity
|
import org.koitharu.kotatsu.base.ui.BaseActivity
|
||||||
import org.koitharu.kotatsu.browser.BrowserActivity
|
import org.koitharu.kotatsu.browser.BrowserActivity
|
||||||
import org.koitharu.kotatsu.browser.cloudflare.CloudFlareDialog
|
import org.koitharu.kotatsu.core.exceptions.resolve.ExceptionResolver
|
||||||
import org.koitharu.kotatsu.core.exceptions.CloudFlareProtectedException
|
|
||||||
import org.koitharu.kotatsu.core.model.parcelable.ParcelableManga
|
import org.koitharu.kotatsu.core.model.parcelable.ParcelableManga
|
||||||
import org.koitharu.kotatsu.core.os.ShortcutsRepository
|
import org.koitharu.kotatsu.core.os.ShortcutsRepository
|
||||||
import org.koitharu.kotatsu.databinding.ActivityDetailsBinding
|
import org.koitharu.kotatsu.databinding.ActivityDetailsBinding
|
||||||
@@ -87,9 +86,8 @@ class DetailsActivity : BaseActivity<ActivityDetailsBinding>(), TabLayoutMediato
|
|||||||
|
|
||||||
private fun onError(e: Throwable) {
|
private fun onError(e: Throwable) {
|
||||||
when {
|
when {
|
||||||
e is CloudFlareProtectedException -> {
|
ExceptionResolver.canResolve(e) -> {
|
||||||
CloudFlareDialog.newInstance(e.url)
|
resolveError(e)
|
||||||
.show(supportFragmentManager, CloudFlareDialog.TAG)
|
|
||||||
}
|
}
|
||||||
viewModel.manga.value == null -> {
|
viewModel.manga.value == null -> {
|
||||||
Toast.makeText(this, e.getDisplayMessage(resources), Toast.LENGTH_LONG).show()
|
Toast.makeText(this, e.getDisplayMessage(resources), Toast.LENGTH_LONG).show()
|
||||||
@@ -284,6 +282,17 @@ class DetailsActivity : BaseActivity<ActivityDetailsBinding>(), TabLayoutMediato
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun resolveError(e: Throwable) {
|
||||||
|
lifecycleScope.launch {
|
||||||
|
if (exceptionResolver.resolve(e)) {
|
||||||
|
viewModel.reload()
|
||||||
|
} else if (viewModel.manga.value == null) {
|
||||||
|
Toast.makeText(this@DetailsActivity, e.getDisplayMessage(resources), Toast.LENGTH_LONG).show()
|
||||||
|
finishAfterTransition()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
fun newIntent(context: Context, manga: Manga): Intent {
|
fun newIntent(context: Context, manga: Manga): Intent {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import androidx.lifecycle.asFlow
|
|||||||
import androidx.lifecycle.asLiveData
|
import androidx.lifecycle.asLiveData
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.flow.*
|
import kotlinx.coroutines.flow.*
|
||||||
import kotlinx.coroutines.plus
|
import kotlinx.coroutines.plus
|
||||||
import org.koitharu.kotatsu.base.domain.MangaDataRepository
|
import org.koitharu.kotatsu.base.domain.MangaDataRepository
|
||||||
@@ -29,7 +30,7 @@ import org.koitharu.kotatsu.utils.ext.mapToSet
|
|||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
|
||||||
class DetailsViewModel(
|
class DetailsViewModel(
|
||||||
intent: MangaIntent,
|
private val intent: MangaIntent,
|
||||||
private val historyRepository: HistoryRepository,
|
private val historyRepository: HistoryRepository,
|
||||||
private val favouritesRepository: FavouritesRepository,
|
private val favouritesRepository: FavouritesRepository,
|
||||||
private val localMangaRepository: LocalMangaRepository,
|
private val localMangaRepository: LocalMangaRepository,
|
||||||
@@ -38,6 +39,7 @@ class DetailsViewModel(
|
|||||||
private val settings: AppSettings,
|
private val settings: AppSettings,
|
||||||
) : BaseViewModel() {
|
) : BaseViewModel() {
|
||||||
|
|
||||||
|
private var loadingJob: Job
|
||||||
private val mangaData = MutableStateFlow<Manga?>(intent.manga)
|
private val mangaData = MutableStateFlow<Manga?>(intent.manga)
|
||||||
private val selectedBranch = MutableStateFlow<String?>(null)
|
private val selectedBranch = MutableStateFlow<String?>(null)
|
||||||
|
|
||||||
@@ -109,28 +111,12 @@ class DetailsViewModel(
|
|||||||
}.asLiveData(viewModelScope.coroutineContext + Dispatchers.Default)
|
}.asLiveData(viewModelScope.coroutineContext + Dispatchers.Default)
|
||||||
|
|
||||||
init {
|
init {
|
||||||
launchLoadingJob(Dispatchers.Default) {
|
loadingJob = doLoad()
|
||||||
var manga = mangaDataRepository.resolveIntent(intent)
|
}
|
||||||
?: throw MangaNotFoundException("Cannot find manga")
|
|
||||||
mangaData.value = manga
|
fun reload() {
|
||||||
manga = MangaRepository(manga.source).getDetails(manga)
|
loadingJob.cancel()
|
||||||
// find default branch
|
loadingJob = doLoad()
|
||||||
val hist = historyRepository.getOne(manga)
|
|
||||||
selectedBranch.value = if (hist != null) {
|
|
||||||
manga.chapters?.find { it.id == hist.chapterId }?.branch
|
|
||||||
} else {
|
|
||||||
predictBranch(manga.chapters)
|
|
||||||
}
|
|
||||||
mangaData.value = manga
|
|
||||||
remoteManga.value = runCatching {
|
|
||||||
if (manga.source == MangaSource.LOCAL) {
|
|
||||||
val m = localMangaRepository.getRemoteManga(manga) ?: return@runCatching null
|
|
||||||
MangaRepository(m.source).getDetails(m)
|
|
||||||
} else {
|
|
||||||
localMangaRepository.findSavedManga(manga)
|
|
||||||
}
|
|
||||||
}.getOrNull()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun deleteLocal(manga: Manga) {
|
fun deleteLocal(manga: Manga) {
|
||||||
@@ -156,6 +142,29 @@ class DetailsViewModel(
|
|||||||
return remoteManga.value
|
return remoteManga.value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun doLoad() = launchLoadingJob(Dispatchers.Default) {
|
||||||
|
var manga = mangaDataRepository.resolveIntent(intent)
|
||||||
|
?: throw MangaNotFoundException("Cannot find manga")
|
||||||
|
mangaData.value = manga
|
||||||
|
manga = MangaRepository(manga.source).getDetails(manga)
|
||||||
|
// find default branch
|
||||||
|
val hist = historyRepository.getOne(manga)
|
||||||
|
selectedBranch.value = if (hist != null) {
|
||||||
|
manga.chapters?.find { it.id == hist.chapterId }?.branch
|
||||||
|
} else {
|
||||||
|
predictBranch(manga.chapters)
|
||||||
|
}
|
||||||
|
mangaData.value = manga
|
||||||
|
remoteManga.value = runCatching {
|
||||||
|
if (manga.source == MangaSource.LOCAL) {
|
||||||
|
val m = localMangaRepository.getRemoteManga(manga) ?: return@runCatching null
|
||||||
|
MangaRepository(m.source).getDetails(m)
|
||||||
|
} else {
|
||||||
|
localMangaRepository.findSavedManga(manga)
|
||||||
|
}
|
||||||
|
}.getOrNull()
|
||||||
|
}
|
||||||
|
|
||||||
private fun mapChapters(
|
private fun mapChapters(
|
||||||
chapters: List<MangaChapter>,
|
chapters: List<MangaChapter>,
|
||||||
downloadedChapters: List<MangaChapter>?,
|
downloadedChapters: List<MangaChapter>?,
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
xmlns:tools="http://schemas.android.com/tools"
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="?android:listPreferredItemHeightSmall"
|
android:layout_height="?android:listPreferredItemHeightSmall"
|
||||||
|
android:background="?android:windowBackground"
|
||||||
android:gravity="center_vertical"
|
android:gravity="center_vertical"
|
||||||
android:orientation="horizontal">
|
android:orientation="horizontal">
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user