Fix default branch selection
This commit is contained in:
@@ -69,7 +69,7 @@ class ShortcutsRepository(
|
||||
.setLongLabel(manga.title)
|
||||
.setIcon(icon)
|
||||
.setIntent(
|
||||
ReaderActivity.newIntent(context, manga.id, null)
|
||||
ReaderActivity.newIntent(context, manga.id)
|
||||
.setAction(ReaderActivity.ACTION_MANGA_READ)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -128,9 +128,9 @@ class ChaptersFragment :
|
||||
)
|
||||
startActivity(
|
||||
ReaderActivity.newIntent(
|
||||
view.context,
|
||||
viewModel.manga.value ?: return,
|
||||
ReaderState(item.chapter.id, 0, 0)
|
||||
context = view.context,
|
||||
manga = viewModel.manga.value ?: return,
|
||||
state = ReaderState(item.chapter.id, 0, 0),
|
||||
),
|
||||
options.toBundle()
|
||||
)
|
||||
|
||||
@@ -256,9 +256,9 @@ class DetailsActivity : BaseActivity<ActivityDetailsBinding>(), TabLayoutMediato
|
||||
setPositiveButton(R.string.read) { _, _ ->
|
||||
startActivity(
|
||||
ReaderActivity.newIntent(
|
||||
this@DetailsActivity,
|
||||
remoteManga,
|
||||
ReaderState(chapterId, 0, 0)
|
||||
context = this@DetailsActivity,
|
||||
manga = remoteManga,
|
||||
state = ReaderState(chapterId, 0, 0)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -188,9 +188,9 @@ class DetailsFragment :
|
||||
} else {
|
||||
startActivity(
|
||||
ReaderActivity.newIntent(
|
||||
context ?: return,
|
||||
manga,
|
||||
null
|
||||
context = context ?: return,
|
||||
manga = manga,
|
||||
branch = viewModel.selectedBranchValue,
|
||||
)
|
||||
)
|
||||
}
|
||||
@@ -227,11 +227,14 @@ class DetailsFragment :
|
||||
v.showPopupMenu(R.menu.popup_read) {
|
||||
when (it.itemId) {
|
||||
R.id.action_read -> {
|
||||
val branch = viewModel.selectedBranchValue
|
||||
startActivity(
|
||||
ReaderActivity.newIntent(
|
||||
context ?: return@showPopupMenu false,
|
||||
viewModel.manga.value ?: return@showPopupMenu false,
|
||||
viewModel.chapters.value?.firstOrNull()?.let { c ->
|
||||
context = context ?: return@showPopupMenu false,
|
||||
manga = viewModel.manga.value ?: return@showPopupMenu false,
|
||||
state = viewModel.chapters.value?.firstOrNull { c ->
|
||||
c.chapter.branch == branch
|
||||
}?.let { c ->
|
||||
ReaderState(c.chapter.id, 0, 0)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -119,6 +119,9 @@ class DetailsViewModel(
|
||||
(if (reversed) list.asReversed() else list).filterSearch(query)
|
||||
}.asLiveData(viewModelScope.coroutineContext + Dispatchers.Default)
|
||||
|
||||
val selectedBranchValue: String?
|
||||
get() = selectedBranch.value
|
||||
|
||||
init {
|
||||
loadingJob = doLoad()
|
||||
}
|
||||
|
||||
@@ -298,7 +298,7 @@ class MainActivity :
|
||||
binding.fab, 0, 0, binding.fab.measuredWidth, binding.fab.measuredHeight
|
||||
)
|
||||
}
|
||||
startActivity(ReaderActivity.newIntent(this, manga, null), options?.toBundle())
|
||||
startActivity(ReaderActivity.newIntent(this, manga), options?.toBundle())
|
||||
}
|
||||
|
||||
private fun onError(e: Throwable) {
|
||||
|
||||
@@ -13,6 +13,15 @@ val readerModule
|
||||
single { PagesCache(get()) }
|
||||
|
||||
viewModel { params ->
|
||||
ReaderViewModel(params[0], params[1], get(), get(), get(), get(), get())
|
||||
ReaderViewModel(
|
||||
intent = params[0],
|
||||
initialState = params[1],
|
||||
preselectedBranch = params[2],
|
||||
dataRepository = get(),
|
||||
historyRepository = get(),
|
||||
shortcutsRepository = get(),
|
||||
settings = get(),
|
||||
downloadManagerHelper = get(),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -54,13 +54,22 @@ import org.koitharu.kotatsu.utils.ext.getDisplayMessage
|
||||
import org.koitharu.kotatsu.utils.ext.hasGlobalPoint
|
||||
import org.koitharu.kotatsu.utils.ext.observeWithPrevious
|
||||
|
||||
class ReaderActivity : BaseFullscreenActivity<ActivityReaderBinding>(),
|
||||
class ReaderActivity :
|
||||
BaseFullscreenActivity<ActivityReaderBinding>(),
|
||||
ChaptersBottomSheet.OnChapterChangeListener,
|
||||
GridTouchHelper.OnGridTouchListener, OnPageSelectListener, ReaderConfigDialog.Callback,
|
||||
ActivityResultCallback<Boolean>, ReaderControlDelegate.OnInteractionListener, OnApplyWindowInsetsListener {
|
||||
GridTouchHelper.OnGridTouchListener,
|
||||
OnPageSelectListener,
|
||||
ReaderConfigDialog.Callback,
|
||||
ActivityResultCallback<Boolean>,
|
||||
ReaderControlDelegate.OnInteractionListener,
|
||||
OnApplyWindowInsetsListener {
|
||||
|
||||
private val viewModel by viewModel<ReaderViewModel> {
|
||||
parametersOf(MangaIntent(intent), intent?.getParcelableExtra<ReaderState>(EXTRA_STATE))
|
||||
parametersOf(
|
||||
MangaIntent(intent),
|
||||
intent?.getParcelableExtra<ReaderState>(EXTRA_STATE),
|
||||
intent?.getStringExtra(EXTRA_BRANCH),
|
||||
)
|
||||
}
|
||||
|
||||
private lateinit var touchHelper: GridTouchHelper
|
||||
@@ -142,7 +151,8 @@ class ReaderActivity : BaseFullscreenActivity<ActivityReaderBinding>(),
|
||||
when (item.itemId) {
|
||||
R.id.action_reader_mode -> {
|
||||
ReaderConfigDialog.show(
|
||||
supportFragmentManager, when (reader) {
|
||||
supportFragmentManager,
|
||||
when (reader) {
|
||||
is PagerReaderFragment -> ReaderMode.STANDARD
|
||||
is WebtoonReaderFragment -> ReaderMode.WEBTOON
|
||||
is ReversedReaderFragment -> ReaderMode.REVERSED
|
||||
@@ -407,18 +417,29 @@ class ReaderActivity : BaseFullscreenActivity<ActivityReaderBinding>(),
|
||||
|
||||
const val ACTION_MANGA_READ = "${BuildConfig.APPLICATION_ID}.action.READ_MANGA"
|
||||
private const val EXTRA_STATE = "state"
|
||||
private const val EXTRA_BRANCH = "branch"
|
||||
private const val TOAST_DURATION = 1500L
|
||||
|
||||
fun newIntent(context: Context, manga: Manga): Intent {
|
||||
return Intent(context, ReaderActivity::class.java)
|
||||
.putExtra(MangaIntent.KEY_MANGA, ParcelableManga(manga))
|
||||
}
|
||||
|
||||
fun newIntent(context: Context, manga: Manga, branch: String?): Intent {
|
||||
return Intent(context, ReaderActivity::class.java)
|
||||
.putExtra(MangaIntent.KEY_MANGA, ParcelableManga(manga))
|
||||
.putExtra(EXTRA_BRANCH, branch)
|
||||
}
|
||||
|
||||
fun newIntent(context: Context, manga: Manga, state: ReaderState?): Intent {
|
||||
return Intent(context, ReaderActivity::class.java)
|
||||
.putExtra(MangaIntent.KEY_MANGA, ParcelableManga(manga))
|
||||
.putExtra(EXTRA_STATE, state)
|
||||
}
|
||||
|
||||
fun newIntent(context: Context, mangaId: Long, state: ReaderState?): Intent {
|
||||
fun newIntent(context: Context, mangaId: Long): Intent {
|
||||
return Intent(context, ReaderActivity::class.java)
|
||||
.putExtra(MangaIntent.KEY_ID, mangaId)
|
||||
.putExtra(EXTRA_STATE, state)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,8 +20,10 @@ data class ReaderState(
|
||||
scroll = history.scroll
|
||||
)
|
||||
|
||||
fun initial(manga: Manga) = ReaderState(
|
||||
chapterId = manga.chapters?.firstOrNull()?.id ?: error("Cannot find first chapter"),
|
||||
fun initial(manga: Manga, branch: String?) = ReaderState(
|
||||
chapterId = manga.chapters?.firstOrNull {
|
||||
it.branch == branch
|
||||
}?.id ?: error("Cannot find first chapter"),
|
||||
page = 0,
|
||||
scroll = 0
|
||||
)
|
||||
|
||||
@@ -34,6 +34,7 @@ import org.koitharu.kotatsu.utils.ext.processLifecycleScope
|
||||
class ReaderViewModel(
|
||||
private val intent: MangaIntent,
|
||||
initialState: ReaderState?,
|
||||
private val preselectedBranch: String?,
|
||||
private val dataRepository: MangaDataRepository,
|
||||
private val historyRepository: HistoryRepository,
|
||||
private val shortcutsRepository: ShortcutsRepository,
|
||||
@@ -188,8 +189,7 @@ class ReaderViewModel(
|
||||
|
||||
private fun loadImpl() {
|
||||
loadingJob = launchLoadingJob(Dispatchers.Default) {
|
||||
var manga = dataRepository.resolveIntent(intent)
|
||||
?: throw MangaNotFoundException("Cannot find manga")
|
||||
var manga = dataRepository.resolveIntent(intent) ?: throw MangaNotFoundException("Cannot find manga")
|
||||
mangaData.value = manga
|
||||
val repo = MangaRepository(manga.source)
|
||||
manga = repo.getDetails(manga)
|
||||
@@ -197,21 +197,20 @@ class ReaderViewModel(
|
||||
chapters.put(it.id, it)
|
||||
}
|
||||
// determine mode
|
||||
val mode =
|
||||
dataRepository.getReaderMode(manga.id) ?: manga.chapters?.randomOrNull()?.let {
|
||||
val pages = repo.getPages(it)
|
||||
val isWebtoon = MangaUtils.determineMangaIsWebtoon(pages)
|
||||
val newMode = getReaderMode(isWebtoon)
|
||||
if (isWebtoon != null) {
|
||||
dataRepository.savePreferences(manga, newMode)
|
||||
}
|
||||
newMode
|
||||
} ?: error("There are no chapters in this manga")
|
||||
val mode = dataRepository.getReaderMode(manga.id) ?: manga.chapters?.randomOrNull()?.let {
|
||||
val pages = repo.getPages(it)
|
||||
val isWebtoon = MangaUtils.determineMangaIsWebtoon(pages)
|
||||
val newMode = getReaderMode(isWebtoon)
|
||||
if (isWebtoon != null) {
|
||||
dataRepository.savePreferences(manga, newMode)
|
||||
}
|
||||
newMode
|
||||
} ?: error("There are no chapters in this manga")
|
||||
// obtain state
|
||||
if (currentState.value == null) {
|
||||
currentState.value = historyRepository.getOne(manga)?.let {
|
||||
ReaderState.from(it)
|
||||
} ?: ReaderState.initial(manga)
|
||||
} ?: ReaderState.initial(manga, preselectedBranch)
|
||||
}
|
||||
|
||||
val branch = chapters[currentState.value?.chapterId ?: 0L].branch
|
||||
@@ -327,6 +326,5 @@ class ReaderViewModel(
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user