Introduced a view mode and shared preference to determine which style of view to use.

This commit is contained in:
Martin Entwistle 2017-10-05 14:13:26 +01:00
parent 3a15f9a1d6
commit 7740a4b7a0
3 changed files with 55 additions and 15 deletions

View file

@ -3,12 +3,14 @@ package fr.free.nrw.commons.nearby;
import android.Manifest; import android.Manifest;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.location.LocationManager; import android.location.LocationManager;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
@ -42,28 +44,46 @@ public class NearbyActivity extends NavigationBaseActivity {
@BindView(R.id.progressBar) @BindView(R.id.progressBar)
ProgressBar progressBar; ProgressBar progressBar;
private boolean isMapViewActive = false;
private static final int LOCATION_REQUEST = 1; private static final int LOCATION_REQUEST = 1;
public static final String MAP_LAST_USED_PREFERENCE = "mapLastUsed";
private LocationServiceManager locationManager; private LocationServiceManager locationManager;
private LatLng curLatLang; private LatLng curLatLang;
private Bundle bundle; private Bundle bundle;
private NearbyAsyncTask nearbyAsyncTask; private NearbyAsyncTask nearbyAsyncTask;
private SharedPreferences sharedPreferences;
private NearbyActivityMode viewMode;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
setContentView(R.layout.activity_nearby); setContentView(R.layout.activity_nearby);
ButterKnife.bind(this); ButterKnife.bind(this);
checkLocationPermission(); checkLocationPermission();
bundle = new Bundle(); bundle = new Bundle();
initDrawer(); initDrawer();
initViewState();
}
private void initViewState() {
if (sharedPreferences.getBoolean(MAP_LAST_USED_PREFERENCE, false)) {
viewMode = NearbyActivityMode.MAP;
} else {
viewMode = NearbyActivityMode.LIST;
}
} }
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater(); MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_nearby, menu); inflater.inflate(R.menu.menu_nearby, menu);
if (viewMode == NearbyActivityMode.MAP) {
MenuItem item = menu.findItem(R.id.action_toggle_view);
item.setIcon(R.drawable.ic_list_white_24dp);
}
return super.onCreateOptionsMenu(menu); return super.onCreateOptionsMenu(menu);
} }
@ -74,19 +94,23 @@ public class NearbyActivity extends NavigationBaseActivity {
case R.id.action_refresh: case R.id.action_refresh:
refreshView(); refreshView();
return true; return true;
case R.id.action_map: case R.id.action_toggle_view:
showMapView(); toggleView();
if (isMapViewActive) { toggleIcon(item);
item.setIcon(R.drawable.ic_list_white_24dp);
} else {
item.setIcon(R.drawable.ic_map_white_24dp);
}
return true; return true;
default: default:
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
} }
private void toggleIcon(MenuItem item) {
if (viewMode == NearbyActivityMode.MAP) {
item.setIcon(R.drawable.ic_list_white_24dp);
} else {
item.setIcon(R.drawable.ic_map_white_24dp);
}
}
private void startLookingForNearby() { private void startLookingForNearby() {
locationManager = new LocationServiceManager(this); locationManager = new LocationServiceManager(this);
locationManager.registerLocationManager(); locationManager.registerLocationManager();
@ -198,23 +222,30 @@ public class NearbyActivity extends NavigationBaseActivity {
} }
} }
private void showMapView() { private void toggleView() {
if (nearbyAsyncTask != null) { if (nearbyAsyncTask != null) {
if (!isMapViewActive) { if (viewMode == NearbyActivityMode.LIST) {
isMapViewActive = true; viewMode = NearbyActivityMode.MAP;
if (nearbyAsyncTask.getStatus() == AsyncTask.Status.FINISHED) { if (nearbyAsyncTask.getStatus() == AsyncTask.Status.FINISHED) {
setMapFragment(); setMapFragment();
setMapViewLastUsed(true);
} }
} else { } else {
isMapViewActive = false; viewMode = NearbyActivityMode.LIST;
if (nearbyAsyncTask.getStatus() == AsyncTask.Status.FINISHED) { if (nearbyAsyncTask.getStatus() == AsyncTask.Status.FINISHED) {
setListFragment(); setListFragment();
setMapViewLastUsed(false);
} }
} }
} }
} }
private void setMapViewLastUsed(boolean lastUsed)
{
sharedPreferences.edit().putBoolean(MAP_LAST_USED_PREFERENCE, lastUsed).apply();
}
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
@ -287,9 +318,12 @@ public class NearbyActivity extends NavigationBaseActivity {
bundle.putString("CurLatLng", gsonCurLatLng); bundle.putString("CurLatLng", gsonCurLatLng);
// Begin the transaction // Begin the transaction
if (isMapViewActive) { if (viewMode == NearbyActivityMode.MAP)
{
setMapFragment(); setMapFragment();
} else { }
else
{
setListFragment(); setListFragment();
} }

View file

@ -0,0 +1,6 @@
package fr.free.nrw.commons.nearby;
enum NearbyActivityMode {
MAP,
LIST
}

View file

@ -3,7 +3,7 @@
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item <item
android:id="@+id/action_map" android:id="@+id/action_toggle_view"
android:title="@string/refresh_button" android:title="@string/refresh_button"
android:icon="@drawable/ic_map_white_24dp" android:icon="@drawable/ic_map_white_24dp"
android:orderInCategory="1" android:orderInCategory="1"