Created
July 16, 2016 15:00
-
-
Save jimitjaishwal/0ef45f1146845ef16e429283b2cac254 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.android.jimitjaishwal.movies.app.Details; | |
| import android.content.ContentUris; | |
| import android.content.ContentValues; | |
| import android.content.Intent; | |
| import android.database.Cursor; | |
| import android.net.Uri; | |
| import android.os.Bundle; | |
| import android.support.annotation.Nullable; | |
| import android.support.v4.app.Fragment; | |
| import android.support.v4.app.LoaderManager; | |
| import android.support.v4.content.CursorLoader; | |
| import android.support.v4.content.Loader; | |
| import android.util.Log; | |
| import android.view.LayoutInflater; | |
| import android.view.View; | |
| import android.view.ViewGroup; | |
| import android.widget.ImageButton; | |
| import android.widget.ImageView; | |
| import com.android.jimitjaishwal.movies.app.Models.MovieModel; | |
| import com.android.jimitjaishwal.movies.app.Models.MovieReviewResponse; | |
| import com.android.jimitjaishwal.movies.app.Models.MovieReviews; | |
| import com.android.jimitjaishwal.movies.app.Models.VideoResponse; | |
| import com.android.jimitjaishwal.movies.app.Models.Videos; | |
| import com.android.jimitjaishwal.movies.app.MovieDataBase.MovieContract; | |
| import com.android.jimitjaishwal.movies.app.R; | |
| import com.android.jimitjaishwal.movies.app.Service.MDB_Service; | |
| import com.squareup.picasso.Picasso; | |
| import java.util.ArrayList; | |
| import retrofit2.Call; | |
| import retrofit2.Callback; | |
| import retrofit2.Response; | |
| import retrofit2.Retrofit; | |
| import retrofit2.converter.gson.GsonConverterFactory; | |
| /** | |
| * A placeholder fragment containing a simple view. | |
| */ | |
| public class Details_ActivityFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> { | |
| private static final String LOG = Details_ActivityFragment.class.getSimpleName(); | |
| private final String BASE_URL = "http://api.themoviedb.org/3/"; | |
| private final String api_Key = "149c3ccaa00eeb61331e4da7734fd1af"; | |
| private ArrayList<MovieReviews> reviewsArrayList; | |
| ArrayList<Videos> videosArrayList; | |
| ImageView imageView; | |
| ImageButton imageButton; | |
| private static final int CURSOR_LOADER_ID = 1; | |
| Cursor cursor; | |
| View rootView; | |
| public Details_ActivityFragment() { | |
| } | |
| public static final String[] MOVIE_DERAILS_COLUMNS = { | |
| MovieContract.Movies.TABLE_NAME + "." + MovieContract.Movies._ID, | |
| MovieContract.Movies.COLUMN_MOVIE_ID, | |
| MovieContract.Movies.COLUMN_BACKDROP_PATH, | |
| MovieContract.Movies.COLUMN_OVERVIEW, | |
| MovieContract.Movies.COLUMN_POSTER_PATH, | |
| MovieContract.Movies.COLUMN_ORIGINAL_TITLE, | |
| MovieContract.Movies.COLUMN_VOTE_AVERAGE, | |
| MovieContract.Movies.COLUMN_RELEASE_DATE | |
| }; | |
| public static final int _ID = 0; | |
| public static final int COLUMN_MOVIE_ID = 1; | |
| public static final int COLUMN_BACKDROP_PATH = 2; | |
| public static final int COLUMN_OVERVIEW = 3; | |
| public static final int COLUMN_POSTER_PATH = 4; | |
| public static final int COLUMN_ORIGINAL_TITLE = 5; | |
| public static final int COLUMN_VOTE_AVERAGE = 6; | |
| public static final int COLUMN_RELEASE_DATE = 7; | |
| @Override | |
| public void onCreate(@Nullable Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| getMovieTrailers(); | |
| } | |
| @Override | |
| public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
| Bundle savedInstanceState) { | |
| rootView = inflater.inflate(R.layout.fragment_details_, container, false); | |
| return rootView; | |
| } | |
| @Override | |
| public void onActivityCreated(@Nullable Bundle savedInstanceState) { | |
| super.onActivityCreated(savedInstanceState); | |
| getLoaderManager().initLoader(CURSOR_LOADER_ID, null, this); | |
| } | |
| public void getMovieReviews(MovieModel movies) { | |
| Retrofit retrofit = new Retrofit.Builder() | |
| .baseUrl(BASE_URL) | |
| .addConverterFactory(GsonConverterFactory.create()) | |
| .build(); | |
| MDB_Service service = retrofit.create(MDB_Service.class); | |
| Call<MovieReviewResponse> call = service.getMovieReviews(movies.getId(), api_Key); | |
| call.enqueue(new Callback<MovieReviewResponse>() { | |
| @Override | |
| public void onResponse(Call<MovieReviewResponse> call, Response<MovieReviewResponse> response) { | |
| reviewsArrayList = response.body().getResults(); | |
| } | |
| @Override | |
| public void onFailure(Call<MovieReviewResponse> call, Throwable t) { | |
| } | |
| }); | |
| } | |
| public void getMovieTrailers() { | |
| Retrofit retrofit = new Retrofit.Builder() | |
| .baseUrl(BASE_URL) | |
| .addConverterFactory(GsonConverterFactory.create()) | |
| .build(); | |
| MDB_Service service = retrofit.create(MDB_Service.class); | |
| Call<VideoResponse> call = | |
| service.getMovieVideos(209112, api_Key); | |
| call.enqueue(new Callback<VideoResponse>() { | |
| @Override | |
| public void onResponse(Call<VideoResponse> call, Response<VideoResponse> response) { | |
| videosArrayList = response.body().getResults(); | |
| for (final Videos videos : videosArrayList) { | |
| addMovieTrailers(209112 | |
| , videos.getName(), videos.getKey()); | |
| } | |
| } | |
| @Override | |
| public void onFailure(Call<VideoResponse> call, Throwable t) { | |
| } | |
| }); | |
| } | |
| public void watchYoutubeVideo(String videos) { | |
| Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=" + videos)); | |
| startActivity(intent); | |
| } | |
| public long addMovieReviewsIntoDatabase(int movieId, String id, String author, String Content) { | |
| long reviewID; | |
| Cursor reviewCursor = getActivity().getContentResolver().query( | |
| MovieContract.Reviews.CONTENT_URI, | |
| new String[]{MovieContract.Reviews._ID}, | |
| MovieContract.Reviews.COLUMN_MOVIE_ID + "=?", | |
| new String[]{id}, | |
| null | |
| ); | |
| if (reviewCursor.moveToFirst()) { | |
| int reviewIndex = reviewCursor.getColumnIndex(MovieContract.Reviews._ID); | |
| reviewID = reviewCursor.getInt(reviewIndex); | |
| } else { | |
| ContentValues values = new ContentValues(); | |
| values.put(MovieContract.Reviews.COLUMN_MOVIE_ID, movieId); | |
| values.put(MovieContract.Reviews.COLUMN_AUTHOR, author); | |
| values.put(MovieContract.Reviews.COLUMN_CONTENT, Content); | |
| Uri buildUri = getActivity().getContentResolver().insert(MovieContract.Reviews.CONTENT_URI, values); | |
| reviewID = ContentUris.parseId(buildUri); | |
| } | |
| reviewCursor.close(); | |
| return reviewID; | |
| } | |
| @Override | |
| public Loader<Cursor> onCreateLoader(int id, Bundle args) { | |
| Intent i = getActivity().getIntent(); | |
| if (i == null) { | |
| return null; | |
| } | |
| return new CursorLoader(getActivity(), | |
| i.getData(), | |
| MOVIE_DERAILS_COLUMNS, | |
| null, | |
| null, | |
| null); | |
| } | |
| public long addMovieTrailers(int movieId, String trailerName, String trailer_key) { | |
| long videoRowId; | |
| Cursor videoCursor = getActivity().getContentResolver() | |
| .query(MovieContract.MovieTrailers.CONTENT_URI, | |
| new String[]{MovieContract.MovieTrailers._ID}, | |
| MovieContract.Movies.COLUMN_MOVIE_ID + " = ?", | |
| new String[]{Integer.toString(movieId)}, | |
| null | |
| ); | |
| if (videoCursor.moveToFirst()) { | |
| int columnIndex = videoCursor.getColumnIndex(MovieContract.MovieTrailers._ID); | |
| videoRowId = videoCursor.getLong(columnIndex); | |
| } else { | |
| ContentValues values = new ContentValues(); | |
| values.put(MovieContract.MovieTrailers.COLUMN_MOVIE_ID, movieId); | |
| values.put(MovieContract.MovieTrailers.COLUMN_TRAILER_NAME, trailerName); | |
| values.put(MovieContract.MovieTrailers.COLUMN_TRAILER_KEY, trailer_key); | |
| Uri insertedUri = getActivity().getContentResolver().insert(MovieContract.MovieTrailers.CONTENT_URI, values); | |
| videoRowId = ContentUris.parseId(insertedUri); | |
| } | |
| videoCursor.close(); | |
| return videoRowId; | |
| } | |
| @Override | |
| public void onLoadFinished(Loader<Cursor> loader, final Cursor data) { | |
| imageView = (ImageView) rootView.findViewById(R.id.backDrown_path); | |
| final String baseUrl = "http://image.tmdb.org/t/p/w185" + | |
| data.getString(COLUMN_BACKDROP_PATH); | |
| Log.d(LOG, "onLoadFinished: " + baseUrl); | |
| Picasso.with(getActivity()).load(baseUrl).into(imageView); | |
| imageButton = (ImageButton) rootView.findViewById(R.id.play_Button); | |
| imageButton.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| watchYoutubeVideo | |
| (data.getString(data.getColumnIndex(MovieContract.MovieTrailers.COLUMN_TRAILER_KEY))); | |
| } | |
| }); | |
| } | |
| @Override | |
| public void onLoaderReset(Loader<Cursor> loader) { | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment