Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/fragments/ResultListFragment.java
Show All 17 Lines | |||||
package it.reyboz.bustorino.fragments; | package it.reyboz.bustorino.fragments; | ||||
import android.content.Context; | import android.content.Context; | ||||
import android.database.sqlite.SQLiteDatabase; | import android.database.sqlite.SQLiteDatabase; | ||||
import android.os.Bundle; | import android.os.Bundle; | ||||
import android.os.Parcelable; | import android.os.Parcelable; | ||||
import android.support.annotation.Nullable; | import androidx.annotation.Nullable; | ||||
import android.support.v4.app.Fragment; | import androidx.fragment.app.Fragment; | ||||
import android.support.v4.widget.SwipeRefreshLayout; | import androidx.swiperefreshlayout.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 com.google.android.material.floatingactionbutton.FloatingActionButton; | ||||
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; | ||||
import it.reyboz.bustorino.middleware.UserDB; | import it.reyboz.bustorino.middleware.UserDB; | ||||
/** | /** | ||||
* 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"; | |||||
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"; | protected 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; | protected TextView messageTextView; | ||||
private ImageButton addToFavorites; | protected ListView resultsListView; | ||||
private FloatingActionButton fabutton; | private FloatingActionButton fabutton; | ||||
private ListView resultsListView; | |||||
private ListAdapter mListAdapter = null; | private ListAdapter mListAdapter = null; | ||||
boolean listShown; | boolean listShown; | ||||
private Parcelable mListInstanceState = null; | private Parcelable mListInstanceState = null; | ||||
public ResultListFragment() { | public ResultListFragment() { | ||||
// Required empty public constructor | // Required empty public constructor | ||||
} | } | ||||
public ListView getResultsListView() { | public ListView getResultsListView() { | ||||
return resultsListView; | return resultsListView; | ||||
} | } | ||||
/** | /** | ||||
* Use this factory method to create a new instance of | * Use this factory method to create a new instance of | ||||
* this fragment using the provided parameters. | * this fragment using the provided parameters. | ||||
* | * | ||||
* @param listType whether the list is used for STOPS or LINES (Orari) | * @param listType whether the list is used for STOPS or LINES (Orari) | ||||
* @return A new instance of fragment ResultListFragment. | * @return A new instance of fragment ResultListFragment. | ||||
*/ | */ | ||||
public static ResultListFragment newInstance(FragmentKind listType, String eventualStopTitle) { | public static ResultListFragment newInstance(FragmentKind listType, String eventualStopTitle) { | ||||
ResultListFragment fragment = new ResultListFragment(); | ResultListFragment fragment = new ResultListFragment(); | ||||
Bundle args = new Bundle(); | Bundle args = new Bundle(); | ||||
args.putSerializable(LIST_TYPE, listType); | args.putSerializable(LIST_TYPE, listType); | ||||
if (eventualStopTitle != null) { | if (eventualStopTitle != null) { | ||||
args.putString(STOP_TITLE, eventualStopTitle); | args.putString(ArrivalsFragment.STOP_TITLE, eventualStopTitle); | ||||
} | } | ||||
fragment.setArguments(args); | fragment.setArguments(args); | ||||
return fragment; | return fragment; | ||||
} | } | ||||
public static ResultListFragment newInstance(FragmentKind listType) { | public static ResultListFragment newInstance(FragmentKind listType) { | ||||
return newInstance(listType, null); | return newInstance(listType, null); | ||||
} | } | ||||
@Override | @Override | ||||
public void onCreate(Bundle savedInstanceState) { | public void onCreate(Bundle savedInstanceState) { | ||||
super.onCreate(savedInstanceState); | super.onCreate(savedInstanceState); | ||||
if (getArguments() != null) { | if (getArguments() != null) { | ||||
adapterKind = (FragmentKind) getArguments().getSerializable(LIST_TYPE); | adapterKind = (FragmentKind) getArguments().getSerializable(LIST_TYPE); | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Check if the last Bus Stop is in the favorites | * Check if the last Bus Stop is in the favorites | ||||
* @return | * @return true if it iss | ||||
*/ | */ | ||||
public boolean isStopInFavorites(String busStopId) { | public boolean isStopInFavorites(String busStopId) { | ||||
boolean found = false; | boolean found = false; | ||||
// no stop no party | // no stop no party | ||||
if(busStopId != null) { | if(busStopId != null) { | ||||
SQLiteDatabase userDB = new UserDB(getContext()).getReadableDatabase(); | SQLiteDatabase userDB = new UserDB(getContext()).getReadableDatabase(); | ||||
found = UserDB.isStopInFavorites(userDB, busStopId); | found = UserDB.isStopInFavorites(userDB, busStopId); | ||||
} | } | ||||
return found; | return found; | ||||
} | } | ||||
@Override | @Override | ||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, | public View onCreateView(LayoutInflater inflater, ViewGroup container, | ||||
Bundle savedInstanceState) { | Bundle savedInstanceState) { | ||||
View root = inflater.inflate(R.layout.fragment_list_view, container, false); | View root = inflater.inflate(R.layout.fragment_list_view, container, false); | ||||
messageTextView = (TextView) root.findViewById(R.id.messageTextView); | messageTextView = (TextView) root.findViewById(R.id.messageTextView); | ||||
addToFavorites = (ImageButton) root.findViewById(R.id.addToFavorites); | |||||
if (adapterKind != null) { | if (adapterKind != null) { | ||||
resultsListView = (ListView) root.findViewById(R.id.resultsListView); | resultsListView = (ListView) root.findViewById(R.id.resultsListView); | ||||
switch (adapterKind) { | switch (adapterKind) { | ||||
case STOPS: | case STOPS: | ||||
resultsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { | resultsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { | ||||
@Override | @Override | ||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | ||||
Show All 24 Lines | public View onCreateView(LayoutInflater inflater, ViewGroup container, | ||||
Toast.makeText(getContext(), | Toast.makeText(getContext(), | ||||
getString(R.string.route_towards_unknown, routeName), Toast.LENGTH_SHORT).show(); | getString(R.string.route_towards_unknown, routeName), Toast.LENGTH_SHORT).show(); | ||||
} else { | } else { | ||||
Toast.makeText(getContext(), | Toast.makeText(getContext(), | ||||
getString(R.string.route_towards_destination, routeName, r.destinazione), Toast.LENGTH_SHORT).show(); | getString(R.string.route_towards_destination, routeName, r.destinazione), Toast.LENGTH_SHORT).show(); | ||||
} | } | ||||
} | } | ||||
}); | }); | ||||
String displayName = getArguments().getString(STOP_TITLE); | String displayName = getArguments().getString(ArrivalsFragment.STOP_TITLE); | ||||
setTextViewMessage(String.format( | setTextViewMessage(String.format( | ||||
getString(R.string.passages), displayName)); | getString(R.string.passages), displayName)); | ||||
break; | break; | ||||
default: | default: | ||||
throw new IllegalStateException("Argument passed was not of a supported type"); | throw new IllegalStateException("Argument passed was not of a supported type"); | ||||
} | } | ||||
String probablemessage = getArguments().getString(MESSAGE_TEXT_VIEW); | String probablemessage = getArguments().getString(MESSAGE_TEXT_VIEW); | ||||
Show All 20 Lines | public class ResultListFragment extends Fragment{ | ||||
@Override | @Override | ||||
public void onResume() { | public void onResume() { | ||||
super.onResume(); | super.onResume(); | ||||
//Log.d(getString(R.string.list_fragment_debug),"Fragment restored, saved listAdapter is "+(mListAdapter)); | //Log.d(getString(R.string.list_fragment_debug),"Fragment restored, saved listAdapter is "+(mListAdapter)); | ||||
if (mListAdapter != null) { | if (mListAdapter != null) { | ||||
ListAdapter adapter = mListAdapter; | ListAdapter adapter = mListAdapter; | ||||
mListAdapter = null; | mListAdapter = null; | ||||
setListAdapter(adapter); | resetListAdapter(adapter); | ||||
} | } | ||||
if (mListInstanceState != null) { | if (mListInstanceState != null) { | ||||
Log.d("resultsListView", "trying to restore instance state"); | Log.d("resultsListView", "trying to restore instance state"); | ||||
resultsListView.onRestoreInstanceState(mListInstanceState); | resultsListView.onRestoreInstanceState(mListInstanceState); | ||||
} | } | ||||
switch (adapterKind) { | switch (adapterKind) { | ||||
case ARRIVALS: | case ARRIVALS: | ||||
resultsListView.setOnScrollListener(new CommonScrollListener(mListener, true)); | resultsListView.setOnScrollListener(new CommonScrollListener(mListener, true)); | ||||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | public void onViewStateRestored(@Nullable Bundle savedInstanceState) { | ||||
super.onViewStateRestored(savedInstanceState); | super.onViewStateRestored(savedInstanceState); | ||||
Log.d("ResultListFragment", "onViewStateRestored"); | Log.d("ResultListFragment", "onViewStateRestored"); | ||||
if (savedInstanceState != null) { | if (savedInstanceState != null) { | ||||
mListInstanceState = savedInstanceState.getParcelable(LIST_STATE); | mListInstanceState = savedInstanceState.getParcelable(LIST_STATE); | ||||
Log.d("ResultListFragment", "listInstanceStatePresent :" + mListInstanceState); | Log.d("ResultListFragment", "listInstanceStatePresent :" + mListInstanceState); | ||||
} | } | ||||
} | } | ||||
public void setListAdapter(ListAdapter adapter) { | protected void resetListAdapter(ListAdapter adapter) { | ||||
boolean hadAdapter = mListAdapter != null; | boolean hadAdapter = mListAdapter != null; | ||||
mListAdapter = adapter; | mListAdapter = adapter; | ||||
if (resultsListView != null) { | if (resultsListView != null) { | ||||
resultsListView.setAdapter(adapter); | resultsListView.setAdapter(adapter); | ||||
resultsListView.setVisibility(View.VISIBLE); | resultsListView.setVisibility(View.VISIBLE); | ||||
} | } | ||||
} | } | ||||
public void setNewListAdapter(ListAdapter adapter){ | |||||
resetListAdapter(adapter); | |||||
} | |||||
/** | /** | ||||
* Set the message textView | * Set the message textView | ||||
* @param message the whole message to write in the textView | * @param message the whole message to write in the textView | ||||
*/ | */ | ||||
public void setTextViewMessage(String message) { | public void setTextViewMessage(String message) { | ||||
messageTextView.setText(message); | messageTextView.setText(message); | ||||
switch (adapterKind) { | |||||
case ARRIVALS: | |||||
addToFavorites.setClickable(true); | |||||
addToFavorites.setOnClickListener(new View.OnClickListener() { | |||||
@Override | |||||
public void onClick(View v) { | |||||
// add/remove the stop in the favorites | |||||
mListener.toggleLastStopToFavorites(); | |||||
} | |||||
}); | |||||
break; | |||||
case STOPS: | |||||
addToFavorites.setClickable(false); | |||||
addToFavorites.setVisibility(View.INVISIBLE); | |||||
break; | |||||
} | |||||
messageTextView.setVisibility(View.VISIBLE); | messageTextView.setVisibility(View.VISIBLE); | ||||
} | } | ||||
} | } | ||||
No newline at end of file | 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