Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add date formate #44

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,6 @@ dependencies {

// Debug
implementation "com.jakewharton.timber:timber:$timberVersion"
debugImplementation "com.amitshekhar.android:debug-db:$debugDbVersion"

}
16 changes: 16 additions & 0 deletions app/src/main/java/com/akshay/newsapp/news/Utilities/TimeUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.akshay.newsapp.news.Utilities

import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*


fun String.getFormattedDate(): Date? {
val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'",Locale.getDefault())
dateFormat.timeZone = TimeZone.getTimeZone("GMT")
try {
return dateFormat.parse(this)
} catch (e: ParseException) {
return null
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.akshay.newsapp.news.ui.activity

import android.os.Bundle
import android.widget.Toast
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.akshay.newsapp.R
import com.akshay.newsapp.core.ui.ViewState
import com.akshay.newsapp.core.ui.base.BaseActivity
import com.akshay.newsapp.core.utils.observeNotNull
import com.akshay.newsapp.core.utils.toast
import com.akshay.newsapp.news.storage.entity.NewsArticleDb
import com.akshay.newsapp.news.ui.adapter.NewsArticlesAdapter
import com.akshay.newsapp.news.ui.viewmodel.NewsArticleViewModel
import kotlinx.android.synthetic.main.activity_main.*
Expand All @@ -31,6 +33,7 @@ class NewsActivity : BaseActivity() {
newsList.setProgressView(progress_view)

val adapter = NewsArticlesAdapter { toast("Clicked on item") }

newsList.adapter = adapter
newsList.layoutManager = LinearLayoutManager(this)

Expand All @@ -42,6 +45,6 @@ class NewsActivity : BaseActivity() {
is ViewState.Error -> toast("Something went wrong ¯\\_(ツ)_/¯ => ${state.message}")
}
}

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.akshay.newsapp.news.ui.adapter

import android.annotation.SuppressLint
import android.util.Log
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
Expand All @@ -8,16 +10,15 @@ import androidx.recyclerview.widget.RecyclerView
import coil.api.load
import com.akshay.newsapp.R
import com.akshay.newsapp.core.utils.inflate
import com.akshay.newsapp.news.Utilities.getFormattedDate
import com.akshay.newsapp.news.storage.entity.NewsArticleDb
import com.akshay.newsapp.news.ui.model.NewsAdapterEvent
import kotlinx.android.synthetic.main.row_news_article.view.*

/**
* The News adapter to show the news in a list.
*/
class NewsArticlesAdapter(
private val listener: (NewsAdapterEvent) -> Unit
) : ListAdapter<NewsArticleDb, NewsArticlesAdapter.NewsHolder>(DIFF_CALLBACK) {
class NewsArticlesAdapter(private val listener: (NewsAdapterEvent) -> Unit):ListAdapter<NewsArticleDb, NewsArticlesAdapter.NewsHolder>(DIFF_CALLBACK) {

/**
* Inflate the view
Expand All @@ -37,17 +38,16 @@ class NewsArticlesAdapter(
/**
* Binds the UI with the data and handles clicks
*/
@SuppressLint("SetTextI18n")
fun bind(newsArticle: NewsArticleDb, listener: (NewsAdapterEvent) -> Unit) = with(itemView) {
newsTitle.text = newsArticle.title
newsAuthor.text = newsArticle.author
//TODO: need to format date
//tvListItemDateTime.text = getFormattedDate(newsArticle.publishedAt)
newsPublishedAt.text = newsArticle.publishedAt
newsTitle.text = newsArticle.title
newsAuthor.text = newsArticle.author
newsPublishedAt.text = newsArticle.publishedAt?.getFormattedDate().toString()
newsImage.load(newsArticle.urlToImage) {
placeholder(R.drawable.tools_placeholder)
error(R.drawable.tools_placeholder)
}
setOnClickListener { listener(NewsAdapterEvent.ClickEvent) }
setOnClickListener { listener(NewsAdapterEvent.ClickEvent(newsArticle)) }
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.akshay.newsapp.news.ui.model

import com.akshay.newsapp.news.storage.entity.NewsArticleDb
import com.akshay.newsapp.news.ui.adapter.NewsArticlesAdapter

/**
* Describes all the events originated from
* [NewsArticlesAdapter].
*/
sealed class NewsAdapterEvent {

/* Describes item click event */
object ClickEvent : NewsAdapterEvent()
/* Describes item click event*/
data class ClickEvent(val newsArticleDb: NewsArticleDb) : NewsAdapterEvent()
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@

<!-- Empty -->
<string name="no_news">No News</string>
<string name="news_image_description">news_image_description</string>
</resources>
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ buildscript {
// Debug
ext.timberVersion = '4.7.1'

//Glide
ext.version_glide = '4.11.0'
ext.version_glide_compiler = '4.11.0'


repositories {
google()
jcenter()
Expand Down