2022-05-07 23:34:55 -04:00
|
|
|
package eu.kanade.presentation.browse
|
2022-04-24 14:35:59 -04:00
|
|
|
|
|
|
|
import androidx.compose.foundation.clickable
|
|
|
|
import androidx.compose.foundation.layout.Column
|
|
|
|
import androidx.compose.foundation.layout.WindowInsets
|
|
|
|
import androidx.compose.foundation.layout.asPaddingValues
|
|
|
|
import androidx.compose.foundation.layout.fillMaxWidth
|
|
|
|
import androidx.compose.foundation.layout.navigationBars
|
|
|
|
import androidx.compose.foundation.layout.padding
|
|
|
|
import androidx.compose.foundation.lazy.items
|
|
|
|
import androidx.compose.material.icons.Icons
|
|
|
|
import androidx.compose.material.icons.filled.PushPin
|
|
|
|
import androidx.compose.material.icons.outlined.PushPin
|
|
|
|
import androidx.compose.material3.AlertDialog
|
|
|
|
import androidx.compose.material3.Icon
|
|
|
|
import androidx.compose.material3.IconButton
|
2022-04-24 15:04:00 -04:00
|
|
|
import androidx.compose.material3.LocalTextStyle
|
2022-04-24 14:35:59 -04:00
|
|
|
import androidx.compose.material3.MaterialTheme
|
|
|
|
import androidx.compose.material3.Text
|
|
|
|
import androidx.compose.material3.TextButton
|
|
|
|
import androidx.compose.runtime.Composable
|
2022-07-16 14:44:37 -04:00
|
|
|
import androidx.compose.runtime.LaunchedEffect
|
2022-04-24 14:35:59 -04:00
|
|
|
import androidx.compose.ui.Modifier
|
|
|
|
import androidx.compose.ui.platform.LocalContext
|
|
|
|
import androidx.compose.ui.res.stringResource
|
|
|
|
import androidx.compose.ui.unit.dp
|
2022-09-03 10:16:30 -04:00
|
|
|
import eu.kanade.domain.source.interactor.GetRemoteManga
|
2022-04-24 14:35:59 -04:00
|
|
|
import eu.kanade.domain.source.model.Pin
|
|
|
|
import eu.kanade.domain.source.model.Source
|
2022-05-07 23:34:55 -04:00
|
|
|
import eu.kanade.presentation.browse.components.BaseSourceItem
|
2022-04-24 14:35:59 -04:00
|
|
|
import eu.kanade.presentation.components.EmptyScreen
|
2022-04-27 08:36:16 -04:00
|
|
|
import eu.kanade.presentation.components.LoadingScreen
|
2022-05-23 18:03:46 -04:00
|
|
|
import eu.kanade.presentation.components.ScrollbarLazyColumn
|
2022-04-24 14:35:59 -04:00
|
|
|
import eu.kanade.presentation.theme.header
|
2022-07-22 22:44:05 -04:00
|
|
|
import eu.kanade.presentation.util.bottomNavPaddingValues
|
2022-04-24 14:35:59 -04:00
|
|
|
import eu.kanade.presentation.util.horizontalPadding
|
2022-05-15 14:00:35 -04:00
|
|
|
import eu.kanade.presentation.util.plus
|
2022-05-15 16:19:55 -04:00
|
|
|
import eu.kanade.presentation.util.topPaddingValues
|
2022-04-24 14:35:59 -04:00
|
|
|
import eu.kanade.tachiyomi.R
|
|
|
|
import eu.kanade.tachiyomi.source.LocalSource
|
2022-05-15 17:03:57 -04:00
|
|
|
import eu.kanade.tachiyomi.ui.browse.source.SourcesPresenter
|
2022-04-24 14:35:59 -04:00
|
|
|
import eu.kanade.tachiyomi.util.system.LocaleHelper
|
2022-07-16 14:44:37 -04:00
|
|
|
import eu.kanade.tachiyomi.util.system.toast
|
|
|
|
import kotlinx.coroutines.flow.collectLatest
|
2022-04-24 14:35:59 -04:00
|
|
|
|
|
|
|
@Composable
|
2022-05-15 17:03:57 -04:00
|
|
|
fun SourcesScreen(
|
|
|
|
presenter: SourcesPresenter,
|
2022-09-03 10:16:30 -04:00
|
|
|
onClickItem: (Source, String) -> Unit,
|
2022-04-24 14:35:59 -04:00
|
|
|
onClickDisable: (Source) -> Unit,
|
|
|
|
onClickPin: (Source) -> Unit,
|
|
|
|
) {
|
2022-07-16 14:44:37 -04:00
|
|
|
val context = LocalContext.current
|
|
|
|
when {
|
|
|
|
presenter.isLoading -> LoadingScreen()
|
|
|
|
presenter.isEmpty -> EmptyScreen(R.string.source_empty_screen)
|
|
|
|
else -> {
|
|
|
|
SourceList(
|
|
|
|
state = presenter,
|
|
|
|
onClickItem = onClickItem,
|
|
|
|
onClickDisable = onClickDisable,
|
|
|
|
onClickPin = onClickPin,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LaunchedEffect(Unit) {
|
|
|
|
presenter.events.collectLatest { event ->
|
|
|
|
when (event) {
|
|
|
|
SourcesPresenter.Event.FailedFetchingSources -> {
|
|
|
|
context.toast(R.string.internal_error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-24 14:35:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
fun SourceList(
|
2022-07-16 14:44:37 -04:00
|
|
|
state: SourcesState,
|
2022-09-03 10:16:30 -04:00
|
|
|
onClickItem: (Source, String) -> Unit,
|
2022-04-24 14:35:59 -04:00
|
|
|
onClickDisable: (Source) -> Unit,
|
|
|
|
onClickPin: (Source) -> Unit,
|
|
|
|
) {
|
2022-05-23 18:03:46 -04:00
|
|
|
ScrollbarLazyColumn(
|
2022-07-22 22:44:05 -04:00
|
|
|
contentPadding = bottomNavPaddingValues + WindowInsets.navigationBars.asPaddingValues() + topPaddingValues,
|
2022-04-24 14:35:59 -04:00
|
|
|
) {
|
|
|
|
items(
|
2022-07-16 14:44:37 -04:00
|
|
|
items = state.items,
|
2022-04-24 14:35:59 -04:00
|
|
|
contentType = {
|
|
|
|
when (it) {
|
2022-05-01 22:34:58 -04:00
|
|
|
is SourceUiModel.Header -> "header"
|
|
|
|
is SourceUiModel.Item -> "item"
|
2022-04-24 14:35:59 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
key = {
|
|
|
|
when (it) {
|
2022-05-01 22:34:58 -04:00
|
|
|
is SourceUiModel.Header -> it.hashCode()
|
|
|
|
is SourceUiModel.Item -> it.source.key()
|
2022-04-24 14:35:59 -04:00
|
|
|
}
|
2022-05-10 17:54:52 -04:00
|
|
|
},
|
2022-04-24 14:35:59 -04:00
|
|
|
) { model ->
|
|
|
|
when (model) {
|
2022-05-01 22:34:58 -04:00
|
|
|
is SourceUiModel.Header -> {
|
2022-04-24 14:35:59 -04:00
|
|
|
SourceHeader(
|
|
|
|
modifier = Modifier.animateItemPlacement(),
|
2022-05-10 17:54:52 -04:00
|
|
|
language = model.language,
|
2022-04-24 14:35:59 -04:00
|
|
|
)
|
|
|
|
}
|
2022-05-01 22:34:58 -04:00
|
|
|
is SourceUiModel.Item -> SourceItem(
|
2022-04-24 14:35:59 -04:00
|
|
|
modifier = Modifier.animateItemPlacement(),
|
2022-04-27 08:36:16 -04:00
|
|
|
source = model.source,
|
2022-04-24 14:35:59 -04:00
|
|
|
onClickItem = onClickItem,
|
2022-08-29 17:18:06 -04:00
|
|
|
onLongClickItem = { state.dialog = SourcesPresenter.Dialog(it) },
|
2022-04-24 14:35:59 -04:00
|
|
|
onClickPin = onClickPin,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-16 14:44:37 -04:00
|
|
|
if (state.dialog != null) {
|
|
|
|
val source = state.dialog!!.source
|
2022-04-24 14:35:59 -04:00
|
|
|
SourceOptionsDialog(
|
2022-07-16 14:44:37 -04:00
|
|
|
source = source,
|
2022-04-24 14:35:59 -04:00
|
|
|
onClickPin = {
|
2022-07-16 14:44:37 -04:00
|
|
|
onClickPin(source)
|
|
|
|
state.dialog = null
|
2022-04-24 14:35:59 -04:00
|
|
|
},
|
|
|
|
onClickDisable = {
|
2022-07-16 14:44:37 -04:00
|
|
|
onClickDisable(source)
|
|
|
|
state.dialog = null
|
2022-04-24 14:35:59 -04:00
|
|
|
},
|
2022-07-16 14:44:37 -04:00
|
|
|
onDismiss = { state.dialog = null },
|
2022-04-24 14:35:59 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
fun SourceHeader(
|
|
|
|
modifier: Modifier = Modifier,
|
2022-05-10 17:54:52 -04:00
|
|
|
language: String,
|
2022-04-24 14:35:59 -04:00
|
|
|
) {
|
|
|
|
val context = LocalContext.current
|
|
|
|
Text(
|
|
|
|
text = LocaleHelper.getSourceDisplayName(language, context),
|
|
|
|
modifier = modifier
|
|
|
|
.padding(horizontal = horizontalPadding, vertical = 8.dp),
|
2022-05-10 17:54:52 -04:00
|
|
|
style = MaterialTheme.typography.header,
|
2022-04-24 14:35:59 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
fun SourceItem(
|
|
|
|
modifier: Modifier = Modifier,
|
2022-04-27 08:36:16 -04:00
|
|
|
source: Source,
|
2022-09-03 10:16:30 -04:00
|
|
|
onClickItem: (Source, String) -> Unit,
|
2022-04-24 14:35:59 -04:00
|
|
|
onLongClickItem: (Source) -> Unit,
|
2022-05-10 17:54:52 -04:00
|
|
|
onClickPin: (Source) -> Unit,
|
2022-04-24 14:35:59 -04:00
|
|
|
) {
|
2022-04-27 08:36:16 -04:00
|
|
|
BaseSourceItem(
|
|
|
|
modifier = modifier,
|
|
|
|
source = source,
|
2022-09-03 10:16:30 -04:00
|
|
|
onClickItem = { onClickItem(source, GetRemoteManga.QUERY_POPULAR) },
|
2022-04-27 08:36:16 -04:00
|
|
|
onLongClickItem = { onLongClickItem(source) },
|
|
|
|
action = { source ->
|
|
|
|
if (source.supportsLatest) {
|
2022-09-03 10:16:30 -04:00
|
|
|
TextButton(onClick = { onClickItem(source, GetRemoteManga.QUERY_LATEST) }) {
|
2022-04-27 08:36:16 -04:00
|
|
|
Text(
|
2022-05-19 17:43:27 -04:00
|
|
|
text = stringResource(R.string.latest),
|
2022-04-27 08:36:16 -04:00
|
|
|
style = LocalTextStyle.current.copy(
|
2022-05-10 17:54:52 -04:00
|
|
|
color = MaterialTheme.colorScheme.primary,
|
|
|
|
),
|
2022-04-27 08:36:16 -04:00
|
|
|
)
|
|
|
|
}
|
2022-04-24 14:35:59 -04:00
|
|
|
}
|
2022-04-27 08:36:16 -04:00
|
|
|
SourcePinButton(
|
|
|
|
isPinned = Pin.Pinned in source.pin,
|
2022-05-10 17:54:52 -04:00
|
|
|
onClick = { onClickPin(source) },
|
2022-04-27 08:36:16 -04:00
|
|
|
)
|
|
|
|
},
|
|
|
|
)
|
2022-04-24 14:35:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
fun SourcePinButton(
|
|
|
|
isPinned: Boolean,
|
2022-05-10 17:54:52 -04:00
|
|
|
onClick: () -> Unit,
|
2022-04-24 14:35:59 -04:00
|
|
|
) {
|
|
|
|
val icon = if (isPinned) Icons.Filled.PushPin else Icons.Outlined.PushPin
|
|
|
|
val tint = if (isPinned) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onBackground
|
|
|
|
IconButton(onClick = onClick) {
|
|
|
|
Icon(
|
|
|
|
imageVector = icon,
|
|
|
|
contentDescription = "",
|
2022-05-10 17:54:52 -04:00
|
|
|
tint = tint,
|
2022-04-24 14:35:59 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
fun SourceOptionsDialog(
|
|
|
|
source: Source,
|
|
|
|
onClickPin: () -> Unit,
|
|
|
|
onClickDisable: () -> Unit,
|
|
|
|
onDismiss: () -> Unit,
|
|
|
|
) {
|
|
|
|
AlertDialog(
|
|
|
|
title = {
|
2022-07-30 13:55:07 -04:00
|
|
|
Text(text = source.visualName)
|
2022-04-24 14:35:59 -04:00
|
|
|
},
|
|
|
|
text = {
|
|
|
|
Column {
|
|
|
|
val textId = if (Pin.Pinned in source.pin) R.string.action_unpin else R.string.action_pin
|
|
|
|
Text(
|
2022-06-25 11:20:34 -04:00
|
|
|
text = stringResource(textId),
|
2022-04-24 14:35:59 -04:00
|
|
|
modifier = Modifier
|
|
|
|
.clickable(onClick = onClickPin)
|
|
|
|
.fillMaxWidth()
|
2022-05-10 17:54:52 -04:00
|
|
|
.padding(vertical = 16.dp),
|
2022-04-24 14:35:59 -04:00
|
|
|
)
|
|
|
|
if (source.id != LocalSource.ID) {
|
|
|
|
Text(
|
2022-05-19 17:43:27 -04:00
|
|
|
text = stringResource(R.string.action_disable),
|
2022-04-24 14:35:59 -04:00
|
|
|
modifier = Modifier
|
|
|
|
.clickable(onClick = onClickDisable)
|
|
|
|
.fillMaxWidth()
|
2022-05-10 17:54:52 -04:00
|
|
|
.padding(vertical = 16.dp),
|
2022-04-24 14:35:59 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onDismissRequest = onDismiss,
|
|
|
|
confirmButton = {},
|
|
|
|
)
|
|
|
|
}
|
2022-05-01 22:34:58 -04:00
|
|
|
|
|
|
|
sealed class SourceUiModel {
|
|
|
|
data class Item(val source: Source) : SourceUiModel()
|
|
|
|
data class Header(val language: String) : SourceUiModel()
|
|
|
|
}
|