Skip to content

Commit

Permalink
Merge pull request #70 from timeforcoffee/fix-network-error
Browse files Browse the repository at this point in the history
Last release quick fixes
  • Loading branch information
lucasardonini authored Mar 19, 2018
2 parents b63e788 + 6b7800d commit 5fff88c
Show file tree
Hide file tree
Showing 21 changed files with 258 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
public class DepartureService {

private final EventBus eventBus;
private String stationId;

@Inject
public DepartureService(EventBus eventBus) {
Expand All @@ -26,14 +27,20 @@ public DepartureService(EventBus eventBus) {

@Subscribe
public void onEvent(FetchDeparturesEvent event) {
eventBus.post(new FetchZvvDeparturesEvent(event.getStationId()));
stationId = event.getStationId();
eventBus.post(new FetchZvvDeparturesEvent(stationId));
}

@Subscribe
public void onEvent(ZvvDeparturesFetchedEvent event) {
ArrayList<Departure> departures = new ArrayList<>();
int stationId = Integer.parseInt(this.stationId);

for (ch.liip.timeforcoffee.zvv.Departure zvvDeparture : event.getDepartures()) {
departures.add(DepartureMapper.fromZvv(zvvDeparture));
Departure departure = DepartureMapper.fromZvv(zvvDeparture, stationId);
if(departure != null) {
departures.add(departure);
}
}

eventBus.post(new DeparturesFetchedEvent(departures));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@

import android.graphics.Color;

import java.util.Date;

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

public class DepartureMapper {

public static Departure fromZvv(ch.liip.timeforcoffee.zvv.Departure zvvDeparture) {
public static Departure fromZvv(ch.liip.timeforcoffee.zvv.Departure zvvDeparture, int stationId) {
int id = zvvDeparture.getId() != null ? Integer.parseInt(zvvDeparture.getId()) : 0;
int colorBg = Color.WHITE;
int colorFg = Color.BLACK;

try {
colorBg = Color.parseColor(zvvDeparture.getColors().getBg());
colorFg = Color.parseColor(zvvDeparture.getColors().getFg());
} catch (Throwable e) {
//
}
catch (Throwable ignored) { }

Date departureScheduled = zvvDeparture.getDeparture() == null ? null : zvvDeparture.getDeparture().getScheduled();
Date departureRealtime = zvvDeparture.getDeparture() == null ? null : zvvDeparture.getDeparture().getRealtime();
Date arrivalScheduled = zvvDeparture.getArrival() == null ? null : zvvDeparture.getArrival().getScheduled();

// If destination id and station id are equal, it is not a valid departure
if(id == stationId) {
return null;
}

return new Departure(zvvDeparture.getName(),
Expand All @@ -23,9 +34,9 @@ public static Departure fromZvv(ch.liip.timeforcoffee.zvv.Departure zvvDeparture
zvvDeparture.getPlatform(),
colorFg,
colorBg,
zvvDeparture.getDeparture().getScheduled(),
zvvDeparture.getDeparture().getRealtime(),
zvvDeparture.getArrival().getScheduled(),
departureScheduled,
departureRealtime,
arrivalScheduled,
zvvDeparture.getAccessible(),
false
);
Expand Down
24 changes: 14 additions & 10 deletions api/src/main/java/ch/liip/timeforcoffee/api/models/Departure.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,25 @@ public String getDepartureRealtimeStr() {
}

public String getDepartureStrForZvv() {
DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
return dateFormatter.format(departureScheduled);
DateFormat dt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
if (departureScheduled != null) {
return dt.format(departureScheduled);
}

return null;
}

public String getArrivalStrForZvv() {
DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
return dateFormatter.format(arrivalScheduled);
DateFormat dt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
if (arrivalScheduled != null) {
return dt.format(arrivalScheduled);
}

return null;
}

//realtime != schedule time
public Boolean isLate() {
if (departureRealtime != null && departureScheduled != null && departureRealtime.compareTo(departureScheduled) != 0) {
return true;
}
return false;
return departureRealtime != null && departureScheduled != null && departureRealtime.compareTo(departureScheduled) != 0;
}

public String departureInMinutes() {
Expand All @@ -133,7 +137,7 @@ public String departureInMinutes() {
return timeInterval +"'";
}

long getDepartureTimeDiffInMinutes() {
private long getDepartureTimeDiffInMinutes() {
long diff = -1;
Date now = new Date();
if (departureRealtime != null){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import ch.liip.timeforcoffee.fragment.StationMapFragment;
import ch.liip.timeforcoffee.presenter.ConnectionsPresenter;

public class ConnectionsActivity extends AppCompatActivity implements SlidingUpPanelLayout.PanelSlideListener, ConnectionListFragment.Callbacks {
public class ConnectionsActivity extends AppCompatActivity implements SlidingUpPanelLayout.PanelSlideListener, StationMapFragment.Callbacks, ConnectionListFragment.Callbacks {

private SlidingUpPanelLayout mSlidingLayout;
private StationMapFragment mStationMapFragment;
Expand Down Expand Up @@ -105,10 +105,11 @@ protected void onCreate(Bundle savedInstanceState) {

// View
mStationMapFragment = (StationMapFragment) getFragmentManager().findFragmentById(R.id.station_map);
mProgressLayout = findViewById(R.id.progressLayout);

mProgressLayout = findViewById(R.id.progressLayout);
mSlidingLayout = findViewById(R.id.sliding_layout);
mSlidingLayout.setPanelSlideListener(this);
mSlidingLayout.setTouchEnabled(false);
}

@Override
Expand Down Expand Up @@ -193,7 +194,9 @@ public void onPanelAnchored(View panel) { }
public void onPanelHidden(View panel) { }

@Override
public void onConnectionSelected(Connection connection) { }
public void onMapLoaded() {
mSlidingLayout.setTouchEnabled(true);
}

public void performConnectionsUpdate() {
mPresenter.updateConnections();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.astuetz.PagerSlidingTabStrip;
Expand All @@ -29,13 +28,11 @@
import ch.liip.timeforcoffee.fragment.StationMapFragment;
import ch.liip.timeforcoffee.presenter.DeparturesPresenter;

public class DeparturesActivity extends AppCompatActivity implements SlidingUpPanelLayout.PanelSlideListener, FavoritesListFragment.Callbacks, DepartureListFragment.Callbacks {
public class DeparturesActivity extends AppCompatActivity implements SlidingUpPanelLayout.PanelSlideListener, StationMapFragment.Callbacks, FavoritesListFragment.Callbacks, DepartureListFragment.Callbacks {

private SlidingUpPanelLayout mSlidingLayout;
private StationMapFragment mStationMapFragment;
private ViewPager mTabsViewPager;
private RelativeLayout mProgressLayout;


private DeparturesPresenter mPresenter;
private DepartureListFragment mDepartureListFragment;
Expand Down Expand Up @@ -77,21 +74,15 @@ protected void onCreate(Bundle savedInstanceState) {
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);

mStationMapFragment = (StationMapFragment) getFragmentManager().findFragmentById(R.id.station_map);
mStationMapFragment.setup(station);

mProgressLayout = findViewById(R.id.progressLayout);
mSlidingLayout = findViewById(R.id.sliding_layout);
mSlidingLayout.setPanelSlideListener(this);

// Fragments
Bundle favoritesFragmentArgs = new Bundle();
favoritesFragmentArgs.putInt(FavoritesListFragment.ARG_MODE, FavoritesListFragment.ARG_MODE_DEPARTURES);

if (savedInstanceState == null) {
mDepartureListFragment = (DepartureListFragment) Fragment.instantiate(this, DepartureListFragment.class.getName());
mFavoriteListFragment = (FavoritesListFragment) Fragment.instantiate(this, FavoritesListFragment.class.getName(), favoritesFragmentArgs);
} else{
}
else{
mDepartureListFragment = (DepartureListFragment) getSupportFragmentManager().getFragment(savedInstanceState, DEPARTURE_LIST_FRAGMENT_KEY);
mFavoriteListFragment = (FavoritesListFragment) getSupportFragmentManager().getFragment(savedInstanceState, FAVORITE_LIST_FRAGMENT_KEY);
}
Expand All @@ -100,7 +91,7 @@ protected void onCreate(Bundle savedInstanceState) {
fragments.add(mDepartureListFragment);
fragments.add(mFavoriteListFragment);

// Initialize the ViewPager and bind to tabs
// View
mTabsViewPager = super.findViewById(R.id.viewpager);
mTabsViewPager.setAdapter(new TabsAdapter(
this,
Expand All @@ -117,6 +108,13 @@ public void onPageSelected(int position) {
mPresenter.updateFavorites();
}
});

mStationMapFragment = (StationMapFragment) getFragmentManager().findFragmentById(R.id.station_map);
mStationMapFragment.setup(station);

mSlidingLayout = findViewById(R.id.sliding_layout);
mSlidingLayout.setPanelSlideListener(this);
mSlidingLayout.setTouchEnabled(false);
}

@Override
Expand Down Expand Up @@ -202,6 +200,11 @@ public void onPanelAnchored(View panel) { }
@Override
public void onPanelHidden(View panel) { }

@Override
public void onMapLoaded() {
mSlidingLayout.setTouchEnabled(true);
}

@Override
public void onDepartureSelected(Departure departure) {
selectDeparture(departure);
Expand Down Expand Up @@ -264,13 +267,8 @@ public void displayToastMessage(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}

public void showProgressLayout(boolean show) {
if (show) {
mProgressLayout.setVisibility(View.VISIBLE);
}
else {
mProgressLayout.setVisibility(View.GONE);
}
public void setAreDeparturesLoading(boolean loading) {
mDepartureListFragment.showLoadingDeparturesProgressBar(loading);
}

public void displayDepartureList() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,29 @@ public ConnectionListAdapter(Context context, List<Connection> connexions) {
}

public View getView(int position, View convertView, ViewGroup parent) {

ConnectionListAdapter.ConnexionViewHolder viewHolder;
String departureLabel = mContext.getResources().getString(R.string.connection_departure);
String arrivalLabel = mContext.getResources().getString(R.string.connection_arrival);

if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.fragment_connection_list_row, parent, false);

viewHolder = new ConnectionListAdapter.ConnexionViewHolder();
viewHolder.stationTextView = (TextView) convertView.findViewById(R.id.station);
viewHolder.timeLabelTextView = (TextView) convertView.findViewById(R.id.time_label);
viewHolder.timeTextView = (TextView) convertView.findViewById(R.id.time);
viewHolder.realtimeDepartureTextView = (TextView) convertView.findViewById(R.id.realtime);
viewHolder.departureTextView = (TextView) convertView.findViewById(R.id.departure);
viewHolder.stationTextView = convertView.findViewById(R.id.station);
viewHolder.timeLabelTextView = convertView.findViewById(R.id.time_label);
viewHolder.timeTextView = convertView.findViewById(R.id.time);
viewHolder.realtimeDepartureTextView = convertView.findViewById(R.id.realtime);
viewHolder.departureTextView = convertView.findViewById(R.id.departure);


convertView.setTag(viewHolder);

} else {
}
else {
viewHolder = (ConnectionListAdapter.ConnexionViewHolder) convertView.getTag();
}

String departureLabel = mContext.getResources().getString(R.string.connection_departure);
String arrivalLabel = mContext.getResources().getString(R.string.connection_arrival);

Connection connection = this.mConnexions.get(position);
viewHolder.stationTextView.setText(connection.getName());
viewHolder.timeLabelTextView.setText(connection.getTimeLabel(departureLabel, arrivalLabel));
Expand All @@ -67,18 +66,15 @@ public View getView(int position, View convertView, ViewGroup parent) {
viewHolder.realtimeDepartureTextView.setVisibility(View.VISIBLE);
viewHolder.realtimeDepartureTextView.setText(connection.getRealtimeDepartureStr());
viewHolder.timeTextView.setPaintFlags(viewHolder.timeTextView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
} else {
}
else {
viewHolder.realtimeDepartureTextView.setVisibility(View.GONE);
viewHolder.timeTextView.setPaintFlags(viewHolder.timeTextView.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
}

return convertView;
}

public Connection getConnexion(int position) {
return this.mConnexions.get(position);
}

public void setConnexions(List<Connection> connexions) {
this.mConnexions.clear();
this.mConnexions.addAll(connexions);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.liip.timeforcoffee.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
Expand All @@ -9,7 +10,6 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -25,23 +25,12 @@ public class ConnectionListFragment extends ListFragment implements SwipeRefresh
private ConnectionListAdapter mConnectionListAdapter;
private SwipeRefreshLayout mSwipeRefreshLayout;

private ConnectionListFragment.Callbacks mCallbacks = sDummyCallbacks;
private Callbacks mCallbacks;

public interface Callbacks {
void onConnectionSelected(Connection connection);
void onRefresh();
}

private static ConnectionListFragment.Callbacks sDummyCallbacks = new ConnectionListFragment.Callbacks() {
@Override
public void onConnectionSelected(Connection connection) {
}

@Override
public void onRefresh() {
}
};

/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
Expand Down Expand Up @@ -81,24 +70,28 @@ public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mCallbacks.onRefresh();
mSwipeRefreshLayout.setRefreshing(false);
if(mCallbacks != null) {
mCallbacks.onRefresh();
}
}
}, 100);
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (!(context instanceof ConnectionListFragment.Callbacks)) {
public void onAttach(Activity activity) {
super.onAttach(activity);
if (!(activity instanceof ConnectionListFragment.Callbacks)) {
throw new IllegalStateException("Activity must implement fragment's callbacks.");
}
mCallbacks = (ConnectionListFragment.Callbacks) context;

mCallbacks = (ConnectionListFragment.Callbacks) activity;
}

public void onListItemClick(ListView listView, View view, int position, long id) {
super.onListItemClick(listView, view, position, id);
mCallbacks.onConnectionSelected(mConnectionListAdapter.getConnexion(position));
@Override
public void onDetach() {
super.onDetach();
mCallbacks = null;
}

public void setConnections(List<Connection> connections) {
Expand Down
Loading

0 comments on commit 5fff88c

Please sign in to comment.