mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 12:23:58 +01:00
Introduced a view mode and shared preference to determine which style of view to use.
This commit is contained in:
parent
cfcfcaf194
commit
30104af6a0
3 changed files with 55 additions and 15 deletions
|
|
@ -4,12 +4,14 @@ import android.Manifest;
|
|||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.location.LocationManager;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.app.ActivityCompat;
|
||||
import android.support.v4.app.Fragment;
|
||||
|
|
@ -43,28 +45,46 @@ public class NearbyActivity extends NavigationBaseActivity {
|
|||
|
||||
@BindView(R.id.progressBar)
|
||||
ProgressBar progressBar;
|
||||
private boolean isMapViewActive = false;
|
||||
private static final int LOCATION_REQUEST = 1;
|
||||
public static final String MAP_LAST_USED_PREFERENCE = "mapLastUsed";
|
||||
|
||||
private LocationServiceManager locationManager;
|
||||
private LatLng curLatLang;
|
||||
private Bundle bundle;
|
||||
private NearbyAsyncTask nearbyAsyncTask;
|
||||
private SharedPreferences sharedPreferences;
|
||||
private NearbyActivityMode viewMode;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
|
||||
setContentView(R.layout.activity_nearby);
|
||||
ButterKnife.bind(this);
|
||||
checkLocationPermission();
|
||||
bundle = new Bundle();
|
||||
initDrawer();
|
||||
initViewState();
|
||||
}
|
||||
|
||||
private void initViewState() {
|
||||
if (sharedPreferences.getBoolean(MAP_LAST_USED_PREFERENCE, false)) {
|
||||
viewMode = NearbyActivityMode.MAP;
|
||||
} else {
|
||||
viewMode = NearbyActivityMode.LIST;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
MenuInflater inflater = getMenuInflater();
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
@ -75,19 +95,23 @@ public class NearbyActivity extends NavigationBaseActivity {
|
|||
case R.id.action_refresh:
|
||||
refreshView();
|
||||
return true;
|
||||
case R.id.action_map:
|
||||
showMapView();
|
||||
if (isMapViewActive) {
|
||||
item.setIcon(R.drawable.ic_list_white_24dp);
|
||||
} else {
|
||||
item.setIcon(R.drawable.ic_map_white_24dp);
|
||||
}
|
||||
case R.id.action_toggle_view:
|
||||
toggleView();
|
||||
toggleIcon(item);
|
||||
return true;
|
||||
default:
|
||||
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() {
|
||||
locationManager = new LocationServiceManager(this);
|
||||
locationManager.registerLocationManager();
|
||||
|
|
@ -214,23 +238,30 @@ public class NearbyActivity extends NavigationBaseActivity {
|
|||
}
|
||||
}
|
||||
|
||||
private void showMapView() {
|
||||
private void toggleView() {
|
||||
if (nearbyAsyncTask != null) {
|
||||
if (!isMapViewActive) {
|
||||
isMapViewActive = true;
|
||||
if (viewMode == NearbyActivityMode.LIST) {
|
||||
viewMode = NearbyActivityMode.MAP;
|
||||
if (nearbyAsyncTask.getStatus() == AsyncTask.Status.FINISHED) {
|
||||
setMapFragment();
|
||||
setMapViewLastUsed(true);
|
||||
}
|
||||
|
||||
} else {
|
||||
isMapViewActive = false;
|
||||
viewMode = NearbyActivityMode.LIST;
|
||||
if (nearbyAsyncTask.getStatus() == AsyncTask.Status.FINISHED) {
|
||||
setListFragment();
|
||||
setMapViewLastUsed(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setMapViewLastUsed(boolean lastUsed)
|
||||
{
|
||||
sharedPreferences.edit().putBoolean(MAP_LAST_USED_PREFERENCE, lastUsed).apply();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
|
|
@ -303,9 +334,12 @@ public class NearbyActivity extends NavigationBaseActivity {
|
|||
bundle.putString("CurLatLng", gsonCurLatLng);
|
||||
|
||||
// Begin the transaction
|
||||
if (isMapViewActive) {
|
||||
if (viewMode == NearbyActivityMode.MAP)
|
||||
{
|
||||
setMapFragment();
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
setListFragment();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
package fr.free.nrw.commons.nearby;
|
||||
|
||||
enum NearbyActivityMode {
|
||||
MAP,
|
||||
LIST
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@
|
|||
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
|
||||
|
||||
<item
|
||||
android:id="@+id/action_map"
|
||||
android:id="@+id/action_toggle_view"
|
||||
android:title="@string/refresh_button"
|
||||
android:icon="@drawable/ic_map_white_24dp"
|
||||
android:orderInCategory="1"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue