Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #851 - added filter by arrivals/departures option to ArrivalsListFragment #1290

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spaces

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,10 @@ public class ArrivalsListFragment extends ListFragment implements LoaderManager.

private SurveyManager surveyManager;

private final ArrivalFilterDialog mArrivalFilterDialog = new ArrivalFilterDialog();

private ArrivalFilter mArrivalFilter = mArrivalFilterDialog.arrivalFilter;

public interface Listener {

/**
Expand Down Expand Up @@ -641,6 +647,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
if (mStop != null) {
showRoutesFilterDialog();
}
} else if (id == R.id.filter_arrivals_departures) {
mArrivalFilterDialog.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 +1389,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 +1400,67 @@ public void onClick(DialogInterface arg0, int which, boolean isChecked) {
}
}

public static class ArrivalFilterDialog extends DialogFragment
implements DialogInterface.OnClickListener {

// The default state
private ArrivalFilter arrivalFilter = ArrivalFilter.BOTH;

private int which = arrivalFilter.arrayResourceIndex;

@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 onClick(DialogInterface dialog, int which) {
//updates the current state of the ArrivalFilterDialog only if a radio button is clicked
this.which = which >=0 ? which : this.which;
if (which == DialogInterface.BUTTON_POSITIVE) {
//exports the current state to the ArrivalsListFragment
getArrivalsListFragment(getActivity()).setArrivalFilter(
arrivalFilter = ArrivalFilter.fromArrayResourceIndex(this.which)
);
dialog.dismiss();
}
}

@Override
public void onDismiss(@NonNull DialogInterface dialog) {
super.onDismiss(dialog);
//makes sure the state is consistent
which = arrivalFilter.arrayResourceIndex;
}
}

@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 +1579,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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please explain your logic here ?

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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here can you explain the logic

result.add(info);
}
}
Expand All @@ -82,6 +85,43 @@ 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not used ?

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);
}

public static ArrivalFilter fromArrayResourceIndex(int arrayResourceIndex) {
switch (arrayResourceIndex) {
case 0: return BOTH;
case 1: return ARRIVALS_ONLY;
case 2: return DEPARTURES_ONLY;
default: throw new IllegalArgumentException();
}
}
}

/**
* 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 +146,15 @@ private static boolean shouldAddEta(ArrivalInfo info) {
return false;
}

private static boolean shouldAddArrivalFilter(ArrivalInfo info, @NonNull ArrivalFilter arrivalFilter) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please write comments explaining this function?

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 @@ -33,6 +33,9 @@
<item android:id="@+id/show_header_arrivals"
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/filter"
android:title="@string/stop_info_option_filter"
android:icon="@drawable/ic_menu_trip"/>
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">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are using localization to support multiple languages so make sure to translate those strings to all supported languages

<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 @@ -1280,4 +1280,5 @@
<string name="display_test_wide_alerts_for_regions">Display test-wide alerts for regions</string>
<string name="do_you_want_to_plan_the_trip_now">Do you want to plan the trip now?</string>
<string name="plan_trip">Plan Trip?</string>
<string name="stop_info_option_filter_arrivals_departures">Filter arrivals/departures</string>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Translate that to multiple languages

</resources>
Loading