Skip to content

Commit

Permalink
Reconstruct the logic in now showing page, to handle and display list…
Browse files Browse the repository at this point in the history
… request correctly.
  • Loading branch information
Sheng-Xuan committed Mar 9, 2017
1 parent 444d98d commit ec4bd7f
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
import org.example.team_pigeon.movie_pigeon.eventCenter.UpdateMovieListEvent;
import org.example.team_pigeon.movie_pigeon.models.Cinema;
import org.example.team_pigeon.movie_pigeon.models.Movie;
import org.example.team_pigeon.movie_pigeon.models.Schedule;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
Expand Down Expand Up @@ -59,13 +61,16 @@ public class NowShowingFragment extends Fragment implements AdapterView.OnItemSe
private ArrayList<Cinema> gvCinemas = new ArrayList<>();
private ArrayList<Cinema> sbCinemas = new ArrayList<>();
private ArrayList<Cinema> cathayCinemas = new ArrayList<>();
private ArrayList<Movie> movieList,moviesOfTheDay;
private ArrayList<ArrayList<Movie>> oneWeekMovieList;
private ArrayList<Movie> movieList;
private ArrayList<Movie> moviesOfTheDay = new ArrayList<>();
private NowShowingTask nowShowingTask;
private List<String> dateListInString;
private List<Date> dateList;
private CinemaAdapter cinemaAdapter = null;
private NowShowingListAdapter nowShowingListAdapter = null;
private boolean isCinemasLoaded = false;
private int currentDay;


