Changed Discover and Detail Implementation

This commit is contained in:
Aargonian
2016-02-02 10:06:56 -05:00
parent 721107b0e2
commit 71f5b77413
10 changed files with 180 additions and 60 deletions

View File

@@ -17,9 +17,16 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SettingsActivity"
android:label="@string/settings_activity_title"
android:parentActivityName="com.example.android.popularmovies.MainActivity">
<activity
android:name=".SettingsActivity"
android:label="@string/settings_activity_title"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.android.popularmovies.MainActivity" />
</activity>
<activity android:name=".DetailActivity"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.android.popularmovies.MainActivity"/>

View File

@@ -0,0 +1,19 @@
package com.example.android.popularmovies;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
/**
* Created by Aaron Helton on 2/2/2016
*/
public class DetailActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setTitle(this.getIntent().getStringExtra("MOVIE_TITLE"));
setContentView(R.layout.activity_detail);
getSupportFragmentManager().beginTransaction().add(R.id.detail_container,
new DetailFragment()).commit();
}
}

View File

@@ -1,5 +1,7 @@
package com.example.android.popularmovies;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
@@ -9,7 +11,7 @@ import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by mint on 1/31/16.
* Created by Aaron Helton on 1/31/2016
*/
public class DetailFragment extends Fragment
{
@@ -21,6 +23,22 @@ public class DetailFragment extends Fragment
private TextView release;
private TextView overview;
private ImageView poster;
private boolean viewCreated;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent = this.getActivity().getIntent();
if(intent != null)
{
Integer id = intent.getIntExtra("MOVIE_ID", -1);
if(id != -1)
{
setMovie(id);
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
@@ -31,23 +49,36 @@ public class DetailFragment extends Fragment
overview = (TextView) root.findViewById(R.id.overview);
release = (TextView) root.findViewById(R.id.releaseDate);
poster = (ImageView) root.findViewById(R.id.posterImage);
viewCreated = true;
if(movie != null) {
title.setText(movie.getTitle());
movie.applyPoster(poster);
overview.setText(movie.getOverview());
release.setText(movie.getReleaseDate());
rating.setText(movie.getRating() + "/10");
setMovieInfo(movie);
}
return root;
}
public final void receiveMovie(Movie movie)
public void setMovie(Integer id)
{
new AsyncTask<Integer, Void, Movie>() {
@Override
public Movie doInBackground(Integer... params)
{
if(params.length < 1)
return null;
return Movie.getMovie(params[0], DetailFragment.this.getContext());
}
@Override
public void onPostExecute(Movie movie)
{
DetailFragment.this.setMovieInfo(movie);
}
}.execute(id);
}
private void setMovieInfo(Movie movie)
{
this.movie = movie;
if(this.isVisible() && movie != null)
{
if(viewCreated) {
title.setText(movie.getTitle());
movie.applyPoster(poster);
overview.setText(movie.getOverview());

View File

@@ -3,6 +3,7 @@ package com.example.android.popularmovies;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
@@ -21,15 +22,13 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link DiscoverFragment.OnMovieSelectedListener} interface
* to handle interaction events.
* Created by Aaron Helton on 1/30/2016
*/
public class DiscoverFragment extends Fragment implements MovieAdapter.MovieItemClickListener
{
@@ -88,9 +87,9 @@ public class DiscoverFragment extends Fragment implements MovieAdapter.MovieItem
}
@Override
public void movieClicked(Movie movie)
public void movieClicked(Integer id, String title)
{
mListener.onMovieSelected(movie);
mListener.onMovieSelected(id, title);
}
@Override
@@ -111,10 +110,10 @@ public class DiscoverFragment extends Fragment implements MovieAdapter.MovieItem
}
public interface OnMovieSelectedListener {
void onMovieSelected(Movie movie);
void onMovieSelected(Integer id, String title);
}
private class DiscoverTask extends AsyncTask<Void, Movie, Void>
private class DiscoverTask extends AsyncTask<Void, MovieAdapter.MovieReference, Void>
{
private final String LOG_TAG = DiscoverTask.class.getSimpleName();
@@ -122,7 +121,7 @@ public class DiscoverFragment extends Fragment implements MovieAdapter.MovieItem
public Void doInBackground(Void... params)
{
String TMDB_DISCOVER_URL = Movie.TMDB_URL_BASE+"/discover/movie?";
Context context = DiscoverFragment.this.getActivity();
SharedPreferences prefs =
PreferenceManager
.getDefaultSharedPreferences(DiscoverFragment.this.getActivity());
@@ -140,8 +139,15 @@ public class DiscoverFragment extends Fragment implements MovieAdapter.MovieItem
JSONArray results = movieList.getJSONArray("results");
for(int i = 0; i < results.length(); i++) {
Integer id = results.getJSONObject(i).getInt("id");
publishProgress(Movie.getMovie(id, DiscoverFragment.this.getActivity(),
Movie.API_KEY));
String title = results.getJSONObject(i).getString("original_title");
Bitmap poster = null;
try {
poster = Movie.getPoster(context,
results.getJSONObject(i).getString("poster_path"));
} catch (IOException ex) {
Log.e(LOG_TAG, "Unable to retrive poster! " + ex.getMessage(), ex);
}
publishProgress(new MovieAdapter.MovieReference(id, poster, title));
}
} catch (MalformedURLException ex) {
Log.e(LOG_TAG, "Malformed URL: " + ex.getMessage(), ex);
@@ -152,7 +158,7 @@ public class DiscoverFragment extends Fragment implements MovieAdapter.MovieItem
}
@Override
public void onProgressUpdate(Movie... progress)
public void onProgressUpdate(MovieAdapter.MovieReference... progress)
{
DiscoverFragment.this.mAdapter.addMovie(progress[0]);
}

View File

@@ -6,23 +6,26 @@ import android.preference.PreferenceManager;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
/**
* Created by Aaron Helton on 1/30/2016
*/
public class MainActivity extends AppCompatActivity
implements DiscoverFragment.OnMovieSelectedListener
{
private static final String LOG_TAG = MainActivity.class.getSimpleName();
private DetailFragment details;
DetailFragment details;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
if (savedInstanceState == null) {
getSupportActionBar().setElevation(8);
if(getSupportActionBar() != null)
getSupportActionBar().setElevation(8);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
DiscoverFragment discoverFragment = new DiscoverFragment();
@@ -55,18 +58,29 @@ public class MainActivity extends AppCompatActivity
return super.onOptionsItemSelected(item);
}
public void onMovieSelected(Movie movie)
public void onMovieSelected(Integer movieID, String title)
{
Log.d(LOG_TAG, "Movie Selected: " + movie.getTitle());
if(details == null) {
if(findViewById(R.id.detail_container) != null && details == null)
{
details = new DetailFragment();
FragmentManager fm = this.getSupportFragmentManager();
FragmentTransaction transaction =fm.beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.add(R.id.detail_container, details);
transaction.addToBackStack("Replacement");
transaction.commit();
}
if(findViewById(R.id.detail_container) != null)
{
details.setMovie(movieID);
}
else
{
Intent detailIntent = new Intent(this, DetailActivity.class);
detailIntent.putExtra("MOVIE_TITLE", title);
detailIntent.putExtra("MOVIE_ID", movieID);
this.startActivity(detailIntent);
}
details.receiveMovie(movie);
FragmentManager fm = this.getSupportFragmentManager();
FragmentTransaction transaction =fm.beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.replace(R.id.container, details);
transaction.addToBackStack("Replacement");
transaction.commit();
}
}

View File

@@ -2,6 +2,7 @@ package com.example.android.popularmovies;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.util.Log;
import android.widget.ImageView;
@@ -14,7 +15,7 @@ import org.json.JSONObject;
import java.io.IOException;
/**
* Created by mint on 1/30/16.
* Created by Aaron Helton on 1/30/2016
*/
public class Movie implements Comparable<Movie>
{
@@ -24,7 +25,7 @@ public class Movie implements Comparable<Movie>
public static final String TMDB_URL_BASE = "http://api.themoviedb.org/3";
public static final String TMDB_POSTER_URL = "http://image.tmdb.org/t/p/w500";
public static final Movie getMovie(Integer id, Context context, String API_KEY)
public static Movie getMovie(Integer id, Context context)
{
String url = TMDB_URL_BASE+"/movie/"+id +"?api_key="+API_KEY;
String movieJSON = NetworkUtil.getURL(url);
@@ -47,20 +48,23 @@ public class Movie implements Comparable<Movie>
Bitmap poster = null;
try {
poster = Picasso.with(context).load(TMDB_POSTER_URL + posterPath).get();
poster = getPoster(context, posterPath);
} catch (IOException ex) {
Log.e(LOG_TAG, "Unable To Get Movie Poster: " + ex.getMessage(), ex);
}
Movie movie = new Movie(title, overview, poster,
releaseDate, runTime, vote, votes, genres);
return movie;
return new Movie(title, overview, poster, releaseDate, runTime, vote, votes, genres);
} catch (JSONException ex) {
Log.e(LOG_TAG, "ERROR PARSING MOVIE: " + ex.getMessage(), ex);
return null;
}
}
public static Bitmap getPoster(Context context, String posterPath) throws IOException
{
return Picasso.with(context).load(TMDB_POSTER_URL + posterPath).get();
}
private String title;
private String overview;
private Bitmap poster;
@@ -83,7 +87,8 @@ public class Movie implements Comparable<Movie>
this.genres = genreList;
}
public int compareTo(Movie other)
@Override
public int compareTo(@NonNull Movie other)
{
if(this.rating < other.rating) {
return 1;

View File

@@ -1,5 +1,7 @@
package com.example.android.popularmovies;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
@@ -9,16 +11,34 @@ import android.widget.ImageView;
import java.util.ArrayList;
/**
* Created by Aaron Helton on 1/30/16.
* Created by Aaron Helton on 1/30/2016
*/
public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder>
{
private ArrayList<Movie> dataset;
//Struct-like class for inner use
public static class MovieReference {
public final Integer id;
public final Bitmap poster;
public final String title;
public MovieReference(Integer id, Bitmap poster, String title) {
this.id = id;
this.poster = poster;
this.title = title;
}
}
private ArrayList<MovieReference> dataset;
private MovieItemClickListener listener;
private Context context;
public MovieAdapter(Context context) {
this.context = context;
}
public interface MovieItemClickListener
{
void movieClicked(Movie movie);
void movieClicked(Integer movieID, String title);
}
public class ViewHolder extends RecyclerView.ViewHolder
@@ -33,7 +53,8 @@ public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder>
@Override
public void onClick(View v) {
listener.movieClicked(dataset.get(this.getLayoutPosition()));
MovieReference reference = dataset.get(this.getLayoutPosition());
listener.movieClicked(reference.id, reference.title);
}
}
@@ -46,7 +67,7 @@ public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder>
public MovieAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
ImageView view = new ImageView(parent.getContext());
view.setAdjustViewBounds(true);
view.setAdjustViewBounds(false);
view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
view.setScaleType(ImageView.ScaleType.FIT_XY);
return new ViewHolder(view);
@@ -55,15 +76,16 @@ public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder>
@Override
public void onBindViewHolder(ViewHolder holder, int position)
{
Movie movie = dataset.get(position);
if(movie != null)
dataset.get(position).applyPoster(holder.imageView);
MovieReference movie = dataset.get(position);
if(movie != null) {
holder.imageView.setImageBitmap(movie.poster);
}
}
public void addMovie(Movie movie)
public void addMovie(MovieReference reference)
{
dataset.add(movie);
notifyItemInserted(dataset.size()-1);
dataset.add(reference);
notifyItemInserted(dataset.size() - 1);
}
@Override

View File

@@ -10,17 +10,17 @@ import java.net.MalformedURLException;
import java.net.URL;
/**
* Created by mint on 1/30/16.
* Created by Aaron Helton on 1/30/2016
*/
public final class NetworkUtil
{
private static final String LOG_TAG = NetworkUtil.class.getSimpleName();
public static final String getURL(String urlString)
public static String getURL(String urlString)
{
if(urlString == null || urlString.isEmpty())
return null;
URL url = null;
URL url;
HttpURLConnection connection = null;
BufferedReader reader = null;
StringBuilder builder = null;

View File

@@ -7,6 +7,9 @@ import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
/**
* Created by Aaron Helton on 2/1/2016
*/
public class SettingsActivity extends AppCompatActivity
{
@Override

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/detail_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.android.popularmovies.DetailActivity">
</RelativeLayout>