Search in sources :

Example 1 with StationResult

use of de.tum.in.tumcampusapp.component.ui.transportation.model.efa.StationResult in project TumCampusApp by TCA-Team.

the class TransportController method getStationsFromExternal.

/**
 * Find stations by station name prefix
 *
 * @param prefix Name prefix
 * @return List of StationResult
 */
public static List<StationResult> getStationsFromExternal(Context context, String prefix) {
    prefix = Utils.escapeUmlauts(prefix);
    try {
        String language = LANGUAGE + Locale.getDefault().getLanguage();
        // ISO-8859-1 is needed for mvv
        String stationQuery = STATION_SEARCH_QUERY + UrlEscapers.urlPathSegmentEscaper().escape(prefix);
        String query = STATION_SEARCH_CONST + language + '&' + stationQuery;
        Utils.log(query);
        NetUtils net = new NetUtils(context);
        // Download possible stations
        Optional<JSONObject> jsonObj = net.downloadJsonObject(query, CacheManager.VALIDITY_DO_NOT_CACHE, true);
        if (!jsonObj.isPresent()) {
            return Collections.emptyList();
        }
        List<StationResult> results = new ArrayList<>();
        JSONObject stopfinder = jsonObj.get().getJSONObject("stopFinder");
        // Possible values for points: Object, Array or null
        JSONArray pointsArray = stopfinder.optJSONArray(POINTS);
        if (pointsArray == null) {
            JSONObject points = stopfinder.optJSONObject(POINTS);
            if (points == null) {
                return Collections.emptyList();
            }
            JSONObject point = points.getJSONObject("point");
            addStationResult(results, point);
        } else {
            for (int i = 0; i < pointsArray.length(); i++) {
                JSONObject point = pointsArray.getJSONObject(i);
                addStationResult(results, point);
            }
        }
        // Sort by quality
        Collections.sort(results, (lhs, rhs) -> rhs.getQuality() - lhs.getQuality());
        return results;
    } catch (JSONException e) {
        Utils.log(e, ERROR_INVALID_JSON + STATION_SEARCH);
    }
    return Collections.emptyList();
}
Also used : NetUtils(de.tum.in.tumcampusapp.utils.NetUtils) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) StationResult(de.tum.in.tumcampusapp.component.ui.transportation.model.efa.StationResult)

Example 2 with StationResult

use of de.tum.in.tumcampusapp.component.ui.transportation.model.efa.StationResult in project TumCampusApp by TCA-Team.

the class TransportationActivity method onItemClick.

/**
 * Click on station in list
 */
@Override
public void onItemClick(final AdapterView<?> av, View v, int position, long id) {
    StationResult stationResult = (StationResult) av.getAdapter().getItem(position);
    showStation(stationResult);
}
Also used : StationResult(de.tum.in.tumcampusapp.component.ui.transportation.model.efa.StationResult)

Example 3 with StationResult

use of de.tum.in.tumcampusapp.component.ui.transportation.model.efa.StationResult in project TumCampusApp by TCA-Team.

the class MVVWidgetConfigureActivity method onSearchFinished.

/**
 * Shows the stations
 *
 * @param possibleStationList list of possible StationResult
 */
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
@Override
protected void onSearchFinished(Optional<List<StationResult>> possibleStationList) {
    if (!possibleStationList.isPresent()) {
        return;
    }
    List<StationResult> stationResultList = possibleStationList.get();
    showLoadingEnded();
    // mQuery is not null if it was a real search
    if (stationResultList.isEmpty()) {
        // So show no results found
        listViewResults.setAdapter(new NoResultsAdapter(this));
        listViewResults.requestFocus();
        return;
    }
    adapterStations.clear();
    adapterStations.addAll(stationResultList);
    adapterStations.notifyDataSetChanged();
    listViewResults.setAdapter(adapterStations);
    listViewResults.requestFocus();
}
Also used : NoResultsAdapter(de.tum.in.tumcampusapp.component.other.generic.adapter.NoResultsAdapter) StationResult(de.tum.in.tumcampusapp.component.ui.transportation.model.efa.StationResult)

Example 4 with StationResult

use of de.tum.in.tumcampusapp.component.ui.transportation.model.efa.StationResult in project TumCampusApp by TCA-Team.

the class MVVWidgetConfigureActivity method onItemClick.

/**
 * Click on station in list
 */
@Override
public void onItemClick(final AdapterView<?> av, View v, int position, long id) {
    StationResult stationResult = (StationResult) av.getAdapter().getItem(position);
    widgetDepartures.setStation(stationResult.getStation());
    widgetDepartures.setStationId(stationResult.getId());
    saveAndReturn();
}
Also used : StationResult(de.tum.in.tumcampusapp.component.ui.transportation.model.efa.StationResult)

Example 5 with StationResult

use of de.tum.in.tumcampusapp.component.ui.transportation.model.efa.StationResult in project TumCampusApp by TCA-Team.

the class TransportController method addStationResult.

private static void addStationResult(Collection<StationResult> results, JSONObject point) throws JSONException {
    String name = point.getString("name");
    String id = point.getJSONObject("ref").getString("id");
    int quality = point.getInt("quality");
    results.add(new StationResult(name, id, quality));
}
Also used : StationResult(de.tum.in.tumcampusapp.component.ui.transportation.model.efa.StationResult)

Aggregations

StationResult (de.tum.in.tumcampusapp.component.ui.transportation.model.efa.StationResult)7 Departure (de.tum.in.tumcampusapp.component.ui.transportation.model.efa.Departure)2 Recent (de.tum.in.tumcampusapp.component.other.general.model.Recent)1 NoResultsAdapter (de.tum.in.tumcampusapp.component.other.generic.adapter.NoResultsAdapter)1 LocationManager (de.tum.in.tumcampusapp.component.other.locations.LocationManager)1 NetUtils (de.tum.in.tumcampusapp.utils.NetUtils)1 ArrayList (java.util.ArrayList)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1