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 Images to news, fixed code style. #2

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 @@ -64,6 +64,10 @@ dependencies {
implementation "com.squareup.okhttp3:okhttp:$okHttpVersion"
implementation "com.squareup.okhttp3:logging-interceptor:$okHttpVersion"

// GLIDE
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

// TESTING
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package com.androiddevs.newsflash.data

import com.google.gson.annotations.SerializedName

object NewsError
{
object NewsError {
data class Error(
@SerializedName("status")
val status: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package com.androiddevs.newsflash.data

import com.google.gson.annotations.SerializedName

object NewsResult
{
object NewsResult {

data class News(
@SerializedName("status")
Expand All @@ -12,8 +11,7 @@ object NewsResult
val totalResults: Int,
@SerializedName("articles")
val articles: List<Article>
)
{
) {
data class Article(
@SerializedName("source")
val source: Source,
Expand All @@ -31,8 +29,7 @@ object NewsResult
val publishedAt: String,
@SerializedName("content")
val content: String
)
{
) {
data class Source(
@SerializedName("id")
val id: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ package com.androiddevs.newsflash.data

import com.google.gson.annotations.SerializedName

object NewsSources
{
object NewsSources {

data class Sources(
@SerializedName("status")
val status: String,
@SerializedName("sources")
val sources: List<Source>
)
{
) {
data class Source(
@SerializedName("id")
val id: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@ interface NewsApiService {
@GET("everything")
fun getEverything(
@Query("q") keyword: String,
@Query("qInTitle") keyWordInArtcile : String,
@Query("qInTitle") keyWordInArtcile: String,
@Query("sources") sources: String,
@Query("domains") domain: String,
@Query("excludeDomains") excludeDomains: String,
@Query("from") from: String,
@Query("to") to: String,
@Query("language") language:String,
@Query("sortBy") sortBy:String,
@Query("language") language: String,
@Query("sortBy") sortBy: String,
@Query("pageSize") pageSize: Int,
@Query("page") page: Int):Observable<NewsResult.News>
@Query("page") page: Int
): Observable<NewsResult.News>


companion object {
Expand Down Expand Up @@ -81,12 +82,9 @@ interface NewsApiService {
.client(okHttpClient)
.build()


return retrofit.create(NewsApiService::class.java)

}

}


}
11 changes: 2 additions & 9 deletions app/src/main/java/com/androiddevs/newsflash/view/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,10 @@ import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers

class MainActivity : AppCompatActivity()
{
class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?)
{
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)





}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.androiddevs.newsflash.view.adapter


import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.androiddevs.newsflash.R
import com.androiddevs.newsflash.data.NewsResult
import com.androiddevs.newsflash.databinding.HomeNewsRowBinding

import com.bumptech.glide.Glide

class HomeNewsRecyelerAdapter(private var newsList: ArrayList<NewsResult.News.Article>) :
RecyclerView.Adapter<HomeNewsRecyelerAdapter.ViewHolder>() {
Expand All @@ -32,12 +32,19 @@ class HomeNewsRecyelerAdapter(private var newsList: ArrayList<NewsResult.News.Ar
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val newsArticle = newsList[position]
holder.bind(newsArticle)

Glide.with(holder.itemView.context)
.load(newsArticle.urlToImage)
.into(holder.image)
}

class ViewHolder(binding: HomeNewsRowBinding) : RecyclerView.ViewHolder(binding.root) {
private var binding: HomeNewsRowBinding? = binding

fun bind(obj: NewsResult.News.Article){
binding!!.setVariable(com.androiddevs.newsflash.BR.news,obj)
val image: ImageView = binding.image

fun bind(obj: NewsResult.News.Article) {
binding!!.setVariable(com.androiddevs.newsflash.BR.news, obj)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,4 @@ class HomeFragment : Fragment() {
return homeBinding.root
}


}
6 changes: 5 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@
<layout 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">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.MainActivity">

<fragment
android:id="@+id/fragment_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation_graph"/>
app:navGraph="@navigation/navigation_graph" />

</androidx.constraintlayout.widget.ConstraintLayout>

</layout>
13 changes: 9 additions & 4 deletions app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.fragments.HomeFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/homeRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/homeRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/home_news_row" />

</FrameLayout>

</layout>
37 changes: 24 additions & 13 deletions app/src/main/res/layout/home_news_row.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="news"
type="com.androiddevs.newsflash.data.NewsResult.News.Article" />
</data>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>

<variable
name="news"
type="com.androiddevs.newsflash.data.NewsResult.News.Article" />
</data>

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand All @@ -16,30 +19,38 @@
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="200dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="@{news.title}"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="@android:color/black"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toBottomOf="@+id/image" />

<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="@{(news.author.length>0)? news.author : @string/unknown_value}"
android:textSize="18sp"
android:textColor="@android:color/black"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

Expand Down