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

Added ArticleActivity #1

Open
wants to merge 1 commit 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
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<activity android:name=".article.ArticleActivity" />
</application>

</manifest>
11 changes: 11 additions & 0 deletions app/src/main/java/com/naveentp/newsapp/article/Article.kt
Original file line number Diff line number Diff line change
@@ -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
48 changes: 48 additions & 0 deletions app/src/main/java/com/naveentp/newsapp/article/ArticleActivity.kt
Original file line number Diff line number Diff line change
@@ -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>(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)
}
}
21 changes: 12 additions & 9 deletions app/src/main/java/com/naveentp/newsapp/home/NewsListActivity.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import com.squareup.picasso.Picasso
* @author Naveen T P
* @since 02/06/19
*/
class NewsListAdapter(private val articles: List<NewsDetails.Article>) :
class NewsListAdapter(
private val articles: List<NewsDetails.Article>,
private val clickListener: (NewsDetails.Article) -> Unit
) :
RecyclerView.Adapter<NewsListAdapter.ViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
Expand All @@ -25,7 +28,9 @@ class NewsListAdapter(private val articles: List<NewsDetails.Article>) :
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) {
Expand Down
36 changes: 36 additions & 0 deletions app/src/main/res/layout/activity_article.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context=".article.ArticleActivity">

<ImageView
android:id="@+id/articleImage"
android:layout_width="match_parent"
android:layout_height="270dp"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
tools:src="@tools:sample/avatars" />

<TextView
android:id="@+id/articleTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:gravity="center_horizontal"
android:textSize="23sp"
tools:text="@tools:sample/cities" />

<TextView
android:id="@+id/description"
style="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
tools:text="@tools:sample/lorem/random" />

</LinearLayout>
27 changes: 19 additions & 8 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,24 @@
tools:context=".home.NewsListActivity">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvNewsList"
android:layout_width="0dp"
android:layout_height="0dp"
tools:listitem="@layout/news_item"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
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" />

<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>