Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/fragments/FavoritesFragment.java
package it.reyboz.bustorino.fragments; | package it.reyboz.bustorino.fragments; | ||||
import android.app.AlertDialog; | import android.app.AlertDialog; | ||||
import android.content.Context; | import android.content.Context; | ||||
import android.content.DialogInterface; | import android.content.DialogInterface; | ||||
import android.content.Intent; | |||||
import android.os.Bundle; | import android.os.Bundle; | ||||
import android.os.Handler; | import android.util.Log; | ||||
import android.view.ContextMenu; | import android.view.ContextMenu; | ||||
import android.view.LayoutInflater; | import android.view.LayoutInflater; | ||||
import android.view.MenuInflater; | import android.view.MenuInflater; | ||||
import android.view.MenuItem; | import android.view.MenuItem; | ||||
import android.view.View; | import android.view.View; | ||||
import android.view.ViewGroup; | import android.view.ViewGroup; | ||||
import android.widget.AdapterView; | import android.widget.AdapterView; | ||||
import android.widget.EditText; | import android.widget.EditText; | ||||
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 java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.List; | import java.util.List; | ||||
import it.reyboz.bustorino.ActivityFavorites; | import it.reyboz.bustorino.*; | ||||
import it.reyboz.bustorino.ActivityMain; | |||||
import it.reyboz.bustorino.ActivityMap; | |||||
import it.reyboz.bustorino.R; | |||||
import it.reyboz.bustorino.adapters.StopAdapter; | import it.reyboz.bustorino.adapters.StopAdapter; | ||||
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.data.UserDB; | |||||
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 ListView favoriteListView; | ||||
private EditText busStopNameText; | private EditText busStopNameText; | ||||
private TextView favoriteTipTextView; | private TextView favoriteTipTextView; | ||||
private ImageView angeryBusImageView; | private ImageView angeryBusImageView; | ||||
▲ Show 20 Lines • Show All 105 Lines • ▼ Show 20 Lines | public boolean onContextItemSelected(MenuItem item) { | ||||
}).execute(busStop); | }).execute(busStop); | ||||
return true; | return true; | ||||
case R.id.action_rename_bus_stop_username: | case R.id.action_rename_bus_stop_username: | ||||
showBusStopUsernameInputDialog(busStop); | showBusStopUsernameInputDialog(busStop); | ||||
return true; | return true; | ||||
case R.id.action_view_on_map: | case R.id.action_view_on_map: | ||||
final String theGeoUrl = busStop.getGeoURL(); | if (busStop.getLatitude() == null | busStop.getLongitude() == null | | ||||
/* | mListener==null | ||||
if(theGeoUrl==null){ | ) { | ||||
//doesn't have a position | |||||
Toast.makeText(getContext(),R.string.cannot_show_on_map_no_position,Toast.LENGTH_SHORT).show(); | Toast.makeText(getContext(), R.string.cannot_show_on_map_no_position, Toast.LENGTH_SHORT).show(); | ||||
return true; | return true; | ||||
} | } | ||||
// start ActivityMap with these extras in intent | //GeoPoint point = new GeoPoint(busStop.getLatitude(), busStop.getLongitude()); | ||||
Intent intent = new Intent(getContext(), ActivityMap.class); | |||||
Bundle b = new Bundle(); | |||||
double lat, lon; | |||||
if (busStop.getLatitude()!=null) | |||||
lat = busStop.getLatitude(); | |||||
else lat = 200; | |||||
if (busStop.getLongitude()!=null) | |||||
lon = busStop.getLongitude(); | |||||
else lon = 200; | |||||
b.putDouble("lat", lat); | |||||
b.putDouble("lon",lon); | |||||
b.putString("name", busStop.getStopDefaultName()); | |||||
b.putString("ID", busStop.ID); | |||||
intent.putExtras(b); | |||||
startActivity(intent); | mListener.showMapCenteredOnStop(busStop); | ||||
TODO: start map on button press | |||||
*/ | |||||
return true; | return true; | ||||
default: | default: | ||||
return super.onContextItemSelected(item); | return super.onContextItemSelected(item); | ||||
} | } | ||||
} | } | ||||
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) | |||||
Log.d("BusTO - Favorites", "We have "+busStops.size()+" favorites in the list"); | |||||
if (busStops.size() == 0) { | if (busStops.size() == 0) { | ||||
favoriteListView.setVisibility(View.INVISIBLE); | favoriteListView.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); | favoriteListView.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)); | favoriteListView.setAdapter(new StopAdapter(getContext(), busStops)); | ||||
} | } | ||||
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(); | ||||
▲ Show 20 Lines • Show All 60 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