Skip to content

Commit

Permalink
Fix padding
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrave-dev committed Jul 23, 2024
1 parent a266350 commit 933b67c
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 37 deletions.
4 changes: 2 additions & 2 deletions .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 38 additions & 8 deletions app/src/main/java/com/maxrave/simpmusic/extension/UIExt.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.maxrave.simpmusic.extension

import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.graphics.Point
import android.os.Build
import android.util.Log
import android.view.View
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.infiniteRepeatable
Expand Down Expand Up @@ -30,9 +35,9 @@ import androidx.compose.ui.graphics.drawscope.ContentDrawScope
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.graphics.ColorUtils
import com.kmpalette.palette.graphics.Palette
Expand All @@ -46,7 +51,6 @@ import kotlin.math.atan2
import kotlin.math.cos
import kotlin.math.hypot
import kotlin.math.max
import kotlin.math.roundToInt
import kotlin.math.sin
import kotlin.random.Random

Expand Down Expand Up @@ -249,18 +253,44 @@ enum class GradientAngle {
CW0, CW45, CW90, CW135, CW180, CW225, CW270, CW315
}

fun Context.getActivityOrNull(): Activity? {
var context = this
while (context is ContextWrapper) {
if (context is Activity) return context
context = context.baseContext
}

return null
}

