Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/fragments/FavoritesFragment.java
Show All 32 Lines | |||||
import android.widget.ImageView; | import android.widget.ImageView; | ||||
import android.widget.ListView; | import android.widget.ListView; | ||||
import android.widget.TextView; | import android.widget.TextView; | ||||
import android.widget.Toast; | import android.widget.Toast; | ||||
import androidx.annotation.NonNull; | import androidx.annotation.NonNull; | ||||
import androidx.annotation.Nullable; | import androidx.annotation.Nullable; | ||||
import androidx.lifecycle.ViewModelProvider; | import androidx.lifecycle.ViewModelProvider; | ||||
import androidx.recyclerview.widget.DividerItemDecoration; | |||||
import androidx.recyclerview.widget.LinearLayoutManager; | |||||
import androidx.recyclerview.widget.RecyclerView; | |||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.List; | import java.util.List; | ||||
import it.reyboz.bustorino.*; | import it.reyboz.bustorino.*; | ||||
import it.reyboz.bustorino.adapters.AdapterListener; | |||||
import it.reyboz.bustorino.adapters.StopAdapter; | import it.reyboz.bustorino.adapters.StopAdapter; | ||||
import it.reyboz.bustorino.adapters.StopRecyclerAdapter; | |||||
import it.reyboz.bustorino.backend.Stop; | import it.reyboz.bustorino.backend.Stop; | ||||
import it.reyboz.bustorino.data.FavoritesViewModel; | import it.reyboz.bustorino.data.FavoritesViewModel; | ||||
import it.reyboz.bustorino.middleware.AsyncStopFavoriteAction; | import it.reyboz.bustorino.middleware.AsyncStopFavoriteAction; | ||||
public class FavoritesFragment extends BaseFragment { | public class FavoritesFragment extends BaseFragment { | ||||
private ListView favoriteListView; | private RecyclerView favoriteRecyclerView; | ||||
private EditText busStopNameText; | private EditText busStopNameText; | ||||
private TextView favoriteTipTextView; | private TextView favoriteTipTextView; | ||||
private ImageView angeryBusImageView; | private ImageView angeryBusImageView; | ||||
private LinearLayoutManager llManager; | |||||
@Nullable | @Nullable | ||||
private CommonFragmentListener mListener; | private CommonFragmentListener mListener; | ||||
public static final String FRAGMENT_TAG = "BusTOFavFragment"; | public static final String FRAGMENT_TAG = "BusTOFavFragment"; | ||||
private final AdapterListener adapterListener = new AdapterListener() { | |||||
@Override | |||||
public void onTappedStop(Stop stop) { | |||||
mListener.requestArrivalsForStopID(stop.ID); | |||||
} | |||||
}; | |||||
public static FavoritesFragment newInstance() { | public static FavoritesFragment newInstance() { | ||||
FavoritesFragment fragment = new FavoritesFragment(); | FavoritesFragment fragment = new FavoritesFragment(); | ||||
Bundle args = new Bundle(); | Bundle args = new Bundle(); | ||||
//args.putString(ARG_PARAM1, param1); | //args.putString(ARG_PARAM1, param1); | ||||
//args.putString(ARG_PARAM2, param2); | //args.putString(ARG_PARAM2, param2); | ||||
fragment.setArguments(args); | fragment.setArguments(args); | ||||
Show All 10 Lines | public void onCreate(Bundle savedInstanceState) { | ||||
//do nothing | //do nothing | ||||
} | } | ||||
} | } | ||||
@Nullable | @Nullable | ||||
@Override | @Override | ||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | ||||
View root = inflater.inflate(R.layout.fragment_favorites, container, false); | View root = inflater.inflate(R.layout.fragment_favorites, container, false); | ||||
favoriteListView = root.findViewById(R.id.favoriteListView); | favoriteRecyclerView = root.findViewById(R.id.favoritesRecyclerView); | ||||
favoriteListView.setOnItemClickListener((parent, view, position, id) -> { | //favoriteListView = root.findViewById(R.id.favoriteListView); | ||||
/** | /*favoriteRecyclerView.setOn((parent, view, position, id) -> { | ||||
/* | |||||
* Casting because of Javamerda | * Casting because of Javamerda | ||||
* @url http://stackoverflow.com/questions/30549485/androids-list-view-parameterized-type-in-adapterview-onitemclicklistener | * @url http://stackoverflow.com/questions/30549485/androids-list-view-parameterized-type-in-adapterview-onitemclicklistener | ||||
*/ | */ | ||||
/* | |||||
Stop busStop = (Stop) parent.getItemAtPosition(position); | Stop busStop = (Stop) parent.getItemAtPosition(position); | ||||
if(mListener!=null){ | if(mListener!=null){ | ||||
mListener.requestArrivalsForStopID(busStop.ID); | mListener.requestArrivalsForStopID(busStop.ID); | ||||
} | } | ||||
}); | }); | ||||
*/ | |||||
llManager = new LinearLayoutManager(getContext()); | |||||
llManager.setOrientation(LinearLayoutManager.VERTICAL); | |||||
favoriteRecyclerView.setLayoutManager(llManager); | |||||
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(favoriteRecyclerView.getContext(), | |||||
llManager.getOrientation()); | |||||
favoriteRecyclerView.addItemDecoration(dividerItemDecoration); | |||||
angeryBusImageView = root.findViewById(R.id.angeryBusImageView); | angeryBusImageView = root.findViewById(R.id.angeryBusImageView); | ||||
favoriteTipTextView = root.findViewById(R.id.favoriteTipTextView); | favoriteTipTextView = root.findViewById(R.id.favoriteTipTextView); | ||||
registerForContextMenu(favoriteListView); | registerForContextMenu(favoriteRecyclerView); | ||||
FavoritesViewModel model = new ViewModelProvider(this).get(FavoritesViewModel.class); | FavoritesViewModel model = new ViewModelProvider(this).get(FavoritesViewModel.class); | ||||
model.getFavorites().observe(getViewLifecycleOwner(), this::showStops); | model.getFavorites().observe(getViewLifecycleOwner(), this::showStops); | ||||
showStops(new ArrayList<>()); | showStops(new ArrayList<>()); | ||||
return root; | return root; | ||||
} | } | ||||
@Override | @Override | ||||
public void onAttach(@NonNull Context context) { | public void onAttach(@NonNull Context context) { | ||||
super.onAttach(context); | super.onAttach(context); | ||||
if (context instanceof CommonFragmentListener) { | if (context instanceof CommonFragmentListener) { | ||||
mListener = (CommonFragmentListener) context; | mListener = (CommonFragmentListener) context; | ||||
} else { | } else { | ||||
throw new RuntimeException(context.toString() | throw new RuntimeException(context.toString() | ||||
+ " must implement CommonFragmentListener"); | + " must implement CommonFragmentListener"); | ||||
} | } | ||||
} | } | ||||
@Override | @Override | ||||
public void onDetach() { | public void onDetach() { | ||||
super.onDetach(); | super.onDetach(); | ||||
mListener = null; | mListener = null; | ||||
} | } | ||||
/* | |||||
This method is apparently NOT CALLED ANYMORE | |||||
*/ | |||||
@Override | @Override | ||||
public void onCreateContextMenu(@NonNull ContextMenu menu, @NonNull View v, | public void onCreateContextMenu(@NonNull ContextMenu menu, @NonNull View v, | ||||
ContextMenu.ContextMenuInfo menuInfo) { | ContextMenu.ContextMenuInfo menuInfo) { | ||||
super.onCreateContextMenu(menu, v, menuInfo); | super.onCreateContextMenu(menu, v, menuInfo); | ||||
if (v.getId() == R.id.favoriteListView) { | Log.d("Favorites Fragment", "Creating context menu on "+v); | ||||
if (v.getId() == R.id.favoritesRecyclerView) { | |||||
// if we aren't attached to activity, return null | // if we aren't attached to activity, return null | ||||
if (getActivity()==null) return; | if (getActivity()==null) return; | ||||
MenuInflater inflater = getActivity().getMenuInflater(); | MenuInflater inflater = getActivity().getMenuInflater(); | ||||
inflater.inflate(R.menu.menu_favourites_entry, menu); | inflater.inflate(R.menu.menu_favourites_entry, menu); | ||||
} | } | ||||
} | } | ||||
@Override | @Override | ||||
public void onResume() { | public void onResume() { | ||||
super.onResume(); | super.onResume(); | ||||
if (mListener!=null) mListener.readyGUIfor(FragmentKind.FAVORITES); | if (mListener!=null) mListener.readyGUIfor(FragmentKind.FAVORITES); | ||||
} | } | ||||
@Override | @Override | ||||
public boolean onContextItemSelected(MenuItem item) { | public boolean onContextItemSelected(MenuItem item) { | ||||
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item | AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item | ||||
.getMenuInfo(); | .getMenuInfo(); | ||||
Stop busStop = (Stop) favoriteListView.getItemAtPosition(info.position); | if(!(favoriteRecyclerView.getAdapter() instanceof StopRecyclerAdapter)) | ||||
return false; | |||||
StopRecyclerAdapter adapter = (StopRecyclerAdapter) favoriteRecyclerView.getAdapter(); | |||||
Stop busStop = (Stop) adapter.getStops().get(adapter.getPosition()); | |||||
switch (item.getItemId()) { | switch (item.getItemId()) { | ||||
case R.id.action_favourite_entry_delete: | case R.id.action_favourite_entry_delete: | ||||
if (getContext()!=null) | if (getContext()!=null) | ||||
new AsyncStopFavoriteAction(getContext().getApplicationContext(), AsyncStopFavoriteAction.Action.REMOVE, | new AsyncStopFavoriteAction(getContext().getApplicationContext(), AsyncStopFavoriteAction.Action.REMOVE, | ||||
result -> { | result -> { | ||||
}).execute(busStop); | }).execute(busStop); | ||||
Show All 21 Lines | public class FavoritesFragment extends BaseFragment { | ||||
} | } | ||||
void showStops(List<Stop> busStops){ | void showStops(List<Stop> busStops){ | ||||
// If no data is found show a friendly message | // If no data is found show a friendly message | ||||
if(BuildConfig.DEBUG) | if(BuildConfig.DEBUG) | ||||
Log.d("BusTO - Favorites", "We have "+busStops.size()+" favorites in the list"); | Log.d("BusTO - Favorites", "We have "+busStops.size()+" favorites in the list"); | ||||
if (busStops.size() == 0) { | if (busStops.size() == 0) { | ||||
favoriteListView.setVisibility(View.INVISIBLE); | favoriteRecyclerView.setVisibility(View.INVISIBLE); | ||||
// TextView favoriteTipTextView = (TextView) findViewById(R.id.favoriteTipTextView); | // TextView favoriteTipTextView = (TextView) findViewById(R.id.favoriteTipTextView); | ||||
//assert favoriteTipTextView != null; | //assert favoriteTipTextView != null; | ||||
favoriteTipTextView.setVisibility(View.VISIBLE); | favoriteTipTextView.setVisibility(View.VISIBLE); | ||||
//ImageView angeryBusImageView = (ImageView) findViewById(R.id.angeryBusImageView); | //ImageView angeryBusImageView = (ImageView) findViewById(R.id.angeryBusImageView); | ||||
angeryBusImageView.setVisibility(View.VISIBLE); | angeryBusImageView.setVisibility(View.VISIBLE); | ||||
} else { | } else { | ||||
favoriteListView.setVisibility(View.VISIBLE); | favoriteRecyclerView.setVisibility(View.VISIBLE); | ||||
favoriteTipTextView.setVisibility(View.INVISIBLE); | favoriteTipTextView.setVisibility(View.INVISIBLE); | ||||
angeryBusImageView.setVisibility(View.INVISIBLE); | angeryBusImageView.setVisibility(View.INVISIBLE); | ||||
} | } | ||||
/* There's a nice method called notifyDataSetChanged() to avoid building the ListView | /* There's a nice method called notifyDataSetChanged() to avoid building the ListView | ||||
* all over again. This method exists in a billion answers on Stack Overflow, but | * all over again. This method exists in a billion answers on Stack Overflow, but | ||||
* it's nowhere to be seen around here, Android Studio can't find it no matter what. | * it's nowhere to be seen around here, Android Studio can't find it no matter what. | ||||
* Anyway, it only works from Android 2.3 onward (which is why it refuses to appear, I | * Anyway, it only works from Android 2.3 onward (which is why it refuses to appear, I | ||||
* guess) and requires to modify the list with .add() and .clear() and some other | * guess) and requires to modify the list with .add() and .clear() and some other | ||||
* methods, so to update a single stop we need to completely rebuild the list for no | * methods, so to update a single stop we need to completely rebuild the list for no | ||||
* reason. It would probably end up as "slow" as throwing away the old ListView and | * reason. It would probably end up as "slow" as throwing away the old ListView and | ||||
* redrwaing everything. | * redrwaing everything. | ||||
*/ | */ | ||||
// Show results | // Show results | ||||
favoriteListView.setAdapter(new StopAdapter(getContext(), busStops)); | favoriteRecyclerView.setAdapter(new StopRecyclerAdapter(busStops,adapterListener)); | ||||
} | } | ||||
public void showBusStopUsernameInputDialog(final Stop busStop) { | public void showBusStopUsernameInputDialog(final Stop busStop) { | ||||
AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); | AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); | ||||
LayoutInflater inflater = this.getLayoutInflater(); | LayoutInflater inflater = this.getLayoutInflater(); | ||||
View renameDialogLayout = inflater.inflate(R.layout.rename_dialog, null); | View renameDialogLayout = inflater.inflate(R.layout.rename_dialog, null); | ||||
▲ Show 20 Lines • Show All 58 Lines • Show Last 20 Lines |
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