Add progressbar to MapMarkersDialog; refactor some methods

This commit is contained in:
Alexander Sytnyk 2018-03-21 17:04:04 +02:00
parent b60130f7e8
commit b71ae36c09
15 changed files with 122 additions and 81 deletions

View file

@ -30,16 +30,34 @@
app:contentInsetLeft="54dp"
app:contentInsetStart="54dp">
<net.osmand.plus.widgets.TextViewEx
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="center_vertical"
android:maxLines="1"
android:text="@string/map_markers"
android:textColor="@color/color_white"
android:textSize="@dimen/dialog_header_text_size"
osmand:typeface="@string/font_roboto_medium"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<net.osmand.plus.widgets.TextViewEx
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:ellipsize="end"
android:gravity="center_vertical"
android:maxLines="1"
android:text="@string/map_markers"
android:textColor="@color/color_white"
android:textSize="@dimen/dialog_header_text_size"
osmand:typeface="@string/font_roboto_medium"/>
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:indeterminate="true"
android:visibility="gone"/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

View file

@ -109,9 +109,9 @@ public class FavouritesDbHelper {
return changed;
}
private void syncWithMarkers(FavoriteGroup favGroup) {
private void runSyncWithMarkers(FavoriteGroup favGroup) {
MapMarkersHelper helper = context.getMapMarkersHelper();
helper.syncGroupAsync(helper.getOrCreateGroup(favGroup));
helper.runSynchronization(helper.getOrCreateGroup(favGroup));
}
private void removeFromMarkers(FavoriteGroup favGroup) {
@ -121,7 +121,7 @@ public class FavouritesDbHelper {
private void addToMarkers(FavoriteGroup favGroup) {
MapMarkersHelper helper = context.getMapMarkersHelper();
helper.addAndSyncGroup(helper.getOrCreateGroup(favGroup), null);
helper.syncWithMarkers(helper.getOrCreateGroup(favGroup));
}
private File getInternalFile() {
@ -140,7 +140,7 @@ public class FavouritesDbHelper {
cachedFavoritePoints.remove(p);
}
for (FavoriteGroup gr : groupsToSync) {
syncWithMarkers(gr);
runSyncWithMarkers(gr);
}
}
if (groupsToDelete != null) {
@ -163,7 +163,7 @@ public class FavouritesDbHelper {
FavoriteGroup group = flatGroups.get(p.getCategory());
if (group != null) {
group.points.remove(p);
syncWithMarkers(group);
runSyncWithMarkers(group);
}
cachedFavoritePoints.remove(p);
}
@ -193,7 +193,7 @@ public class FavouritesDbHelper {
sortAll();
saveCurrentPointsIntoFile();
}
syncWithMarkers(group);
runSyncWithMarkers(group);
return true;
}
@ -288,7 +288,7 @@ public class FavouritesDbHelper {
}
sortAll();
saveCurrentPointsIntoFile();
syncWithMarkers(getOrCreateGroup(p, 0));
runSyncWithMarkers(getOrCreateGroup(p, 0));
return true;
}
@ -296,7 +296,7 @@ public class FavouritesDbHelper {
p.setLatitude(lat);
p.setLongitude(lon);
saveCurrentPointsIntoFile();
syncWithMarkers(getOrCreateGroup(p, 0));
runSyncWithMarkers(getOrCreateGroup(p, 0));
return true;
}
@ -622,7 +622,7 @@ public class FavouritesDbHelper {
for (FavouritePoint p : gr.points) {
p.setColor(color);
}
syncWithMarkers(gr);
runSyncWithMarkers(gr);
}
if (group.visible != visible) {
FavoriteGroup gr = flatGroups.get(group.name);
@ -630,7 +630,7 @@ public class FavouritesDbHelper {
for (FavouritePoint p : gr.points) {
p.setVisible(visible);
}
syncWithMarkers(gr);
runSyncWithMarkers(gr);
}
if (!group.name.equals(newName)) {
FavoriteGroup gr = flatGroups.remove(group.name);

View file

@ -539,7 +539,7 @@ public class GpxSelectionHelper {
File gpx = new File(gpxFile.path);
if (gpx.exists()) {
MapMarkersHelper mapMarkersHelper = app.getMapMarkersHelper();
mapMarkersHelper.syncGroupAsync(mapMarkersHelper.getOrCreateGroup(gpx));
mapMarkersHelper.runSynchronization(mapMarkersHelper.getOrCreateGroup(gpx));
}
}

View file

@ -29,6 +29,7 @@ import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@ -64,6 +65,7 @@ public class MapMarkersHelper {
private List<MapMarkersGroup> mapMarkersGroups = new ArrayList<>();
private List<MapMarkerChangedListener> listeners = new ArrayList<>();
private Set<OnGroupSyncedListener> syncListeners = new HashSet<>();
private boolean startFromMyLocation;
@ -162,7 +164,7 @@ public class MapMarkersHelper {
private void syncAllGroupsAsync() {
for (MapMarkersGroup gr : mapMarkersGroups) {
if (gr.getId() != null && gr.getName() != null) {
syncGroupAsync(gr);
runSynchronization(gr);
}
}
}
@ -282,16 +284,11 @@ public class MapMarkersHelper {
return getMapMarkerGroupById(id) != null;
}
public void syncGroupAsync(@NonNull MapMarkersGroup group) {
syncGroupAsync(group, null);
}
public void syncGroupAsync(@NonNull final MapMarkersGroup group,
@Nullable final OnGroupSyncedListener listener) {
public void runSynchronization(@NonNull final MapMarkersGroup group) {
ctx.runInUIThread(new Runnable() {
@Override
public void run() {
new SyncGroupTask(group, listener).executeOnExecutor(executorService);
new SyncGroupTask(group).executeOnExecutor(executorService);
}
});
}
@ -310,14 +307,14 @@ public class MapMarkersHelper {
return getMapMarker(favouritePoint) != null;
}
public void addAndSyncGroup(@NonNull MapMarkersGroup group, @Nullable OnGroupSyncedListener listener) {
public void syncWithMarkers(@NonNull MapMarkersGroup group) {
if (!isGroupSynced(group.getId())) {
markersDbHelper.addGroup(group);
addToGroupsList(group);
} else {
} else if (group.wptCategories != null) {
markersDbHelper.updateGroupCategories(group.getId(), group.getWptCategoriesString());
}
syncGroupAsync(group, listener);
runSynchronization(group);
}
public void removeMarkersGroup(MapMarkersGroup group) {
@ -851,6 +848,14 @@ public class MapMarkersHelper {
}
}
public void addSyncListener(OnGroupSyncedListener listener) {
syncListeners.add(listener);
}
public void removeSyncListener(OnGroupSyncedListener listener) {
syncListeners.remove(listener);
}
public void addListener(MapMarkerChangedListener l) {
if (!listeners.contains(l)) {
listeners.add(l);
@ -906,7 +911,6 @@ public class MapMarkersHelper {
return fout.getAbsolutePath();
}
// TODO update all 3 collections at once?
// ---------------------------------------------------------------------------------------------
// accessors to active markers:
@ -994,17 +998,32 @@ public class MapMarkersHelper {
}
public interface OnGroupSyncedListener {
void onSyncStarted();
void onSyncDone();
}
private class SyncGroupTask extends AsyncTask<Void, Void, Void> {
private MapMarkersGroup group;
private OnGroupSyncedListener listener;
SyncGroupTask(MapMarkersGroup group, OnGroupSyncedListener listener) {
SyncGroupTask(MapMarkersGroup group) {
this.group = group;
this.listener = listener;
}
@Override
protected void onPreExecute() {
if (!syncListeners.isEmpty()) {
ctx.runInUIThread(new Runnable() {
@Override
public void run() {
for (OnGroupSyncedListener listener : syncListeners) {
listener.onSyncStarted();
}
}
});
}
}
@Override
@ -1067,11 +1086,13 @@ public class MapMarkersHelper {
@Override
protected void onPostExecute(Void aVoid) {
if (listener != null) {
if (!syncListeners.isEmpty()) {
ctx.runInUIThread(new Runnable() {
@Override
public void run() {
listener.onSyncDone();
for (OnGroupSyncedListener listener : syncListeners) {
listener.onSyncDone();
}
}
});
}

View file

@ -171,7 +171,7 @@ public class EditFavoriteGroupDialogFragment extends MenuBottomSheetDialogFragme
if (synced) {
markersHelper.removeMarkersGroup(markersGr);
} else {
markersHelper.addAndSyncGroup(markersGr, null);
markersHelper.syncWithMarkers(markersGr);
}
dismiss();
MapActivity.launchMapActivityMoveToTop(getActivity());

View file

@ -418,7 +418,7 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment {
FavoriteGroup favGr = helper.getGroup(entry.getKey());
MapMarkersGroup markersGr = markersHelper.getOrCreateGroup(favGr);
if (entry.getValue().size() == favGr.points.size()) {
markersHelper.addAndSyncGroup(markersGr, null);
markersHelper.syncWithMarkers(markersGr);
} else {
for (FavouritePoint fp : entry.getValue()) {
points.add(new LatLon(fp.getLatitude(), fp.getLongitude()));

View file

@ -180,7 +180,7 @@ public class WptPtEditorFragment extends PointEditorFragment {
File gpx = new File(gpxFile.path);
if (gpx.exists()) {
MapMarkersHelper helper = getMyApplication().getMapMarkersHelper();
helper.syncGroupAsync(helper.getOrCreateGroup(gpx));
helper.runSynchronization(helper.getOrCreateGroup(gpx));
}
}

View file

@ -24,7 +24,6 @@ public class AddFavouritesGroupBottomSheetDialogFragment extends AddGroupBottomS
@Override
protected void onItemClick(int position) {
showProgressBar();
FavoriteGroup group = favouritesDbHelper.getFavoriteGroups().get(position - 1);
if (!group.visible) {
favouritesDbHelper.editFavouriteGroup(group, group.name, group.color, true);

View file

@ -2,14 +2,12 @@ package net.osmand.plus.mapmarkers;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ContextThemeWrapper;
import android.view.View;
import net.osmand.plus.MapMarkersHelper.MapMarkersGroup;
import net.osmand.plus.MapMarkersHelper.OnGroupSyncedListener;
import net.osmand.plus.R;
import net.osmand.plus.base.MenuBottomSheetDialogFragment;
import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem;
@ -60,21 +58,8 @@ public abstract class AddGroupBottomSheetDialogFragment extends MenuBottomSheetD
}
protected void addAndSyncGroup(MapMarkersGroup group) {
getMyApplication().getMapMarkersHelper().addAndSyncGroup(group, new OnGroupSyncedListener() {
@Override
public void onSyncDone() {
Fragment parent = getParentFragment();
if (parent instanceof MapMarkersGroupsFragment) {
((MapMarkersGroupsFragment) parent).updateAdapter();
}
dismiss();
}
});
}
protected void showProgressBar() {
mainView.findViewById(R.id.groups_recycler_view).setVisibility(View.GONE);
mainView.findViewById(R.id.progress_bar).setVisibility(View.VISIBLE);
getMyApplication().getMapMarkersHelper().syncWithMarkers(group);
dismiss();
}
protected abstract GroupsAdapter createAdapter();

View file

@ -70,7 +70,6 @@ public class AddTracksGroupBottomSheetDialogFragment extends AddGroupBottomSheet
fragment.show(getParentFragment().getChildFragmentManager(), SelectWptCategoriesBottomSheetDialogFragment.TAG);
dismiss();
} else {
showProgressBar();
addAndSyncGroup(createMapMarkersSyncGroup(getMyApplication(), dataItem));
}
}

View file

@ -18,6 +18,7 @@ import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
@ -26,6 +27,7 @@ import net.osmand.data.LatLon;
import net.osmand.plus.LockableViewPager;
import net.osmand.plus.MapMarkersHelper;
import net.osmand.plus.MapMarkersHelper.MapMarkersSortByDef;
import net.osmand.plus.MapMarkersHelper.OnGroupSyncedListener;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
@ -45,7 +47,7 @@ import java.util.List;
import static net.osmand.plus.mapmarkers.OptionsBottomSheetDialogFragment.GROUPS_MARKERS_MENU;
import static net.osmand.plus.mapmarkers.OptionsBottomSheetDialogFragment.HISTORY_MARKERS_MENU;
public class MapMarkersDialogFragment extends android.support.v4.app.DialogFragment {
public class MapMarkersDialogFragment extends android.support.v4.app.DialogFragment implements OnGroupSyncedListener {
public static final String TAG = "MapMarkersDialogFragment";
@ -62,6 +64,7 @@ public class MapMarkersDialogFragment extends android.support.v4.app.DialogFragm
private Snackbar snackbar;
private LockableViewPager viewPager;
private BottomNavigationView bottomNav;
private ProgressBar progressBar;
private boolean lightTheme;
private String groupIdToOpen;
@ -158,6 +161,8 @@ public class MapMarkersDialogFragment extends android.support.v4.app.DialogFragm
final MapMarkersViewPagerAdapter adapter = new MapMarkersViewPagerAdapter(getChildFragmentManager());
viewPager.setAdapter(adapter);
progressBar = (ProgressBar) mainView.findViewById(R.id.progress_bar);
bottomNav = mainView.findViewById(R.id.map_markers_bottom_navigation);
BottomNavigationViewHelper.disableShiftMode(bottomNav);
if (!lightTheme) {
@ -206,6 +211,35 @@ public class MapMarkersDialogFragment extends android.support.v4.app.DialogFragm
return mainView;
}
@Override
public void onResume() {
super.onResume();
getMyApplication().getMapMarkersHelper().addSyncListener(this);
}
@Override
public void onPause() {
super.onPause();
getMyApplication().getMapMarkersHelper().removeSyncListener(this);
}
@Override
public void onSyncStarted() {
switchProgressbarVisibility(true);
}
@Override
public void onSyncDone() {
updateAdapters();
switchProgressbarVisibility(false);
}
private void switchProgressbarVisibility(boolean visible) {
if (progressBar != null) {
progressBar.setVisibility(visible ? View.VISIBLE : View.GONE);
}
}
private void setupLocationUpdate(boolean activeFr, boolean groupsFr) {
if (activeFr) {
activeFragment.startLocationUpdate();

View file

@ -2,7 +2,6 @@ package net.osmand.plus.mapmarkers;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
@ -123,15 +122,7 @@ public class SelectWptCategoriesBottomSheetDialogFragment extends MenuBottomShee
MapMarkersGroup markersGr = mapMarkersHelper.getOrCreateGroup(new File(gpxFile.path));
markersGr.setWptCategories(selectedCategories);
mapMarkersHelper.addAndSyncGroup(markersGr, new MapMarkersHelper.OnGroupSyncedListener() {
@Override
public void onSyncDone() {
Fragment parent = getParentFragment();
if (parent instanceof MapMarkersGroupsFragment) {
((MapMarkersGroupsFragment) parent).updateAdapter();
}
}
});
mapMarkersHelper.syncWithMarkers(markersGr);
dismiss();
}

View file

@ -19,7 +19,6 @@ import net.osmand.plus.IconsCache;
import net.osmand.plus.MapMarkersHelper.GroupHeader;
import net.osmand.plus.MapMarkersHelper.MapMarker;
import net.osmand.plus.MapMarkersHelper.MapMarkersGroup;
import net.osmand.plus.MapMarkersHelper.OnGroupSyncedListener;
import net.osmand.plus.MapMarkersHelper.ShowHideHistoryButton;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
@ -418,12 +417,7 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
tv.setTextColor(ContextCompat.getColor(mapActivity, R.color.color_dialog_buttons_dark));
snackbar.show();
}
app.getMapMarkersHelper().syncGroupAsync(group, new OnGroupSyncedListener() {
@Override
public void onSyncDone() {
updateDisplayedData();
}
});
app.getMapMarkersHelper().runSynchronization(group);
}
};
headerViewHolder.disableGroupSwitch.setOnCheckedChangeListener(null);

View file

@ -580,14 +580,14 @@ public class TrackPointFragment extends OsmandExpandableListFragment {
File gpx = new File(gpxFile.path);
if (gpx.exists()) {
MapMarkersHelper helper = app.getMapMarkersHelper();
helper.syncGroupAsync(helper.getOrCreateGroup(gpx));
helper.runSynchronization(helper.getOrCreateGroup(gpx));
}
}
private void addMapMarkersSyncGroup() {
MapMarkersHelper markersHelper = app.getMapMarkersHelper();
final MapMarkersGroup markersGr = markersHelper.getOrCreateGroup(getGpxDataItem().getFile());
markersHelper.addAndSyncGroup(markersGr, null);
markersHelper.syncWithMarkers(markersGr);
GPXFile gpxFile = getTrackActivity().getGpx();
if (gpxFile != null) {
app.getSelectedGpxHelper().selectGpxFile(gpxFile, true, false);

View file

@ -664,7 +664,7 @@ public class GPXLayer extends OsmandMapLayer implements ContextMenuLayer.IContex
private void syncGpx(GPXFile gpxFile) {
File gpx = new File(gpxFile.path);
if (gpx.exists()) {
mapMarkersHelper.syncGroupAsync(view.getApplication().getMapMarkersHelper().getOrCreateGroup(gpx));
mapMarkersHelper.runSynchronization(view.getApplication().getMapMarkersHelper().getOrCreateGroup(gpx));
}
}