Skip to content

Commit

Permalink
Merge pull request #37 from timeforcoffee/fix_departures
Browse files Browse the repository at this point in the history
Show all departures from backend
  • Loading branch information
lucasardonini authored Jan 23, 2018
2 parents 33739d2 + d0a5273 commit ee7af4c
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 22 deletions.
19 changes: 17 additions & 2 deletions api/src/main/java/ch/liip/timeforcoffee/api/StationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@

import ch.liip.timeforcoffee.api.events.stationsLocationEvents.FetchOpenDataStationsLocationEvent;
import ch.liip.timeforcoffee.api.events.stationsLocationEvents.FetchStationsLocationEvent;
import ch.liip.timeforcoffee.api.events.FetchStationsSearchEvent;
import ch.liip.timeforcoffee.api.events.stationsSearchEvents.FetchZvvStationsSearchEvent;
import ch.liip.timeforcoffee.api.events.stationsLocationEvents.OpenDataStationsLocationFetchedEvent;
import ch.liip.timeforcoffee.api.events.stationsLocationEvents.StationsLocationFetchedEvent;
import ch.liip.timeforcoffee.api.events.stationsSearchEvents.FetchStationsSearchEvent;
import ch.liip.timeforcoffee.api.events.stationsSearchEvents.FetchZvvStationsSearchEvent;
import ch.liip.timeforcoffee.api.events.stationsSearchEvents.StationsSearchFetchedEvent;
import ch.liip.timeforcoffee.api.events.stationsSearchEvents.ZvvStationsSearchFetchedEvent;
import ch.liip.timeforcoffee.api.events.stationsSearchOneEvents.FetchStationsSearchOneEvent;
import ch.liip.timeforcoffee.api.events.stationsSearchOneEvents.FetchZvvStationsSearchOneEvent;
import ch.liip.timeforcoffee.api.events.stationsSearchOneEvents.StationsSearchOneFetchedEvent;
import ch.liip.timeforcoffee.api.events.stationsSearchOneEvents.ZvvStationsSearchOneFetchedEvent;
import ch.liip.timeforcoffee.api.mappers.StationMapper;
import ch.liip.timeforcoffee.api.models.Station;

Expand All @@ -33,6 +37,11 @@ public void onEvent(FetchStationsSearchEvent event) {
eventBus.post(new FetchZvvStationsSearchEvent(event.getSearchQuery()));
}

@Subscribe
public void onEvent(FetchStationsSearchOneEvent event) {
eventBus.post(new FetchZvvStationsSearchOneEvent(event.getSearchQuery()));
}