public NowShowingFragment() {
Expand Down Expand Up @@ -102,6 +107,50 @@ private void bindViews(View view) {
movieListView.setOnItemClickListener(this);
}

private ArrayList<ArrayList<Movie>> getOneWeekMovieList (ArrayList<Movie> movieList, List<Date> dateList) throws ParseException {
ArrayList<ArrayList<Movie>> oneWeekMovieList = new ArrayList<>();
for(int i=0;i<7;i++){
oneWeekMovieList.add(new ArrayList<Movie>());
}
for(int i = 0; i<dateList.size();i++){
for(int j = 0; j<movieList.size();j++){
Movie movie = movieList.get(j);
Date date = dateList.get(i);
ArrayList<String> showTime = convertToShowTimeArray(date,movie.getSchedule());
if(!showTime.isEmpty()){
movie.setShowTimes(showTime);
oneWeekMovieList.get(i).add(movie);
}
}
}
return oneWeekMovieList;
}

private ArrayList<String> convertToShowTimeArray(Date date, ArrayList<Schedule> scheduleArrayList) {
String timeString;
Calendar calendarOne = Calendar.getInstance();
Calendar calendarTwo = Calendar.getInstance();
calendarOne.setTime(date);
ArrayList<String> showTimeList = new ArrayList<>();
Date time;
SimpleDateFormat stringToDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
SimpleDateFormat dateToString = new SimpleDateFormat("HH:mm");
for (Schedule schedule : scheduleArrayList) {
timeString = schedule.getTime();
try {
time = stringToDate.parse(timeString);
} catch (ParseException e) {
Log.d(TAG,"Parsing schedule with a mistake. TimeString:"+timeString);
return null;
}
calendarTwo.setTime(time);
if (calendarOne.get(Calendar.DAY_OF_MONTH) == calendarTwo.get(Calendar.DAY_OF_MONTH)) {
showTimeList.add(dateToString.format(time));
}
}
return showTimeList;
}

private List<Date> getDateList(){
List<Date> week = new ArrayList<>();
Calendar timeToAdd = Calendar.getInstance();
Expand Down Expand Up @@ -135,19 +184,41 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
outletSpinner.setVisibility(View.VISIBLE);
dateSpinner.setVisibility(View.INVISIBLE);
}
else{
outletSpinner.setVisibility(View.GONE);
}
dateSpinner.setVisibility(View.GONE);
movieListView.setVisibility(View.GONE);
break;
case R.id.spinner_cinema_outlet:
if(position!=0) {
cinemaId = cinemaAdapter.getItem(position).getId();
nowShowingTask = new NowShowingTask();
nowShowingTask.execute(GET_MOVIES, cinemaId);
}
else {
dateSpinner.setVisibility(View.GONE);
}
movieListView.setVisibility(View.GONE);
break;
case R.id.spinner_date:
if(position!=0) {
nowShowingListAdapter = new NowShowingListAdapter(movieList, dateList.get(position-1), this.getContext());
movieListView.setAdapter(nowShowingListAdapter);
nowShowingListAdapter.notifyDataSetChanged();
currentDay = position-1;
if(nowShowingListAdapter==null) {
//init adapter
moviesOfTheDay.addAll(oneWeekMovieList.get(currentDay));
nowShowingListAdapter = new NowShowingListAdapter(moviesOfTheDay, this.getContext());
movieListView.setAdapter(nowShowingListAdapter);
}
else{
moviesOfTheDay.clear();
moviesOfTheDay.addAll(oneWeekMovieList.get(currentDay));
nowShowingListAdapter.notifyDataSetChanged();
}
movieListView.setVisibility(View.VISIBLE);
}
else {
movieListView.setVisibility(View.GONE);
}
break;
}
Expand Down Expand Up @@ -242,6 +313,11 @@ protected void onPostExecute(Void params) {
break;
case SUCCESSFUL_MOVIELIST:
Log.i(TAG,"Request is completed");
try {
oneWeekMovieList = getOneWeekMovieList(movieList,dateList);
} catch (ParseException e) {
e.printStackTrace();
}
dateSpinner.setAdapter(new ArrayAdapter<>(getActivity(), R.layout.spinner_list_item, dateListInString));
dateSpinner.setVisibility(View.VISIBLE);
break;
Expand Down Expand Up @@ -308,7 +384,7 @@ protected void getMovies(String cinemaId) {
}
//Convert json to Arraylist<Movie>
movieList = gson.fromJson(response.body().charStream(), new TypeToken<ArrayList<Movie>>() {}.getType());
if (cinemas.size() == 0) {
if (movieList.size() == 0) {
status = NO_RESULT;
return;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@
import org.example.team_pigeon.movie_pigeon.R;
import org.example.team_pigeon.movie_pigeon.configs.ImageConfig;
import org.example.team_pigeon.movie_pigeon.models.Movie;
import org.example.team_pigeon.movie_pigeon.models.Schedule;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
Expand All @@ -37,10 +33,9 @@ public class NowShowingListAdapter extends BaseAdapter {
private ImageLoader imageLoader = ImageLoader.getInstance();
private DisplayImageOptions options = new ImageConfig().getDisplayImageOption();

public NowShowingListAdapter(ArrayList<Movie> movieList, Date date, Context mContext) {
public NowShowingListAdapter(ArrayList<Movie> movieList, Context mContext) {
this.mContext = mContext;
this.movieList = movieList;
this.date = date;
}

public void addListItemToAdapter(ArrayList<Movie> list) {
Expand Down Expand Up @@ -80,41 +75,28 @@ public long getItemId(int position) {

@Override
public View getView(int position, View convertView, ViewGroup parent) {
NowShowingListAdapter.ViewHolder viewHolder;
ViewHolder viewHolder;
if (convertView == null) {
ArrayList<String> scheduleList = new ArrayList<>();
movie = movieList.get(position);
try {
scheduleList = getScheduleList(date, movie.getSchedule());
} catch (ParseException e) {
e.printStackTrace();
}
if (scheduleList.isEmpty()) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.empty_item, parent, false);
convertView.setVisibility(View.INVISIBLE);
} else {
convertView = LayoutInflater.from(mContext).inflate(R.layout.now_showing_list_item, parent, false);
viewHolder = new NowShowingListAdapter.ViewHolder();
convertView.setVisibility(View.VISIBLE);
viewHolder.txt_title = (TextView) convertView.findViewById(R.id.text_now_showing_list_title);
viewHolder.image_poster = (ImageView) convertView.findViewById(R.id.image_now_showing_list_poster);
viewHolder.txt_length = (TextView) convertView.findViewById(R.id.text_now_showing_list_length);
viewHolder.txt_special = (TextView) convertView.findViewById(R.id.text_now_showing_list_special);
viewHolder.grid_schedule = (GridView) convertView.findViewById(R.id.grid_schedule);
viewHolder.ratingBar = (RatingBar) convertView.findViewById(R.id.ratingBar_now_showing);
convertView.setTag(viewHolder);
viewHolder.txt_title.setText(movie.getTitle());
viewHolder.txt_special.setText(movie.getSchedule().get(0).getType());
viewHolder.txt_length.setText(movie.getLength() + " Min");
setRatingBar(viewHolder.ratingBar, movie);

viewHolder.grid_schedule.setAdapter(new ArrayAdapter<>(this.mContext, R.layout.now_showing_schedule_cell, scheduleList));
viewHolder.grid_schedule.setEnabled(false);
imageLoader.displayImage(movieList.get(position).getPosterURL(), viewHolder.image_poster, options);
}
convertView = LayoutInflater.from(mContext).inflate(R.layout.now_showing_list_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.txt_title = (TextView) convertView.findViewById(R.id.text_now_showing_list_title);
viewHolder.image_poster = (ImageView) convertView.findViewById(R.id.image_now_showing_list_poster);
viewHolder.txt_length = (TextView) convertView.findViewById(R.id.text_now_showing_list_length);
viewHolder.txt_special = (TextView) convertView.findViewById(R.id.text_now_showing_list_special);
viewHolder.grid_schedule = (GridView) convertView.findViewById(R.id.grid_schedule);
viewHolder.ratingBar = (RatingBar) convertView.findViewById(R.id.ratingBar_now_showing);
convertView.setTag(viewHolder);
} else {
viewHolder = (NowShowingListAdapter.ViewHolder) convertView.getTag();
viewHolder = (ViewHolder) convertView.getTag();
}
movie = movieList.get(position);
viewHolder.txt_title.setText(movie.getTitle());
viewHolder.txt_special.setText(movie.getSchedule().get(0).getType());
viewHolder.txt_length.setText(movie.getLength() + " Min");
setRatingBar(viewHolder.ratingBar, movie);
viewHolder.grid_schedule.setAdapter(new ArrayAdapter<>(this.mContext, R.layout.now_showing_schedule_cell, movie.getShowTimes()));
viewHolder.grid_schedule.setEnabled(false);
imageLoader.displayImage(movieList.get(position).getPosterURL(), viewHolder.image_poster, options);
return convertView;
}

Expand All @@ -136,24 +118,4 @@ private static class ViewHolder {
GridView grid_schedule;
RatingBar ratingBar;
}

private ArrayList<String> getScheduleList(Date date, ArrayList<Schedule> schedules) throws ParseException {
String timeString;
Calendar calendarOne = Calendar.getInstance();
Calendar calendarTwo = Calendar.getInstance();
calendarOne.setTime(date);
ArrayList<String> showTimeList = new ArrayList<>();
Date time;
SimpleDateFormat stringToDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
SimpleDateFormat dateToString = new SimpleDateFormat("HH:mm");
for (Schedule schedule : schedules) {
timeString = schedule.getTime();
time = stringToDate.parse(timeString);
calendarTwo.setTime(time);
if (calendarOne.get(Calendar.DAY_OF_MONTH) == calendarTwo.get(Calendar.DAY_OF_MONTH)) {
showTimeList.add(dateToString.format(time));
}
}
return showTimeList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class Movie implements Serializable{
@Expose
@SerializedName("showings")
private ArrayList<Schedule> schedule;
private ArrayList<String> showTimes;
private boolean isRated;
private boolean isReleased;
private boolean isBookmarked;
Expand Down Expand Up @@ -222,4 +223,12 @@ public ArrayList<Schedule> getSchedule() {
public void setSchedule(ArrayList<Schedule> schedule) {
this.schedule = schedule;
}

public ArrayList<String> getShowTimes() {
return showTimes;
}

public void setShowTimes(ArrayList<String> showTimes) {
this.showTimes = showTimes;
}
}
10 changes: 0 additions & 10 deletions frontend/Movie_Pigeon/app/src/main/res/layout/empty_item.xml

This file was deleted.

0 comments on commit ec4bd7f

Please sign in to comment.