komikku/app/src/main/java/exh/ui/intercept/InterceptActivity.kt

184 lines
6.9 KiB
Kotlin
Raw Normal View History

package exh.ui.intercept
import android.content.Intent
import android.os.Bundle
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.lifecycleScope
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import eu.kanade.domain.chapter.model.Chapter
import eu.kanade.domain.manga.model.Manga
import eu.kanade.presentation.components.AppBar
import eu.kanade.presentation.components.Scaffold
import eu.kanade.tachiyomi.R
2020-10-20 00:41:41 +02:00
import eu.kanade.tachiyomi.source.online.UrlImportableSource
import eu.kanade.tachiyomi.ui.base.activity.BaseActivity
2017-08-24 23:33:03 +02:00
import eu.kanade.tachiyomi.ui.main.MainActivity
import eu.kanade.tachiyomi.ui.manga.MangaController
import eu.kanade.tachiyomi.ui.reader.ReaderActivity
import eu.kanade.tachiyomi.util.view.setComposeContent
2020-05-30 00:17:40 +02:00
import exh.GalleryAddEvent
import exh.GalleryAdder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
class InterceptActivity : BaseActivity() {
private var statusJob: Job? = null
private val status: MutableStateFlow<InterceptResult> = MutableStateFlow(InterceptResult.Idle)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setComposeContent {
InterceptActivityContent(status.collectAsState().value)
}
processLink()
}
@Composable
private fun InterceptActivityContent(status: InterceptResult) {
Scaffold(
topBar = {
AppBar(
title = stringResource(R.string.app_name),
navigateUp = ::onBackPressed,
)
},
) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(it),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(8.dp, Alignment.CenterVertically),
) {
when (status) {
InterceptResult.Idle, InterceptResult.Loading -> {
Text(
text = stringResource(R.string.loading_manga),
style = MaterialTheme.typography.titleLarge,
)
CircularProgressIndicator(modifier = Modifier.size(56.dp))
}
is InterceptResult.Success -> Text(
text = stringResource(R.string.launching_app),
style = MaterialTheme.typography.titleLarge,
)
is InterceptResult.Failure -> Text(
text = stringResource(R.string.error_with_reason, status.reason),
style = MaterialTheme.typography.titleLarge,
)
}
}
2018-07-21 21:26:14 +02:00
}
}
private fun processLink() {
if (Intent.ACTION_VIEW == intent.action) {
loadGallery(intent.dataString!!)
}
}
override fun onStart() {
super.onStart()
statusJob?.cancel()
statusJob = status
.onEach {
2020-05-02 06:46:24 +02:00
when (it) {
is InterceptResult.Success -> {
onBackPressed()
startActivity(
if (it.chapter != null) {
ReaderActivity.newIntent(this, it.manga.id, it.chapter.id)
} else {
Intent(this, MainActivity::class.java)
.setAction(MainActivity.SHORTCUT_MANGA)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
.putExtra(MangaController.MANGA_EXTRA, it.mangaId)
Update linter (cherry picked from commit f0eb42e72d1e267049777a303bd97d96517a9a1f) # Conflicts: # app/src/main/java/eu/kanade/tachiyomi/data/backup/full/FullBackupRestore.kt # app/src/main/java/eu/kanade/tachiyomi/data/backup/full/models/Backup.kt # app/src/main/java/eu/kanade/tachiyomi/data/backup/full/models/BackupManga.kt # app/src/main/java/eu/kanade/tachiyomi/data/backup/legacy/models/Backup.kt # app/src/main/java/eu/kanade/tachiyomi/extension/model/Extension.kt # app/src/main/java/eu/kanade/tachiyomi/source/LocalSource.kt # app/src/main/java/eu/kanade/tachiyomi/ui/base/changehandler/OneWayFadeChangeHandler.kt # app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SearchController.kt # app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SearchPresenter.kt # app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/sources/SelectionHeader.kt # app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourceItem.kt # app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourcePresenter.kt # app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceFilterSheet.kt # app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchController.kt # app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt # app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHolder.kt # app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryListHolder.kt # app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt # app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt # app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPageSheet.kt # app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/ChapterLoader.kt # app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt # app/src/main/java/eu/kanade/tachiyomi/ui/reader/model/ReaderPage.kt # app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerPageHolder.kt # app/src/main/java/eu/kanade/tachiyomi/util/view/ViewExtensions.kt
2022-04-08 21:30:30 +02:00
},
2020-05-02 06:46:24 +02:00
)
}
is InterceptResult.Failure -> {
MaterialAlertDialogBuilder(this)
.setTitle(R.string.chapter_error)
.setMessage(getString(R.string.could_not_open_manga, it.reason))
.setPositiveButton(android.R.string.ok, null)
.setOnCancelListener { onBackPressed() }
.setOnDismissListener { onBackPressed() }
2020-05-02 06:46:24 +02:00
.show()
}
else -> Unit
}
2020-05-02 06:46:24 +02:00
}
.launchIn(lifecycleScope)
}
override fun onStop() {
super.onStop()
statusJob?.cancel()
}
2020-05-30 00:17:40 +02:00
private val galleryAdder = GalleryAdder()
@Synchronized
fun loadGallery(gallery: String) {
// Do not load gallery if already loading
if (status.value is InterceptResult.Idle) {
status.value = InterceptResult.Loading
2020-10-20 00:41:41 +02:00
val sources = galleryAdder.pickSource(gallery)
if (sources.size > 1) {
MaterialAlertDialogBuilder(this)
.setTitle(R.string.label_sources)
.setSingleChoiceItems(sources.map { it.toString() }.toTypedArray(), 0) { dialog, index ->
dialog.dismiss()
2020-10-20 00:41:41 +02:00
loadGalleryEnd(gallery, sources[index])
2020-05-30 00:17:40 +02:00
}
2020-10-20 00:41:41 +02:00
.show()
} else {
loadGalleryEnd(gallery)
2020-05-30 00:17:40 +02:00
}
}
}
2020-10-20 00:41:41 +02:00
private fun loadGalleryEnd(gallery: String, source: UrlImportableSource? = null) {
// Load gallery async
lifecycleScope.launch(Dispatchers.IO) {
val result = galleryAdder.addGallery(this@InterceptActivity, gallery, forceSource = source)
2020-10-20 00:41:41 +02:00
status.value = when (result) {
is GalleryAddEvent.Success -> InterceptResult.Success(result.manga.id, result.manga, result.chapter)
is GalleryAddEvent.Fail -> InterceptResult.Failure(result.logMessage)
}
2020-10-20 00:41:41 +02:00
}
}
init {
registerSecureActivity(this)
}
2020-05-30 00:17:40 +02:00
}
sealed class InterceptResult {
2020-08-05 01:32:36 +02:00
object Idle : InterceptResult()
object Loading : InterceptResult()
data class Success(val mangaId: Long, val manga: Manga, val chapter: Chapter? = null) : InterceptResult()
2020-05-30 00:17:40 +02:00
data class Failure(val reason: String) : InterceptResult()
}