@Composable
fun getScreenSizeInfo(): ScreenSizeInfo {
val context = LocalContext.current
val activity = context.getActivityOrNull()
val configuration = LocalConfiguration.current
val localDensity = LocalDensity.current

return remember(configuration) {
ScreenSizeInfo(
hDP = configuration.screenHeightDp,
wDP = configuration.screenWidthDp,
hPX = with(localDensity) { configuration.screenHeightDp.dp.toPx().roundToInt() },
wPX = with(localDensity) { configuration.screenWidthDp.dp.toPx().roundToInt() },
)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val windowMetrics = activity?.windowManager?.currentWindowMetrics
Log.w("getScreenSizeInfo", "WindowMetrics: ${windowMetrics?.bounds?.height()}")
ScreenSizeInfo(
hDP = with(localDensity) { (windowMetrics?.bounds?.height())?.toDp()?.value?.toInt() ?: 0 },
wDP = with(localDensity) { (windowMetrics?.bounds?.height())?.toDp()?.value?.toInt() ?: 0 },
hPX = windowMetrics?.bounds?.height() ?: 0,
wPX = windowMetrics?.bounds?.width() ?: 0
)
} else {
val point = Point()
activity?.windowManager?.defaultDisplay?.getRealSize(point)
Log.w("getScreenSizeInfo", "WindowMetrics: ${point.y}")
ScreenSizeInfo(
hDP = with(localDensity) { point.y.toDp().value.toInt() },
wDP = with(localDensity) { point.x.toDp().value.toInt() },
hPX = point.y,
wPX = point.x
)
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/maxrave/simpmusic/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import androidx.core.os.LocaleListCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.media3.common.MediaItem
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.offline.DownloadService
import androidx.navigation.findNavController
Expand Down Expand Up @@ -272,6 +273,9 @@ class MainActivity : AppCompatActivity() {
}
}
}
if (viewModel.nowPlayingState.value?.mediaItem == MediaItem.EMPTY || viewModel.nowPlayingState.value?.mediaItem == null) {
binding.miniplayer.visibility = View.GONE
}
binding.root.addOnLayoutChangeListener {
v,
left,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ class NowPlayingFragment : Fragment() {

composeView.apply {
setContent {
val activity = requireActivity()
activity.window.navigationBarColor = Color.TRANSPARENT
AppTheme {
Scaffold { paddingValues ->
NowPlayingScreen(sharedViewModel = viewModel, navController = findNavController())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,36 +203,34 @@ fun NowPlayingScreen(
}

//Height
var topAppBarHeightPx by rememberSaveable {
var topAppBarHeightDp by rememberSaveable {
mutableIntStateOf(0)
}
var middleLayoutHeightPx by rememberSaveable {
var middleLayoutHeightDp by rememberSaveable {
mutableIntStateOf(0)
}
var infoLayoutHeightPx by rememberSaveable {
var infoLayoutHeightDp by rememberSaveable {
mutableIntStateOf(0)
}
var middleLayoutPaddingPx by rememberSaveable {
var middleLayoutPaddingDp by rememberSaveable {
mutableIntStateOf(0)
}
val minimumPaddingPx by rememberSaveable {
val minimumPaddingDp by rememberSaveable {
mutableIntStateOf(
with(localDensity) {
15.dp.toPx().toInt()
}
30
)
}
LaunchedEffect(
topAppBarHeightPx, screenInfo, infoLayoutHeightPx, minimumPaddingPx
topAppBarHeightDp, screenInfo, infoLayoutHeightDp, minimumPaddingDp
) {
if (topAppBarHeightPx > 0 && middleLayoutHeightPx > 0 && infoLayoutHeightPx > 0) {
val result = (screenInfo.hPX - topAppBarHeightPx - middleLayoutHeightPx - infoLayoutHeightPx - minimumPaddingPx) / 2
middleLayoutPaddingPx = if (result > minimumPaddingPx) {
if (topAppBarHeightDp > 0 && middleLayoutHeightDp > 0 && infoLayoutHeightDp > 0 && screenInfo.hDP > 0) {
val result = (screenInfo.hDP - topAppBarHeightDp - middleLayoutHeightDp - infoLayoutHeightDp - minimumPaddingDp) / 2
middleLayoutPaddingDp = if (result > minimumPaddingDp) {
result
} else {
minimumPaddingPx
minimumPaddingDp
}
Log.w(TAG, "MiddleLayoutPadding: $middleLayoutPaddingPx")
Log.w(TAG, "MiddleLayoutPadding: $middleLayoutPaddingDp")
Log.w(TAG, "Screen Height: ${screenInfo.hPX}")
}
}
Expand Down Expand Up @@ -495,7 +493,7 @@ fun NowPlayingScreen(
modifier = Modifier
.align(Alignment.TopCenter)
.onGloballyPositioned {
topAppBarHeightPx = it.size.height
topAppBarHeightDp = with(localDensity) {it.size.height.toDp().value.toInt()}
},
colors = TopAppBarDefaults.topAppBarColors().copy(
containerColor = Color.Transparent
Expand Down Expand Up @@ -550,7 +548,7 @@ fun NowPlayingScreen(
)
Column {
Spacer(modifier = Modifier.height(
with(localDensity) { topAppBarHeightPx.toDp() }
topAppBarHeightDp.dp
)
)
Box {
Expand All @@ -561,7 +559,7 @@ fun NowPlayingScreen(
Spacer(modifier = Modifier
.animateContentSize()
.height(
with(localDensity) { middleLayoutPaddingPx.toDp() }
middleLayoutPaddingDp.dp
)
.fillMaxWidth()
)
Expand All @@ -571,7 +569,7 @@ fun NowPlayingScreen(
.fillMaxWidth()
.padding(horizontal = 40.dp)
.onGloballyPositioned {
middleLayoutHeightPx = it.size.height
middleLayoutHeightDp = with(localDensity) {it.size.height.toDp().value.toInt()}
}
.alpha(
if (showHideMiddleLayout) 1f else 0f
Expand Down Expand Up @@ -740,7 +738,7 @@ fun NowPlayingScreen(
Spacer(modifier = Modifier
.animateContentSize()
.height(
with(localDensity) { middleLayoutPaddingPx.toDp() }
middleLayoutPaddingDp.dp
)
.fillMaxWidth()
)
Expand All @@ -752,7 +750,7 @@ fun NowPlayingScreen(
.alpha(controlLayoutAlpha)
.onGloballyPositioned {
Log.w(TAG, "InfoLayout: ${with(localDensity) { it.size.height.toDp() }}")
infoLayoutHeightPx = it.size.height
infoLayoutHeightDp = with(localDensity) {it.size.height.toDp().value.toInt()}
}
)
{
Expand Down Expand Up @@ -1144,9 +1142,7 @@ fun NowPlayingScreen(
Box(
modifier = Modifier
.height(
with(localDensity) {
infoLayoutHeightPx.toDp()
}
infoLayoutHeightDp.dp
)
.fillMaxWidth()
.padding(
Expand Down Expand Up @@ -1203,9 +1199,7 @@ fun NowPlayingScreen(
modifier = Modifier
.fillMaxWidth()
.height(
with(localDensity) {
(middleLayoutPaddingPx * 2 + middleLayoutHeightPx).toDp()
}
(middleLayoutPaddingDp * 2 + middleLayoutHeightDp).dp
)
.clickable(
onClick = {
Expand Down Expand Up @@ -1407,7 +1401,7 @@ fun NowPlayingScreen(
)
Spacer(modifier = Modifier.height(10.dp))
Text(
text = stringResource(id = R.string.view_count, String.format("%,d", screenDataState.songInfoData?.viewCount)),
text = stringResource(id = R.string.view_count, String.format(java.util.Locale.getDefault(), "%,d", screenDataState.songInfoData?.viewCount)),
style = typo.labelMedium,
color = Color.White,
)
Expand Down

0 comments on commit 933b67c

Please sign in to comment.