From a52e4daf22f9711765813f756eed0023834378b3 Mon Sep 17 00:00:00 2001 From: Guo Mingxuan Date: Thu, 23 Mar 2017 17:00:57 +0800 Subject: [PATCH] Implemented cinema list structure --- .../movie_pigeon/CinemaFragment.java | 116 ++++++++++++++---- .../movie_pigeon/CinemaListAdapter.java | 54 ++++++++ .../movie_pigeon/GlobalReceiver.java | 7 +- .../src/main/res/layout/cinema_list_item.xml | 21 ++++ .../main/res/layout/fragment_cinema_new.xml | 11 ++ 5 files changed, 186 insertions(+), 23 deletions(-) create mode 100644 frontend/Movie_Pigeon/app/src/main/java/org/example/team_pigeon/movie_pigeon/CinemaListAdapter.java create mode 100644 frontend/Movie_Pigeon/app/src/main/res/layout/cinema_list_item.xml create mode 100644 frontend/Movie_Pigeon/app/src/main/res/layout/fragment_cinema_new.xml diff --git a/frontend/Movie_Pigeon/app/src/main/java/org/example/team_pigeon/movie_pigeon/CinemaFragment.java b/frontend/Movie_Pigeon/app/src/main/java/org/example/team_pigeon/movie_pigeon/CinemaFragment.java index f96aa83..4db4277 100644 --- a/frontend/Movie_Pigeon/app/src/main/java/org/example/team_pigeon/movie_pigeon/CinemaFragment.java +++ b/frontend/Movie_Pigeon/app/src/main/java/org/example/team_pigeon/movie_pigeon/CinemaFragment.java @@ -1,17 +1,27 @@ package org.example.team_pigeon.movie_pigeon; import android.content.Intent; +import android.content.IntentFilter; +import android.graphics.Color; import android.os.AsyncTask; import android.os.Bundle; +import android.os.Handler; +import android.os.Message; import android.support.v4.app.Fragment; +import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; +import android.widget.LinearLayout; import android.widget.ListView; +import android.widget.ScrollView; import android.widget.Spinner; +import android.widget.TableLayout; +import android.widget.TableRow; +import android.widget.TextView; import android.widget.Toast; import com.google.gson.Gson; @@ -62,6 +72,7 @@ public class CinemaFragment extends Fragment implements AdapterView.OnItemSelect private ArrayList gvCinemas = new ArrayList<>(); private ArrayList sbCinemas = new ArrayList<>(); private ArrayList cathayCinemas = new ArrayList<>(); + private ArrayList allCinemas; private ArrayList> oneWeekMovieList; private ArrayList movieList; private ArrayList moviesOfTheDay = new ArrayList<>(); @@ -73,6 +84,13 @@ public class CinemaFragment extends Fragment implements AdapterView.OnItemSelect private boolean isCinemasLoaded = false; private int currentDay; private TimeUtil timeUtil = new TimeUtil(); + private GlobalReceiver receiver; + private final int cinemasLoaded = 1; + private TableLayout cinemaTable; + private ScrollView sv; + private LinearLayout ll; + private CinemaListAdapter cinemaListAdapter; + private ListView cinemaList; public CinemaFragment() { @@ -82,22 +100,73 @@ public CinemaFragment() { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.fragment_cinema, container, false); - bindViews(view); - dateList = timeUtil.getDateList(); - dateListInString = timeUtil.getDateListToString_MMDDE(dateList); - brands = this.getActivity().getResources().getStringArray(R.array.cinemaBrands); - brandAdapter = new ArrayAdapter<>(this.getActivity(), R.layout.spinner_list_item,brands); - brandSpinner.setAdapter(brandAdapter); - nowShowingTask = new NowShowingTask(); - nowShowingTask.execute(GET_CINEMAS); - if(!EventBus.getDefault().isRegistered(this)){ - EventBus.getDefault().register(this); +// View view = inflater.inflate(R.layout.fragment_cinema, container, false); +// bindViews(view); +// dateList = timeUtil.getDateList(); +// dateListInString = timeUtil.getDateListToString_MMDDE(dateList); +// brands = this.getActivity().getResources().getStringArray(R.array.cinemaBrands); +// brandAdapter = new ArrayAdapter<>(this.getActivity(), R.layout.spinner_list_item,brands); +// brandSpinner.setAdapter(brandAdapter); +// nowShowingTask = new NowShowingTask(); +// nowShowingTask.execute(GET_CINEMAS); +// if(!EventBus.getDefault().isRegistered(this)){ +// EventBus.getDefault().register(this); +// } + View view = inflater.inflate(R.layout.fragment_cinema_new, container, false); +// sv = (ScrollView) view.findViewById(R.id.cinema_scroll_view); +// ll = (LinearLayout) view.findViewById(R.id.ll); +// cinemaTable = new TableLayout(getContext()); +// cinemaTable.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT)); +// sv.addView(cinemaTable); + cinemaList = (ListView) view.findViewById(R.id.cinema_list); + if (!isCinemasLoaded) { // make sure cinemaTable is loaded + loadCinemaList(); } + receiver = new GlobalReceiver(new Handler() { + public void handleMessage(Message msg) { + final int what = msg.what; + switch(what) { + case cinemasLoaded: + Log.i(TAG, "Successfully loaded cinemas"); + showCinemas(); + break; + } + } + }); + IntentFilter filter = new IntentFilter(); + filter.addAction("cinemasLoaded"); + getContext().registerReceiver(receiver, filter); view.requestFocus(); return view; } + private void loadCinemaList() { + allCinemas = new ArrayList<>(); + nowShowingTask = new NowShowingTask(); + nowShowingTask.execute(GET_CINEMAS); + } + + @Override + public void onResume(){ + super.onResume(); + } + + @Override + public void onPause() { + super.onPause(); + Log.i(TAG, "Cinema fragment paused"); + getContext().unregisterReceiver(receiver); + } + + private void showCinemas() { + Log.i(TAG, "Preparing to generate cinema table"); + // sort cinemas according to distance + + // load table rows containing cinema name and distance + cinemaListAdapter = new CinemaListAdapter(allCinemas, getContext()); + cinemaList.setAdapter(cinemaListAdapter); + } + private void bindViews(View view) { brandSpinner = (Spinner) view.findViewById(R.id.spinner_cinema_brand); outletSpinner = (Spinner) view.findViewById(R.id.spinner_cinema_outlet); @@ -303,6 +372,8 @@ protected void onPostExecute(Void params) { case SUCCESSFUL_CINEMALIST: Log.i(TAG, "Requset is completed"); isCinemasLoaded = true; + Intent intent = new Intent("cinemasLoaded"); + getContext().sendBroadcast(intent); break; case SUCCESSFUL_MOVIELIST: Log.i(TAG,"Request is completed"); @@ -347,17 +418,18 @@ protected void getCinemas() { } else { status = SUCCESSFUL_CINEMALIST; for (Cinema cinema : cinemas) { - switch (cinema.getProvider()) { - case PROVIDER_CATHAY: - cathayCinemas.add(cinema); - break; - case PROVIDER_GV: - gvCinemas.add(cinema); - break; - case PROVIDER_SB: - sbCinemas.add(cinema); - break; - } +// switch (cinema.getProvider()) { +// case PROVIDER_CATHAY: +// cathayCinemas.add(cinema); +// break; +// case PROVIDER_GV: +// gvCinemas.add(cinema); +// break; +// case PROVIDER_SB: +// sbCinemas.add(cinema); +// break; +// } + allCinemas.add(cinema); } } } catch (IOException e) { diff --git a/frontend/Movie_Pigeon/app/src/main/java/org/example/team_pigeon/movie_pigeon/CinemaListAdapter.java b/frontend/Movie_Pigeon/app/src/main/java/org/example/team_pigeon/movie_pigeon/CinemaListAdapter.java new file mode 100644 index 0000000..b772f8f --- /dev/null +++ b/frontend/Movie_Pigeon/app/src/main/java/org/example/team_pigeon/movie_pigeon/CinemaListAdapter.java @@ -0,0 +1,54 @@ +package org.example.team_pigeon.movie_pigeon; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.TextView; + +import org.example.team_pigeon.movie_pigeon.models.Cinema; + +import java.util.ArrayList; + +/** + * Created by Guo Mingxuan on 23/3/2017. + */ + +public class CinemaListAdapter extends BaseAdapter { + private ArrayList cinemaList; + private Cinema cinema; + private Context mContext; + + public CinemaListAdapter(ArrayList cinemaList, Context context) { + this.cinemaList = cinemaList; + mContext = context; + } + @Override + public int getCount() { + int count = cinemaList.size(); + return count; + } + + @Override + public Cinema getItem(int position) { + return cinemaList.get(position); + } + + @Override + public long getItemId(int position) { + return position; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + cinema = cinemaList.get(position); + convertView = LayoutInflater.from(mContext).inflate(R.layout.cinema_list_item, null); + TextView name = (TextView) convertView.findViewById(R.id.cinema_name); + name.setText(cinema.getName()); + TextView distance = (TextView) convertView.findViewById(R.id.cinema_distance); + // TODO set distance + distance.setText("Distance"); + return convertView; + } +} diff --git a/frontend/Movie_Pigeon/app/src/main/java/org/example/team_pigeon/movie_pigeon/GlobalReceiver.java b/frontend/Movie_Pigeon/app/src/main/java/org/example/team_pigeon/movie_pigeon/GlobalReceiver.java index 6a910a8..620f889 100644 --- a/frontend/Movie_Pigeon/app/src/main/java/org/example/team_pigeon/movie_pigeon/GlobalReceiver.java +++ b/frontend/Movie_Pigeon/app/src/main/java/org/example/team_pigeon/movie_pigeon/GlobalReceiver.java @@ -19,7 +19,7 @@ class GlobalReceiver extends BroadcastReceiver { Handler uiHandler; private final static int VCodeSuccess = 0; private final static int ResetSuccess = 1; - private final static int changeUsername = 0; + private final static int cinemasLoaded = 1; private static UserInfoSingleton userInfoBulk = UserInfoSingleton.getInstance(); GlobalReceiver() { @@ -98,6 +98,11 @@ public void onReceive(Context context, Intent intent) { Log.i(TAG, "Received msg to update username"); userInfoBulk.reset(); break; + + case "cinemasLoaded": + Log.i(TAG, "Received msg that cinemas loaded"); + uiHandler.sendEmptyMessage(cinemasLoaded); + break; } } } diff --git a/frontend/Movie_Pigeon/app/src/main/res/layout/cinema_list_item.xml b/frontend/Movie_Pigeon/app/src/main/res/layout/cinema_list_item.xml new file mode 100644 index 0000000..5ec2221 --- /dev/null +++ b/frontend/Movie_Pigeon/app/src/main/res/layout/cinema_list_item.xml @@ -0,0 +1,21 @@ + + + + + + + \ No newline at end of file diff --git a/frontend/Movie_Pigeon/app/src/main/res/layout/fragment_cinema_new.xml b/frontend/Movie_Pigeon/app/src/main/res/layout/fragment_cinema_new.xml new file mode 100644 index 0000000..a51a703 --- /dev/null +++ b/frontend/Movie_Pigeon/app/src/main/res/layout/fragment_cinema_new.xml @@ -0,0 +1,11 @@ + + + + \ No newline at end of file