Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/development-fronten…
Browse files Browse the repository at this point in the history
…d' into dev-gmx
  • Loading branch information
Carl26 committed Mar 19, 2017
2 parents 5737c4b + 7244c6f commit 5cb91d2
Show file tree
Hide file tree
Showing 21 changed files with 464 additions and 191 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
import okhttp3.Request;
import okhttp3.Response;

public class NowShowingFragment extends Fragment implements AdapterView.OnItemSelectedListener,AdapterView.OnItemClickListener{
private static final String TAG = "NowShowingFragment";
public class CinemaFragment extends Fragment implements AdapterView.OnItemSelectedListener,AdapterView.OnItemClickListener{
private static final String TAG = "CinemaFragment";
private static final String PROVIDER_GV = "gv";
private static final String PROVIDER_SB = "sb";
private static final String PROVIDER_CATHAY = "cathay";
Expand Down Expand Up @@ -73,14 +73,14 @@ public class NowShowingFragment extends Fragment implements AdapterView.OnItemSe
private int currentDay;


public NowShowingFragment() {
public CinemaFragment() {
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_now_showing, container, false);
View view = inflater.inflate(R.layout.fragment_cinema, container, false);
bindViews(view);
dateList = getDateList();
dateListInString = getDateListToString(dateList);
Expand Down Expand Up @@ -262,20 +262,26 @@ public void onNothingSelected(AdapterView<?> parent) {

@Subscribe
public void onAddMovieToMovieListEvent(AddMovieToMovieListEvent event){
nowShowingListAdapter.addMovieItemToAdapter(event.movie, event.position);
Log.i(TAG, "New movie is added to local list");
if(nowShowingListAdapter!=null) {
nowShowingListAdapter.addMovieItemToAdapter(event.movie, event.position);
Log.i(TAG, "New movie is added to local list");
}
}

@Subscribe
public void onDeleteMovieFromMovieListEvent(DeleteMovieFromMovieListEvent event){
nowShowingListAdapter.removeMovieItemToAdapter(event.position);
Log.i(TAG, "A movie is removed from local list");
if(nowShowingListAdapter!=null) {
nowShowingListAdapter.removeMovieItemToAdapter(event.position);
Log.i(TAG, "A movie is removed from local list");
}
}

@Subscribe
public void onUpdateMovieListEvent(UpdateMovieListEvent event){
nowShowingListAdapter.updateMovieItemToAdapter(event.movie, event.position);
Log.i(TAG, "A movie is updated to local list at pos "+String.valueOf(event.position));
if(nowShowingListAdapter!=null) {
nowShowingListAdapter.updateMovieItemToAdapter(event.movie, event.position);
Log.i(TAG, "A movie is updated to local list at pos " + String.valueOf(event.position));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ protected void onCreate(Bundle savedInstanceState) {
fragmentTransaction.replace(R.id.fl_content,moviePageFragment);
fragmentTransaction.commit();
}
else if(type.equals("search")){
SearchPageFragment searchPageFragment = new SearchPageFragment();
searchPageFragment.setArguments(argument);
android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fl_content, searchPageFragment);
fragmentTransaction.commit();
}
//Handle movie list requests
else {
MovieListFragment movieListFragment = new MovieListFragment();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.example.team_pigeon.movie_pigeon;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.WindowManager;

/**
* Created by SHENGX on 2017/3/15.
*/

public class LoadingDialog extends ProgressDialog {
private Context mContext;
public LoadingDialog(Context context){
super(context);
mContext = context;
}
public LoadingDialog(Context context, int theme){
super(context,theme);
mContext = context;
}
@Override
protected void onCreate(Bundle save){
super.onCreate(save);
init(mContext);
}
private void init (Context context){
setCancelable(false);
setCanceledOnTouchOutside(false);
setContentView(R.layout.loading_dialog);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
getWindow().setAttributes(params);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public class MovieListFragment extends Fragment implements AdapterView.OnItemCli
private android.app.FragmentManager fragmentManager;
private ArrayList<Movie> movies;
private ListView list_movies;
private SearchMoreTask searchMoreTask;
private MovieListAdapter movieListAdapter;
private RequestHttpBuilderSingleton searchRequestHttpBuilder;
private Gson gson = new Gson();
Expand Down Expand Up @@ -76,26 +75,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
if(!EventBus.getDefault().isRegistered(this)){
EventBus.getDefault().register(this);
}
if(type.equals("search")){
toolbar.setSubtitle(bundle.getString("count"));
list_movies.setOnScrollListener(new PauseOnScrollListener(ImageLoader.getInstance(),true,true,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();
}
}
}));
}
else{
list_movies.setOnScrollListener(new PauseOnScrollListener(ImageLoader.getInstance(),true,true));
}
list_movies.setOnScrollListener(new PauseOnScrollListener(ImageLoader.getInstance(),true,true));
return view;
}

Expand All @@ -121,112 +101,29 @@ public void onDestroy() {
EventBus.getDefault().unregister(this);
}

@Subscribe
public void onEvent(AddMovieToMovieListEvent event){
movieListAdapter.addMovieItemToAdapter(event.movie, event.position);
Log.i(TAG, "New movie is added to local list");
}

@Subscribe
public void onEvent(DeleteMovieFromMovieListEvent event){
movieListAdapter.removeMovieItemToAdapter(event.position);
Log.i(TAG, "A movie is removed from local list");
if(movieListAdapter!=null) {
movieListAdapter.removeMovieItemToAdapter(event.position);
Log.i(TAG, "A movie is removed from local list");
}
}

@Subscribe
public void onEvent(UpdateMovieListEvent event){
movieListAdapter.updateMovieItemToAdapter(event.movie, event.position);
Log.i(TAG, "A movie is updated to local list");
}

//Async thread to handle search request
private class SearchMoreTask extends AsyncTask<Void, Integer, Void> {
private final int SUCCESSFUL = 0;
private final int ERROR = 1;
private final int NO_RESULT = 2;
private final int NO_INTERNET = 3;
private final int NO_MORE_RESULT = 4;
int status;

@Override
protected Void doInBackground(Void... params) {
try {
searchRequestHttpBuilder = RequestHttpBuilderSingleton.getInstance();
OkHttpClient client = searchRequestHttpBuilder.getClient();
Request request = searchRequestHttpBuilder.getNextSearchRequest();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) {
Log.i(TAG, "connect failed");
status = ERROR;
throw new IOException("Unexpected code" + response);
}
//Convert json to Arraylist<Movie>
movieListWithCount = gson.fromJson(response.body().charStream(), new TypeToken<MovieWithCount>() {}.getType());
resultCount = movieListWithCount.getCount();
searchMovieList = movieListWithCount.getMovies();

if (resultCount == 0) {
status = NO_RESULT;
}
else if(searchMovieList.size() < searchRequestHttpBuilder.getLimit()){
status = NO_MORE_RESULT;
}
else {
status = SUCCESSFUL;
}
return null;
} catch (IOException e) {
status = NO_INTERNET;
Log.e(TAG, e.getMessage());
}
return null;

if(movieListAdapter!=null) {
movieListAdapter.updateMovieItemToAdapter(event.movie, event.position);
Log.i(TAG, "A movie is updated to local list");
}
}

@Override
protected void onPreExecute() {
Log.i(TAG, "New search request is initialised");
list_movies.addFooterView(footerView);
return;
@Subscribe
public void onEvent(AddMovieToMovieListEvent event){
if(movieListAdapter!=null) {
movieListAdapter.addMovieItemToAdapter(event.movie, event.position);
Log.i(TAG, "A movie is added to local list");
}
}

@Override
protected void onPostExecute(Void params) {
switch (status) {
case SUCCESSFUL:
Log.i(TAG, "Search is completed");
list_movies.removeFooterView(footerView);
movieListAdapter.addListItemToAdapter(searchMovieList);
isLoading = false;
break;

case ERROR:
Toast.makeText(getActivity(), "Connection error, please check your connection", Toast.LENGTH_SHORT).show();
list_movies.removeFooterView(footerView);
isLoading = false;
break;

case NO_RESULT:
Toast.makeText(getActivity(), "Sorry, there is no more result", Toast.LENGTH_SHORT).show();
list_movies.removeFooterView(footerView);
noMoreResult = true;
isLoading = false;
break;

case NO_MORE_RESULT:
list_movies.removeFooterView(footerView);
noMoreResult = true;
isLoading = false;
movieListAdapter.addListItemToAdapter(searchMovieList);
break;

case NO_INTERNET:
Toast.makeText(getActivity(), "Connection error, please make sure that you have Internet connection.", Toast.LENGTH_SHORT).show();
list_movies.removeFooterView(footerView);
isLoading = false;
break;
}

}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.example.team_pigeon.movie_pigeon;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
Expand Down Expand Up @@ -37,21 +38,21 @@ public class RecommendationFragment extends Fragment implements AdapterView.OnIt
private Gson gson = new Gson();
private Task nowShowingTask,recommendationTask,searchTask;
private Bundle arguments;
private ArrayList<Movie> searchMovieList;
private ArrayList<Movie> nowShowingMovieList;
private ArrayList<Movie> recommendedMovieList;
private ArrayList<Movie> searchMovieList, nowShowingMovieList, recommendedMovieList, filteredNowShowingList;
private HorizontalListAdapter nowShowingMovieAdapter, recommendedMovieAdapter;
private MovieWithCount movieWithCount;
private GridView nowShowingGrid, recommendedGrid;
private int resultCount = 0;
private RequestHttpBuilderSingleton searchRequestHttpBuilder = RequestHttpBuilderSingleton.getInstance();
private LoadingDialog loadingDialog;

public RecommendationFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
loadingDialog = new LoadingDialog(this.getActivity(),R.style.LoadingDialog);
View view = inflater.inflate(R.layout.fragment_recommendation, container, false);
searchView = (SearchView) view.findViewById(R.id.search_view);
nowShowingGrid = (GridView) view.findViewById(R.id.grid_now_showing);
Expand All @@ -64,6 +65,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
@Override
public boolean onQueryTextSubmit(String query) {
Log.d(TAG, "Search query submit = " + query);
filteredNowShowingList = filterNowShowing(nowShowingMovieList,query);
searchTask = new Task();
searchTask.execute("search",query);
return false;
Expand Down Expand Up @@ -111,6 +113,17 @@ private void setGridView(GridView gridView, int size){
gridView.setNumColumns(size);
}

private ArrayList<Movie> filterNowShowing(ArrayList<Movie> allNowShowing, String keywords){
ArrayList<Movie> filteredMovies = new ArrayList<>();
keywords = keywords.toLowerCase();
for(Movie movie:allNowShowing){
if(movie.getTitle().toLowerCase().contains(keywords)){
filteredMovies.add(movie);
}
}
return filteredMovies;
}

private void setOnclickListener(GridView gridView){
gridView.setOnItemClickListener(this);
}
Expand Down Expand Up @@ -217,17 +230,20 @@ protected Void doInBackground(String... params) {
@Override
protected void onPreExecute() {
Log.i(TAG, "Async request is initialised");
loadingDialog.show();
return;
}

@Override
protected void onPostExecute(Void params) {
loadingDialog.dismiss();
switch (status) {
case SUCCESSFUL_SEARCH:
Log.i(TAG, "Search is completed");
Intent displayActivityIntent = new Intent(getActivity(), DisplayActivity.class);
arguments = new Bundle();
arguments.putSerializable("movieList", searchMovieList);
arguments.putSerializable("nowShowingList",filteredNowShowingList);
arguments.putSerializable("allList", searchMovieList);
arguments.putString("type","search");
arguments.putString("title", "Result of '" + searchRequestHttpBuilder.getKeywords() + "'");
arguments.putString("count", "Found "+String.valueOf(resultCount)+ " movies");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ private void request(String query) {
} else if (u3rdParty.equalsIgnoreCase("The Movie DB")) {
registrationURL = "http://128.199.167.57:8080/api/tmdb";
}

connection = (HttpURLConnection) new URL(registrationURL).openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.example.team_pigeon.movie_pigeon;

import java.util.concurrent.TimeUnit;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
Expand Down Expand Up @@ -123,7 +125,12 @@ public void setOffset(int offset) {

public OkHttpClient getClient() {
if(client == null){
client = new OkHttpClient();
//New client with timeout setting
client = new OkHttpClient.Builder().
connectTimeout(10, TimeUnit.SECONDS).
readTimeout(10,TimeUnit.SECONDS).
writeTimeout(10,TimeUnit.SECONDS).
build();
}
return client;
}
Expand Down
Loading

0 comments on commit 5cb91d2

Please sign in to comment.