diff --git a/mobile/src/main/java/ch/liip/timeforcoffee/presenter/MainPresenter.java b/mobile/src/main/java/ch/liip/timeforcoffee/presenter/MainPresenter.java index 815fb28..56f90b4 100644 --- a/mobile/src/main/java/ch/liip/timeforcoffee/presenter/MainPresenter.java +++ b/mobile/src/main/java/ch/liip/timeforcoffee/presenter/MainPresenter.java @@ -82,7 +82,7 @@ public void onPauseView() { @Override public void onRefreshView() { - loadStationsWithLastPosition(); + loadStationsWithLastPositionForce(); } public void onDestroy() { @@ -90,57 +90,12 @@ public void onDestroy() { mEventBus.unregister(this); } - public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { - if (requestCode == PERMISSION_REQUEST_CODE) { - for (int i = 0; i < permissions.length; i++) { - String permission = permissions[i]; - int grantResult = grantResults[i]; - - if (!(permission.equals(locationPermission))) { - continue; - } - - if (grantResult == PackageManager.PERMISSION_GRANTED) { - startLocation(); - } else { - mActivity.setIsPositionLoading(false); - SnackBars.showLocalisationSettings(mActivity); - } - } - } - } - - @Override - public void onLocationUpdated(Location location) { - Log.i(LOG_TAG, "onLocationUpdated : lat = " + location.getLatitude() + " , long = " + location.getLongitude()); - mLastLocation = location; - - loadStations(location); - } - - @Subscribe - public void onStationsFetched(StationsLocationFetchedEvent event) { - mActivity.setIsPositionLoading(false); - - mStations = event.getStations(); - mActivity.updateStations(mStations); - - updateFavorites(); - } - - @Subscribe - public void onFetchErrorEvent(FetchStationsLocationErrorEvent event) { - mActivity.setIsPositionLoading(false); - SnackBars.showNetworkError(mActivity, new View.OnClickListener() { - @Override - public void onClick(View view) { - loadStationsWithLastPosition(); - } - }); - } - public void updateStations() { if (Build.FINGERPRINT.contains("generic")) { //emulator + mLastLocation = new Location("emulator"); + mLastLocation.setLatitude(46.8017); + mLastLocation.setLongitude(7.1456); + loadStationsWithLastPosition(); } else if (!mIsCapturingLocation) { @@ -182,14 +137,16 @@ private void updateFavoritesOnFavoriteList() { private void startLocation() { if (!permissionsChecker.LacksPermission(locationPermission)) { - mActivity.updateStations(new ArrayList()); - mActivity.setIsPositionLoading(true); + if(mStations == null || mStations.size() == 0) { + mActivity.setIsPositionLoading(true); + } if (!SmartLocation.with(mActivity).location().state().locationServicesEnabled()) { SnackBars.showLocalisationServiceOff(mActivity); mActivity.setIsPositionLoading(false); return; - } else if(SmartLocation.with(mActivity).location().state().isGpsAvailable() && !SmartLocation.with(mActivity).location().state().isNetworkAvailable()) { + } + else if(SmartLocation.with(mActivity).location().state().isGpsAvailable() && !SmartLocation.with(mActivity).location().state().isNetworkAvailable()) { SnackBars.showLocalisationServiceSetToDeviceOnly(mActivity); mActivity.setIsPositionLoading(false); return; @@ -201,7 +158,8 @@ private void startLocation() { .config(locationParams) .oneFix() .start(this); - } else { + } + else { permissionsChecker.RequestPermission(mActivity, locationPermission, PERMISSION_REQUEST_CODE, mActivity.getResources().getString(R.string.permission_message)); } } @@ -211,27 +169,84 @@ private void stopLocation() { SmartLocation.with(mActivity).location().stop(); } - private void loadStations(Location location) { - if (location != null) { - Log.i(LOG_TAG, "get stations for lat = " + location.getLatitude() + " and long = " + location.getLongitude()); - mEventBus.post(new FetchStationsLocationEvent(location.getLatitude(), location.getLongitude())); + private void loadStationsWithLastPosition() { + if(mStations == null || mStations.size() == 0) { + mActivity.setIsPositionLoading(true); + } + + if (mLastLocation != null) { + loadStations(mLastLocation); + } + else { + startLocation(); } } - private void loadStationsWithLastPosition() { + private void loadStationsWithLastPositionForce() { mActivity.updateStations(new ArrayList()); mActivity.setIsPositionLoading(true); - if (Build.FINGERPRINT.contains("generic")) { //emulator - mLastLocation = new Location("emulator"); - mLastLocation.setLatitude(46.8017); - mLastLocation.setLongitude(7.1456); - } - if (mLastLocation != null) { loadStations(mLastLocation); - } else { + } + else { startLocation(); } } + + private void loadStations(Location location) { + if (location != null) { + Log.i(LOG_TAG, "get stations for lat = " + location.getLatitude() + " and long = " + location.getLongitude()); + mEventBus.post(new FetchStationsLocationEvent(location.getLatitude(), location.getLongitude())); + } + } + + public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { + if (requestCode == PERMISSION_REQUEST_CODE) { + for (int i = 0; i < permissions.length; i++) { + String permission = permissions[i]; + int grantResult = grantResults[i]; + + if (!(permission.equals(locationPermission))) { + continue; + } + + if (grantResult == PackageManager.PERMISSION_GRANTED) { + startLocation(); + } else { + mActivity.setIsPositionLoading(false); + SnackBars.showLocalisationSettings(mActivity); + } + } + } + } + + @Override + public void onLocationUpdated(Location location) { + Log.i(LOG_TAG, "onLocationUpdated : lat = " + location.getLatitude() + " , long = " + location.getLongitude()); + mLastLocation = location; + + loadStations(location); + } + + @Subscribe + public void onStationsFetched(StationsLocationFetchedEvent event) { + mActivity.setIsPositionLoading(false); + + mStations = event.getStations(); + mActivity.updateStations(mStations); + + updateFavorites(); + } + + @Subscribe + public void onFetchErrorEvent(FetchStationsLocationErrorEvent event) { + mActivity.setIsPositionLoading(false); + SnackBars.showNetworkError(mActivity, new View.OnClickListener() { + @Override + public void onClick(View view) { + loadStationsWithLastPosition(); + } + }); + } }