Skip to content

Commit

Permalink
Added and used Glide library and parcelable implementing for Movie class
Browse files Browse the repository at this point in the history
  • Loading branch information
konik authored and konik committed Jun 30, 2019
1 parent d7624e3 commit 03d240c
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 25 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ android {
}

dependencies {
implementation 'com.github.bumptech.glide:glide:4.9.0'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
Expand Down
Binary file added app/src/main/image_not_available-web.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.kaput.popularmoviesapp.model.MovieListItem;
Expand Down Expand Up @@ -52,7 +53,8 @@ protected void onCreate(Bundle savedInstanceState) {

viewModel = ViewModelProviders.of(this).get(MovieViewModel.class);

final MovieAdapter movieAdapter = new MovieAdapter(this);
final MovieAdapter movieAdapter = new MovieAdapter(this, Glide.with(this));


viewModel.movieList.observe(this, new Observer<PagedList<Movie>>() {
@Override
Expand All @@ -70,7 +72,7 @@ public void onChanged(@Nullable PagedList<Movie> pagedList) {
@Override
public void onClick(View view, Movie m) {
Intent i = new Intent(this, MovieDetailActivity.class);
i.putExtra("title", m.title);
i.putExtra("movie", m);
startActivity(i);
}
}
29 changes: 13 additions & 16 deletions app/src/main/java/com/kaput/popularmoviesapp/MovieAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.kaput.popularmoviesapp.model.Movie;
import com.kaput.popularmoviesapp.R;
import com.kaput.popularmoviesapp.model.MovieListItem;
Expand All @@ -31,10 +33,12 @@ public class MovieAdapter extends PagedListAdapter<Movie, MovieAdapter.MyViewHol
LayoutInflater inflater;
Context context;
private OnMovieClickListener listener;
private RequestManager glide;

public MovieAdapter(OnMovieClickListener listener) {
public MovieAdapter(OnMovieClickListener listener, RequestManager glide) {
super(Movie.DIFF_CALLBACK);
this.listener = listener;
this.glide = glide;
}

@NonNull
Expand All @@ -51,7 +55,7 @@ public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {

myViewHolder.setData(getItem(i));
myViewHolder.setData(getItem(i), glide);
}

/* @Override
Expand Down Expand Up @@ -81,25 +85,18 @@ public void onClick(View v) {

}

public void setData(Movie m) {
public void setData(Movie m, RequestManager glide) {
this.title.setText(m.title);
this.rating.setText(m.rating);
this.ranking.setText(String.valueOf(m.ranking + "."));
this.poster.setImageDrawable(LoadImageFromWebOperations(m.posterPath));
if (m.posterPath != null)
loadImageFromUrl(glide, m.posterPath, this.poster);
}

public Drawable LoadImageFromWebOperations(String path) {

try {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
InputStream is = (InputStream) new URL("https://image.tmdb.org/t/p/w185" + path).getContent();
Drawable d = Drawable.createFromStream(is, "themoviedb");
return d;
} catch (Exception e) {
e.printStackTrace();
return null;
}

private void loadImageFromUrl(RequestManager glide, String path, ImageView view ){
//System.out.println("https://image.tmdb.org/t/p/w185" + path);
glide.load("https://image.tmdb.org/t/p/w185" + path).override(185,278).into(view);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.os.Bundle;
import android.widget.TextView;

import com.kaput.popularmoviesapp.model.Movie;

public class MovieDetailActivity extends AppCompatActivity {

TextView titleView;
Expand All @@ -15,7 +17,7 @@ protected void onCreate(Bundle savedInstanceState) {

titleView = findViewById(R.id.movie_title);

String title = getIntent().getStringExtra("title");
titleView.setText(title);
Movie m = getIntent().getParcelableExtra("movie");
titleView.setText(m.title);
}
}
36 changes: 35 additions & 1 deletion app/src/main/java/com/kaput/popularmoviesapp/model/Movie.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.kaput.popularmoviesapp.model;

import android.arch.persistence.room.Entity;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.v7.util.DiffUtil;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Entity
public class Movie {
public class Movie implements Parcelable {
public static DiffUtil.ItemCallback<Movie> DIFF_CALLBACK = new DiffUtil.ItemCallback<Movie>() {
@Override
public boolean areItemsTheSame(@NonNull Movie movie, @NonNull Movie t1) {
Expand All @@ -33,6 +35,25 @@ public boolean areContentsTheSame(@NonNull Movie movie, @NonNull Movie t1) {
@Expose
public String rating;

protected Movie(Parcel in) {
ranking = in.readInt();
title = in.readString();
posterPath = in.readString();
rating = in.readString();
}

public static final Creator<Movie> CREATOR = new Creator<Movie>() {
@Override
public Movie createFromParcel(Parcel in) {
return new Movie(in);
}

@Override
public Movie[] newArray(int size) {
return new Movie[size];
}
};

public void setRanking(int ranking) {
this.ranking = ranking;
}
Expand All @@ -48,4 +69,17 @@ public void setPosterPath(String posterPath) {
public void setRating(String rating) {
this.rating = rating;
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(ranking);
dest.writeString(title);
dest.writeString(posterPath);
dest.writeString(rating);
}
}
Binary file added app/src/main/res/drawable-v24/no_img_avail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions app/src/main/res/layout/custom_item_card.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

<TextView
android:id="@+id/ranking"
android:layout_width="26dp"
android:layout_width="36dp"
android:layout_height="wrap_content"
android:text="1."
android:textSize="18dp"
Expand All @@ -27,9 +27,9 @@

<ImageView
android:id="@+id/poster"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:srcCompat="@tools:sample/avatars"
android:layout_width="185px"
android:layout_height="278px"
android:src="@drawable/no_img_avail"
/>

<LinearLayout
Expand Down

0 comments on commit 03d240c

Please sign in to comment.