[android] Created split categories data provider, opt cache, changed providers initialisation

This commit is contained in:
Dmitry Donskoy 2019-10-03 12:25:46 +03:00 committed by Daria Volvenkova
parent e19d0b89d6
commit 9032e18ff9
9 changed files with 128 additions and 193 deletions

View file

@ -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<RecyclerView.AdapterDataObserver>
{
@NonNull
private List<BookmarkCategory> mCachedItems = new ArrayList<>();
public void updateItems(@NonNull List<BookmarkCategory> cachedItems)
{
mCachedItems = Collections.unmodifiableList(cachedItems);
notifyChanged();
}
@NonNull
public List<BookmarkCategory> 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();
}
}

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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<BookmarkCategory> categories = getCategories();
for (BookmarkCategory each : categories)
{
if (each.getId() == categoryId)
return each;
}
throw new IllegalArgumentException("There is no category for id : " + categoryId);
}
}

View file

@ -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<BookmarkCategory> items = getCategories();
return new AbstractCategoriesSnapshot.Default(items, new FilterStrategy.All());
}
}
public static class CoreBookmarkCategoriesDataProvider extends AbstractDataProvider
{
@NonNull
@Override
public List<BookmarkCategory> 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<BookmarkCategory> 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<BookmarkCategory> 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());
}
}
}

View file

@ -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<Icon> 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<BookmarksLoadingListener> 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<BookmarkCategory> 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<BookmarkCategory> items = mDataProvider.getCategories();
List<BookmarkCategory> items = mCurrentDataProvider.getCategories();
return new AbstractCategoriesSnapshot.Default(items, new FilterStrategy.Downloaded());
}
@NonNull
public AbstractCategoriesSnapshot.Default getOwnedCategoriesSnapshot()
{
List<BookmarkCategory> items = mDataProvider.getCategories();
List<BookmarkCategory> items = mCurrentDataProvider.getCategories();
return new AbstractCategoriesSnapshot.Default(items, new FilterStrategy.Private());
}
@NonNull
public AbstractCategoriesSnapshot.Default getAllCategoriesSnapshot()
{
List<BookmarkCategory> items = mDataProvider.getCategories();
List<BookmarkCategory> items = mCurrentDataProvider.getCategories();
return new AbstractCategoriesSnapshot.Default(items, new FilterStrategy.All());
}
@NonNull
public AbstractCategoriesSnapshot.Default getCategoriesSnapshot(FilterStrategy strategy)
{
List<BookmarkCategory> items = mDataProvider.getCategories();
List<BookmarkCategory> 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<RecyclerView.AdapterDataObserver>
{
@NonNull
private List<BookmarkCategory> mCachedItems = Collections.emptyList();
public void updateItems(@NonNull List<BookmarkCategory> cachedItems)
{
mCachedItems = Collections.unmodifiableList(cachedItems);
notifyChanged();
}
@NonNull
public List<BookmarkCategory> getItems()
{
return mCachedItems;
}
private void notifyChanged() {
for (int i = mObservers.size() - 1; i >= 0; i--) {
mObservers.get(i).onChanged();
}
}
}
}

View file

@ -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<BookmarkCategory> getCategories()
{
return BookmarkManager.INSTANCE.getBookmarkCategoriesCache().getItems();
}
}

View file

@ -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<BookmarkCategory> getCategories()
{
BookmarkCategory[] categories = BookmarkManager.INSTANCE.nativeGetBookmarkCategories();
return Arrays.asList(categories);
}
}