Skip to content

Commit

Permalink
+ Working with BindingAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Mina Mikhail committed Aug 20, 2021
1 parent eb374ec commit 1a88f19
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.mina_mikhail.newsapp.core.view.extensions
import android.view.View
import android.widget.ImageView
import androidx.constraintlayout.widget.Group
import androidx.databinding.BindingAdapter
import coil.load
import coil.transform.CircleCropTransformation
import coil.transform.RoundedCornersTransformation
Expand Down Expand Up @@ -30,6 +31,14 @@ fun View.invisible() {
}
}

@BindingAdapter("app:goneUnless")
fun View.goneUnless(visible: Boolean) {
visibility = if (visible) View.VISIBLE else View.GONE
if (this is Group) {
this.requestLayout()
}
}

fun View.enable() {
isEnabled = true
alpha = 1f
Expand All @@ -52,6 +61,7 @@ fun View.showSnackBar(message: String, retryActionName: String? = null, action:
snackBar.show()
}

@BindingAdapter("app:loadImage")
fun ImageView.loadImage(imageUrl: String?) {
if (imageUrl != null && imageUrl.isNotEmpty()) {
load(imageUrl) {
Expand All @@ -64,6 +74,7 @@ fun ImageView.loadImage(imageUrl: String?) {
}
}

@BindingAdapter("app:loadCircleImage")
fun ImageView.loadCircleImage(imageUrl: String?) {
if (imageUrl != null && imageUrl.isNotEmpty()) {
load(imageUrl) {
Expand All @@ -84,6 +95,7 @@ fun ImageView.loadCircleImage(imageUrl: String?) {
}
}

@BindingAdapter("app:loadRoundImage")
fun ImageView.loadRoundImage(imageUrl: String?) {
if (imageUrl != null && imageUrl.isNotEmpty()) {
load(imageUrl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.mina_mikhail.newsapp.features.news.domain.entity.model

import androidx.room.Entity
import androidx.room.PrimaryKey
import com.mina_mikhail.newsapp.core.utils.DateUtils
import com.mina_mikhail.newsapp.core.utils.convertDateTimeToTimesAgo
import java.io.Serializable

@Entity(tableName = "articles")
Expand All @@ -16,7 +18,11 @@ data class Article(
val url: String,
val urlToImage: String,
val source: Source
) : Serializable
) : Serializable {

val articleDateFormatted
get() = publishedAt.convertDateTimeToTimesAgo(DateUtils.FULL_DATE_TIME_FORMAT)
}

data class Source(
val name: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.mina_mikhail.newsapp.R.layout
import com.mina_mikhail.newsapp.core.utils.DateUtils
import com.mina_mikhail.newsapp.core.utils.convertDateTimeToTimesAgo
import com.mina_mikhail.newsapp.core.view.extensions.loadRoundImage
import com.mina_mikhail.newsapp.databinding.ItemNewsBinding
import com.mina_mikhail.newsapp.features.news.domain.entity.model.Article
import com.mina_mikhail.newsapp.features.news.presentation.NewsAdapter.ArticlesViewHolder
Expand Down Expand Up @@ -52,7 +49,7 @@ class NewsAdapter(private var itemClick: (Article) -> Unit) : ListAdapter<Articl
private var currentItem: Article? = null

init {
binding.item.setOnClickListener {
binding.llItem.setOnClickListener {
currentItem?.let {
itemClick(it)
}
Expand All @@ -62,10 +59,7 @@ class NewsAdapter(private var itemClick: (Article) -> Unit) : ListAdapter<Articl
fun bind(item: Article) {
currentItem = item

binding.ivArticleImage.loadRoundImage(item.urlToImage)
binding.tvArticleDate.text = item.publishedAt.convertDateTimeToTimesAgo(DateUtils.FULL_DATE_TIME_FORMAT)
binding.tvArticleTitle.text = item.title
binding.tvArticleDescription.text = item.description
binding.item = currentItem
}
}
}
20 changes: 15 additions & 5 deletions app/src/main/res/layout/item_news.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
>

<data>

<variable
name="item"
type="com.mina_mikhail.newsapp.features.news.domain.entity.model.Article"
/>

</data>

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand All @@ -14,7 +24,7 @@
>

<LinearLayout
android:id="@+id/item"
android:id="@+id/ll_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackground"
Expand All @@ -26,10 +36,10 @@
>

<ImageView
android:id="@+id/iv_article_image"
android:layout_width="@dimen/dimen120"
android:layout_height="@dimen/dimen80"
android:scaleType="centerCrop"
app:loadRoundImage="@{item.urlToImage}"
tools:ignore="ContentDescription"
tools:src="@color/backgroundGray"
/>
Expand All @@ -43,35 +53,35 @@
>

<TextView
android:id="@+id/tv_article_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="0.3"
android:gravity="end"
android:text="@{item.articleDateFormatted}"
android:textColor="@color/black"
android:textSize="@dimen/text_size12"
tools:text="2 days ago"
/>

<TextView
android:id="@+id/tv_article_title"
style="@style/TextBoldFont"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="2"
android:text="@{item.title}"
android:textAlignment="viewStart"
android:textColor="@color/black"
android:textSize="@dimen/text_size14"
tools:text="Article Title"
/>

<TextView
android:id="@+id/tv_article_description"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/dimen2"
android:alpha="0.4"
android:maxLines="5"
android:text="@{item.description}"
android:textAlignment="viewStart"
android:textColor="@color/black"
android:textSize="@dimen/text_size13"
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/res/layout/list_general.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:visibility="@{baseViewModel.dataLoadingEvent == DataStatus.NO_DATA ? View.VISIBLE : View.GONE}"
app:goneUnless="@{baseViewModel.dataLoadingEvent == DataStatus.NO_DATA}"
tools:ignore="UseCompoundDrawables"
>

Expand Down Expand Up @@ -55,7 +55,7 @@
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:visibility="@{baseViewModel.dataLoadingEvent == DataStatus.NO_INTERNET ? View.VISIBLE : View.GONE}"
app:goneUnless="@{baseViewModel.dataLoadingEvent == DataStatus.NO_INTERNET}"
tools:ignore="UseCompoundDrawables"
>

Expand Down Expand Up @@ -84,8 +84,8 @@
android:layout_width="@dimen/dimen70"
android:layout_height="@dimen/dimen70"
android:layout_gravity="center"
android:visibility="@{baseViewModel.dataLoadingEvent == DataStatus.LOADING ? View.VISIBLE : View.GONE}"
app:SpinKit_Color="@color/colorAccent"
app:goneUnless="@{baseViewModel.dataLoadingEvent == DataStatus.LOADING}"
/>

<androidx.recyclerview.widget.RecyclerView
Expand All @@ -95,7 +95,7 @@
android:layout_weight="1"
android:clipToPadding="false"
android:padding="@dimen/dimen10"
android:visibility="@{(baseViewModel.dataLoadingEvent == DataStatus.SHOW_DATA || baseViewModel.dataLoadingEvent == DataStatus.LOADING_NEXT_PAGE) ? View.VISIBLE : View.GONE}"
app:goneUnless="@{baseViewModel.dataLoadingEvent == DataStatus.SHOW_DATA || baseViewModel.dataLoadingEvent == DataStatus.LOADING_NEXT_PAGE}"
/>

<com.github.ybq.android.spinkit.SpinKitView
Expand All @@ -104,8 +104,8 @@
android:layout_height="@dimen/dimen50"
android:layout_gravity="center"
android:layout_marginBottom="@dimen/dimen12"
android:visibility="@{baseViewModel.dataLoadingEvent == DataStatus.LOADING_NEXT_PAGE ? View.VISIBLE : View.GONE}"
app:SpinKit_Color="@color/colorPrimary"
app:goneUnless="@{baseViewModel.dataLoadingEvent == DataStatus.LOADING_NEXT_PAGE}"
/>

</LinearLayout>
Expand Down

0 comments on commit 1a88f19

Please sign in to comment.