Skip to content

Commit

Permalink
feed: improve binding and introduce StoryCard
Browse files Browse the repository at this point in the history
  • Loading branch information
iTaysonLab committed Apr 18, 2020
1 parent ee08fee commit feee0e8
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.recyclerview.widget.RecyclerView
import ua.itaysonlab.hfsdk.FeedItem
import ua.itaysonlab.hfsdk.FeedItemType
import ua.itaysonlab.homefeeder.R
import ua.itaysonlab.homefeeder.overlay.feed.binders.StoryCardBinder
import ua.itaysonlab.homefeeder.overlay.feed.binders.TextCardBinder
import ua.itaysonlab.homefeeder.overlay.feed.binders.TextCardWithActionsBinder

Expand Down Expand Up @@ -39,29 +40,27 @@ class FeedAdapter: RecyclerView.Adapter<FeedAdapter.FeedViewHolder>() {
override fun getItemCount() = list.size

override fun getItemViewType(position: Int): Int {
return when (list[position].type) {
FeedItemType.TEXT_CARD -> 1
FeedItemType.TEXT_CARD_ACTIONS -> 2
}
return (list[position].type.ordinal)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FeedViewHolder {
if (!::layoutInflater.isInitialized) layoutInflater = LayoutInflater.from(parent.context)

val layoutResource = when (viewType) {
1 -> R.layout.notification_simple
2 -> R.layout.feed_card_text
else -> R.layout.notification_simple
val layoutResource = when (FeedItemType.values()[viewType]) {
FeedItemType.TEXT_CARD -> R.layout.notification_simple
FeedItemType.TEXT_CARD_ACTIONS -> R.layout.feed_card_text
FeedItemType.STORY_CARD -> R.layout.feed_card_story_large
}

return FeedViewHolder(viewType, layoutInflater.inflate(layoutResource, parent, false))
}

override fun onBindViewHolder(holder: FeedViewHolder, position: Int) {
val item = list[position]
when (holder.type) {
1 -> TextCardBinder.bind(theme, item, holder.itemView)
2 -> TextCardWithActionsBinder.bind(theme, item, holder.itemView)
when (FeedItemType.values()[holder.type]) {
FeedItemType.TEXT_CARD -> TextCardBinder.bind(theme, item, holder.itemView)
FeedItemType.TEXT_CARD_ACTIONS -> TextCardWithActionsBinder.bind(theme, item, holder.itemView)
FeedItemType.STORY_CARD -> StoryCardBinder.bind(theme, item, holder.itemView)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ua.itaysonlab.homefeeder.overlay.feed.binders

import android.text.Html
import android.util.SparseIntArray
import android.view.View
import coil.api.load
import kotlinx.android.synthetic.main.feed_card_story_large.view.*
import ua.itaysonlab.hfsdk.FeedItem
import ua.itaysonlab.hfsdk.content.StoryCardContent

object StoryCardBinder: FeedBinder {
override fun bind(theme: SparseIntArray?, item: FeedItem, view: View) {
val content = item.content as StoryCardContent

view.story_title.text = content.title
view.story_source.text = content.source.title
view.story_desc.text = Html.fromHtml(content.text)

view.story_pic.load(content.background_url)
}
}
83 changes: 38 additions & 45 deletions app/src/main/res/layout/feed_card_story_large.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http:https://schemas.android.com/apk/res/android"
xmlns:app="http:https://schemas.android.com/apk/res-auto"
xmlns:tools="http:https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http:https://schemas.android.com/tools"
android:layout_marginStart="@dimen/rl_margin"
android:layout_marginBottom="8dp"
android:layout_marginEnd="@dimen/rl_margin"
android:layout_marginBottom="8dp"
app:cardBackgroundColor="@color/cardBg"
app:cardCornerRadius="8dp">

<androidx.constraintlayout.widget.ConstraintLayout
android:minHeight="160dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:minHeight="160dp">

<ImageView
android:id="@+id/story_pic"
Expand All @@ -26,63 +27,55 @@
tools:src="@tools:sample/backgrounds/scenic" />

<View
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="@drawable/gradient"
android:backgroundTint="?android:windowBackground"
android:id="@+id/story_dimmer"
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/story_dimmer" />

<LinearLayout
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:paddingTop="8dp"
app:layout_constraintTop_toTopOf="parent"
android:background="@drawable/gradient"
android:backgroundTint="?android:windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textAppearance="@style/TextAppearance.MaterialComponents.Overline"
android:id="@+id/story_source"
android:textSize="16sp"
tools:text="Story Source"
android:textColor="?android:textColorSecondary"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
app:layout_constraintTop_toTopOf="parent" />

<LinearLayout
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:paddingBottom="12dp"
android:orientation="vertical"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="vertical"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:paddingBottom="12dp"
android:paddingTop="12dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="@+id/story_title"
android:textSize="24sp"
tools:text="Story Title"
android:textStyle="bold"
android:textColor="?android:textColorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/story_desc"
android:textSize="16sp"
tools:text="Story Desc"
android:id="@+id/story_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="?android:textColorPrimary"
android:textSize="24sp"
android:textStyle="bold"
tools:text="Story Title" />

<TextView
android:id="@+id/story_source"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="?android:textColorSecondary"
android:textSize="13sp"
tools:text="Story Source" />

<TextView
android:layout_marginTop="8dp"
android:id="@+id/story_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
android:textColor="?android:textColorSecondary"
android:textSize="16sp"
tools:text="Story Desc" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>

0 comments on commit feee0e8

Please sign in to comment.