From 9032e18ff97e65fdaa496da241180ecbe30a8958 Mon Sep 17 00:00:00 2001 From: Dmitry Donskoy Date: Thu, 3 Oct 2019 12:25:46 +0300 Subject: [PATCH] [android] Created split categories data provider, opt cache, changed providers initialisation --- .../maps/BookmarkCategoriesCache.java | 42 ------- .../src/com/mapswithme/maps/MwmActivity.java | 8 +- .../com/mapswithme/maps/MwmApplication.java | 12 -- .../BaseBookmarkCategoriesFragment.java | 5 +- .../bookmarks/data/AbstractDataProvider.java | 23 ++++ .../data/BookmarkCategoriesDataProvider.java | 86 ------------- .../maps/bookmarks/data/BookmarkManager.java | 113 +++++++++++------- .../CacheBookmarkCategoriesDataProvider.java | 15 +++ .../CoreBookmarkCategoriesDataProvider.java | 17 +++ 9 files changed, 128 insertions(+), 193 deletions(-) delete mode 100644 android/src/com/mapswithme/maps/BookmarkCategoriesCache.java create mode 100644 android/src/com/mapswithme/maps/bookmarks/data/AbstractDataProvider.java create mode 100644 android/src/com/mapswithme/maps/bookmarks/data/CacheBookmarkCategoriesDataProvider.java create mode 100644 android/src/com/mapswithme/maps/bookmarks/data/CoreBookmarkCategoriesDataProvider.java diff --git a/android/src/com/mapswithme/maps/BookmarkCategoriesCache.java b/android/src/com/mapswithme/maps/BookmarkCategoriesCache.java deleted file mode 100644 index 2ad7d98265..0000000000 --- a/android/src/com/mapswithme/maps/BookmarkCategoriesCache.java +++ /dev/null @@ -1,42 +0,0 @@ -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 ecd98fdf1e..9e380d8249 100644 --- a/android/src/com/mapswithme/maps/MwmActivity.java +++ b/android/src/com/mapswithme/maps/MwmActivity.java @@ -40,7 +40,6 @@ 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; @@ -2388,12 +2387,7 @@ public class MwmActivity extends BaseMwmFragmentActivity @Override public void onBookmarksLoadingFinished() { - BookmarkCategoriesDataProvider.CoreBookmarkCategoriesDataProvider coreDataProvider = - new BookmarkCategoriesDataProvider.CoreBookmarkCategoriesDataProvider(); - BookmarkCategoriesDataProvider.CacheBookmarkCategoriesDataProvider cacheDataProvider = - new BookmarkCategoriesDataProvider.CacheBookmarkCategoriesDataProvider(getApplicationContext(), coreDataProvider); - BookmarkCategoriesCache.from(getApplicationContext()).updateItems(coreDataProvider.getCategories()); - BookmarkManager.INSTANCE.setDataProvider(cacheDataProvider); + BookmarkManager.INSTANCE.setDataProvider(); } @Override diff --git a/android/src/com/mapswithme/maps/MwmApplication.java b/android/src/com/mapswithme/maps/MwmApplication.java index 7042456209..a40ba7e8d4 100644 --- a/android/src/com/mapswithme/maps/MwmApplication.java +++ b/android/src/com/mapswithme/maps/MwmApplication.java @@ -15,7 +15,6 @@ 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; @@ -89,10 +88,6 @@ public class MwmApplication extends Application implements AppBackgroundTracker. private GeofenceRegistry mGeofenceRegistry; private boolean mFirstLaunch; - @SuppressWarnings("NullableProblems") - @NonNull - private BookmarkCategoriesCache mBookmarkCategoriesCache; - @NonNull public SubwayManager getSubwayManager() { @@ -190,7 +185,6 @@ public class MwmApplication extends Application implements AppBackgroundTracker. mPurchaseOperationObservable = new PurchaseOperationObservable(); mPlayer = new MediaPlayerWrapper(this); mGeofenceRegistry = new GeofenceRegistryImpl(this); - mBookmarkCategoriesCache = new BookmarkCategoriesCache(); } private void initNotificationChannels() @@ -411,12 +405,6 @@ 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 ef6d5912d4..7b43ad64a9 100644 --- a/android/src/com/mapswithme/maps/bookmarks/BaseBookmarkCategoriesFragment.java +++ b/android/src/com/mapswithme/maps/bookmarks/BaseBookmarkCategoriesFragment.java @@ -14,7 +14,6 @@ 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; @@ -106,7 +105,7 @@ public abstract class BaseBookmarkCategoriesFragment extends BaseMwmRecyclerFrag rw.addItemDecoration(decor); mCatalogListener = new CatalogListenerDecorator(createCatalogListener(), this); mCategoriesAdapterObserver = new CategoriesAdapterObserver(); - BookmarkCategoriesCache.from(requireContext()).registerObserver(mCategoriesAdapterObserver); + BookmarkManager.INSTANCE.registerObserver(mCategoriesAdapterObserver); } protected void onPrepareControllers(@NonNull View view) @@ -167,7 +166,7 @@ public abstract class BaseBookmarkCategoriesFragment extends BaseMwmRecyclerFrag public void onDestroyView() { super.onDestroyView(); - BookmarkCategoriesCache.from(requireContext()).unregisterObserver(mCategoriesAdapterObserver); + BookmarkManager.INSTANCE.unregisterObserver(mCategoriesAdapterObserver); } @Override diff --git a/android/src/com/mapswithme/maps/bookmarks/data/AbstractDataProvider.java b/android/src/com/mapswithme/maps/bookmarks/data/AbstractDataProvider.java new file mode 100644 index 0000000000..b4b9414129 --- /dev/null +++ b/android/src/com/mapswithme/maps/bookmarks/data/AbstractDataProvider.java @@ -0,0 +1,23 @@ +package com.mapswithme.maps.bookmarks.data; + +import android.support.annotation.NonNull; + +import java.util.List; + +abstract class AbstractDataProvider implements BookmarkCategoriesDataProvider +{ + @NonNull + @Override + public BookmarkCategory getCategoryById(long categoryId) + { + List categories = getCategories(); + + for (BookmarkCategory each : categories) + { + if (each.getId() == categoryId) + return each; + + } + throw new IllegalArgumentException("There is no category for id : " + categoryId); + } +} diff --git a/android/src/com/mapswithme/maps/bookmarks/data/BookmarkCategoriesDataProvider.java b/android/src/com/mapswithme/maps/bookmarks/data/BookmarkCategoriesDataProvider.java index a796ef554b..a622a66951 100644 --- a/android/src/com/mapswithme/maps/bookmarks/data/BookmarkCategoriesDataProvider.java +++ b/android/src/com/mapswithme/maps/bookmarks/data/BookmarkCategoriesDataProvider.java @@ -1,11 +1,7 @@ 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 @@ -15,86 +11,4 @@ public interface BookmarkCategoriesDataProvider @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/BookmarkManager.java b/android/src/com/mapswithme/maps/bookmarks/data/BookmarkManager.java index 18f056c90b..67a2f8639d 100644 --- a/android/src/com/mapswithme/maps/bookmarks/data/BookmarkManager.java +++ b/android/src/com/mapswithme/maps/bookmarks/data/BookmarkManager.java @@ -1,5 +1,6 @@ package com.mapswithme.maps.bookmarks.data; +import android.database.Observable; import android.support.annotation.IntDef; import android.support.annotation.IntRange; import android.support.annotation.MainThread; @@ -62,7 +63,14 @@ public enum BookmarkManager public static final List ICONS = new ArrayList<>(); @NonNull - private BookmarkCategoriesDataProvider mDataProvider = new BookmarkCategoriesDataProvider.CoreBookmarkCategoriesDataProvider(); + private final CoreBookmarkCategoriesDataProvider mCoreBookmarkCategoriesDataProvider = new CoreBookmarkCategoriesDataProvider(); + + @NonNull + private BookmarkCategoriesDataProvider mCurrentDataProvider = mCoreBookmarkCategoriesDataProvider; + + @NonNull + private final BookmarkCategoriesCache mBookmarkCategoriesCache = new BookmarkManager.BookmarkCategoriesCache(); + @NonNull private final List mListeners = new ArrayList<>(); @@ -106,9 +114,13 @@ public enum BookmarkManager setVisibility(catId, !isVisible); } - public void setDataProvider(@NonNull BookmarkCategoriesDataProvider dataProvider) + public void setDataProvider() { - mDataProvider = dataProvider; + CacheBookmarkCategoriesDataProvider cacheDataProvider = + new CacheBookmarkCategoriesDataProvider(); + getBookmarkCategoriesCache().updateItems(mCurrentDataProvider.getCategories()); + + mCurrentDataProvider = cacheDataProvider; } public Bookmark addNewBookmark(double lat, double lon) @@ -450,41 +462,6 @@ public enum BookmarkManager nativeUploadToCatalog(rules.ordinal(), category.getId()); } -/* *//** - * @return total count - tracks + bookmarks - * @param category - *//* - @Deprecated - public int getCategorySize(@NonNull BookmarkCategory category) - { - return nativeGetBookmarksCount(category.getId()) + nativeGetTracksCount(category.getId()); - } - - public int getCategoriesCount() { return nativeGetCategoriesCount(); } - - @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()); - }*/ - -/* - @Deprecated - public long getCategoryIdByPosition(int position) - { - return nativeGetCategoryIdByPosition(position); - } -*/ - @NonNull public Bookmark updateBookmarkPlacePage(long bmkId) { @@ -560,35 +537,61 @@ public enum BookmarkManager @NonNull public AbstractCategoriesSnapshot.Default getDownloadedCategoriesSnapshot() { - List items = mDataProvider.getCategories(); + List items = mCurrentDataProvider.getCategories(); return new AbstractCategoriesSnapshot.Default(items, new FilterStrategy.Downloaded()); } @NonNull public AbstractCategoriesSnapshot.Default getOwnedCategoriesSnapshot() { - List items = mDataProvider.getCategories(); + List items = mCurrentDataProvider.getCategories(); return new AbstractCategoriesSnapshot.Default(items, new FilterStrategy.Private()); } @NonNull public AbstractCategoriesSnapshot.Default getAllCategoriesSnapshot() { - List items = mDataProvider.getCategories(); + List items = mCurrentDataProvider.getCategories(); return new AbstractCategoriesSnapshot.Default(items, new FilterStrategy.All()); } @NonNull public AbstractCategoriesSnapshot.Default getCategoriesSnapshot(FilterStrategy strategy) { - List items = mDataProvider.getCategories(); + List items = mCurrentDataProvider.getCategories(); return new AbstractCategoriesSnapshot.Default(items, strategy); } + @NonNull + BookmarkCategoriesCache getBookmarkCategoriesCache() + { + return mBookmarkCategoriesCache; + } + + public void registerBookmarkCategoriesListener() + { + + } + + private void updateCache() + { + getBookmarkCategoriesCache().updateItems(mCoreBookmarkCategoriesDataProvider.getCategories()); + } + + public void registerObserver(@NonNull RecyclerView.AdapterDataObserver observer) + { + getBookmarkCategoriesCache().registerObserver(observer); + } + + public void unregisterObserver(@NonNull RecyclerView.AdapterDataObserver observer) + { + getBookmarkCategoriesCache().unregisterObserver(observer); + } + @NonNull public BookmarkCategory getCategoryById(long categoryId) { - return mDataProvider.getCategoryById(categoryId); + return mCurrentDataProvider.getCategoryById(categoryId); } public boolean isUsedCategoryName(@NonNull String name) @@ -1302,4 +1305,28 @@ public enum BookmarkManager UPLOAD_RESULT_ACCESS_ERROR, UPLOAD_RESULT_INVALID_CALL; } + + static class BookmarkCategoriesCache extends Observable + { + @NonNull + private List mCachedItems = Collections.emptyList(); + + 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(); + } + } + } } diff --git a/android/src/com/mapswithme/maps/bookmarks/data/CacheBookmarkCategoriesDataProvider.java b/android/src/com/mapswithme/maps/bookmarks/data/CacheBookmarkCategoriesDataProvider.java new file mode 100644 index 0000000000..3023231d33 --- /dev/null +++ b/android/src/com/mapswithme/maps/bookmarks/data/CacheBookmarkCategoriesDataProvider.java @@ -0,0 +1,15 @@ +package com.mapswithme.maps.bookmarks.data; + +import android.support.annotation.NonNull; + +import java.util.List; + +class CacheBookmarkCategoriesDataProvider extends AbstractDataProvider +{ + @NonNull + @Override + public List getCategories() + { + return BookmarkManager.INSTANCE.getBookmarkCategoriesCache().getItems(); + } +} diff --git a/android/src/com/mapswithme/maps/bookmarks/data/CoreBookmarkCategoriesDataProvider.java b/android/src/com/mapswithme/maps/bookmarks/data/CoreBookmarkCategoriesDataProvider.java new file mode 100644 index 0000000000..0cde2b86f9 --- /dev/null +++ b/android/src/com/mapswithme/maps/bookmarks/data/CoreBookmarkCategoriesDataProvider.java @@ -0,0 +1,17 @@ +package com.mapswithme.maps.bookmarks.data; + +import android.support.annotation.NonNull; + +import java.util.Arrays; +import java.util.List; + +class CoreBookmarkCategoriesDataProvider extends AbstractDataProvider +{ + @NonNull + @Override + public List getCategories() + { + BookmarkCategory[] categories = BookmarkManager.INSTANCE.nativeGetBookmarkCategories(); + return Arrays.asList(categories); + } +}