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
|
|
|
|
import androidx.compose.runtime.collectAsState
|
|
|
|
import androidx.compose.runtime.getValue
|
|
|
|
import androidx.compose.runtime.mutableStateOf
|
|
|
|
import androidx.compose.runtime.remember
|
2022-05-19 17:43:27 -04:00
|
|
|
import androidx.compose.runtime.setValue
|
2022-04-24 14:35:59 -04:00
|
|
|
import androidx.compose.ui.Modifier
|
|
|
|
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
|
|
|
|
import androidx.compose.ui.input.nestedscroll.nestedScroll
|
|
|
|
import androidx.compose.ui.platform.LocalContext
|
|
|
|
import androidx.compose.ui.res.stringResource
|
|
|
|
import androidx.compose.ui.unit.dp
|
|
|
|
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
|
|
|
|
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-01 22:34:58 -04:00
|
|
|
import eu.kanade.tachiyomi.ui.browse.source.SourceState
|
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
|
|
|
|
|
|
|
|
@Composable
|
2022-05-15 17:03:57 -04:00
|
|
|
fun SourcesScreen(
|
2022-04-24 14:35:59 -04:00
|
|
|
nestedScrollInterop: NestedScrollConnection,
|
2022-05-15 17:03:57 -04:00
|
|
|
presenter: SourcesPresenter,
|
2022-04-24 14:35:59 -04:00
|
|
|
onClickItem: (Source) -> Unit,
|
|
|
|
onClickDisable: (Source) -> Unit,
|
|
|
|
onClickLatest: (Source) -> Unit,
|
|
|
|
onClickPin: (Source) -> Unit,
|
|
|
|
) {
|
|
|
|
val state by presenter.state.collectAsState()
|
|
|
|
|
2022-05-01 22:34:58 -04:00
|
|
|
when (state) {
|
|
|
|
is SourceState.Loading -> LoadingScreen()
|
|
|
|
is SourceState.Error -> Text(text = (state as SourceState.Error).error.message!!)
|
|
|
|
is SourceState.Success -> SourceList(
|
2022-04-24 14:35:59 -04:00
|
|
|
nestedScrollConnection = nestedScrollInterop,
|
2022-05-01 22:34:58 -04:00
|
|
|
list = (state as SourceState.Success).uiModels,
|
2022-04-24 14:35:59 -04:00
|
|
|
onClickItem = onClickItem,
|
|
|
|
onClickDisable = onClickDisable,
|
|
|
|
onClickLatest = onClickLatest,
|
|
|
|
onClickPin = onClickPin,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
fun SourceList(
|
|
|
|
nestedScrollConnection: NestedScrollConnection,
|
2022-05-01 22:34:58 -04:00
|
|
|
list: List<SourceUiModel>,
|
2022-04-24 14:35:59 -04:00
|
|
|
onClickItem: (Source) -> Unit,
|
|
|
|
onClickDisable: (Source) -> Unit,
|
|
|
|
onClickLatest: (Source) -> Unit,
|
|
|
|
onClickPin: (Source) -> Unit,
|
|
|
|
) {
|
2022-05-01 22:34:58 -04:00
|
|
|
if (list.isEmpty()) {
|
|
|
|
EmptyScreen(textResource = R.string.source_empty_screen)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-19 17:43:27 -04:00
|
|
|
var sourceState by remember { mutableStateOf<Source?>(null) }
|
|
|
|
|
2022-05-23 18:03:46 -04:00
|
|
|
ScrollbarLazyColumn(
|
2022-05-15 17:03:57 -04:00
|
|
|
modifier = Modifier.nestedScroll(nestedScrollConnection),
|
2022-05-15 14:00:35 -04:00
|
|
|
contentPadding = WindowInsets.navigationBars.asPaddingValues() + topPaddingValues,
|
2022-04-24 14:35:59 -04:00
|
|
|
) {
|
|
|
|
items(
|
|
|
|
items = list,
|
|
|
|
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-05-19 17:43:27 -04:00
|
|
|
onLongClickItem = { sourceState = it },
|
2022-04-24 14:35:59 -04:00
|
|
|
onClickLatest = onClickLatest,
|
|
|
|
onClickPin = onClickPin,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sourceState != null) {
|
|
|
|
SourceOptionsDialog(
|
2022-05-19 17:43:27 -04:00
|
|
|
source = sourceState!!,
|
2022-04-24 14:35:59 -04:00
|
|
|
onClickPin = {
|
2022-05-19 17:43:27 -04:00
|
|
|
onClickPin(sourceState!!)
|
|
|
|
sourceState = null
|
2022-04-24 14:35:59 -04:00
|
|
|
},
|
|
|
|
onClickDisable = {
|
2022-05-19 17:43:27 -04:00
|
|
|
onClickDisable(sourceState!!)
|
|
|
|
sourceState = null
|
2022-04-24 14:35:59 -04:00
|
|
|
},
|
2022-05-19 17:43:27 -04:00
|
|
|
onDismiss = { sourceState = 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-04-24 14:35:59 -04:00
|
|
|
onClickItem: (Source) -> Unit,
|
|
|
|
onLongClickItem: (Source) -> Unit,
|
|
|
|
onClickLatest: (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,
|
|
|
|
onClickItem = { onClickItem(source) },
|
|
|
|
onLongClickItem = { onLongClickItem(source) },
|
|
|
|
action = { source ->
|
|
|
|
if (source.supportsLatest) {
|
|
|
|
TextButton(onClick = { onClickLatest(source) }) {
|
|
|
|
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 = {
|
|
|
|
Text(text = source.nameWithLanguage)
|
|
|
|
},
|
|
|
|
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()
|
|
|
|
}
|