Skip to content
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
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies {

implementation "com.google.android.material:material:${versions.material}"
implementation "androidx.appcompat:appcompat:${versions.androidxAppcompat}"
implementation "androidx.constraintlayout:constraintlayout:${versions.constraint}"
implementation "androidx.recyclerview:recyclerview:${versions.androidx}"

implementation "androidx.lifecycle:lifecycle-extensions:${versions.lifecycle}"
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">

<activity android:name=".view.movie.MovieActivity" />
<activity android:name=".view.MoviesActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand All @@ -23,4 +23,4 @@
</activity>
</application>

</manifest>
</manifest>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add endline

Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package io.github.erikjhordanrey.arch_components_paging_library.data.room

import android.os.Parcelable
import androidx.room.Entity
import androidx.room.PrimaryKey
import io.github.erikjhordanrey.arch_components_paging_library.data.room.DATABASE.TABLE_MOVIE
import kotlinx.android.parcel.Parcelize

@Parcelize
@Entity(tableName = TABLE_MOVIE)
data class Movie(@PrimaryKey val id: Long,
val title: String,
val popularity: Int,
val voteAverage: Int,
val posterUrl: String,
val description: String)
val description: String) : Parcelable
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can create a read it from database using dao en detail instead to use Parcelable

Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package io.github.erikjhordanrey.arch_components_paging_library.view

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import io.github.erikjhordanrey.arch_components_paging_library.R
import io.github.erikjhordanrey.arch_components_paging_library.di.provideMoviesViewModel
import io.github.erikjhordanrey.arch_components_paging_library.view.decorator.MarginDecoration
import io.github.erikjhordanrey.arch_components_paging_library.view.movie.MovieActivity
import io.github.erikjhordanrey.arch_components_paging_library.viewmodel.MoviesViewModel
import kotlinx.android.synthetic.main.activity_movies.movies_recycler

class MoviesActivity : AppCompatActivity() {

private lateinit var moviesViewModel: MoviesViewModel
private val moviePagedListAdapter by lazy { MoviesPagedListAdapter() }

private val moviePagedListAdapter by lazy {
MoviesPagedListAdapter {
val intent = Intent(this, MovieActivity::class.java)
intent.putExtras(Bundle().apply { putParcelable(ARG_MOVIE, it) })
startActivity(intent)
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -39,4 +48,8 @@ class MoviesActivity : AppCompatActivity() {
moviePagedListAdapter.submitList(it)
})
}

companion object {
const val ARG_MOVIE = "movie"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import androidx.recyclerview.widget.DiffUtil
import io.github.erikjhordanrey.arch_components_paging_library.R
import io.github.erikjhordanrey.arch_components_paging_library.data.room.Movie

class MoviesPagedListAdapter : PagedListAdapter<Movie, MoviesViewHolder>(movieDiffCallback) {
class MoviesPagedListAdapter(val clickListener: (Movie) -> Unit) : PagedListAdapter<Movie, MoviesViewHolder>(movieDiffCallback) {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MoviesViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_movie, parent, false)
Expand All @@ -17,6 +17,10 @@ class MoviesPagedListAdapter : PagedListAdapter<Movie, MoviesViewHolder>(movieDi
override fun onBindViewHolder(holder: MoviesViewHolder, position: Int) {
val movie = getItem(position)
if (movie != null) holder.render(movie) else holder.clear()

holder.itemView.setOnClickListener {
movie?.let { movie -> clickListener(movie) }
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ package io.github.erikjhordanrey.arch_components_paging_library.view
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import io.github.erikjhordanrey.arch_components_paging_library.R
import io.github.erikjhordanrey.arch_components_paging_library.data.room.Movie
import kotlinx.android.synthetic.main.item_movie.view.image

class MoviesViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

fun render(movie: Movie) = itemView.run {
Glide.with(image.context).load(movie.posterUrl).into(image)
Glide.with(image.context)
.load(movie.posterUrl)
.placeholder(R.drawable.ic_launcher_background)
.into(image)
}

fun clear() = itemView.run {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.github.erikjhordanrey.arch_components_paging_library.view.movie

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import com.bumptech.glide.Glide
import io.github.erikjhordanrey.arch_components_paging_library.R
import io.github.erikjhordanrey.arch_components_paging_library.data.room.Movie
import io.github.erikjhordanrey.arch_components_paging_library.view.MoviesActivity.Companion.ARG_MOVIE
import kotlinx.android.synthetic.main.activity_movie.*
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove wildcard imports please


class MovieActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_movie)
supportActionBar?.setDisplayHomeAsUpEnabled(true)

val movie = intent.getParcelableExtra<Movie>(ARG_MOVIE)

Glide.with(this)
.load(movie.posterUrl)
.placeholder(R.drawable.ic_launcher_background)
.into(movie_image)

movie_title.text = movie.title
movie_description.text = movie.description
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
android.R.id.home -> onBackPressed()
}
return super.onOptionsItemSelected(item)
}
}
41 changes: 41 additions & 0 deletions app/src/main/res/layout/activity_movie.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".view.movie.MovieActivity">

<ImageView
android:id="@+id/movie_image"
android:layout_width="0dp"
android:layout_height="400dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:background="@drawable/ic_launcher_background" />

<TextView
android:id="@+id/movie_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/movie_image"
tools:text="@string/app_name" />

<TextView
android:id="@+id/movie_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/movie_title"
tools:text="@string/app_name" />

</androidx.constraintlayout.widget.ConstraintLayout>
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ buildscript {
'androidx' : '1.1.0',
'material' : '1.1.0',
'androidxAppcompat': '1.1.0',
'constraint' : '1.1.3',
'lifecycle' : '2.2.0',
'persistence' : '2.2.5',
'paging' : '2.1.2',
Expand Down