From 79d57d5cf34073c397fe16f9dd6a13f33e98185d Mon Sep 17 00:00:00 2001 From: GeorgCantor Date: Mon, 13 Jul 2020 21:07:08 +0300 Subject: [PATCH] added ArticleActivity, progress bar, removed setupRecyclerView fun --- app/build.gradle | 4 ++ app/src/main/AndroidManifest.xml | 2 + .../com/naveentp/newsapp/article/Article.kt | 11 +++++ .../newsapp/article/ArticleActivity.kt | 48 +++++++++++++++++++ .../naveentp/newsapp/home/NewsListActivity.kt | 21 ++++---- .../naveentp/newsapp/home/NewsListAdapter.kt | 9 +++- app/src/main/res/layout/activity_article.xml | 36 ++++++++++++++ app/src/main/res/layout/activity_main.xml | 27 +++++++---- 8 files changed, 139 insertions(+), 19 deletions(-) create mode 100644 app/src/main/java/com/naveentp/newsapp/article/Article.kt create mode 100644 app/src/main/java/com/naveentp/newsapp/article/ArticleActivity.kt create mode 100644 app/src/main/res/layout/activity_article.xml diff --git a/app/build.gradle b/app/build.gradle index 16c20ea..6bd16e7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,6 +2,10 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' +androidExtensions { + experimental = true +} + android { def globalConfiguration = rootProject.extensions.getByName("ext") compileSdkVersion globalConfiguration["compileSdkVersion"] diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f56d4ba..afd5392 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -21,6 +21,8 @@ + + \ No newline at end of file diff --git a/app/src/main/java/com/naveentp/newsapp/article/Article.kt b/app/src/main/java/com/naveentp/newsapp/article/Article.kt new file mode 100644 index 0000000..ea1fdaf --- /dev/null +++ b/app/src/main/java/com/naveentp/newsapp/article/Article.kt @@ -0,0 +1,11 @@ +package com.naveentp.newsapp.article + +import android.os.Parcelable +import kotlinx.android.parcel.Parcelize + +@Parcelize +data class Article( + val imageUrl: String?, + val title: String?, + val description: String? +) : Parcelable \ No newline at end of file diff --git a/app/src/main/java/com/naveentp/newsapp/article/ArticleActivity.kt b/app/src/main/java/com/naveentp/newsapp/article/ArticleActivity.kt new file mode 100644 index 0000000..09cdef9 --- /dev/null +++ b/app/src/main/java/com/naveentp/newsapp/article/ArticleActivity.kt @@ -0,0 +1,48 @@ +package com.naveentp.newsapp.article + +import android.content.Context +import android.content.Intent +import androidx.appcompat.app.AppCompatActivity +import android.os.Bundle +import android.view.MenuItem +import com.naveentp.newsapp.R +import com.squareup.picasso.Picasso +import kotlinx.android.synthetic.main.activity_article.* + +class ArticleActivity : AppCompatActivity() { + + companion object { + private const val ARTICLE = "article" + + fun start(context: Context, article: Article) { + val intent = Intent(context, ArticleActivity::class.java) + intent.putExtra(ARTICLE, article) + context.startActivity(intent) + } + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_article) + supportActionBar?.setDisplayHomeAsUpEnabled(true) + supportActionBar?.setDisplayShowHomeEnabled(true) + + val article = intent.getParcelableExtra
(ARTICLE) + + Picasso.get().load(article.imageUrl) + .placeholder(R.drawable.news_placeholder) + .error(R.drawable.news_placeholder) + .into(articleImage) + + articleTitle.text = article.title + description.text = article.description + } + + override fun onOptionsItemSelected(item: MenuItem?) = when (item?.itemId) { + android.R.id.home -> { + onBackPressed() + true + } + else -> super.onOptionsItemSelected(item) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/naveentp/newsapp/home/NewsListActivity.kt b/app/src/main/java/com/naveentp/newsapp/home/NewsListActivity.kt index 9c2fada..f50b581 100644 --- a/app/src/main/java/com/naveentp/newsapp/home/NewsListActivity.kt +++ b/app/src/main/java/com/naveentp/newsapp/home/NewsListActivity.kt @@ -1,12 +1,16 @@ package com.naveentp.newsapp.home import android.os.Bundle +import android.view.View.GONE +import android.view.View.VISIBLE import android.widget.Toast +import android.widget.Toast.LENGTH_SHORT import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.Observer -import androidx.recyclerview.widget.LinearLayoutManager import com.naveentp.Resource import com.naveentp.newsapp.R +import com.naveentp.newsapp.article.Article +import com.naveentp.newsapp.article.ArticleActivity.Companion.start import com.naveentp.presentation.NewsDetailsViewModel import kotlinx.android.synthetic.main.activity_main.* import org.koin.android.viewmodel.ext.android.viewModel @@ -18,25 +22,24 @@ class NewsListActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) - setupRecyclerView() triggerGetTopHeadlines() } - private fun setupRecyclerView() { - rvNewsList.layoutManager = LinearLayoutManager(this) - } - private fun triggerGetTopHeadlines() { newsViewModel.getTopHeadlines().observe(this, Observer { when (it) { is Resource.Loading -> { - Toast.makeText(this, "Loading", Toast.LENGTH_SHORT).show() + progressBar.visibility = VISIBLE } is Resource.Success -> { - rvNewsList.adapter = NewsListAdapter(it.data.articles) + rvNewsList.adapter = NewsListAdapter(it.data.articles) { + start(this, Article(it.urlToImage, it.title, it.description)) + } + progressBar.visibility = GONE } is Resource.Failure -> { - Toast.makeText(this, "Failure: ${it.throwable.localizedMessage}", Toast.LENGTH_SHORT).show() + Toast.makeText(this, "Failure: ${it.throwable.localizedMessage}", LENGTH_SHORT).show() + progressBar.visibility = GONE } } }) diff --git a/app/src/main/java/com/naveentp/newsapp/home/NewsListAdapter.kt b/app/src/main/java/com/naveentp/newsapp/home/NewsListAdapter.kt index 3d903a6..ab802cc 100644 --- a/app/src/main/java/com/naveentp/newsapp/home/NewsListAdapter.kt +++ b/app/src/main/java/com/naveentp/newsapp/home/NewsListAdapter.kt @@ -14,7 +14,10 @@ import com.squareup.picasso.Picasso * @author Naveen T P * @since 02/06/19 */ -class NewsListAdapter(private val articles: List) : +class NewsListAdapter( + private val articles: List, + private val clickListener: (NewsDetails.Article) -> Unit +) : RecyclerView.Adapter() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { @@ -25,7 +28,9 @@ class NewsListAdapter(private val articles: List) : override fun getItemCount(): Int = articles.count() override fun onBindViewHolder(holder: ViewHolder, position: Int) { - holder.bind(articles[position]) + val article = articles[position] + holder.bind(article) + holder.itemView.setOnClickListener { clickListener(article) } } class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { diff --git a/app/src/main/res/layout/activity_article.xml b/app/src/main/res/layout/activity_article.xml new file mode 100644 index 0000000..44fafe4 --- /dev/null +++ b/app/src/main/res/layout/activity_article.xml @@ -0,0 +1,36 @@ + + + + + + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 6e37614..0616596 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -9,13 +9,24 @@ tools:context=".home.NewsListActivity"> + android:id="@+id/rvNewsList" + android:layout_width="0dp" + android:layout_height="0dp" + app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintLeft_toLeftOf="parent" + app:layout_constraintRight_toRightOf="parent" + app:layout_constraintTop_toTopOf="parent" + tools:listitem="@layout/news_item" /> + + \ No newline at end of file