Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/fragments/ResultListFragment.java
/* | /* | ||||
BusTO - Fragments components | BusTO - Fragments components | ||||
Copyright (C) 2016 Fabio Mazza | Copyright (C) 2016 Fabio Mazza | ||||
This program is free software: you can redistribute it and/or modify | This program is free software: you can redistribute it and/or modify | ||||
it under the terms of the GNU General Public License as published by | it under the terms of the GNU General Public License as published by | ||||
the Free Software Foundation, either version 3 of the License, or | the Free Software Foundation, either version 3 of the License, or | ||||
(at your option) any later version. | (at your option) any later version. | ||||
This program is distributed in the hope that it will be useful, | This program is distributed in the hope that it will be useful, | ||||
but WITHOUT ANY WARRANTY; without even the implied warranty of | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
GNU General Public License for more details. | GNU General Public License for more details. | ||||
You should have received a copy of the GNU General Public License | You should have received a copy of the GNU General Public License | ||||
along with this program. If not, see <http://www.gnu.org/licenses/>. | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
package it.reyboz.bustorino.fragments; | package it.reyboz.bustorino.fragments; | ||||
import android.content.Context; | import android.content.Context; | ||||
import android.os.Bundle; | import android.os.Bundle; | ||||
import android.os.Parcelable; | import android.os.Parcelable; | ||||
import android.support.annotation.Nullable; | import android.support.annotation.Nullable; | ||||
import android.support.v4.app.Fragment; | import android.support.v4.app.Fragment; | ||||
import android.support.v4.widget.SwipeRefreshLayout; | import android.support.v4.widget.SwipeRefreshLayout; | ||||
import android.util.Log; | import android.util.Log; | ||||
import android.view.LayoutInflater; | import android.view.LayoutInflater; | ||||
import android.view.View; | import android.view.View; | ||||
import android.view.ViewGroup; | import android.view.ViewGroup; | ||||
import android.widget.*; | import android.widget.*; | ||||
import android.support.design.widget.FloatingActionButton; | import android.support.design.widget.FloatingActionButton; | ||||
import it.reyboz.bustorino.ActivityMain; | import it.reyboz.bustorino.ActivityMain; | ||||
import it.reyboz.bustorino.R; | import it.reyboz.bustorino.R; | ||||
import it.reyboz.bustorino.backend.FiveTNormalizer; | import it.reyboz.bustorino.backend.FiveTNormalizer; | ||||
import it.reyboz.bustorino.backend.Palina; | import it.reyboz.bustorino.backend.Palina; | ||||
import it.reyboz.bustorino.backend.Route; | import it.reyboz.bustorino.backend.Route; | ||||
import it.reyboz.bustorino.backend.Stop; | import it.reyboz.bustorino.backend.Stop; | ||||
/** | /** | ||||
* This is a generalized fragment that can be used both for | * This is a generalized fragment that can be used both for | ||||
* | * | ||||
* | * | ||||
*/ | */ | ||||
public class ResultListFragment extends Fragment{ | public class ResultListFragment extends Fragment{ | ||||
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER | // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER | ||||
static final String LIST_TYPE = "list-type"; | static final String LIST_TYPE = "list-type"; | ||||
private static final String STOP_TITLE = "messageExtra"; | private static final String STOP_TITLE = "messageExtra"; | ||||
protected static final String LIST_STATE = "list_state"; | protected static final String LIST_STATE = "list_state"; | ||||
private static final String MESSAGE_TEXT_VIEW = "message_text_view"; | private static final String MESSAGE_TEXT_VIEW = "message_text_view"; | ||||
private FragmentKind adapterKind; | private FragmentKind adapterKind; | ||||
private boolean adapterSet = false; | private boolean adapterSet = false; | ||||
protected FragmentListener mListener; | protected FragmentListener mListener; | ||||
private TextView messageTextView; | private TextView messageTextView; | ||||
private ImageButton addToFavorites; | |||||
private FloatingActionButton fabutton; | |||||
private ListView resultsListView; | private FloatingActionButton fabutton; | ||||
private ListAdapter mListAdapter = null; | private ListView resultsListView; | ||||
boolean listShown; | private ListAdapter mListAdapter = null; | ||||
private Parcelable mListInstanceState = null; | boolean listShown; | ||||
private Parcelable mListInstanceState = null; | |||||
public ResultListFragment() { | |||||
// Required empty public constructor | public ResultListFragment() { | ||||
} | // Required empty public constructor | ||||
} | |||||
public ListView getResultsListView() { | |||||
return resultsListView; | public ListView getResultsListView() { | ||||
} | return resultsListView; | ||||
} | |||||
/** | |||||
* Use this factory method to create a new instance of | /** | ||||
* this fragment using the provided parameters. | * Use this factory method to create a new instance of | ||||
* | * this fragment using the provided parameters. | ||||
* @param listType whether the list is used for STOPS or LINES (Orari) | * | ||||
* @return A new instance of fragment ResultListFragment. | * @param listType whether the list is used for STOPS or LINES (Orari) | ||||
*/ | * @return A new instance of fragment ResultListFragment. | ||||
public static ResultListFragment newInstance(FragmentKind listType, String eventualStopTitle) { | */ | ||||
ResultListFragment fragment = new ResultListFragment(); | public static ResultListFragment newInstance(FragmentKind listType, String eventualStopTitle) { | ||||
Bundle args = new Bundle(); | ResultListFragment fragment = new ResultListFragment(); | ||||
args.putSerializable(LIST_TYPE, listType); | Bundle args = new Bundle(); | ||||
if (eventualStopTitle != null) | args.putSerializable(LIST_TYPE, listType); | ||||
args.putString(STOP_TITLE, eventualStopTitle); | if (eventualStopTitle != null) | ||||
fragment.setArguments(args); | args.putString(STOP_TITLE, eventualStopTitle); | ||||
return fragment; | fragment.setArguments(args); | ||||
} | return fragment; | ||||
} | |||||
public static ResultListFragment newInstance(FragmentKind listType) { | |||||
return newInstance(listType, null); | public static ResultListFragment newInstance(FragmentKind listType) { | ||||
} | return newInstance(listType, null); | ||||
} | |||||
@Override | |||||
public void onCreate(Bundle savedInstanceState) { | @Override | ||||
super.onCreate(savedInstanceState); | public void onCreate(Bundle savedInstanceState) { | ||||
if (getArguments() != null) { | super.onCreate(savedInstanceState); | ||||
adapterKind = (FragmentKind) getArguments().getSerializable(LIST_TYPE); | if (getArguments() != null) { | ||||
} | adapterKind = (FragmentKind) getArguments().getSerializable(LIST_TYPE); | ||||
} | } | ||||
} | |||||
@Override | |||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, | @Override | ||||
Bundle savedInstanceState) { | public View onCreateView(LayoutInflater inflater, ViewGroup container, | ||||
View root = inflater.inflate(R.layout.fragment_list_view, container, false); | Bundle savedInstanceState) { | ||||
messageTextView = (TextView) root.findViewById(R.id.messageTextView); | View root = inflater.inflate(R.layout.fragment_list_view, container, false); | ||||
if (adapterKind != null) { | messageTextView = (TextView) root.findViewById(R.id.messageTextView); | ||||
resultsListView = (ListView) root.findViewById(R.id.resultsListView); | addToFavorites = (ImageButton) root.findViewById(R.id.addToFavorites); | ||||
switch (adapterKind) { | if (adapterKind != null) { | ||||
case STOPS: | resultsListView = (ListView) root.findViewById(R.id.resultsListView); | ||||
resultsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { | switch (adapterKind) { | ||||
case STOPS: | |||||
@Override | resultsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { | ||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |||||
/* | @Override | ||||
* Casting because of Javamerda | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | ||||
* @url http://stackoverflow.com/questions/30549485/androids-list-view-parameterized-type-in-adapterview-onitemclicklistener | /* | ||||
*/ | * Casting because of Javamerda | ||||
Stop busStop = (Stop) parent.getItemAtPosition(position); | * @url http://stackoverflow.com/questions/30549485/androids-list-view-parameterized-type-in-adapterview-onitemclicklistener | ||||
mListener.createFragmentForStop(busStop.ID); | */ | ||||
} | Stop busStop = (Stop) parent.getItemAtPosition(position); | ||||
}); | mListener.createFragmentForStop(busStop.ID); | ||||
} | |||||
//set the textviewMessage | }); | ||||
setTextViewMessage(getString(R.string.results)); | |||||
break; | //set the textviewMessage | ||||
case ARRIVALS: | setTextViewMessage(getString(R.string.results)); | ||||
resultsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { | break; | ||||
@Override | case ARRIVALS: | ||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | resultsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { | ||||
String routeName; | @Override | ||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |||||
Route r = (Route) parent.getItemAtPosition(position); | String routeName; | ||||
routeName = FiveTNormalizer.routeInternalToDisplay(r.getNameForDisplay()); | |||||
if (routeName == null) { | Route r = (Route) parent.getItemAtPosition(position); | ||||
routeName = r.getNameForDisplay(); | routeName = FiveTNormalizer.routeInternalToDisplay(r.getNameForDisplay()); | ||||
} | if (routeName == null) { | ||||
if (r.destinazione == null || r.destinazione.length() == 0) { | routeName = r.getNameForDisplay(); | ||||
Toast.makeText(getContext(), | } | ||||
getString(R.string.route_towards_unknown, routeName), Toast.LENGTH_SHORT).show(); | if (r.destinazione == null || r.destinazione.length() == 0) { | ||||
} else { | Toast.makeText(getContext(), | ||||
Toast.makeText(getContext(), | getString(R.string.route_towards_unknown, routeName), Toast.LENGTH_SHORT).show(); | ||||
getString(R.string.route_towards_destination, routeName, r.destinazione), Toast.LENGTH_SHORT).show(); | } else { | ||||
} | Toast.makeText(getContext(), | ||||
} | getString(R.string.route_towards_destination, routeName, r.destinazione), Toast.LENGTH_SHORT).show(); | ||||
}); | } | ||||
String displayName = getArguments().getString(STOP_TITLE); | } | ||||
setTextViewMessage(String.format( | }); | ||||
getString(R.string.passages), displayName)); | String displayName = getArguments().getString(STOP_TITLE); | ||||
break; | setTextViewMessage(String.format( | ||||
default: | getString(R.string.passages), displayName)); | ||||
throw new IllegalStateException("Argument passed was not of a supported type"); | break; | ||||
} | default: | ||||
throw new IllegalStateException("Argument passed was not of a supported type"); | |||||
String probablemessage = getArguments().getString(MESSAGE_TEXT_VIEW); | } | ||||
if (probablemessage != null) { | |||||
//Log.d("BusTO fragment " + this.getTag(), "We have a possible message here in the savedInstaceState: " + probablemessage); | String probablemessage = getArguments().getString(MESSAGE_TEXT_VIEW); | ||||
messageTextView.setText(probablemessage); | if (probablemessage != null) { | ||||
messageTextView.setVisibility(View.VISIBLE); | //Log.d("BusTO fragment " + this.getTag(), "We have a possible message here in the savedInstaceState: " + probablemessage); | ||||
} | messageTextView.setText(probablemessage); | ||||
messageTextView.setVisibility(View.VISIBLE); | |||||
} else | } | ||||
Log.d(getString(R.string.list_fragment_debug), "No content root for fragment"); | |||||
return root; | } else | ||||
} | Log.d(getString(R.string.list_fragment_debug), "No content root for fragment"); | ||||
return root; | |||||
public boolean isFragmentForTheSameStop(Palina p) { | } | ||||
return adapterKind.equals(FragmentKind.ARRIVALS) && getTag().equals(getFragmentTag(p)); | |||||
} | public boolean isFragmentForTheSameStop(Palina p) { | ||||
return adapterKind.equals(FragmentKind.ARRIVALS) && getTag().equals(getFragmentTag(p)); | |||||
public static String getFragmentTag(Palina p) { | } | ||||
return p.ID; | |||||
} | public static String getFragmentTag(Palina p) { | ||||
return p.ID; | |||||
} | |||||
@Override | |||||
public void onResume() { | |||||
super.onResume(); | @Override | ||||
//Log.d(getString(R.string.list_fragment_debug),"Fragment restored, saved listAdapter is "+(mListAdapter)); | public void onResume() { | ||||
if (mListAdapter != null) { | super.onResume(); | ||||
//Log.d(getString(R.string.list_fragment_debug),"Fragment restored, saved listAdapter is "+(mListAdapter)); | |||||
ListAdapter adapter = mListAdapter; | if (mListAdapter != null) { | ||||
mListAdapter = null; | |||||
setListAdapter(adapter); | ListAdapter adapter = mListAdapter; | ||||
} | mListAdapter = null; | ||||
if (mListInstanceState != null) { | setListAdapter(adapter); | ||||
Log.d("resultsListView", "trying to restore instance state"); | } | ||||
resultsListView.onRestoreInstanceState(mListInstanceState); | if (mListInstanceState != null) { | ||||
} | Log.d("resultsListView", "trying to restore instance state"); | ||||
switch (adapterKind) { | resultsListView.onRestoreInstanceState(mListInstanceState); | ||||
case ARRIVALS: | } | ||||
resultsListView.setOnScrollListener(new CommonScrollListener(mListener, true)); | switch (adapterKind) { | ||||
fabutton.show(); | case ARRIVALS: | ||||
break; | resultsListView.setOnScrollListener(new CommonScrollListener(mListener, true)); | ||||
case STOPS: | fabutton.show(); | ||||
resultsListView.setOnScrollListener(new CommonScrollListener(mListener, false)); | break; | ||||
break; | case STOPS: | ||||
default: | resultsListView.setOnScrollListener(new CommonScrollListener(mListener, false)); | ||||
//NONE | break; | ||||
} | default: | ||||
mListener.readyGUIfor(adapterKind); | //NONE | ||||
} | |||||
} | mListener.readyGUIfor(adapterKind); | ||||
@Override | } | ||||
public void onPause() { | |||||
if (adapterKind.equals(FragmentKind.ARRIVALS)) { | @Override | ||||
SwipeRefreshLayout reflay = (SwipeRefreshLayout) getActivity().findViewById(R.id.listRefreshLayout); | public void onPause() { | ||||
reflay.setEnabled(false); | if (adapterKind.equals(FragmentKind.ARRIVALS)) { | ||||
Log.d("BusTO Fragment " + this.getTag(), "RefreshLayout disabled"); | SwipeRefreshLayout reflay = (SwipeRefreshLayout) getActivity().findViewById(R.id.listRefreshLayout); | ||||
} | reflay.setEnabled(false); | ||||
super.onPause(); | Log.d("BusTO Fragment " + this.getTag(), "RefreshLayout disabled"); | ||||
} | } | ||||
super.onPause(); | |||||
@Override | } | ||||
public void onAttach(Context context) { | |||||
super.onAttach(context); | @Override | ||||
if (context instanceof FragmentListener) { | public void onAttach(Context context) { | ||||
mListener = (FragmentListener) context; | super.onAttach(context); | ||||
fabutton = (FloatingActionButton) getActivity().findViewById(R.id.floatingActionButton); | if (context instanceof FragmentListener) { | ||||
} else { | mListener = (FragmentListener) context; | ||||
throw new RuntimeException(context.toString() | fabutton = (FloatingActionButton) getActivity().findViewById(R.id.floatingActionButton); | ||||
+ " must implement ResultFragmentListener"); | } else { | ||||
} | throw new RuntimeException(context.toString() | ||||
+ " must implement ResultFragmentListener"); | |||||
} | } | ||||
} | |||||
@Override | |||||
public void onDetach() { | |||||
mListener = null; | @Override | ||||
if (fabutton != null) | public void onDetach() { | ||||
fabutton.show(); | mListener = null; | ||||
super.onDetach(); | if (fabutton != null) | ||||
} | fabutton.show(); | ||||
super.onDetach(); | |||||
} | |||||
@Override | |||||
public void onDestroyView() { | |||||
resultsListView = null; | @Override | ||||
//Log.d(getString(R.string.list_fragment_debug), "called onDestroyView"); | public void onDestroyView() { | ||||
getArguments().putString(MESSAGE_TEXT_VIEW, messageTextView.getText().toString()); | resultsListView = null; | ||||
super.onDestroyView(); | //Log.d(getString(R.string.list_fragment_debug), "called onDestroyView"); | ||||
} | getArguments().putString(MESSAGE_TEXT_VIEW, messageTextView.getText().toString()); | ||||
super.onDestroyView(); | |||||
} | |||||
@Override | |||||
public void onViewStateRestored(@Nullable Bundle savedInstanceState) { | |||||
super.onViewStateRestored(savedInstanceState); | @Override | ||||
Log.d("ResultListFragment", "onViewStateRestored"); | public void onViewStateRestored(@Nullable Bundle savedInstanceState) { | ||||
if (savedInstanceState != null) { | super.onViewStateRestored(savedInstanceState); | ||||
mListInstanceState = savedInstanceState.getParcelable(LIST_STATE); | Log.d("ResultListFragment", "onViewStateRestored"); | ||||
Log.d("ResultListFragment", "listInstanceStatePresent :" + mListInstanceState); | if (savedInstanceState != null) { | ||||
} | mListInstanceState = savedInstanceState.getParcelable(LIST_STATE); | ||||
} | Log.d("ResultListFragment", "listInstanceStatePresent :" + mListInstanceState); | ||||
} | |||||
public void setListAdapter(ListAdapter adapter) { | } | ||||
boolean hadAdapter = mListAdapter != null; | |||||
mListAdapter = adapter; | public void setListAdapter(ListAdapter adapter) { | ||||
if (resultsListView != null) { | boolean hadAdapter = mListAdapter != null; | ||||
resultsListView.setAdapter(adapter); | mListAdapter = adapter; | ||||
resultsListView.setVisibility(View.VISIBLE); | if (resultsListView != null) { | ||||
} | resultsListView.setAdapter(adapter); | ||||
} | resultsListView.setVisibility(View.VISIBLE); | ||||
} | |||||
/** | } | ||||
* Set the message textView | |||||
* @param message the whole message to write in the textView | /** | ||||
*/ | * Set the message textView | ||||
public void setTextViewMessage(String message) { | * @param message the whole message to write in the textView | ||||
messageTextView.setText(message); | */ | ||||
switch (adapterKind) { | public void setTextViewMessage(String message) { | ||||
case ARRIVALS: | messageTextView.setText(message); | ||||
final ActivityMain activ = (ActivityMain) getActivity(); | switch (adapterKind) { | ||||
messageTextView.setClickable(true); | case ARRIVALS: | ||||
messageTextView.setOnClickListener(new View.OnClickListener() { | final ActivityMain activ = (ActivityMain) getActivity(); | ||||
@Override | addToFavorites.setClickable(true); | ||||
public void onClick(View v) { | addToFavorites.setOnClickListener(new View.OnClickListener() { | ||||
mListener.addLastStopToFavorites(); | @Override | ||||
} | public void onClick(View v) { | ||||
}); | mListener.addLastStopToFavorites(); | ||||
break; | } | ||||
case STOPS: | }); | ||||
messageTextView.setClickable(false); | break; | ||||
break; | case STOPS: | ||||
} | addToFavorites.setClickable(false); | ||||
break; | |||||
messageTextView.setVisibility(View.VISIBLE); | } | ||||
} | |||||
} | messageTextView.setVisibility(View.VISIBLE); | ||||
} | |||||
} | |||||
No newline at end of file |
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register