Skip to content

Commit

Permalink
display latest series
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed Nov 1, 2023
1 parent d89fd24 commit 2fb77b2
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.datlag.burningseries.common

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.combinedClickable
import androidx.compose.ui.Modifier

@OptIn(ExperimentalFoundationApi::class)
actual fun Modifier.onClick(
enabled: Boolean,
onDoubleClick: (() -> Unit)?,
onLongClick: (() -> Unit)?,
onClick: () -> Unit
): Modifier {
return this.combinedClickable(
enabled = enabled,
onDoubleClick = onDoubleClick,
onLongClick = onLongClick,
onClick = onClick
)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,61 @@
package dev.datlag.burningseries.common

import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.gestures.awaitFirstDown
import androidx.compose.foundation.gestures.waitForUpOrCancellation
import androidx.compose.foundation.lazy.grid.GridItemSpan
import androidx.compose.foundation.lazy.grid.LazyGridItemScope
import androidx.compose.foundation.lazy.grid.LazyGridScope
import androidx.compose.runtime.Composable
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.input.pointer.pointerInput

inline fun Modifier.ifTrue(predicate: Boolean, builder: Modifier.() -> Modifier) = then(if (predicate) builder() else Modifier)
inline fun Modifier.ifFalse(predicate: Boolean, builder: Modifier.() -> Modifier) = then(if (!predicate) builder() else Modifier)

fun LazyGridScope.header(
content: @Composable LazyGridItemScope.() -> Unit
) {
item(span = { GridItemSpan(this.maxLineSpan) }, content = content)
}

fun Modifier.bounceClick(minScale: Float = 0.9F) = composed {
var buttonState by remember { mutableStateOf(false) }
val scale by animateFloatAsState(if (buttonState) minScale else 1F)

graphicsLayer {
scaleX = scale
scaleY = scale
}.pointerInput(buttonState) {
awaitPointerEventScope {
buttonState = if (buttonState) {
waitForUpOrCancellation()
false
} else {
awaitFirstDown(false)
true
}
}
}
}

fun Modifier.pressClick(maxTranslation: Float = 10F) = composed {
var buttonState by remember { mutableStateOf(false) }
val translation by animateFloatAsState(if (buttonState) maxTranslation else 0F)

graphicsLayer {
translationY = translation
}.pointerInput(buttonState) {
awaitPointerEventScope {
buttonState = if (buttonState) {
waitForUpOrCancellation()
false
} else {
awaitFirstDown(false)
true
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.datlag.burningseries.common

import androidx.compose.ui.Modifier

expect fun Modifier.onClick(
enabled: Boolean = true,
onDoubleClick: (() -> Unit)? = null,
onLongClick: (() -> Unit)? = null,
onClick: () -> Unit
) : Modifier
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import dev.datlag.burningseries.common.header
import dev.datlag.burningseries.common.lifecycle.collectAsStateWithLifecycle
import dev.datlag.burningseries.model.Home
import dev.datlag.burningseries.model.state.HomeState
import dev.datlag.burningseries.shared.SharedRes
import dev.datlag.burningseries.ui.screen.initial.home.component.EpisodeItem
import dev.datlag.burningseries.ui.screen.initial.home.component.SeriesItem
import dev.icerock.moko.resources.compose.stringResource

@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
@Composable
Expand Down Expand Up @@ -52,7 +55,7 @@ fun HomeScreen(component: HomeComponent) {
component.retryLoadingHome()
}
) {
Text(text = "Retry")
Text(text = stringResource(SharedRes.strings.retry))
}
}
}
Expand Down Expand Up @@ -107,7 +110,7 @@ private fun MainView(home: Home, component: HomeComponent, modifier: Modifier =
) {
header {
Text(
text = "Episodes",
text = stringResource(SharedRes.strings.newest_episodes),
style = MaterialTheme.typography.headlineLarge,
fontWeight = FontWeight.Bold
)
Expand All @@ -124,13 +127,17 @@ private fun MainView(home: Home, component: HomeComponent, modifier: Modifier =
}
header {
Text(
text = "Series",
text = stringResource(SharedRes.strings.newest_series),
style = MaterialTheme.typography.headlineLarge,
fontWeight = FontWeight.Bold
)
}
items(home.series) { series ->

items(home.series, key = {
it.href
}) { series ->
SeriesItem(series) {
// series clicked
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import dev.datlag.burningseries.common.bounceClick
import dev.datlag.burningseries.common.ifTrue
import dev.datlag.burningseries.common.onClick
import dev.datlag.burningseries.model.BSUtil
import dev.datlag.burningseries.model.Home
import dev.datlag.burningseries.ui.theme.CountryImage
Expand All @@ -26,7 +30,11 @@ import io.kamel.image.asyncPainterResource
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun LazyGridItemScope.EpisodeItem(episode: Home.Episode, onclick: () -> Unit) {
ElevatedCard(modifier = Modifier.animateItemPlacement().height(200.dp)) {
ElevatedCard(
modifier = Modifier.animateItemPlacement().height(200.dp).bounceClick().onClick {
onclick()
}
) {
Row(
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
Expand Down Expand Up @@ -79,16 +87,37 @@ fun LazyGridItemScope.EpisodeItem(episode: Home.Episode, onclick: () -> Unit) {
) {
val code = remember(episode.href) { episode.flags.firstOrNull()?.bestCountryCode }
if (code != null) {
Image(
painter = painterResource(CountryImage.getByCode(code)),
contentDescription = episode.flags.firstOrNull()?.title,
modifier = Modifier
.size(24.dp)
.clip(MaterialTheme.shapes.extraSmall)
.border(1.dp, LocalContentColor.current, MaterialTheme.shapes.extraSmall)
)
val res = remember(code) { CountryImage.getByFlag(code) }

Box(
modifier = Modifier.size(36.dp),
contentAlignment = Alignment.Center
) {
Image(
painter = painterResource(res.last()),
contentDescription = episode.flags.firstOrNull()?.title,
modifier = Modifier
.size(24.dp)
.clip(MaterialTheme.shapes.extraSmall)
.border(1.dp, LocalContentColor.current, MaterialTheme.shapes.extraSmall)
.ifTrue(res.size >= 2) { this.align(Alignment.TopStart).alpha(0.75F) }
)
if (res.size >= 2) {
Image(
painter = painterResource(res.first()),
contentDescription = episode.flags.firstOrNull()?.title,
modifier = Modifier
.size(24.dp)
.clip(MaterialTheme.shapes.extraSmall)
.border(1.dp, LocalContentColor.current, MaterialTheme.shapes.extraSmall)
.align(Alignment.BottomEnd)
)
}
}
}
Text(text = episode.info)
Text(
text = episode.info
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package dev.datlag.burningseries.ui.screen.initial.home.component

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.grid.LazyGridItemScope
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import dev.datlag.burningseries.common.bounceClick
import dev.datlag.burningseries.common.onClick
import dev.datlag.burningseries.model.BSUtil
import dev.datlag.burningseries.model.Home
import io.kamel.core.Resource
import io.kamel.image.asyncPainterResource

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun LazyGridItemScope.SeriesItem(series: Home.Series, onClick: () -> Unit) {
ElevatedCard(
modifier = Modifier.animateItemPlacement().height(200.dp).bounceClick().onClick {
onClick()
}
) {
Row(
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
Surface(
shape = CardDefaults.elevatedShape
) {
series.coverHref?.let { cover ->
when (val resource = asyncPainterResource(BSUtil.getBurningSeriesLink(cover))) {
is Resource.Loading, is Resource.Failure -> {
Box(
modifier = Modifier
.aspectRatio(1F, true)
.clip(CardDefaults.elevatedShape)
.background(MaterialTheme.colorScheme.tertiaryContainer)
)
}
is Resource.Success -> {
Image(
painter = resource.value,
contentScale = ContentScale.FillWidth,
contentDescription = series.title,
modifier = Modifier.aspectRatio(1F, true)
)
}
}
}
}
Column(
modifier = Modifier.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
Text(
text = series.title,
fontWeight = FontWeight.SemiBold,
style = MaterialTheme.typography.titleLarge,
modifier = Modifier.padding(bottom = 8.dp),
maxLines = 3
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,26 @@ object CountryImage {
else -> SharedRes.images.COUNTRY_UNKNOWN
}
}

fun getByFlag(code: String): List<ImageResource> {
val defaultByCode = getByCode(code)
return if (defaultByCode == SharedRes.images.COUNTRY_UNKNOWN) {
when {
code.equals("DES", true) -> listOf(
SharedRes.images.US,
SharedRes.images.DE
)
code.equals("EN", true) -> listOf(
SharedRes.images.US
)
code.equals("JPS", true) -> listOf(
SharedRes.images.JP,
SharedRes.images.US
)
else -> listOf(defaultByCode)
}
} else {
listOf(defaultByCode)
}
}
}
3 changes: 3 additions & 0 deletions app/shared/src/commonMain/resources/MR/base/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
<string name="home">Home</string>
<string name="favorites">Favorites</string>
<string name="search">Search</string>
<string name="newest_episodes">Newest Episodes</string>
<string name="newest_series">Newest Series</string>
<string name="retry">Retry</string>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.datlag.burningseries.common

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.ui.Modifier
import androidx.compose.foundation.onClick

@OptIn(ExperimentalFoundationApi::class)
actual fun Modifier.onClick(
enabled: Boolean,
onDoubleClick: (() -> Unit)?,
onLongClick: (() -> Unit)?,
onClick: () -> Unit
): Modifier {
return this.onClick(
enabled = enabled,
onDoubleClick = onDoubleClick,
onLongClick = onLongClick,
onClick = onClick
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.datlag.burningseries.common

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.ui.Modifier
import androidx.compose.foundation.onClick

@OptIn(ExperimentalFoundationApi::class)
actual fun Modifier.onClick(
enabled: Boolean,
onDoubleClick: (() -> Unit)?,
onLongClick: (() -> Unit)?,
onClick: () -> Unit
): Modifier {
return this.onClick(
enabled = enabled,
onDoubleClick = onDoubleClick,
onLongClick = onLongClick,
onClick = onClick
)
}

0 comments on commit 2fb77b2

Please sign in to comment.