diff --git a/android/src/com/mapswithme/maps/BookmarkCategoriesCache.java b/android/src/com/mapswithme/maps/BookmarkCategoriesCache.java new file mode 100644 index 0000000000..2ad7d98265 --- /dev/null +++ b/android/src/com/mapswithme/maps/BookmarkCategoriesCache.java @@ -0,0 +1,42 @@ +package com.mapswithme.maps; + +import android.content.Context; +import android.database.Observable; +import android.support.annotation.NonNull; +import android.support.v7.widget.RecyclerView; + +import com.mapswithme.maps.bookmarks.data.BookmarkCategory; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class BookmarkCategoriesCache extends Observable +{ + @NonNull + private List mCachedItems = new ArrayList<>(); + + public void updateItems(@NonNull List cachedItems) + { + mCachedItems = Collections.unmodifiableList(cachedItems); + notifyChanged(); + } + + @NonNull + public List getItems() + { + return mCachedItems; + } + + private void notifyChanged() { + for (int i = mObservers.size() - 1; i >= 0; i--) { + mObservers.get(i).onChanged(); + } + } + + @NonNull + public static BookmarkCategoriesCache from(@NonNull Context context) + { + return MwmApplication.from(context).getBookmarkCategoriesCache(); + } +} diff --git a/android/src/com/mapswithme/maps/MwmActivity.java b/android/src/com/mapswithme/maps/MwmActivity.java index 1cb24cba1c..ecd98fdf1e 100644 --- a/android/src/com/mapswithme/maps/MwmActivity.java +++ b/android/src/com/mapswithme/maps/MwmActivity.java @@ -40,6 +40,7 @@ import com.mapswithme.maps.base.BaseMwmFragmentActivity; import com.mapswithme.maps.base.OnBackPressListener; import com.mapswithme.maps.bookmarks.BookmarkCategoriesActivity; import com.mapswithme.maps.bookmarks.BookmarksCatalogActivity; +import com.mapswithme.maps.bookmarks.data.BookmarkCategoriesDataProvider; import com.mapswithme.maps.bookmarks.data.BookmarkCategory; import com.mapswithme.maps.bookmarks.data.BookmarkManager; import com.mapswithme.maps.bookmarks.data.CatalogCustomProperty; @@ -2387,7 +2388,12 @@ public class MwmActivity extends BaseMwmFragmentActivity @Override public void onBookmarksLoadingFinished() { - // Do nothing + BookmarkCategoriesDataProvider.CoreBookmarkCategoriesDataProvider coreDataProvider = + new BookmarkCategoriesDataProvider.CoreBookmarkCategoriesDataProvider(); + BookmarkCategoriesDataProvider.CacheBookmarkCategoriesDataProvider cacheDataProvider = + new BookmarkCategoriesDataProvider.CacheBookmarkCategoriesDataProvider(getApplicationContext(), coreDataProvider); + BookmarkCategoriesCache.from(getApplicationContext()).updateItems(coreDataProvider.getCategories()); + BookmarkManager.INSTANCE.setDataProvider(cacheDataProvider); } @Override diff --git a/android/src/com/mapswithme/maps/MwmApplication.java b/android/src/com/mapswithme/maps/MwmApplication.java index a40ba7e8d4..7042456209 100644 --- a/android/src/com/mapswithme/maps/MwmApplication.java +++ b/android/src/com/mapswithme/maps/MwmApplication.java @@ -15,6 +15,7 @@ import com.mapswithme.maps.background.NotificationChannelFactory; import com.mapswithme.maps.background.NotificationChannelProvider; import com.mapswithme.maps.background.Notifier; import com.mapswithme.maps.base.MediaPlayerWrapper; +import com.mapswithme.maps.bookmarks.data.BookmarkCategoriesDataProvider; import com.mapswithme.maps.bookmarks.data.BookmarkManager; import com.mapswithme.maps.downloader.CountryItem; import com.mapswithme.maps.downloader.MapManager; @@ -88,6 +89,10 @@ public class MwmApplication extends Application implements AppBackgroundTracker. private GeofenceRegistry mGeofenceRegistry; private boolean mFirstLaunch; + @SuppressWarnings("NullableProblems") + @NonNull + private BookmarkCategoriesCache mBookmarkCategoriesCache; + @NonNull public SubwayManager getSubwayManager() { @@ -185,6 +190,7 @@ public class MwmApplication extends Application implements AppBackgroundTracker. mPurchaseOperationObservable = new PurchaseOperationObservable(); mPlayer = new MediaPlayerWrapper(this); mGeofenceRegistry = new GeofenceRegistryImpl(this); + mBookmarkCategoriesCache = new BookmarkCategoriesCache(); } private void initNotificationChannels() @@ -405,6 +411,12 @@ public class MwmApplication extends Application implements AppBackgroundTracker. nativeOnTransit(foreground); } + @NonNull + public BookmarkCategoriesCache getBookmarkCategoriesCache() + { + return mBookmarkCategoriesCache; + } + private static class VisibleAppLaunchListener implements AppBackgroundTracker.OnVisibleAppLaunchListener { @Override diff --git a/android/src/com/mapswithme/maps/bookmarks/BaseBookmarkCategoriesFragment.java b/android/src/com/mapswithme/maps/bookmarks/BaseBookmarkCategoriesFragment.java index c383e442f5..ef6d5912d4 100644 --- a/android/src/com/mapswithme/maps/bookmarks/BaseBookmarkCategoriesFragment.java +++ b/android/src/com/mapswithme/maps/bookmarks/BaseBookmarkCategoriesFragment.java @@ -14,12 +14,16 @@ import android.view.MenuItem; import android.view.View; import com.cocosw.bottomsheet.BottomSheet; +import com.mapswithme.maps.BookmarkCategoriesCache; import com.mapswithme.maps.R; import com.mapswithme.maps.adapter.OnItemClickListener; import com.mapswithme.maps.base.BaseMwmRecyclerFragment; +import com.mapswithme.maps.base.Detachable; +import com.mapswithme.maps.bookmarks.data.AbstractCategoriesSnapshot; import com.mapswithme.maps.bookmarks.data.BookmarkCategory; import com.mapswithme.maps.bookmarks.data.BookmarkManager; import com.mapswithme.maps.bookmarks.data.BookmarkSharingResult; +import com.mapswithme.maps.bookmarks.data.FilterStrategy; import com.mapswithme.maps.dialog.EditTextDialogFragment; import com.mapswithme.maps.ugc.routes.UgcRouteEditSettingsActivity; import com.mapswithme.maps.ugc.routes.UgcRouteSharingOptionsActivity; @@ -31,6 +35,8 @@ import com.mapswithme.util.sharing.SharingHelper; import com.mapswithme.util.statistics.Analytics; import com.mapswithme.util.statistics.Statistics; +import java.util.List; + public abstract class BaseBookmarkCategoriesFragment extends BaseMwmRecyclerFragment implements EditTextDialogFragment.EditTextDialogInterface, MenuItem.OnMenuItemClickListener, @@ -59,6 +65,10 @@ public abstract class BaseBookmarkCategoriesFragment extends BaseMwmRecyclerFrag @NonNull private BookmarkManager.BookmarksCatalogListener mCatalogListener; + @SuppressWarnings("NullableProblems") + @NonNull + private CategoriesAdapterObserver mCategoriesAdapterObserver; + @Override @LayoutRes protected int getLayoutRes() @@ -70,7 +80,10 @@ public abstract class BaseBookmarkCategoriesFragment extends BaseMwmRecyclerFrag @Override protected BookmarkCategoriesAdapter createAdapter() { - return new BookmarkCategoriesAdapter(getActivity()); + FilterStrategy strategy = getType().getFilterStrategy(); + List items = BookmarkManager.INSTANCE.getCategoriesSnapshot(strategy) + .getItems(); + return new BookmarkCategoriesAdapter(requireContext(), getType(), items); } @CallSuper @@ -92,6 +105,8 @@ public abstract class BaseBookmarkCategoriesFragment extends BaseMwmRecyclerFrag .createVerticalDefaultDecorator(getContext()); rw.addItemDecoration(decor); mCatalogListener = new CatalogListenerDecorator(createCatalogListener(), this); + mCategoriesAdapterObserver = new CategoriesAdapterObserver(); + BookmarkCategoriesCache.from(requireContext()).registerObserver(mCategoriesAdapterObserver); } protected void onPrepareControllers(@NonNull View view) @@ -148,6 +163,13 @@ public abstract class BaseBookmarkCategoriesFragment extends BaseMwmRecyclerFrag super.onPause(); } + @Override + public void onDestroyView() + { + super.onDestroyView(); + BookmarkCategoriesCache.from(requireContext()).unregisterObserver(mCategoriesAdapterObserver); + } + @Override public boolean onMenuItemClick(MenuItem item) { @@ -161,7 +183,6 @@ public abstract class BaseBookmarkCategoriesFragment extends BaseMwmRecyclerFrag return true; } - protected final void showBottomMenu(@NonNull BookmarkCategory item) { mSelectedCategory = item; @@ -267,6 +288,9 @@ public abstract class BaseBookmarkCategoriesFragment extends BaseMwmRecyclerFrag return new CategoryValidator(); } + @NonNull + protected abstract BookmarkCategory.Type getType(); + @Override public void onItemClick(@NonNull View v, @NonNull BookmarkCategory category) { @@ -278,7 +302,7 @@ public abstract class BaseBookmarkCategoriesFragment extends BaseMwmRecyclerFrag private Intent makeBookmarksListIntent(@NonNull BookmarkCategory category) { return new Intent(getActivity(), BookmarkListActivity.class) - .putExtra(BookmarksListFragment.EXTRA_CATEGORY, category); + .putExtra(BookmarksListFragment.EXTRA_CATEGORY, category); } protected void onShareActionSelected(@NonNull BookmarkCategory category) @@ -376,9 +400,12 @@ public abstract class BaseBookmarkCategoriesFragment extends BaseMwmRecyclerFrag { SET_SHARE(R.id.share, shareAction(), new Analytics(Statistics.ParamValue.SEND_AS_FILE)), SET_EDIT(R.id.edit, editAction(), new Analytics(Statistics.ParamValue.EDIT)), - SHOW_ON_MAP(R.id.show_on_map, showAction(), new Analytics(Statistics.ParamValue.MAKE_INVISIBLE_ON_MAP)), - SHARING_OPTIONS(R.id.sharing_options, showSharingOptions(), new Analytics(Statistics.ParamValue.SHARING_OPTIONS)), - LIST_SETTINGS(R.id.settings, showListSettings(), new Analytics(Statistics.ParamValue.LIST_SETTINGS)), + SHOW_ON_MAP(R.id.show_on_map, showAction(), + new Analytics(Statistics.ParamValue.MAKE_INVISIBLE_ON_MAP)), + SHARING_OPTIONS(R.id.sharing_options, showSharingOptions(), + new Analytics(Statistics.ParamValue.SHARING_OPTIONS)), + LIST_SETTINGS(R.id.settings, showListSettings(), + new Analytics(Statistics.ParamValue.LIST_SETTINGS)), DELETE_LIST(R.id.delete, deleteAction(), new Analytics(Statistics.ParamValue.DELETE_GROUP)); @NonNull @@ -527,4 +554,35 @@ public abstract class BaseBookmarkCategoriesFragment extends BaseMwmRecyclerFrag } } } + + private static class CategoriesAdapterObserver extends RecyclerView.AdapterDataObserver implements Detachable + { + @Nullable + private BaseBookmarkCategoriesFragment mFragment; + + @Override + public void attach(@NonNull BaseBookmarkCategoriesFragment object) + { + mFragment = object; + } + + @Override + public void detach() + { + mFragment = null; + } + + @Override + public void onChanged() + { + super.onChanged(); + if (mFragment == null) + return; + + FilterStrategy strategy = mFragment.getType().getFilterStrategy(); + AbstractCategoriesSnapshot.Default snapshot = + BookmarkManager.INSTANCE.getCategoriesSnapshot(strategy); + mFragment.getAdapter().setItems(snapshot.getItems()); + } + } } diff --git a/android/src/com/mapswithme/maps/bookmarks/BaseBookmarkCategoryAdapter.java b/android/src/com/mapswithme/maps/bookmarks/BaseBookmarkCategoryAdapter.java index d53714b3dd..51285f0c56 100644 --- a/android/src/com/mapswithme/maps/bookmarks/BaseBookmarkCategoryAdapter.java +++ b/android/src/com/mapswithme/maps/bookmarks/BaseBookmarkCategoryAdapter.java @@ -14,10 +14,18 @@ public abstract class BaseBookmarkCategoryAdapter mItems; - BaseBookmarkCategoryAdapter(@NonNull Context context) + BaseBookmarkCategoryAdapter(@NonNull Context context, @NonNull List items) { mContext = context; + mItems = items; + } + + public void setItems(@NonNull List items) + { + mItems = items; } @NonNull @@ -29,7 +37,7 @@ public abstract class BaseBookmarkCategoryAdapter getBookmarkCategories() { - return BookmarkManager.INSTANCE.getOwnedCategoriesSnapshot().getItems(); + return mItems; } @Override diff --git a/android/src/com/mapswithme/maps/bookmarks/BookmarkCategoriesAdapter.java b/android/src/com/mapswithme/maps/bookmarks/BookmarkCategoriesAdapter.java index 9bdd6d3022..c942b664bd 100644 --- a/android/src/com/mapswithme/maps/bookmarks/BookmarkCategoriesAdapter.java +++ b/android/src/com/mapswithme/maps/bookmarks/BookmarkCategoriesAdapter.java @@ -14,6 +14,8 @@ import com.mapswithme.maps.bookmarks.data.BookmarkCategory; import com.mapswithme.maps.bookmarks.data.BookmarkManager; import com.mapswithme.util.UiUtils; +import java.util.List; + import static com.mapswithme.maps.bookmarks.Holders.CategoryViewHolder; import static com.mapswithme.maps.bookmarks.Holders.HeaderViewHolder; import static com.mapswithme.util.UiUtils.PHRASE_SEPARATOR; @@ -37,18 +39,14 @@ public class BookmarkCategoriesAdapter extends BaseBookmarkCategoryAdapter categories) { - super(context.getApplicationContext()); + super(context.getApplicationContext(), categories); mType = type; mResProvider = type.getFactory().getResProvider(); } - BookmarkCategoriesAdapter(@NonNull Context context) - { - this(context, BookmarkCategory.Type.PRIVATE); - } - public void setOnClickListener(@Nullable OnItemClickListener listener) { mClickListener = listener; diff --git a/android/src/com/mapswithme/maps/bookmarks/BookmarkCategoriesFragment.java b/android/src/com/mapswithme/maps/bookmarks/BookmarkCategoriesFragment.java index afc0f4f18e..d27280696b 100644 --- a/android/src/com/mapswithme/maps/bookmarks/BookmarkCategoriesFragment.java +++ b/android/src/com/mapswithme/maps/bookmarks/BookmarkCategoriesFragment.java @@ -10,6 +10,7 @@ import com.cocosw.bottomsheet.BottomSheet; import com.mapswithme.maps.R; import com.mapswithme.maps.auth.Authorizer; import com.mapswithme.maps.auth.TargetFragmentCallback; +import com.mapswithme.maps.bookmarks.data.BookmarkCategory; import com.mapswithme.maps.bookmarks.data.BookmarkManager; import com.mapswithme.maps.widget.BookmarkBackupView; import com.mapswithme.util.UiUtils; @@ -78,6 +79,13 @@ public class BookmarkCategoriesFragment extends BaseBookmarkCategoriesFragment getSelectedCategory().isSharingOptionsAllowed()); } + @NonNull + @Override + protected BookmarkCategory.Type getType() + { + return BookmarkCategory.Type.PRIVATE; + } + @Override public void onAuthCompleted() { diff --git a/android/src/com/mapswithme/maps/bookmarks/CachedBookmarkCategoriesFragment.java b/android/src/com/mapswithme/maps/bookmarks/CachedBookmarkCategoriesFragment.java index 34a799fb03..dbb99ec1e2 100644 --- a/android/src/com/mapswithme/maps/bookmarks/CachedBookmarkCategoriesFragment.java +++ b/android/src/com/mapswithme/maps/bookmarks/CachedBookmarkCategoriesFragment.java @@ -19,7 +19,6 @@ import com.mapswithme.maps.bookmarks.data.CatalogTagsGroup; import com.mapswithme.util.SharedPropertiesUtils; import com.mapswithme.util.UTM; import com.mapswithme.util.UiUtils; -import com.mapswithme.util.sharing.TargetUtils; import com.mapswithme.util.statistics.Statistics; import java.util.List; @@ -77,9 +76,9 @@ public class CachedBookmarkCategoriesFragment extends BaseBookmarkCategoriesFrag @NonNull @Override - protected CatalogBookmarkCategoriesAdapter createAdapter() + protected BookmarkCategory.Type getType() { - return new CatalogBookmarkCategoriesAdapter(requireContext()); + return BookmarkCategory.Type.DOWNLOADED; } @Override diff --git a/android/src/com/mapswithme/maps/bookmarks/CatalogBookmarkCategoriesAdapter.java b/android/src/com/mapswithme/maps/bookmarks/CatalogBookmarkCategoriesAdapter.java deleted file mode 100644 index d3debac9b2..0000000000 --- a/android/src/com/mapswithme/maps/bookmarks/CatalogBookmarkCategoriesAdapter.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.mapswithme.maps.bookmarks; - -import android.content.Context; -import android.support.annotation.NonNull; - -import com.mapswithme.maps.bookmarks.data.BookmarkCategory; -import com.mapswithme.maps.bookmarks.data.BookmarkManager; - -import java.util.List; - -public class CatalogBookmarkCategoriesAdapter extends BookmarkCategoriesAdapter -{ - CatalogBookmarkCategoriesAdapter(@NonNull Context context) - { - super(context, BookmarkCategory.Type.DOWNLOADED); - } - - @Override - @NonNull - public List getBookmarkCategories() - { - return BookmarkManager.INSTANCE.getDownloadedCategoriesSnapshot().getItems(); - } -} diff --git a/android/src/com/mapswithme/maps/bookmarks/ChooseBookmarkCategoryAdapter.java b/android/src/com/mapswithme/maps/bookmarks/ChooseBookmarkCategoryAdapter.java index 84e400147a..26631084eb 100644 --- a/android/src/com/mapswithme/maps/bookmarks/ChooseBookmarkCategoryAdapter.java +++ b/android/src/com/mapswithme/maps/bookmarks/ChooseBookmarkCategoryAdapter.java @@ -1,6 +1,7 @@ package com.mapswithme.maps.bookmarks; import android.content.Context; +import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; @@ -11,6 +12,8 @@ import android.widget.TextView; import com.mapswithme.maps.R; import com.mapswithme.maps.bookmarks.data.BookmarkCategory; +import java.util.List; + public class ChooseBookmarkCategoryAdapter extends BaseBookmarkCategoryAdapter { public static final int VIEW_TYPE_CATEGORY = 0; @@ -27,9 +30,10 @@ public class ChooseBookmarkCategoryAdapter extends BaseBookmarkCategoryAdapter categories) { - super(context); + super(context, categories); mCheckedPosition = pos; } diff --git a/android/src/com/mapswithme/maps/bookmarks/ChooseBookmarkCategoryFragment.java b/android/src/com/mapswithme/maps/bookmarks/ChooseBookmarkCategoryFragment.java index 5504befd60..72338daa8d 100644 --- a/android/src/com/mapswithme/maps/bookmarks/ChooseBookmarkCategoryFragment.java +++ b/android/src/com/mapswithme/maps/bookmarks/ChooseBookmarkCategoryFragment.java @@ -58,7 +58,8 @@ public class ChooseBookmarkCategoryFragment extends BaseMwmDialogFragment final Bundle args = getArguments(); final int catPosition = args.getInt(CATEGORY_POSITION, 0); - mAdapter = new ChooseBookmarkCategoryAdapter(getActivity(), catPosition); + List items = BookmarkManager.INSTANCE.getOwnedCategoriesSnapshot().getItems(); + mAdapter = new ChooseBookmarkCategoryAdapter(getActivity(), catPosition, items); mAdapter.setListener(this); mRecycler.setAdapter(mAdapter); } diff --git a/android/src/com/mapswithme/maps/bookmarks/data/AbstractCategoriesSnapshot.java b/android/src/com/mapswithme/maps/bookmarks/data/AbstractCategoriesSnapshot.java index 4707b4d8e4..e89b845581 100644 --- a/android/src/com/mapswithme/maps/bookmarks/data/AbstractCategoriesSnapshot.java +++ b/android/src/com/mapswithme/maps/bookmarks/data/AbstractCategoriesSnapshot.java @@ -11,9 +11,9 @@ public abstract class AbstractCategoriesSnapshot @NonNull private final List mSnapshot; - AbstractCategoriesSnapshot(@NonNull BookmarkCategory[] items) + AbstractCategoriesSnapshot(@NonNull List items) { - mSnapshot = Collections.unmodifiableList(Arrays.asList(items)); + mSnapshot = Collections.unmodifiableList(items); } @NonNull @@ -27,7 +27,7 @@ public abstract class AbstractCategoriesSnapshot @NonNull private final FilterStrategy mStrategy; - protected Default(@NonNull BookmarkCategory[] items, @NonNull FilterStrategy strategy) + protected Default(@NonNull List items, @NonNull FilterStrategy strategy) { super(items); mStrategy = strategy; diff --git a/android/src/com/mapswithme/maps/bookmarks/data/Bookmark.java b/android/src/com/mapswithme/maps/bookmarks/data/Bookmark.java index 7f3ebd2ac3..1e767e6a76 100644 --- a/android/src/com/mapswithme/maps/bookmarks/data/Bookmark.java +++ b/android/src/com/mapswithme/maps/bookmarks/data/Bookmark.java @@ -116,7 +116,7 @@ public class Bookmark extends MapObject public String getCategoryName() { - return BookmarkManager.INSTANCE.getCategoryName(mCategoryId); + return BookmarkManager.INSTANCE.getCategoryById(mCategoryId).getName(); } public void setCategoryId(@IntRange(from = 0) long catId) diff --git a/android/src/com/mapswithme/maps/bookmarks/data/BookmarkCategoriesDataProvider.java b/android/src/com/mapswithme/maps/bookmarks/data/BookmarkCategoriesDataProvider.java new file mode 100644 index 0000000000..a796ef554b --- /dev/null +++ b/android/src/com/mapswithme/maps/bookmarks/data/BookmarkCategoriesDataProvider.java @@ -0,0 +1,100 @@ +package com.mapswithme.maps.bookmarks.data; + +import android.content.Context; +import android.support.annotation.NonNull; + +import com.mapswithme.maps.BookmarkCategoriesCache; + +import java.util.Arrays; +import java.util.List; + +public interface BookmarkCategoriesDataProvider +{ + @NonNull + List getCategories(); + + @NonNull + BookmarkCategory getCategoryById(long categoryId); + + public abstract class AbstractDataProvider implements BookmarkCategoriesDataProvider + { + + @NonNull + public AbstractCategoriesSnapshot.Default getAllCategoriesSnapshot() + { + List items = getCategories(); + return new AbstractCategoriesSnapshot.Default(items, new FilterStrategy.All()); + } + } + + public static class CoreBookmarkCategoriesDataProvider extends AbstractDataProvider + { + + @NonNull + @Override + public List getCategories() + { + BookmarkCategory[] categories = BookmarkManager.INSTANCE.nativeGetBookmarkCategories(); + return Arrays.asList(categories); + } + + @NonNull + @Override + public BookmarkCategory getCategoryById(long categoryId) + { + BookmarkCategory[] categories = + BookmarkManager.INSTANCE.nativeGetBookmarkCategories(); + + for (BookmarkCategory each : categories) + { + if (each.getId() == categoryId) + return each; + + } + throw new IllegalArgumentException("There is no category for id : " + categoryId); + } + } + + public static class CacheBookmarkCategoriesDataProvider extends AbstractDataProvider + { + @NonNull + private final Context mContext; + @NonNull + private final BookmarkCategoriesDataProvider mCoreDataProvider; + + public CacheBookmarkCategoriesDataProvider(@NonNull Context context, + @NonNull BookmarkCategoriesDataProvider dataProvider) + { + mContext = context; + mCoreDataProvider = dataProvider; + } + + @NonNull + @Override + public List getCategories() + { + return BookmarkCategoriesCache.from(mContext).getItems(); + } + + private void updateCache() + { + BookmarkCategoriesCache cache = BookmarkCategoriesCache.from(mContext); + cache.updateItems(mCoreDataProvider.getCategories()); + } + + @NonNull + public BookmarkCategory getCategoryById(long catId) + { + List items = getAllCategoriesSnapshot().getItems(); + for (BookmarkCategory each : items) + { + if (catId == each.getId()) + return each; + } + throw new IllegalArgumentException(new StringBuilder().append("Category with id = ") + .append(catId) + .append(" missed") + .toString()); + } + } +} diff --git a/android/src/com/mapswithme/maps/bookmarks/data/BookmarkCategory.java b/android/src/com/mapswithme/maps/bookmarks/data/BookmarkCategory.java index 9033680c5c..17e721abb3 100644 --- a/android/src/com/mapswithme/maps/bookmarks/data/BookmarkCategory.java +++ b/android/src/com/mapswithme/maps/bookmarks/data/BookmarkCategory.java @@ -329,9 +329,9 @@ public class BookmarkCategory implements Parcelable DOWNLOADED(BookmarksPageFactory.DOWNLOADED, FilterStrategy.PredicativeStrategy.makeDownloadedInstance()); @NonNull - private BookmarksPageFactory mFactory; + private final BookmarksPageFactory mFactory; @NonNull - private FilterStrategy mFilterStrategy; + private final FilterStrategy mFilterStrategy; Type(@NonNull BookmarksPageFactory pageFactory, @NonNull FilterStrategy filterStrategy) { diff --git a/android/src/com/mapswithme/maps/bookmarks/data/BookmarkManager.java b/android/src/com/mapswithme/maps/bookmarks/data/BookmarkManager.java index 6698597329..18f056c90b 100644 --- a/android/src/com/mapswithme/maps/bookmarks/data/BookmarkManager.java +++ b/android/src/com/mapswithme/maps/bookmarks/data/BookmarkManager.java @@ -7,7 +7,6 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import com.mapswithme.maps.PrivateVariables; -import com.mapswithme.maps.R; import com.mapswithme.maps.metrics.UserActionsLogger; import com.mapswithme.util.UTM; import com.mapswithme.util.statistics.Statistics; @@ -62,6 +61,9 @@ public enum BookmarkManager public static final List ICONS = new ArrayList<>(); + @NonNull + private BookmarkCategoriesDataProvider mDataProvider = new BookmarkCategoriesDataProvider.CoreBookmarkCategoriesDataProvider(); + @NonNull private final List mListeners = new ArrayList<>(); @@ -104,6 +106,11 @@ public enum BookmarkManager setVisibility(catId, !isVisible); } + public void setDataProvider(@NonNull BookmarkCategoriesDataProvider dataProvider) + { + mDataProvider = dataProvider; + } + public Bookmark addNewBookmark(double lat, double lon) { final Bookmark bookmark = nativeAddBookmarkToLastEditedCategory(lat, lon); @@ -336,7 +343,6 @@ public enum BookmarkManager public void onTagsReceived(boolean successful, @NonNull CatalogTagsGroup[] tagsGroups, int maxTagsCount) { - //TODO(@yoksnod): Implement maxTagsCount usage. List unmodifiableData = Collections.unmodifiableList(Arrays.asList(tagsGroups)); for (BookmarksCatalogListener listener : mCatalogListeners) { @@ -405,12 +411,6 @@ public enum BookmarkManager nativeSetVisibility(catId, visible); } - @NonNull - public String getCategoryName(long catId) - { - return nativeGetCategoryName(catId); - } - public void setCategoryName(long catId, @NonNull String name) { nativeSetCategoryName(catId, name); @@ -450,10 +450,10 @@ public enum BookmarkManager nativeUploadToCatalog(rules.ordinal(), category.getId()); } - /** +/* *//** * @return total count - tracks + bookmarks * @param category - */ + *//* @Deprecated public int getCategorySize(@NonNull BookmarkCategory category) { @@ -475,23 +475,15 @@ public enum BookmarkManager .append(catId) .append(" missed") .toString()); - } + }*/ +/* @Deprecated public long getCategoryIdByPosition(int position) { return nativeGetCategoryIdByPosition(position); } - - public int getBookmarksCount(long catId) - { - return nativeGetBookmarksCount(catId); - } - - public int getTracksCount(long catId) - { - return nativeGetTracksCount(catId); - } +*/ @NonNull public Bookmark updateBookmarkPlacePage(long bmkId) @@ -568,28 +560,35 @@ public enum BookmarkManager @NonNull public AbstractCategoriesSnapshot.Default getDownloadedCategoriesSnapshot() { - BookmarkCategory[] items = nativeGetBookmarkCategories(); + List items = mDataProvider.getCategories(); return new AbstractCategoriesSnapshot.Default(items, new FilterStrategy.Downloaded()); } @NonNull public AbstractCategoriesSnapshot.Default getOwnedCategoriesSnapshot() { - BookmarkCategory[] items = nativeGetBookmarkCategories(); + List items = mDataProvider.getCategories(); return new AbstractCategoriesSnapshot.Default(items, new FilterStrategy.Private()); } @NonNull public AbstractCategoriesSnapshot.Default getAllCategoriesSnapshot() { - BookmarkCategory[] items = nativeGetBookmarkCategories(); + List items = mDataProvider.getCategories(); return new AbstractCategoriesSnapshot.Default(items, new FilterStrategy.All()); } @NonNull public AbstractCategoriesSnapshot.Default getCategoriesSnapshot(FilterStrategy strategy) { - return new AbstractCategoriesSnapshot.Default(nativeGetBookmarkCategories(), strategy); + List items = mDataProvider.getCategories(); + return new AbstractCategoriesSnapshot.Default(items, strategy); + } + + @NonNull + public BookmarkCategory getCategoryById(long categoryId) + { + return mDataProvider.getCategoryById(categoryId); } public boolean isUsedCategoryName(@NonNull String name) @@ -607,16 +606,6 @@ public enum BookmarkManager public void prepareForSearch(long catId) { nativePrepareForSearch(catId); } - public boolean areAllCatalogCategoriesVisible() - { - return areAllCategoriesVisible(BookmarkCategory.Type.DOWNLOADED); - } - - public boolean areAllOwnedCategoriesVisible() - { - return areAllCategoriesVisible(BookmarkCategory.Type.PRIVATE); - } - public boolean areAllCategoriesVisible(BookmarkCategory.Type type) { return nativeAreAllCategoriesVisible(type.ordinal()); @@ -936,7 +925,7 @@ public enum BookmarkManager private native int nativeGetTracksCount(long catId); - private native BookmarkCategory[] nativeGetBookmarkCategories(); + public native BookmarkCategory[] nativeGetBookmarkCategories(); @NonNull private native Bookmark nativeUpdateBookmarkPlacePage(long bmkId); diff --git a/android/src/com/mapswithme/util/sharing/SharingHelper.java b/android/src/com/mapswithme/util/sharing/SharingHelper.java index 1c13161437..91e929a815 100644 --- a/android/src/com/mapswithme/util/sharing/SharingHelper.java +++ b/android/src/com/mapswithme/util/sharing/SharingHelper.java @@ -225,7 +225,7 @@ public enum SharingHelper case BookmarkSharingResult.FILE_ERROR: DialogUtils.showAlertDialog(context, R.string.dialog_routing_system_error, R.string.bookmarks_error_message_share_general); - String catName = BookmarkManager.INSTANCE.getCategoryName(result.getCategoryId()); + String catName = BookmarkManager.INSTANCE.getCategoryById(result.getCategoryId()).getName(); LOGGER.e(TAG, "Failed to share bookmark category '" + catName + "', error code: " + result.getCode()); break;