Skip to content

Commit

Permalink
Fix star and headline backgrounds ordering.
Browse files Browse the repository at this point in the history
  • Loading branch information
oxyroid committed Jun 1, 2024
1 parent 46a0636 commit f467151
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ fun AppNavHost(

val tv = isTelevision()

StarBackground()
NavHost(
navController = navController,
startDestination = startDestination,
Expand Down
1 change: 1 addition & 0 deletions androidApp/src/main/java/com/m3u/androidApp/ui/Scaffold.kt
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ internal fun MainContent(
) { padding ->
Background {
Box {
StarBackground()
HeadlineBackground()
content(padding)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package com.m3u.androidApp.ui.internal

import android.util.Log
import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.offset
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.graphics.Color
Expand All @@ -25,27 +21,27 @@ import coil.request.CachePolicy
import coil.request.ImageRequest
import com.m3u.core.architecture.preferences.hiltPreferences
import com.m3u.material.ktx.BlurTransformation
import com.m3u.material.ktx.Edge
import com.m3u.material.ktx.blurEdge
import com.m3u.ui.helper.LocalHelper
import com.m3u.ui.helper.Metadata
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import com.m3u.ui.helper.useRailNav
import kotlin.math.roundToInt

@Composable
fun HeadlineBackground(modifier: Modifier = Modifier) {
val context = LocalContext.current
val configuration = LocalConfiguration.current
val helper = LocalHelper.current

val preferences = hiltPreferences()

val useDarkTheme =
preferences.darkMode || (preferences.followSystemTheme && isSystemInDarkTheme())

val colorScheme = MaterialTheme.colorScheme
val url = Metadata.headlineUrl
val fraction = Metadata.headlineFraction

val headlineAspectRatio = Metadata.headlineAspectRatio(helper.useRailNav)

val currentMaskColor by animateColorAsState(
targetValue = lerp(
start = if (useDarkTheme) Color.Black.copy(0.56f)
Expand All @@ -57,11 +53,6 @@ fun HeadlineBackground(modifier: Modifier = Modifier) {
)

if (!preferences.noPictureMode) {
LaunchedEffect(Unit) {
snapshotFlow { Metadata.headlineFraction }
.onEach { Log.e("TAG", "$it") }
.launchIn(this)
}
AsyncImage(
model = remember(url) {
ImageRequest.Builder(context)
Expand All @@ -80,21 +71,16 @@ fun HeadlineBackground(modifier: Modifier = Modifier) {
.offset {
IntOffset(
x = 0,
y = ((configuration.screenWidthDp * Metadata.HEADLINE_ASPECT_RATIO) * -fraction).roundToInt()
y = ((configuration.screenWidthDp * headlineAspectRatio) * -fraction).roundToInt()
)
}
.aspectRatio(Metadata.HEADLINE_ASPECT_RATIO)
.aspectRatio(headlineAspectRatio)
.drawWithContent {
drawContent()
if (url.isNotEmpty()) {
drawRect(color = currentMaskColor, size = size)
}
}
.blurEdge(
color = colorScheme.background,
edge = Edge.Bottom,
dimen = 256f
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import com.m3u.i18n.R.string
import com.m3u.material.ktx.plus
import com.m3u.material.model.LocalHazeState
import com.m3u.material.model.LocalSpacing
import com.m3u.ui.helper.LocalHelper
import com.m3u.ui.helper.Metadata
import com.m3u.ui.helper.useRailNav
import dev.chrisbanes.haze.HazeDefaults
import dev.chrisbanes.haze.haze
import kotlinx.coroutines.flow.launchIn
Expand All @@ -55,8 +57,12 @@ internal fun PlaylistGallery(
) {
val spacing = LocalSpacing.current
val configuration = LocalConfiguration.current
val helper = LocalHelper.current

val colorScheme = MaterialTheme.colorScheme

val headlineAspectRatio = Metadata.headlineAspectRatio(helper.useRailNav)

val state = rememberLazyGridState()
val viewportStartOffset by remember {
derivedStateOf { state.layoutInfo.visibleItemsInfo.firstOrNull()?.offset?.y ?: 0 }
Expand All @@ -73,7 +79,7 @@ internal fun PlaylistGallery(
snapshotFlow { viewportStartOffset }
.onEach {
val fraction = (it.absoluteValue /
(configuration.screenWidthDp * Metadata.HEADLINE_ASPECT_RATIO))
(configuration.screenWidthDp * headlineAspectRatio))
.coerceIn(0f, 1f)
Metadata.headlineFraction = fraction
}
Expand Down
38 changes: 5 additions & 33 deletions material/src/main/java/com/m3u/material/ktx/Coils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,9 @@ import android.renderscript.RenderScript
import android.renderscript.ScriptIntrinsicBlur
import androidx.core.graphics.applyCanvas
import androidx.core.graphics.createBitmap
import androidx.core.graphics.withScale
import androidx.core.graphics.withTranslation
import coil.size.Size
import coil.transform.Transformation

class ScaleIfHasAlphaTransformation(
private val scaleX: Float,
private val scaleY: Float = scaleX
) : Transformation {
override val cacheKey: String = "${javaClass.name}_${scaleX}_${scaleY}"

@Suppress("USELESS_ELVIS")
override suspend fun transform(
input: Bitmap,
size: Size
): Bitmap {
if (!input.hasAlpha()) return input
val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG)
val width = input.width
val height = input.height
return createBitmap(
width = width,
height = height,
config = input.config ?: Bitmap.Config.ARGB_8888
).applyCanvas {
withScale(scaleX, scaleY) {
withTranslation(width * (1 - scaleX) / 2, height * (1 - scaleY) / 2) {
drawBitmap(input, 0f, 0f, paint)
}
}
}
}
}

@Suppress("DEPRECATION")
class BlurTransformation @JvmOverloads constructor(
private val context: Context,
Expand All @@ -63,8 +32,11 @@ class BlurTransformation @JvmOverloads constructor(

val scaledWidth = (input.width / sampling).toInt()
val scaledHeight = (input.height / sampling).toInt()
val output =
createBitmap(scaledWidth, scaledHeight, input.config ?: Bitmap.Config.ARGB_8888)
val output = createBitmap(
scaledWidth,
scaledHeight,
input.config ?: Bitmap.Config.ARGB_8888
)
output.applyCanvas {
scale(1 / sampling, 1 / sampling)
drawBitmap(input, 0f, 0f, paint)
Expand Down
3 changes: 2 additions & 1 deletion ui/src/main/java/com/m3u/ui/helper/Metadata.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ object Metadata {
var color: Color by mutableStateOf(Color.Unspecified)
var contentColor: Color by mutableStateOf(Color.Unspecified)

const val HEADLINE_ASPECT_RATIO = 7 / 3f
fun headlineAspectRatio(rail: Boolean): Float = if (rail) 14 / 3f
else 7 / 3f
}

0 comments on commit f467151

Please sign in to comment.