@Subscribe
public void onEvent(FetchStationsLocationEvent event) {
eventBus.post(new FetchOpenDataStationsLocationEvent(event.getQuery()));
Expand All @@ -48,6 +57,12 @@ public void onEvent(ZvvStationsSearchFetchedEvent event) {
eventBus.post(new StationsSearchFetchedEvent(stations));
}

@Subscribe
public void onEvent(ZvvStationsSearchOneFetchedEvent event) {
Station station = StationMapper.fromZvv(event.getStation());
eventBus.post(new StationsSearchOneFetchedEvent(station));
}

@Subscribe
public void onEvent(OpenDataStationsLocationFetchedEvent event) {
ArrayList<Station> stations = new ArrayList<>();
Expand Down
39 changes: 35 additions & 4 deletions api/src/main/java/ch/liip/timeforcoffee/api/ZvvApiService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import ch.liip.timeforcoffee.api.events.stationsSearchEvents.FetchStationsSearchErrorEvent;
import ch.liip.timeforcoffee.api.events.stationsSearchEvents.FetchZvvStationsSearchEvent;
import ch.liip.timeforcoffee.api.events.stationsSearchEvents.ZvvStationsSearchFetchedEvent;
import ch.liip.timeforcoffee.api.events.stationsSearchOneEvents.FetchZvvStationsSearchOneEvent;
import ch.liip.timeforcoffee.api.events.stationsSearchOneEvents.ZvvStationsSearchOneFetchedEvent;
import ch.liip.timeforcoffee.zvv.ConnectionsResponse;
import ch.liip.timeforcoffee.zvv.Station;
import ch.liip.timeforcoffee.zvv.StationboardResponse;
Expand Down Expand Up @@ -53,8 +55,12 @@ public void onEvent(FetchZvvStationsSearchEvent event) {
fetchZvvStations(event.getSearchQuery());
}

public void fetchZvvConnections(String fromStationId, String toStationId, String startDateStr, String endDateStr) {
@Subscribe
public void onEvent(FetchZvvStationsSearchOneEvent event) {
fetchZvvStation(event.getSearchQuery());
}

public void fetchZvvConnections(String fromStationId, String toStationId, String startDateStr, String endDateStr) {
zvvService.getConnections(fromStationId, toStationId, startDateStr, endDateStr)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
Expand All @@ -77,7 +83,6 @@ public void onNext(ConnectionsResponse connections) {
}

public void fetchZvvDepartures(String stationId) {

zvvService.getDepartures(stationId)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
Expand All @@ -100,9 +105,8 @@ public void onNext(StationboardResponse stationboard) {
}

public void fetchZvvStations(String searchQuery) {

if (searchQuery.isEmpty()) { //search query is empty => return an empty station list
List<Station> stations = new ArrayList<Station>();
List<Station> stations = new ArrayList<>();
eventBus.post(new ZvvStationsSearchFetchedEvent(stations));
return;
}
Expand All @@ -127,4 +131,31 @@ public void onNext(StationsResponse stationsResponse) {
}
});
}

public void fetchZvvStation(String searchQuery) {
if (searchQuery.isEmpty()) { //search query is empty => return null
eventBus.post(new ZvvStationsSearchOneFetchedEvent(null));
return;
}

zvvService.getStations(searchQuery)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<StationsResponse>() {
@Override
public void onCompleted() {

}

@Override
public void onError(Throwable e) {
eventBus.post(new FetchStationsSearchErrorEvent(e)) ;
}

@Override
public void onNext(StationsResponse stationsResponse) {
eventBus.post(new ZvvStationsSearchOneFetchedEvent(stationsResponse.getStations().get(0)));
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ch.liip.timeforcoffee.api.events;
package ch.liip.timeforcoffee.api.events.stationsSearchEvents;

public class FetchStationsSearchEvent {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ch.liip.timeforcoffee.api.events.stationsSearchOneEvents;

import retrofit.RetrofitError;

public class FetchStationsOneSearchErrorEvent {

private RetrofitError error;
private Throwable throwable;

public FetchStationsOneSearchErrorEvent(RetrofitError error) {
this.error = error;
}

public FetchStationsOneSearchErrorEvent(Throwable throwable) {
this.throwable = throwable;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ch.liip.timeforcoffee.api.events.stationsSearchOneEvents;

public class FetchStationsSearchOneEvent {

private String searchQuery;

public FetchStationsSearchOneEvent(String searchQuery) {
this.searchQuery = searchQuery;
}

public String getSearchQuery() {
return searchQuery;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ch.liip.timeforcoffee.api.events.stationsSearchOneEvents;

public class FetchZvvStationsSearchOneEvent {

private final String searchQuery;

public FetchZvvStationsSearchOneEvent(String searchQuery) {
this.searchQuery = searchQuery;
}

public String getSearchQuery() {
return searchQuery;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ch.liip.timeforcoffee.api.events.stationsSearchOneEvents;

import ch.liip.timeforcoffee.api.models.Station;

public class StationsSearchOneFetchedEvent {

private Station station;

public StationsSearchOneFetchedEvent(Station station) {
this.station = station;
}

public Station getStation() {
return station;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ch.liip.timeforcoffee.api.events.stationsSearchOneEvents;

import ch.liip.timeforcoffee.zvv.Station;

public class ZvvStationsSearchOneFetchedEvent {

private Station station;

public ZvvStationsSearchOneFetchedEvent(Station station) {
this.station = station;
}

public Station getStation() {
return station;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
public class DepartureMapper {

public static Departure fromZvv(ch.liip.timeforcoffee.zvv.Departure zvvDeparture) {
if(zvvDeparture.getId() == null) {
return null;
}

int id = zvvDeparture.getId() != null ? Integer.parseInt(zvvDeparture.getId()) : 0;
int colorBg = Color.WHITE;
int colorFg = Color.BLACK;
try {
Expand All @@ -21,7 +18,7 @@ public static Departure fromZvv(ch.liip.timeforcoffee.zvv.Departure zvvDeparture
}

return new Departure(zvvDeparture.getName(),
Integer.parseInt(zvvDeparture.getId()),
id,
zvvDeparture.getTo(),
zvvDeparture.getPlatform(),
colorFg,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public String getDestinationIdStr() {
return String.valueOf(destinationId);
}

public void setDestinationId(int destinationId) {
this.destinationId = destinationId;
}

public String getDestinationName() {
return destinationName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import ch.liip.timeforcoffee.api.events.connectionsEvents.ConnectionsFetchedEvent;
import ch.liip.timeforcoffee.api.events.connectionsEvents.FetchConnectionsErrorEvent;
import ch.liip.timeforcoffee.api.events.connectionsEvents.FetchConnectionsEvent;
import ch.liip.timeforcoffee.api.events.stationsSearchOneEvents.FetchStationsSearchOneEvent;
import ch.liip.timeforcoffee.api.events.stationsSearchOneEvents.StationsSearchOneFetchedEvent;
import ch.liip.timeforcoffee.api.models.Connection;
import ch.liip.timeforcoffee.api.models.Departure;
import ch.liip.timeforcoffee.api.models.Station;
Expand Down Expand Up @@ -67,7 +69,19 @@ public void updateConnections() {
mActivity.showProgressLayout(true);
}

mEventBus.post(new FetchConnectionsEvent(mStation.getIdStr(), mDeparture.getDestinationIdStr(), mDeparture.getDepartureStrForZvv(), mDeparture.getArrivalStrForZvv()));
if (mDeparture.getDestinationId() == 0) {
mEventBus.post(new FetchStationsSearchOneEvent(mDeparture.getDestinationName()));
} else {
mEventBus.post(new FetchConnectionsEvent(mStation.getIdStr(), mDeparture.getDestinationIdStr(), mDeparture.getDepartureStrForZvv(), mDeparture.getArrivalStrForZvv()));
}
}

@Subscribe
public void onStationsFetchedEvent(StationsSearchOneFetchedEvent event) {
int destinationId = event.getStation().getId();
mDeparture.setDestinationId(destinationId);

updateConnections();
}

@Subscribe
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import ch.liip.timeforcoffee.activity.StationSearchActivity;
import ch.liip.timeforcoffee.api.StationService;
import ch.liip.timeforcoffee.api.ZvvApiService;
import ch.liip.timeforcoffee.api.events.FetchStationsSearchEvent;
import ch.liip.timeforcoffee.api.events.stationsSearchEvents.FetchStationsSearchErrorEvent;
import ch.liip.timeforcoffee.api.events.stationsSearchEvents.FetchStationsSearchEvent;
import ch.liip.timeforcoffee.api.events.stationsSearchEvents.StationsSearchFetchedEvent;
import ch.liip.timeforcoffee.api.models.Station;
import ch.liip.timeforcoffee.common.presenter.Presenter;
Expand Down Expand Up @@ -49,14 +49,7 @@ public StationSearchPresenter(StationSearchActivity activity, String searchQuery

@Override
public void onResumeView() {

}

public void search() {
if (mSearchQuery != null && !mSearchQuery.isEmpty()) {
mActivity.showProgressLayout(true);
mEventBus.post(new FetchStationsSearchEvent(mSearchQuery));
}
search();
}

@Override
Expand All @@ -67,6 +60,13 @@ public void onRefreshView() {
@Override
public void onPauseView() { }

public void search() {
if (mSearchQuery != null && !mSearchQuery.isEmpty()) {
mActivity.showProgressLayout(true);
mEventBus.post(new FetchStationsSearchEvent(mSearchQuery));
}
}

@Subscribe
public void onStationsFetchedEvent(StationsSearchFetchedEvent event) {
mActivity.showProgressLayout(false);
Expand Down

0 comments on commit ee7af4c

Please sign in to comment.