Skip to content

Commit

Permalink
Update logic of movie list and movie page, which are used for search,…
Browse files Browse the repository at this point in the history
… my ratings and my bookmarks.
  • Loading branch information
Sheng-Xuan committed Feb 20, 2017
1 parent 5f21c13 commit c92f331
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package org.example.team_pigeon.movie_pigeon;

import android.app.Fragment;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
Expand All @@ -21,6 +18,7 @@

import org.example.team_pigeon.movie_pigeon.adapters.MovieListAdapter;
import org.example.team_pigeon.movie_pigeon.models.Movie;
import org.example.team_pigeon.movie_pigeon.models.MovieWithCount;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -40,42 +38,51 @@ public class MovieListFragment extends Fragment implements AdapterView.OnItemCli
private ListView list_movies;
private SearchMoreTask searchMoreTask;
private MovieListAdapter movieListAdapter;
private ListRequestHttpBuilderSingleton searchRequestHttpBuilder;
private RequestHttpBuilderSingleton searchRequestHttpBuilder;
private Gson gson = new Gson();
public View footerView;
public boolean isLoading = false;
public boolean noMoreResult = false;
private ArrayList<Movie> searchMovieList;
private MovieWithCount movieListWithCount;
private int resultCount;
private Toolbar toolbar;
private Bundle bundle;
private String type;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
//Load common views
fragmentManager = getFragmentManager();
movies = (ArrayList<Movie>)getArguments().getSerializable("searchMovieList");
View view = inflater.inflate(R.layout.fragment_movie_list,container,false);
Toolbar toolbar = (Toolbar)getActivity().findViewById(R.id.toolbar_display_page);
toolbar.setTitle(getActivity().getIntent().getBundleExtra("bundle").getString("title"));
list_movies = (ListView)view.findViewById(R.id.list_movies);
footerView = inflater.inflate(R.layout.footer_load_more,null);
toolbar = (Toolbar)getActivity().findViewById(R.id.toolbar_display_page);
//Load pass-in content
bundle = getActivity().getIntent().getBundleExtra("bundle");
type = bundle.getString("type");
movies = (ArrayList<Movie>)getArguments().getSerializable("movieList");
movieListAdapter = new MovieListAdapter(movies,getActivity());
list_movies.setAdapter(movieListAdapter);
toolbar.setTitle(bundle.getString("title"));
list_movies.setOnItemClickListener(this);
if(type.equals("search")){
toolbar.setSubtitle(bundle.getString("count"));
list_movies.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}

//Scrolling to load more function will be implement in the next version
list_movies.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {

}

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(view.getLastVisiblePosition() == totalItemCount-1 && list_movies.getCount() >= 20 && !isLoading && !noMoreResult) {
isLoading = true;
searchMoreTask = new SearchMoreTask();
searchMoreTask.execute();
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(view.getLastVisiblePosition() == totalItemCount-1 && list_movies.getCount() >= 20 && !isLoading && !noMoreResult) {
isLoading = true;
searchMoreTask = new SearchMoreTask();
searchMoreTask.execute();
}
}
}
});
list_movies.setOnItemClickListener(this);
});
}
return view;
}

Expand All @@ -85,8 +92,8 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
MoviePageFragment moviePageFragment = new MoviePageFragment();
Bundle bundle = new Bundle();
bundle.putSerializable("movie", movieListAdapter.getItem(position));
Toolbar toolbar = (Toolbar)getActivity().findViewById(R.id.toolbar_display_page);
toolbar.setTitle(movieListAdapter.getItem(position).getTitle());
toolbar.setSubtitle(null);
moviePageFragment.setArguments(bundle);
fragmentTransaction.replace(R.id.fl_content,moviePageFragment);
fragmentTransaction.addToBackStack(null);
Expand All @@ -105,7 +112,7 @@ private class SearchMoreTask extends AsyncTask<Void, Integer, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
searchRequestHttpBuilder = ListRequestHttpBuilderSingleton.getInstance();
searchRequestHttpBuilder = RequestHttpBuilderSingleton.getInstance();
OkHttpClient client = searchRequestHttpBuilder.getClient();
Request request = searchRequestHttpBuilder.getNextSearchRequest();
Response response = client.newCall(request).execute();
Expand All @@ -115,13 +122,14 @@ protected Void doInBackground(Void... params) {
throw new IOException("Unexpected code" + response);
}
//Convert json to Arraylist<Movie>
searchMovieList = gson.fromJson(response.body().charStream(), new TypeToken<ArrayList<Movie>>() {
}.getType());
Log.i(TAG,String.valueOf(searchMovieList.size()));
movieListWithCount = gson.fromJson(response.body().charStream(), new TypeToken<MovieWithCount>() {}.getType());
resultCount = movieListWithCount.getCount();
searchMovieList = movieListWithCount.getMovies();

if (searchMovieList.size() == 0) {
if (resultCount == 0) {
status = NO_RESULT;
} else if(searchMovieList.size() < searchRequestHttpBuilder.getLimit()){
}
else if(searchMovieList.size() < searchRequestHttpBuilder.getLimit()){
status = NO_MORE_RESULT;
}
else {
Expand Down Expand Up @@ -177,6 +185,7 @@ protected void onPostExecute(Void params) {
Toast.makeText(getContext(), "Connection error, please make sure that you have Internet connection.", Toast.LENGTH_SHORT).show();
list_movies.removeFooterView(footerView);
isLoading = false;
break;
}

}
Expand Down
Loading

0 comments on commit c92f331

Please sign in to comment.