Skip to content

Commit

Permalink
fixed OneBusAway#851 - add filter by arrivals/departures feature in A…
Browse files Browse the repository at this point in the history
…rrivalsListFragment
  • Loading branch information
arctik-circle committed Dec 22, 2024
1 parent b4f3d3e commit 7e182b8
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.onebusaway.android.io.elements.ObaArrivalInfo;
import org.onebusaway.android.util.ArrayAdapter;
import org.onebusaway.android.util.ArrivalInfoUtils.ArrivalFilter;

import android.content.ContentQueryMap;
import android.content.Context;
Expand All @@ -41,5 +42,9 @@ public void setTripsForStop(ContentQueryMap tripsForStop) {
notifyDataSetChanged();
}

abstract public void setData(ObaArrivalInfo[] arrivals, ArrayList<String> routesFilter, long currentTime);
abstract public void setData(ObaArrivalInfo[] arrivals, ArrayList<String> routesFilter, long currentTime, ArrivalFilter arrivalFilter);
public void setData(ObaArrivalInfo[] arrivals, ArrayList<String> routesFilter, long currentTime) {
setData(arrivals, routesFilter, currentTime, ArrivalFilter.BOTH);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.onebusaway.android.io.elements.Status;
import org.onebusaway.android.provider.ObaContract;
import org.onebusaway.android.util.ArrivalInfoUtils;
import org.onebusaway.android.util.ArrivalInfoUtils.ArrivalFilter;
import org.onebusaway.android.util.UIUtils;

import java.util.ArrayList;
Expand All @@ -53,11 +54,12 @@ public ArrivalsListAdapterStyleA(Context context) {
* @param currentTime current time in milliseconds
*/
public void setData(ObaArrivalInfo[] arrivals, ArrayList<String> routesFilter,
long currentTime) {
long currentTime, ArrivalFilter arrivalFilter) {
if (arrivals != null) {
ArrayList<ArrivalInfo> list =
ArrivalInfoUtils.convertObaArrivalInfo(getContext(),
arrivals, routesFilter, currentTime, false);
arrivals, routesFilter, currentTime, false,
arrivalFilter);
setData(list);
} else {
setData(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.onebusaway.android.io.elements.Status;
import org.onebusaway.android.provider.ObaContract;
import org.onebusaway.android.util.ArrivalInfoUtils;
import org.onebusaway.android.util.ArrivalInfoUtils.ArrivalFilter;
import org.onebusaway.android.util.UIUtils;
import org.onebusaway.util.comparators.AlphanumComparator;

Expand Down Expand Up @@ -75,11 +76,12 @@ public void setFragment(ArrivalsListFragment fragment) {
* @param currentTime current time in milliseconds
*/
public void setData(ObaArrivalInfo[] arrivals, ArrayList<String> routesFilter,
long currentTime) {
long currentTime, ArrivalFilter arrivalFilter) {
if (arrivals != null) {
ArrayList<ArrivalInfo> list =
ArrivalInfoUtils.convertObaArrivalInfo(getContext(),
arrivals, routesFilter, currentTime, true);
arrivals, routesFilter, currentTime, true,
arrivalFilter);

// Sort list by route and headsign, in that order
Collections.sort(list, new Comparator<ArrivalInfo>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.util.Pair;
Expand Down Expand Up @@ -81,6 +82,7 @@
import org.onebusaway.android.io.request.survey.model.StudyResponse;
import org.onebusaway.android.util.ArrayAdapterWithIcon;
import org.onebusaway.android.util.ArrivalInfoUtils;
import org.onebusaway.android.util.ArrivalInfoUtils.ArrivalFilter;
import org.onebusaway.android.util.BuildFlavorUtils;
import org.onebusaway.android.util.DBUtil;
import org.onebusaway.android.util.FragmentUtils;
Expand Down Expand Up @@ -182,6 +184,8 @@ public class ArrivalsListFragment extends ListFragment implements LoaderManager.

private SurveyManager surveyManager;

private ArrivalFilter mArrivalFilter = ArrivalFilterDialog.arrivalFilter;

public interface Listener {

/**
Expand Down Expand Up @@ -641,6 +645,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
if (mStop != null) {
showRoutesFilterDialog();
}
} else if (id == R.id.filter_arrivals_departures) {
new ArrivalFilterDialog().show(getActivity().getSupportFragmentManager(), ".ArrivalFilterDialog");
} else if (id == R.id.show_header_arrivals) {
doShowHideHeaderArrivals();
} else if (id == R.id.edit_name) {
Expand Down Expand Up @@ -1381,15 +1387,7 @@ public void onSaveInstanceState(Bundle outState) {
@Override
public void onClick(DialogInterface dialog, int which) {
Activity act = getActivity();
ArrivalsListFragment frag = null;

// Get the fragment we want...
if (act instanceof ArrivalsListActivity) {
frag = ((ArrivalsListActivity) act).getArrivalsListFragment();
} else if (act instanceof HomeActivity) {
frag = ((HomeActivity) act).getArrivalsListFragment();
}

ArrivalsListFragment frag = getArrivalsListFragment(act);
frag.setRoutesFilter(mChecks);
dialog.dismiss();
}
Expand All @@ -1400,6 +1398,59 @@ public void onClick(DialogInterface arg0, int which, boolean isChecked) {
}
}

public static class ArrivalFilterDialog extends DialogFragment
implements DialogInterface.OnClickListener {
private static ArrivalFilter arrivalFilter = ArrivalFilter.BOTH;

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setSingleChoiceItems(ArrivalFilter.getOptionsArray(getResources()), arrivalFilter.arrayResourceIndex, this)
.setPositiveButton(R.string.stop_info_save, this)
.setNegativeButton(R.string.stop_info_cancel, null)
.create();
}

@Override
public void onDestroyView() {
super.onDestroyView();
arrivalFilter = ArrivalFilter.BOTH;
}

@Override
public void onClick(DialogInterface dialog, int which) {
if (which == ArrivalFilter.BOTH.arrayResourceIndex) {
arrivalFilter = ArrivalFilter.BOTH;
} else if (which == ArrivalFilter.ARRIVALS_ONLY.arrayResourceIndex) {
arrivalFilter = ArrivalFilter.ARRIVALS_ONLY;
} else if (which == ArrivalFilter.DEPARTURES_ONLY.arrayResourceIndex) {
arrivalFilter = ArrivalFilter.DEPARTURES_ONLY;
} else if (which == DialogInterface.BUTTON_POSITIVE) {
getArrivalsListFragment(getActivity()).setArrivalFilter(arrivalFilter);
dialog.dismiss();
}
}
}

@Nullable
private static ArrivalsListFragment getArrivalsListFragment(Activity act) {
ArrivalsListFragment frag = null;

// Get the fragment we want...
if (act instanceof ArrivalsListActivity) {
frag = ((ArrivalsListActivity) act).getArrivalsListFragment();
} else if (act instanceof HomeActivity) {
frag = ((HomeActivity) act).getArrivalsListFragment();
}
return frag;
}

private void setArrivalFilter(ArrivalFilter arrivalFilter) {
mArrivalFilter = arrivalFilter;
refreshLocal();
}

private void setRoutesFilter(boolean[] checks) {
final int len = checks.length;
final ArrayList<String> newFilter = new ArrayList<String>(len);
Expand Down Expand Up @@ -1518,7 +1569,7 @@ public void refreshLocal() {
// Nothing to refresh yet
return;
}
mAdapter.setData(response.getArrivalInfo(), mRoutesFilter, System.currentTimeMillis());
mAdapter.setData(response.getArrivalInfo(), mRoutesFilter, System.currentTimeMillis(), mArrivalFilter);
}
if (mHeader != null) {
mHeader.refresh();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import android.content.Context;
import android.content.res.Resources;

import androidx.annotation.NonNull;

import org.onebusaway.android.R;
import org.onebusaway.android.app.Application;
import org.onebusaway.android.io.elements.ObaArrivalInfo;
Expand Down Expand Up @@ -52,7 +54,8 @@ public int compare(ArrivalInfo lhs, ArrivalInfo rhs) {
public static ArrayList<ArrivalInfo> convertObaArrivalInfo(Context context,
ObaArrivalInfo[] arrivalInfo,
ArrayList<String> filter, long ms,
boolean includeArrivalDepartureInStatusLabel) {
boolean includeArrivalDepartureInStatusLabel,
ArrivalFilter arrivalFilter) {
final int len = arrivalInfo.length;
ArrayList<ArrivalInfo> result = new ArrayList<ArrivalInfo>(len);
if (filter != null && filter.size() > 0) {
Expand All @@ -61,7 +64,7 @@ public static ArrayList<ArrivalInfo> convertObaArrivalInfo(Context context,
if (filter.contains(arrival.getRouteId())) {
ArrivalInfo info = new ArrivalInfo(context, arrival, ms,
includeArrivalDepartureInStatusLabel);
if (shouldAddEta(info)) {
if (shouldAddEta(info) && shouldAddArrivalFilter(info, arrivalFilter)) {
result.add(info);
}
}
Expand All @@ -71,7 +74,7 @@ public static ArrayList<ArrivalInfo> convertObaArrivalInfo(Context context,
for (ObaArrivalInfo obaArrivalInfo : arrivalInfo) {
ArrivalInfo info = new ArrivalInfo(context, obaArrivalInfo, ms,
includeArrivalDepartureInStatusLabel);
if (shouldAddEta(info)) {
if (shouldAddEta(info) && shouldAddArrivalFilter(info, arrivalFilter)) {
result.add(info);
}
}
Expand All @@ -82,6 +85,34 @@ public static ArrayList<ArrivalInfo> convertObaArrivalInfo(Context context,
return result;
}

public static ArrayList<ArrivalInfo> convertObaArrivalInfo(Context context,
ObaArrivalInfo[] arrivalInfo,
ArrayList<String> filter, long ms,
boolean includeArrivalDepartureInStatusLabel) {
return convertObaArrivalInfo(context, arrivalInfo, filter, ms, includeArrivalDepartureInStatusLabel, ArrivalFilter.BOTH);
}

// This enum class represents the string array resource R.array.stop_info_arrival_filter_options
public enum ArrivalFilter {
BOTH(0),
ARRIVALS_ONLY(1),
DEPARTURES_ONLY(2);

public final int arrayResourceIndex;

ArrivalFilter(int arrayResourceIndex) {
this.arrayResourceIndex = arrayResourceIndex;
}

public String getOptionString(Resources appResources) {
return appResources.getStringArray(R.array.stop_info_arrival_filter_options)[arrayResourceIndex];
}

public static String[] getOptionsArray(Resources appResources) {
return appResources.getStringArray(R.array.stop_info_arrival_filter_options);
}
}

/**
* Returns true if this ETA should be added based on the user preference for adding negative
* arrival times, and false if it should not
Expand All @@ -106,6 +137,15 @@ private static boolean shouldAddEta(ArrivalInfo info) {
return false;
}

private static boolean shouldAddArrivalFilter(ArrivalInfo info, @NonNull ArrivalFilter arrivalFilter) {
switch (arrivalFilter) {
case BOTH: return true;
case ARRIVALS_ONLY: return info.isArrival();
case DEPARTURES_ONLY: return !info.isArrival();
default: throw new IllegalArgumentException();
}
}

/**
* Returns the index in the provided infoList for the first non-negative arrival ETA in the
* list, or -1 if no non-negative ETAs exist in the list
Expand Down
3 changes: 3 additions & 0 deletions onebusaway-android/src/main/res/menu-v14/arrivals_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
<item android:id="@+id/filter"
android:title="@string/stop_info_option_filter"
android:icon="@drawable/ic_menu_trip"/>
<item android:id="@+id/filter_arrivals_departures"
android:title="@string/stop_info_option_filter_arrivals_departures"
android:icon="@drawable/ic_menu_trip"/>
<item android:id="@+id/edit_name"
android:title="@string/stop_info_option_editname"
android:icon="@drawable/android:ic_menu_edit"/>
Expand Down
3 changes: 3 additions & 0 deletions onebusaway-android/src/main/res/menu/arrivals_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
<item android:id="@+id/filter"
android:title="@string/stop_info_option_filter"
android:icon="@drawable/ic_menu_trip"/>
<item android:id="@+id/filter_arrivals_departures"
android:title="@string/stop_info_option_filter_arrivals_departures"
android:icon="@drawable/ic_menu_trip"/>
<item android:id="@+id/edit_name"
android:title="@string/stop_info_option_editname"
android:icon="@drawable/android:ic_menu_edit"/>
Expand Down
6 changes: 6 additions & 0 deletions onebusaway-android/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,10 @@
<item>@string/trip_plan_arriving</item>
</string-array>

<string-array name="stop_info_arrival_filter_options">
<item>Both</item>
<item>Arrivals only</item>
<item>Departures only</item>
</string-array>

</resources>
1 change: 1 addition & 0 deletions onebusaway-android/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1278,4 +1278,5 @@
<string name="more_info">More Info</string>
<string name="display_test_alerts">Display test alerts</string>
<string name="display_test_wide_alerts_for_regions">Display test-wide alerts for regions</string>
<string name="stop_info_option_filter_arrivals_departures">Filter arrivals/departures</string>
</resources>

0 comments on commit 7e182b8

Please sign in to comment.