Skip to content

Commit

Permalink
Fix Grid View
Browse files Browse the repository at this point in the history
  • Loading branch information
brahmkshatriya committed Sep 22, 2024
1 parent 598feea commit 57819ce
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,10 @@ class GridViewHolder(
ItemShelfMediaGridBinding.inflate(inflater, parent, false)
)
}

fun <T> Shelf.Lists<*>.ifGrid(block: (Shelf.Lists<*>) -> T): T? {
return if (type == Shelf.Lists.Type.Grid) block(this)
else null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.recyclerview.widget.DiffUtil
import dev.brahmkshatriya.echo.common.models.EchoMediaItem
import dev.brahmkshatriya.echo.common.models.Shelf
import dev.brahmkshatriya.echo.common.models.Track
import dev.brahmkshatriya.echo.ui.adapter.ShowButtonViewHolder.Companion.ifShowingButton

class ShelfListItemViewAdapter(
private val clientId: String,
Expand All @@ -15,8 +16,9 @@ class ShelfListItemViewAdapter(
) : LifeCycleListAdapter<Any, ShelfListItemViewHolder>(DiffCallback) {

init {
if (shelf !is Shelf.Lists.Tracks) submitList(shelf.list)
else ShowButtonViewHolder.initialize(this, shelf)
shelf.ifShowingButton {
ShowButtonViewHolder.initialize(this, it)
} ?: submitList(shelf.list)
}

object DiffCallback : DiffUtil.ItemCallback<Any>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import androidx.core.view.isVisible
import androidx.core.view.updatePaddingRelative
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexboxLayoutManager
import com.google.android.flexbox.JustifyContent.SPACE_BETWEEN
import dev.brahmkshatriya.echo.common.models.EchoMediaItem
import dev.brahmkshatriya.echo.common.models.Shelf
import dev.brahmkshatriya.echo.databinding.ItemShelfCategoryBinding
import dev.brahmkshatriya.echo.databinding.ItemShelfListsBinding
import dev.brahmkshatriya.echo.databinding.ItemShelfMediaBinding
import dev.brahmkshatriya.echo.ui.adapter.GridViewHolder.Companion.ifGrid
import dev.brahmkshatriya.echo.ui.adapter.MediaItemViewHolder.Companion.bind
import dev.brahmkshatriya.echo.ui.adapter.ShowButtonViewHolder.Companion.ifShowingButton
import dev.brahmkshatriya.echo.utils.dpToPx
import java.lang.ref.WeakReference

Expand All @@ -41,36 +43,27 @@ sealed class ShelfViewHolder(
binding.subtitle.text = item.subtitle
binding.subtitle.isVisible = item.subtitle.isNullOrBlank().not()
binding.more.isVisible = item.more != null
binding.shuffle.isVisible = item is Shelf.Lists.Tracks

binding.shuffle.isVisible = if (item is Shelf.Lists.Tracks) {
binding.shuffle.setOnClickListener {
listener.onShuffleClick(clientId, item)
}
true
} else false
binding.recyclerView.setRecycledViewPool(sharedPool)
val position = bindingAdapterPosition
val transition = transitionView.transitionName + item.id
val context = binding.root.context
val layoutManager = when (item.type) {
Shelf.Lists.Type.Grid -> {
FlexboxLayoutManager(context).apply {
flexDirection = FlexDirection.COLUMN
}
}

Shelf.Lists.Type.Linear -> when (item) {
is Shelf.Lists.Tracks -> {
binding.recyclerView.updatePaddingRelative(start = 0, end = 0)
binding.shuffle.setOnClickListener {
listener.onShuffleClick(clientId, item)
}
LinearLayoutManager(context, RecyclerView.VERTICAL, false)
}

else -> {
val padding = 16.dpToPx(context)
binding.recyclerView.updatePaddingRelative(start = padding, end = padding)
LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
}
}
}

val (layoutManager, padding) = item.ifGrid {
FlexboxLayoutManager(context).apply {
justifyContent = SPACE_BETWEEN
} to 16.dpToPx(context)
} ?: item.ifShowingButton {
LinearLayoutManager(context, RecyclerView.VERTICAL, false) to 0
} ?: (LinearLayoutManager(
context, RecyclerView.HORIZONTAL, false
) to 16.dpToPx(context))

binding.recyclerView.updatePaddingRelative(start = padding, end = padding)
layoutManager.apply {
val state: Parcelable? = viewModel.layoutManagerStates[position]
if (state != null) onRestoreInstanceState(state)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,10 @@ class ShowButtonViewHolder(
adapter.submitList(list + false)
}
}

fun <T> Shelf.ifShowingButton(block: (Shelf.Lists.Tracks) -> T): T? {
return if (this is Shelf.Lists.Tracks && type == Shelf.Lists.Type.Linear) block(this)
else null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ inline fun <reified T> Fragment.configureFeedUI(
clientId: String? = null
) {


FastScrollerHelper.applyTo(recyclerView)
swipeRefresh.configure {
viewModel.refresh(true)
Expand Down
9 changes: 6 additions & 3 deletions app/src/main/res/layout/item_shelf_media_grid.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
style="@style/Selectable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:minHeight="64dp"
android:layout_marginEnd="-8dp"
app:contentPadding="8dp"
app:cardCornerRadius="8dp"
app:cardBackgroundColor="@android:color/transparent">

<LinearLayout
Expand All @@ -19,6 +20,7 @@
<androidx.cardview.widget.CardView
android:layout_width="64dp"
android:layout_height="64dp"
app:cardCornerRadius="8dp"
app:cardBackgroundColor="?attr/colorSurfaceContainer">

<ImageView
Expand Down Expand Up @@ -64,7 +66,8 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="16dp"
android:layout_marginTop="-8dp"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:orientation="vertical">

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/new_item_media_title.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
android:ellipsize="end"
android:gravity="center_vertical"
android:maxLines="2"
tools:text="@tools:sample/lorem/random" />
tools:text="@tools:sample/full_names" />

<TextView
android:id="@+id/subtitle"
Expand Down

0 comments on commit 57819ce

Please sign in to comment.