Skip to content

Commit

Permalink
Fix folder size on large screens (#2230)
Browse files Browse the repository at this point in the history
  • Loading branch information
geekygecko authored May 15, 2024
1 parent 6672fc9 commit 23c1c40
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import au.com.shiftyjelly.pocketcasts.ui.extensions.getThemeColor
import au.com.shiftyjelly.pocketcasts.ui.extensions.themed
import au.com.shiftyjelly.pocketcasts.ui.theme.Theme
import au.com.shiftyjelly.pocketcasts.utils.extensions.dpToPx
import au.com.shiftyjelly.pocketcasts.utils.extensions.pxToDp
import au.com.shiftyjelly.pocketcasts.utils.featureflag.Feature
import au.com.shiftyjelly.pocketcasts.utils.featureflag.FeatureFlag
import au.com.shiftyjelly.pocketcasts.views.adapter.FolderItemDiffCallback
Expand Down Expand Up @@ -95,11 +94,9 @@ class FolderAdapter(

FolderItem.Folder.viewTypeId -> {
val podcastsLayout = settings.podcastGridLayout.value
val gridWidthDp = UiUtil.getGridImageWidthPx(smallArtwork = podcastsLayout == PodcastGridLayoutType.SMALL_ARTWORK, context = context).pxToDp(parent.context).toInt()
FolderViewHolder(
composeView = ComposeView(parent.context),
theme = theme,
gridWidthDp = gridWidthDp,
podcastsLayout = podcastsLayout,
onFolderClick = { clickListener.onFolderClick(it.uuid, isUserInitiated = true) },
podcastGridLayout = podcastsLayout,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package au.com.shiftyjelly.pocketcasts.podcasts.view.podcasts

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.unit.dp
import androidx.recyclerview.widget.RecyclerView
import au.com.shiftyjelly.pocketcasts.compose.AppTheme
import au.com.shiftyjelly.pocketcasts.compose.components.HorizontalDivider
Expand All @@ -24,7 +23,6 @@ import kotlin.math.min
class FolderViewHolder(
val composeView: ComposeView,
val theme: Theme,
val gridWidthDp: Int,
val podcastsLayout: PodcastGridLayoutType,
val onFolderClick: (Folder) -> Unit,
val podcastGridLayout: PodcastGridLayoutType,
Expand Down Expand Up @@ -57,7 +55,7 @@ class FolderViewHolder(
badgeType = badgeType,
podcastGridLayout = podcastGridLayout,
onClick = { onFolderClick(folder) },
modifier = Modifier.size(gridWidthDp.dp),
modifier = Modifier.fillMaxSize(),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ class PodcastsFragment : BaseFragment(), FolderAdapter.ClickListener, PodcastTou
private fun adjustViewIfNeeded() {
val context = activity ?: return
val orientation = resources.configuration.orientation
val widthPx = UiUtil.getContentViewWidthPx(context)
val widthPx = UiUtil.getWindowWidthPx(context)
if (orientation == lastOrientationRefreshed && lastWidthPx == widthPx) return

// screen has rotated, redraw the grid to the right size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,15 @@ fun FolderImage(
val elevation = if (FeatureFlag.isEnabled(Feature.PODCASTS_GRID_VIEW_DESIGN_CHANGES)) 1.dp else 0.dp
BoxWithConstraints(
contentAlignment = Alignment.Center,
modifier = modifier,
) {
val constraints = this
Card(
elevation = elevation,
shape = RoundedCornerShape(cornerRadius),
backgroundColor = color,
modifier = modifier
modifier = Modifier
.fillMaxSize()
.aspectRatio(1f),
) {
Box(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package au.com.shiftyjelly.pocketcasts.views.helper

import android.content.Context
import android.content.res.Configuration
import android.view.Menu
import android.view.View
import android.view.inputmethod.InputMethodManager
Expand All @@ -15,8 +14,6 @@ import au.com.shiftyjelly.pocketcasts.localization.R as LR

object UiUtil {

private const val MINIMUM_DIMENSION_DP_FOR_NAVIGATION_DRAWER = 600

fun hideKeyboard(view: View) {
val imm = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, 0)
Expand Down Expand Up @@ -86,14 +83,13 @@ object UiUtil {

fun getGridColumnCount(smallArtwork: Boolean, context: Context?): Int {
context ?: return 1
val contentViewWidthDp = getContentViewWidthDp(context)
val isDrawerHidden = isNavigationDrawerHidden(context)
return getGridColumnCount(contentViewWidthDp, context.resources.configuration.inPortrait(), isDrawerHidden, smallArtwork)
val contentViewWidthDp = getWindowWidthDp(context)
return getGridColumnCount(contentViewWidthDp, context.resources.configuration.inPortrait(), smallArtwork)
}

fun getDiscoverGridColumnCount(context: Context?): Int {
context ?: return 2
val contentViewWidthDp = getContentViewWidthDp(context)
val contentViewWidthDp = getWindowWidthDp(context)
val inPortrait = context.resources.configuration.inPortrait()
return if (inPortrait) {
when {
Expand All @@ -113,45 +109,22 @@ object UiUtil {
fun getDiscoverGridImageWidthPx(context: Context): Int {
val columns = getDiscoverGridColumnCount(context)
val padding = (columns + 1) * (16f * context.resources.displayMetrics.density)
return ((getContentViewWidthPx(context) - padding) / columns).toInt()
return ((getWindowWidthPx(context) - padding) / columns).toInt()
}

fun getWindowWidthPx(context: Context): Int {
return context.resources.displayMetrics.widthPixels
}

fun getWindowWidthDp(context: Context): Int {
val displayMetrics = context.resources.displayMetrics
return (displayMetrics.widthPixels * displayMetrics.density).toInt()
}

fun getWindowHeightPx(context: Context): Int {
return context.resources.displayMetrics.heightPixels
}

fun getWindowHeightDp(context: Context): Int {
val displayMetrics = context.resources.displayMetrics
return (displayMetrics.heightPixels * displayMetrics.density).toInt()
}

fun getContentViewWidthPx(context: Context): Int {
var windowWidthPx = getWindowWidthPx(context)
val drawerHidden = isNavigationDrawerHidden(context)
if (!drawerHidden) {
windowWidthPx -= getNavigationDrawerWidthPx(context)
}
return windowWidthPx
}

fun getContentViewWidthDp(context: Context): Int {
return (getContentViewWidthPx(context) / getDensity(context)).toInt()
return (getWindowWidthPx(context) / getDensity(context)).toInt()
}

fun getDensity(context: Context): Float {
return context.resources.displayMetrics.density
}

private fun getGridColumnCount(contentViewWidthDp: Int, portrait: Boolean, isNavigationDrawerHidden: Boolean, smallArtwork: Boolean): Int {
private fun getGridColumnCount(contentViewWidthDp: Int, portrait: Boolean, smallArtwork: Boolean): Int {
val num: Int

if (portrait) {
Expand All @@ -163,59 +136,27 @@ object UiUtil {
num = if (smallArtwork) 6 else 5
}
} else {
if (isNavigationDrawerHidden) {
num = if (smallArtwork) 6 else 5
} else {
if (contentViewWidthDp > 700) {
num = if (smallArtwork) 7 else 6
} else {
num = if (smallArtwork) 6 else 5
}
}
num = if (smallArtwork) 6 else 5
}

return num
}

fun getGridImageWidthPx(smallArtwork: Boolean, context: Context): Int {
val columnCount = getGridColumnCount(smallArtwork, context)
val contentWidthPx = getContentViewWidthPx(context)
val contentWidthPx = getWindowWidthPx(context)
val spacingWidth = if (FeatureFlag.isEnabled(Feature.PODCASTS_GRID_VIEW_DESIGN_CHANGES)) {
2 * (columnCount * context.resources.getDimensionPixelSize(R.dimen.grid_item_padding) + context.resources.getDimensionPixelSize(R.dimen.grid_outer_padding))
// add the spacing between the columns and the padding on the sides of the grid
val resources = context.resources
val gridItemPadding = resources.getDimensionPixelSize(R.dimen.grid_item_padding)
val gridOuterPadding = resources.getDimensionPixelSize(R.dimen.grid_outer_padding)
((columnCount - 1) * gridItemPadding) + (2 * gridOuterPadding)
} else {
0
}
return ((contentWidthPx.toFloat() - spacingWidth) / columnCount).toInt()
}

fun isNavigationDrawerHidden(context: Context): Boolean {
val config = context.resources.configuration
val displayMetrics = context.resources.displayMetrics
val width = (displayMetrics.widthPixels / displayMetrics.density).toInt()
val height = (displayMetrics.heightPixels / displayMetrics.density).toInt()
return width < MINIMUM_DIMENSION_DP_FOR_NAVIGATION_DRAWER || height < MINIMUM_DIMENSION_DP_FOR_NAVIGATION_DRAWER || config.orientation == Configuration.ORIENTATION_PORTRAIT
}

fun getNavigationDrawerWidthPx(context: Context): Int {
return getNavigationDrawerWidthPx(getWindowWidthPx(context), getDensity(context))
}

private fun getNavigationDrawerWidthPx(windowWidthPx: Int, density: Float): Int {
val windowWidthDp = (windowWidthPx / density).toInt()

val defaultDrawerWidthDp = 330
val defaultDrawerWidthPx = (defaultDrawerWidthDp * density).toInt()

// thin screen use most of the screen
if (windowWidthDp < 350) {
return (windowWidthPx * 0.9f).toInt()
} else if (windowWidthDp < 370) {
return (windowWidthPx * 0.85f).toInt()
}

return defaultDrawerWidthPx
}

fun displayDialogNoEmailApp(context: Context) {
displayAlertError(context, context.getString(LR.string.settings_no_email_app_title), context.getString(LR.string.settings_no_email_app), null)
}
Expand Down

0 comments on commit 23c1c40

Please sign in to comment.