Skip to content

Commit bbb4f9a

Browse files
article global feed working
Signed-off-by: Arnav Gupta <[email protected]>
1 parent 3b24add commit bbb4f9a

File tree

5 files changed

+135
-14
lines changed

5 files changed

+135
-14
lines changed

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package io.realworld.android.ui.feed
2+
3+
import android.graphics.Color
4+
import android.graphics.drawable.ColorDrawable
5+
import android.view.LayoutInflater
6+
import android.view.View
7+
import android.view.ViewGroup
8+
import androidx.recyclerview.widget.DiffUtil
9+
import androidx.recyclerview.widget.ListAdapter
10+
import androidx.recyclerview.widget.RecyclerView
11+
import io.realworld.android.R
12+
import io.realworld.android.databinding.ListItemArticleBinding
13+
import io.realworld.api.models.entities.Article
14+
15+
class ArticleFeedAdapter : ListAdapter<Article, ArticleFeedAdapter.ArticleViewHolder>(
16+
object : DiffUtil.ItemCallback<Article>() {
17+
override fun areItemsTheSame(oldItem: Article, newItem: Article): Boolean {
18+
return oldItem == newItem
19+
}
20+
21+
override fun areContentsTheSame(oldItem: Article, newItem: Article): Boolean {
22+
return oldItem.toString() == newItem.toString()
23+
}
24+
}
25+
) {
26+
27+
28+
inner class ArticleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
29+
30+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ArticleViewHolder {
31+
return ArticleViewHolder(
32+
parent.context.getSystemService(LayoutInflater::class.java).inflate(
33+
R.layout.list_item_article,
34+
parent,
35+
false
36+
)
37+
)
38+
}
39+
40+
override fun onBindViewHolder(holder: ArticleViewHolder, position: Int) {
41+
ListItemArticleBinding.bind(holder.itemView).apply {
42+
val article = getItem(position)
43+
44+
authorTextView.text = article.author.username
45+
titleTextView.text = article.title
46+
bodySnippetTextView.text = article.body
47+
dateTextView.text = "December 15, 2020"
48+
avatarImageView.background = ColorDrawable(Color.GRAY)
49+
50+
}
51+
52+
}
53+
54+
55+
}

app/src/main/java/io/realworld/android/ui/feed/GlobalFeedFragment.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,29 @@ import android.view.View
66
import android.view.ViewGroup
77
import androidx.fragment.app.Fragment
88
import androidx.lifecycle.ViewModelProvider
9+
import androidx.recyclerview.widget.LinearLayoutManager
910
import io.realworld.android.databinding.FragmentFeedBinding
1011

1112
class GlobalFeedFragment : Fragment() {
1213

1314
private var _binding: FragmentFeedBinding? = null
1415
private lateinit var viewModel: FeedViewModel
16+
private lateinit var feedAdapter: ArticleFeedAdapter
1517

1618
override fun onCreateView(
1719
inflater: LayoutInflater,
1820
container: ViewGroup?,
1921
savedInstanceState: Bundle?
2022
): View? {
2123
viewModel = ViewModelProvider(this).get(FeedViewModel::class.java)
24+
feedAdapter = ArticleFeedAdapter()
2225

2326
_binding = FragmentFeedBinding.inflate(inflater, container, false)
24-
25-
_binding?.fetchFeedButton?.setOnClickListener {
26-
viewModel.fetchGlobalFeed()
27+
_binding?.feedRecyclerView?.layoutManager = LinearLayoutManager(context)
28+
_binding?.feedRecyclerView?.adapter = feedAdapter
29+
viewModel.fetchGlobalFeed()
30+
viewModel.feed.observe({ lifecycle }) {
31+
feedAdapter.submitList(it)
2732
}
2833

2934
return _binding?.root

app/src/main/res/layout/fragment_feed.xml

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,9 @@
33
android:layout_width="match_parent"
44
android:layout_height="match_parent">
55

6-
<Button
7-
android:id="@+id/fetchFeedButton"
8-
android:text="Fetch Feed"
9-
android:layout_width="wrap_content"
10-
android:layout_height="wrap_content"/>
11-
12-
13-
<!-- <androidx.recyclerview.widget.RecyclerView-->
14-
<!-- android:id="@+id/feedRecyclerView"-->
15-
<!-- android:layout_width="match_parent"-->
16-
<!-- android:layout_height="match_parent" />-->
6+
<androidx.recyclerview.widget.RecyclerView
7+
android:id="@+id/feedRecyclerView"
8+
android:layout_width="match_parent"
9+
android:layout_height="match_parent" />
1710

1811
</FrameLayout>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="wrap_content"
7+
android:orientation="vertical"
8+
android:padding="24dp">
9+
10+
<ImageView
11+
android:id="@+id/avatarImageView"
12+
android:layout_width="32dp"
13+
android:layout_height="33dp"
14+
app:layout_constraintStart_toStartOf="parent"
15+
app:layout_constraintTop_toTopOf="parent"
16+
tools:srcCompat="@tools:sample/avatars" />
17+
18+
<TextView
19+
android:id="@+id/titleTextView"
20+
android:layout_width="wrap_content"
21+
android:layout_height="wrap_content"
22+
android:layout_marginTop="8dp"
23+
android:textAppearance="@style/TextAppearance.AppCompat.Subhead"
24+
app:layout_constraintStart_toStartOf="parent"
25+
app:layout_constraintTop_toBottomOf="@+id/avatarImageView"
26+
tools:text="A great article" />
27+
28+
<TextView
29+
android:id="@+id/bodySnippetTextView"
30+
android:layout_width="match_parent"
31+
android:layout_height="wrap_content"
32+
android:maxLines="3"
33+
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
34+
app:layout_constraintEnd_toEndOf="parent"
35+
app:layout_constraintStart_toStartOf="parent"
36+
app:layout_constraintTop_toBottomOf="@+id/titleTextView"
37+
tools:text="This is a sample article body to show in the preview of the list item. Long enough text for multiple lines" />
38+
39+
<TextView
40+
android:id="@+id/dateTextView"
41+
android:layout_width="wrap_content"
42+
android:layout_height="wrap_content"
43+
android:fontFamily="sans-serif-light"
44+
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
45+
android:textSize="12sp"
46+
app:layout_constraintStart_toStartOf="@+id/authorTextView"
47+
app:layout_constraintTop_toBottomOf="@+id/authorTextView"
48+
tools:text="December 15, 2020" />
49+
50+
<TextView
51+
android:id="@+id/authorTextView"
52+
android:layout_width="wrap_content"
53+
android:layout_height="wrap_content"
54+
android:layout_marginStart="8dp"
55+
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
56+
android:textSize="12sp"
57+
android:textStyle="bold"
58+
app:layout_constraintStart_toEndOf="@+id/avatarImageView"
59+
app:layout_constraintTop_toTopOf="parent"
60+
tools:text="author" />
61+
62+
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)