Skip to content

Commit

Permalink
Finish main functionality for P1.
Browse files Browse the repository at this point in the history
Add basic formatting for movie details activity.
Complete sort settings.
  • Loading branch information
boykoc committed Sep 23, 2015
1 parent 759d392 commit 7e83a44
Show file tree
Hide file tree
Showing 14 changed files with 363 additions and 79 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 23,7 @@ dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.android.support:support-v4:23.0.0'
}

repositories {
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 23,10 @@
android:name=".Main2Activity"
android:label="@string/title_activity_main2" >
</activity>
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings" >
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 48,11 @@ public View getView(int position, View convertView, ViewGroup parent) {
Movie movie = (Movie) objects.get(position);

// TODO: remove Logs as needed
Log.w("myApp", TMDB_IMAGE_URL_BASE TMDB_IMAGE_SIZE movie.getPoster_path());
Log.w("myApp", TMDB_IMAGE_URL_BASE TMDB_IMAGE_SIZE movie.getPosterPath());

// Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Picasso.with(context).load(TMDB_IMAGE_URL_BASE TMDB_IMAGE_SIZE movie.getPoster_path()).into(imageView);
Picasso.with(context).load(TMDB_IMAGE_URL_BASE TMDB_IMAGE_SIZE movie.getPosterPath())
.into(imageView);
return imageView;
//return super.getView(position, convertView, parent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 6,38 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

/**
* A placeholder fragment containing a simple view.
*/
public class Main2ActivityFragment extends Fragment {

public Main2ActivityFragment() {
}

// TODO: enable back button.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_main2, container, false);
Intent intent = getActivity().getIntent();

Bundle movie = intent.getExtras();
Movie m = movie.getParcelable("com.package.Movie");
Bundle movie = intent.getExtras();
Movie m = movie.getParcelable("com.package.Movie");
final String TMDB_IMAGE_URL_BASE = "http://image.tmdb.org/t/p/";
final String TMDB_IMAGE_SIZE = "w185";

((TextView) rootView.findViewById(R.id.detail_textView)).setText(m.toString());
((TextView) rootView.findViewById(R.id.title_textView)).setText(m.getOriginalTitle());
((TextView) rootView.findViewById(R.id.year_textView)).setText(m.getReleaseDate());
((TextView) rootView.findViewById(R.id.rating_textView)).setText(m.getVoteAverage() "/10");
((TextView) rootView.findViewById(R.id.detail_textView)).setText(m.getOverview());

Picasso.with(getActivity()).load(TMDB_IMAGE_URL_BASE TMDB_IMAGE_SIZE m.getPosterPath())
.into((ImageView) rootView.findViewById(R.id.movie_poster_imageView));


return rootView;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 1,8 @@
package com.example.android.popularmovies;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

Expand All @@ -21,18 20,5 @@ public boolean onCreateOptionsMenu(Menu menu) {
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 2,15 @@

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
Expand Down Expand Up @@ -35,6 38,29 @@ public class MainActivityFragment extends Fragment {
public MainActivityFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Add this line in order for this fragment to handle menu events.
setHasOptionsMenu(true);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
startActivity(new Intent(getActivity(), SettingsActivity.class));
return true;
}

return super.onOptionsItemSelected(item);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Expand Down Expand Up @@ -97,7 123,12 @@ public void onStart() {
private void getMovieData() {
// Create movie task and execute it
FetchMovieDataTask movieDataTask = new FetchMovieDataTask();
movieDataTask.execute("popularity.desc");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
String sort_by = prefs.getString("sortOrder", "popularity.desc");
Log.w("myApp", "URI : " sort_by);
movieDataTask.execute(sort_by);

//movieDataTask.execute("popularity.desc");
}

public class FetchMovieDataTask extends AsyncTask<String, Void, Movie[]> {
Expand Down Expand Up @@ -173,21 204,37 @@ protected Movie[] doInBackground(String... params) {

// Will contain the raw JSON response as a string.
String movieDataJsonStr = null;
String api_key = "YOUR_API_KEY_HERE";
String api_key = "YOUR_KEY_HERE";

try {
// Construct the URL for the OpenWeatherMap query
// Possible parameters are avaiable at OWM's forecast API page, at
// http://openweathermap.org/API#forecast
// Construct the URL for the TMDB query
// After reviewing the TMDB documentation for their API I've decided to
// stick with using the discovery end-point for project 1.
// I've found that there are many ways to get the highest rated movies (e.g.
// sort_by=vote_average.desc only or top_rated end-point). To keep it consistent
// and to help ensure newer movies, with a high number of votes are included I have
// combined various params. I have compared the returned JSON strings from
// top_rated and what I am using and feel the results are consistent while using
// the same end-point.

final String FORECAST_BASE_URL =
"http://api.themoviedb.org/3/discover/movie?";
final String QUERY_PARAM = "sort_by";
final String API_KEY = "api_key";

Uri builtUri = Uri.parse(FORECAST_BASE_URL).buildUpon()
.appendQueryParameter(QUERY_PARAM, params[0])
.appendQueryParameter(API_KEY, api_key)
.build();
// Get string for pref_sort_order_highest_rated.
String sort_by = getResources().getString(R.string.pref_sort_order_highest_rated);

Uri.Builder builtUri = Uri.parse(FORECAST_BASE_URL).buildUpon();
builtUri.appendQueryParameter(QUERY_PARAM, params[0]);
// I want to append more query params to the URI builder if the param is requesting
// the movies to be sorted by highest rated.
if ( params[0].equals(sort_by) ) {
builtUri.appendQueryParameter("vote_average.gte", "8.0")
.appendQueryParameter("vote_count.gte", "50");
}

builtUri.appendQueryParameter(API_KEY, api_key).build();

Log.w("myApp", "URI : " builtUri);

Expand Down
63 changes: 41 additions & 22 deletions app/src/main/java/com/example/android/popularmovies/Movie.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 17,29 @@ public class Movie implements Parcelable {

// TODO: change datatypes as needed
private String id;
private String poster_path;
private String original_title;
private String posterPath;
private String originalTitle;
private String overview;
private String vote_average;
private String release_date;
private String voteAverage;
private String releaseDate;

public Movie(String id, String poster_path, String original_title, String overview,
String vote_average, String release_date) {
this.id = id;
this.poster_path = poster_path;
this.original_title = original_title;
this.posterPath = poster_path;
this.originalTitle = original_title;
this.overview = overview;
this.vote_average = vote_average;
this.release_date = release_date;
this.voteAverage = vote_average;
this.releaseDate = release_date;
}

protected Movie(Parcel in) {
id = in.readString();
poster_path = in.readString();
original_title = in.readString();
posterPath = in.readString();
originalTitle = in.readString();
overview = in.readString();
vote_average = in.readString();
release_date = in.readString();
voteAverage = in.readString();
releaseDate = in.readString();
}

public static final Creator<Movie> CREATOR = new Creator<Movie>() {
Expand All @@ -62,27 62,46 @@ public int describeContents() {
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(poster_path);
dest.writeString(original_title);
dest.writeString(posterPath);
dest.writeString(originalTitle);
dest.writeString(overview);
dest.writeString(vote_average);
dest.writeString(release_date);
dest.writeString(voteAverage);
dest.writeString(releaseDate);
}

public String getId() {
return id;
}

public String getPosterPath() {
return posterPath;
}

public String getOriginalTitle() {
return originalTitle;
}

public String getOverview() {
return overview;
}

public String getVoteAverage() {
return voteAverage;
}

public String getPoster_path() {
return poster_path;
public String getReleaseDate() {
return releaseDate;
}

@Override
public String toString() {
return "Movie{"
"id='" id '\''
", poster_path='" poster_path '\''
", original_title='" original_title '\''
", posterPath='" posterPath '\''
", originalTitle='" originalTitle '\''
", overview='" overview '\''
", vote_average='" vote_average '\''
", release_date='" release_date '\''
", voteAverage='" voteAverage '\''
", releaseDate='" releaseDate '\''
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 1,77 @@
package com.example.android.popularmovies;

/**
* Created by boykoco on 09/22/2015.
*/

import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

/**
* Created by boykoco on 09/08/2015.
*/

/**
* A {@link PreferenceActivity} that presents a set of application settings.
* <p>
* See <a href="http://developer.android.com/design/patterns/settings.html">
* Android Design: Settings</a> for design guidelines and the <a
* href="http://developer.android.com/guide/topics/ui/settings.html">Settings
* API Guide</a> for more information on developing a Settings UI.
*/
public class SettingsActivity extends PreferenceActivity
implements Preference.OnPreferenceChangeListener {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Add 'general' preferences, defined in the XML file
// Yes, this function is deprecated, but it is actually the current best practice given
// that we're targeting Gingerbread!
addPreferencesFromResource(R.xml.pref_general);

// For all preferences, attach an OnPreferenceChangeListener so the UI summary can be
// updated when the preference changes.
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_sort_order_key)));
}

/**
* Attaches a listener so the summary is always updated with the preference value.
* Also fires the listener once, to initialize the summary (so it shows up before the value
* is changed.)
*/
private void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(this);

// Trigger the listener immediately with the preference's
// current value.
onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}

@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();

if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list (since they have separate labels/values).
ListPreference listPreference = (ListPreference) preference;
int prefIndex = listPreference.findIndexOfValue(stringValue);
if (prefIndex >= 0) {
preference.setSummary(listPreference.getEntries()[prefIndex]);
}
} else {
// For other preferences, set the summary to the value's simple string representation.
preference.setSummary(stringValue);
}
return true;
}

}
Loading

0 comments on commit 7e83a44

Please sign in to comment.