diff --git a/android/src/com/mapswithme/maps/base/BaseMwmRecyclerFragment.java b/android/src/com/mapswithme/maps/base/BaseMwmRecyclerFragment.java index ac6f2b834d..a07c0c15b4 100644 --- a/android/src/com/mapswithme/maps/base/BaseMwmRecyclerFragment.java +++ b/android/src/com/mapswithme/maps/base/BaseMwmRecyclerFragment.java @@ -20,25 +20,33 @@ import com.mapswithme.maps.widget.recycler.ItemDecoratorFactory; import com.mapswithme.util.UiUtils; import com.mapswithme.util.Utils; -public abstract class BaseMwmRecyclerFragment extends Fragment +public abstract class BaseMwmRecyclerFragment extends Fragment { private Toolbar mToolbar; - @Nullable + + @SuppressWarnings("NullableProblems") + @NonNull private RecyclerView mRecycler; + @Nullable private PlaceholderView mPlaceholder; - protected abstract RecyclerView.Adapter createAdapter(); + @SuppressWarnings("NullableProblems") + @NonNull + private T mAdapter; - protected @LayoutRes int getLayoutRes() + protected abstract T createAdapter(); + + @LayoutRes + protected int getLayoutRes() { return R.layout.fragment_recycler; } - @Nullable - protected RecyclerView.Adapter getAdapter() + @NonNull + protected T getAdapter() { - return mRecycler != null ? mRecycler.getAdapter() : null; + return mAdapter; } @Override @@ -60,21 +68,14 @@ public abstract class BaseMwmRecyclerFragment extends Fragment { super.onViewCreated(view, savedInstanceState); - mToolbar = (Toolbar) view.findViewById(R.id.toolbar); + mToolbar = view.findViewById(R.id.toolbar); if (mToolbar != null) { UiUtils.showHomeUpButton(mToolbar); - mToolbar.setNavigationOnClickListener(new View.OnClickListener() - { - @Override - public void onClick(View v) - { - Utils.navigateToParent(getActivity()); - } - }); + mToolbar.setNavigationOnClickListener(v -> Utils.navigateToParent(getActivity())); } - mRecycler = (RecyclerView) view.findViewById(R.id.recycler); + mRecycler = view.findViewById(R.id.recycler); if (mRecycler == null) throw new IllegalStateException("RecyclerView not found in layout"); @@ -84,7 +85,8 @@ public abstract class BaseMwmRecyclerFragment extends Fragment RecyclerView.ItemDecoration decor = ItemDecoratorFactory .createDefaultDecorator(getContext(), LinearLayoutManager.VERTICAL); mRecycler.addItemDecoration(decor); - mRecycler.setAdapter(createAdapter()); + mAdapter = createAdapter(); + mRecycler.setAdapter(mAdapter); mPlaceholder = view.findViewById(R.id.placeholder); setupPlaceholder(mPlaceholder); diff --git a/android/src/com/mapswithme/maps/bookmarks/BaseBookmarkCategoriesFragment.java b/android/src/com/mapswithme/maps/bookmarks/BaseBookmarkCategoriesFragment.java index 0a83c7f48f..45fec75dc9 100644 --- a/android/src/com/mapswithme/maps/bookmarks/BaseBookmarkCategoriesFragment.java +++ b/android/src/com/mapswithme/maps/bookmarks/BaseBookmarkCategoriesFragment.java @@ -24,7 +24,7 @@ import com.mapswithme.util.BottomSheetHelper; import com.mapswithme.util.UiUtils; import com.mapswithme.util.sharing.SharingHelper; -public abstract class BaseBookmarkCategoriesFragment extends BaseMwmRecyclerFragment +public abstract class BaseBookmarkCategoriesFragment extends BaseMwmRecyclerFragment implements EditTextDialogFragment.EditTextDialogInterface, MenuItem.OnMenuItemClickListener, BookmarkManager.BookmarksLoadingListener, @@ -53,19 +53,11 @@ public abstract class BaseBookmarkCategoriesFragment extends BaseMwmRecyclerFrag } @Override - protected RecyclerView.Adapter createAdapter() + protected BookmarkCategoriesAdapter createAdapter() { return new BookmarkCategoriesAdapter(getActivity()); } - @Nullable - @Override - protected BookmarkCategoriesAdapter getAdapter() - { - RecyclerView.Adapter adapter = super.getAdapter(); - return adapter != null ? (BookmarkCategoriesAdapter) adapter : null; - } - @CallSuper @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) @@ -73,12 +65,9 @@ public abstract class BaseBookmarkCategoriesFragment extends BaseMwmRecyclerFrag super.onViewCreated(view, savedInstanceState); onPrepareControllers(view); - if (getAdapter() != null) - { - getAdapter().setOnClickListener(this); - getAdapter().setOnLongClickListener(this); - getAdapter().setCategoryListCallback(this); - } + getAdapter().setOnClickListener(this); + getAdapter().setOnLongClickListener(this); + getAdapter().setCategoryListCallback(this); RecyclerView rw = getRecyclerView(); if (rw == null) return; @@ -130,8 +119,7 @@ public abstract class BaseBookmarkCategoriesFragment extends BaseMwmRecyclerFrag { super.onResume(); updateLoadingPlaceholder(); - if (getAdapter() != null) - getAdapter().notifyDataSetChanged(); + getAdapter().notifyDataSetChanged(); if (!BookmarkManager.INSTANCE.isAsyncBookmarksLoadingInProgress()) mImportKmlTask.run(); } @@ -206,8 +194,7 @@ public abstract class BaseBookmarkCategoriesFragment extends BaseMwmRecyclerFrag public void onBookmarksLoadingFinished() { updateLoadingPlaceholder(); - if (getAdapter() != null) - getAdapter().notifyDataSetChanged(); + getAdapter().notifyDataSetChanged(); mImportKmlTask.run(); } @@ -244,20 +231,14 @@ public abstract class BaseBookmarkCategoriesFragment extends BaseMwmRecyclerFrag @Override public void onFinishKmlImport() { - if (getAdapter() != null) getAdapter().notifyDataSetChanged(); + getAdapter().notifyDataSetChanged(); } @NonNull @Override public EditTextDialogFragment.OnTextSaveListener getSaveTextListener() { - return text -> { - if (mCategoryEditor != null) - mCategoryEditor.commit(text); - - if (getAdapter() != null) - getAdapter().notifyDataSetChanged(); - }; + return this::onSaveText; } @NonNull @@ -297,6 +278,14 @@ public abstract class BaseBookmarkCategoriesFragment extends BaseMwmRecyclerFrag return R.id.set_delete; } + private void onSaveText(String text) + { + if (mCategoryEditor != null) + mCategoryEditor.commit(text); + + getAdapter().notifyDataSetChanged(); + } + interface CategoryEditor { void commit(@NonNull String newName); @@ -388,8 +377,7 @@ public abstract class BaseBookmarkCategoriesFragment extends BaseMwmRecyclerFrag @NonNull BookmarkCategory category) { BookmarkManager.INSTANCE.toggleCategoryVisibility(category.getId()); - if (frag.getAdapter() != null) - frag.getAdapter().notifyDataSetChanged(); + frag.getAdapter().notifyDataSetChanged(); } } @@ -410,8 +398,7 @@ public abstract class BaseBookmarkCategoriesFragment extends BaseMwmRecyclerFrag @NonNull BookmarkCategory category) { BookmarkManager.INSTANCE.deleteCategory(category.getId()); - if (frag.getAdapter() != null) - frag.getAdapter().notifyDataSetChanged(); + frag.getAdapter().notifyDataSetChanged(); } } diff --git a/android/src/com/mapswithme/maps/bookmarks/BookmarksCatalogFragment.java b/android/src/com/mapswithme/maps/bookmarks/BookmarksCatalogFragment.java index 5e4b9e0c7e..e0059fea53 100644 --- a/android/src/com/mapswithme/maps/bookmarks/BookmarksCatalogFragment.java +++ b/android/src/com/mapswithme/maps/bookmarks/BookmarksCatalogFragment.java @@ -200,7 +200,7 @@ public class BookmarksCatalogFragment extends BaseWebViewMwmFragment } } - private static class ImportCategoryListener implements BookmarkManager.BookmarksCatalogListener + private static class ImportCategoryListener extends BookmarkManager.DefaultBookmarksCatalogListener { @NonNull private final Context mContext; @@ -210,20 +210,11 @@ public class BookmarksCatalogFragment extends BaseWebViewMwmFragment mContext = context.getApplicationContext(); } - @Override - public void onImportStarted(@NonNull String serverId) - { - - } - @Override public void onImportFinished(@NonNull String serverId, boolean successful) { if (successful) - { - Toast.makeText(mContext, - R.string.bookmarks_webview_success_toast , Toast.LENGTH_SHORT).show(); - } + Toast.makeText(mContext, R.string.bookmarks_webview_success_toast, Toast.LENGTH_SHORT).show(); } } } diff --git a/android/src/com/mapswithme/maps/bookmarks/BookmarksListFragment.java b/android/src/com/mapswithme/maps/bookmarks/BookmarksListFragment.java index 10351752d8..98e22ce18c 100644 --- a/android/src/com/mapswithme/maps/bookmarks/BookmarksListFragment.java +++ b/android/src/com/mapswithme/maps/bookmarks/BookmarksListFragment.java @@ -32,7 +32,7 @@ import com.mapswithme.util.UiUtils; import com.mapswithme.util.sharing.ShareOption; import com.mapswithme.util.sharing.SharingHelper; -public class BookmarksListFragment extends BaseMwmRecyclerFragment +public class BookmarksListFragment extends BaseMwmRecyclerFragment implements RecyclerLongClickListener, RecyclerClickListener, MenuItem.OnMenuItemClickListener, BookmarkManager.BookmarksSharingListener @@ -52,7 +52,7 @@ public class BookmarksListFragment extends BaseMwmRecyclerFragment } @Override - protected RecyclerView.Adapter createAdapter() + protected BookmarkListAdapter createAdapter() { return new BookmarkListAdapter(mCategory); } @@ -89,9 +89,7 @@ public class BookmarksListFragment extends BaseMwmRecyclerFragment public void onResume() { super.onResume(); - BookmarkListAdapter adapter = (BookmarkListAdapter) getAdapter(); - if (adapter == null) - return; + BookmarkListAdapter adapter = getAdapter(); adapter.startLocationUpdate(); adapter.notifyDataSetChanged(); @@ -102,9 +100,8 @@ public class BookmarksListFragment extends BaseMwmRecyclerFragment { super.onPause(); - BookmarkListAdapter adapter = (BookmarkListAdapter) getAdapter(); - if (adapter != null) - adapter.stopLocationUpdate(); + BookmarkListAdapter adapter = getAdapter(); + adapter.stopLocationUpdate(); } @Override @@ -116,9 +113,7 @@ public class BookmarksListFragment extends BaseMwmRecyclerFragment private void configureAdapter() { - BookmarkListAdapter adapter = (BookmarkListAdapter) getAdapter(); - if (adapter == null) - return; + BookmarkListAdapter adapter = getAdapter(); adapter.startLocationUpdate(); adapter.setOnClickListener(this); @@ -130,26 +125,23 @@ public class BookmarksListFragment extends BaseMwmRecyclerFragment { final Intent i = new Intent(getActivity(), MwmActivity.class); - BookmarkListAdapter adapter = (BookmarkListAdapter) getAdapter(); + BookmarkListAdapter adapter = getAdapter(); - if (adapter != null) + switch (adapter.getItemViewType(position)) { - switch (adapter.getItemViewType(position)) - { - case BookmarkListAdapter.TYPE_SECTION: - case BookmarkListAdapter.TYPE_DESC: - return; - case BookmarkListAdapter.TYPE_BOOKMARK: - final Bookmark bookmark = (Bookmark) adapter.getItem(position); - i.putExtra(MwmActivity.EXTRA_TASK, - new MwmActivity.ShowBookmarkTask(bookmark.getCategoryId(), bookmark.getBookmarkId())); - break; - case BookmarkListAdapter.TYPE_TRACK: - final Track track = (Track) adapter.getItem(position); - i.putExtra(MwmActivity.EXTRA_TASK, - new MwmActivity.ShowTrackTask(track.getCategoryId(), track.getTrackId())); - break; - } + case BookmarkListAdapter.TYPE_SECTION: + case BookmarkListAdapter.TYPE_DESC: + return; + case BookmarkListAdapter.TYPE_BOOKMARK: + final Bookmark bookmark = (Bookmark) adapter.getItem(position); + i.putExtra(MwmActivity.EXTRA_TASK, + new MwmActivity.ShowBookmarkTask(bookmark.getCategoryId(), bookmark.getBookmarkId())); + break; + case BookmarkListAdapter.TYPE_TRACK: + final Track track = (Track) adapter.getItem(position); + i.putExtra(MwmActivity.EXTRA_TASK, + new MwmActivity.ShowTrackTask(track.getCategoryId(), track.getTrackId())); + break; } i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); @@ -159,9 +151,7 @@ public class BookmarksListFragment extends BaseMwmRecyclerFragment @Override public void onLongItemClick(View v, int position) { - BookmarkListAdapter adapter = (BookmarkListAdapter) getAdapter(); - if (adapter == null) - return; + BookmarkListAdapter adapter = getAdapter(); mSelectedPosition = position; int type = adapter.getItemViewType(mSelectedPosition); @@ -215,9 +205,7 @@ public class BookmarksListFragment extends BaseMwmRecyclerFragment @Override public boolean onMenuItemClick(MenuItem menuItem) { - BookmarkListAdapter adapter = (BookmarkListAdapter) getAdapter(); - if (adapter == null) - return false; + BookmarkListAdapter adapter = getAdapter(); Bookmark item = (Bookmark) adapter.getItem(mSelectedPosition); diff --git a/android/src/com/mapswithme/maps/bookmarks/CachedBookmarkCategoriesFragment.java b/android/src/com/mapswithme/maps/bookmarks/CachedBookmarkCategoriesFragment.java index 152bde9c7f..973c26081e 100644 --- a/android/src/com/mapswithme/maps/bookmarks/CachedBookmarkCategoriesFragment.java +++ b/android/src/com/mapswithme/maps/bookmarks/CachedBookmarkCategoriesFragment.java @@ -51,7 +51,7 @@ public class CachedBookmarkCategoriesFragment extends BaseBookmarkCategoriesFrag } @Override - protected RecyclerView.Adapter createAdapter() + protected CatalogBookmarkCategoriesAdapter createAdapter() { return new CatalogBookmarkCategoriesAdapter(getContext()); } diff --git a/android/src/com/mapswithme/maps/downloader/DownloaderFragment.java b/android/src/com/mapswithme/maps/downloader/DownloaderFragment.java index 110c058d41..2ea5dd983a 100644 --- a/android/src/com/mapswithme/maps/downloader/DownloaderFragment.java +++ b/android/src/com/mapswithme/maps/downloader/DownloaderFragment.java @@ -19,7 +19,7 @@ import com.mapswithme.maps.widget.PlaceholderView; import java.util.ArrayList; import java.util.List; -public class DownloaderFragment extends BaseMwmRecyclerFragment +public class DownloaderFragment extends BaseMwmRecyclerFragment implements OnBackPressListener { private DownloaderToolbarController mToolbarController; @@ -191,7 +191,7 @@ public class DownloaderFragment extends BaseMwmRecyclerFragment } @Override - protected RecyclerView.Adapter createAdapter() + protected DownloaderAdapter createAdapter() { if (mAdapter == null) mAdapter = new DownloaderAdapter(this); @@ -206,8 +206,8 @@ public class DownloaderFragment extends BaseMwmRecyclerFragment mToolbarController.onActivityResult(requestCode, resultCode, data); } + @NonNull @Override - @Nullable public DownloaderAdapter getAdapter() { return mAdapter; diff --git a/android/src/com/mapswithme/maps/downloader/SelectMigrationFragment.java b/android/src/com/mapswithme/maps/downloader/SelectMigrationFragment.java deleted file mode 100644 index 91fb301814..0000000000 --- a/android/src/com/mapswithme/maps/downloader/SelectMigrationFragment.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.mapswithme.maps.downloader; - -import android.support.v7.widget.RecyclerView; - -import com.mapswithme.maps.base.BaseMwmRecyclerFragment; - -public class SelectMigrationFragment extends BaseMwmRecyclerFragment -{ - @Override - protected RecyclerView.Adapter createAdapter() - { - // TODO - return null; - } -} diff --git a/android/src/com/mapswithme/maps/editor/CuisineFragment.java b/android/src/com/mapswithme/maps/editor/CuisineFragment.java index 95e76f38b7..db96fac801 100644 --- a/android/src/com/mapswithme/maps/editor/CuisineFragment.java +++ b/android/src/com/mapswithme/maps/editor/CuisineFragment.java @@ -5,12 +5,12 @@ import android.support.v7.widget.RecyclerView; import com.mapswithme.maps.base.BaseMwmRecyclerFragment; -public class CuisineFragment extends BaseMwmRecyclerFragment +public class CuisineFragment extends BaseMwmRecyclerFragment { private CuisineAdapter mAdapter; @Override - protected RecyclerView.Adapter createAdapter() + protected CuisineAdapter createAdapter() { mAdapter = new CuisineAdapter(); return mAdapter; diff --git a/android/src/com/mapswithme/maps/editor/FeatureCategoryFragment.java b/android/src/com/mapswithme/maps/editor/FeatureCategoryFragment.java index e1ee89b7ab..03355a9816 100644 --- a/android/src/com/mapswithme/maps/editor/FeatureCategoryFragment.java +++ b/android/src/com/mapswithme/maps/editor/FeatureCategoryFragment.java @@ -16,7 +16,7 @@ import com.mapswithme.maps.widget.SearchToolbarController; import com.mapswithme.maps.widget.ToolbarController; import com.mapswithme.util.Language; -public class FeatureCategoryFragment extends BaseMwmRecyclerFragment +public class FeatureCategoryFragment extends BaseMwmRecyclerFragment { private FeatureCategory mSelectedCategory; protected ToolbarController mToolbarController; @@ -52,12 +52,12 @@ public class FeatureCategoryFragment extends BaseMwmRecyclerFragment private void setFilter(String query) { - ((FeatureCategoryAdapter) getAdapter()).setCategories(query.isEmpty() ? Editor.nativeGetAllFeatureCategories(Language.getKeyboardLocale()) - : Editor.nativeSearchFeatureCategories(query, Language.getKeyboardLocale())); + getAdapter().setCategories(query.isEmpty() ? Editor.nativeGetAllFeatureCategories(Language.getKeyboardLocale()) + : Editor.nativeSearchFeatureCategories(query, Language.getKeyboardLocale())); } @Override - protected RecyclerView.Adapter createAdapter() + protected FeatureCategoryAdapter createAdapter() { return new FeatureCategoryAdapter(this, Editor.nativeGetAllFeatureCategories(Language.getKeyboardLocale()), mSelectedCategory); } diff --git a/android/src/com/mapswithme/maps/editor/LanguagesFragment.java b/android/src/com/mapswithme/maps/editor/LanguagesFragment.java index aec201453c..48e57e7e20 100644 --- a/android/src/com/mapswithme/maps/editor/LanguagesFragment.java +++ b/android/src/com/mapswithme/maps/editor/LanguagesFragment.java @@ -7,7 +7,6 @@ import com.mapswithme.maps.base.BaseMwmRecyclerFragment; import com.mapswithme.maps.editor.data.Language; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; @@ -15,7 +14,7 @@ import java.util.List; import java.util.Set; -public class LanguagesFragment extends BaseMwmRecyclerFragment +public class LanguagesFragment extends BaseMwmRecyclerFragment { final static String EXISTING_LOCALIZED_NAMES = "ExistingLocalizedNames"; @@ -25,7 +24,7 @@ public class LanguagesFragment extends BaseMwmRecyclerFragment } @Override - protected RecyclerView.Adapter createAdapter() + protected LanguagesAdapter createAdapter() { Bundle args = getArguments(); Set existingLanguages = new HashSet<>(args.getStringArrayList(EXISTING_LOCALIZED_NAMES)); diff --git a/android/src/com/mapswithme/maps/editor/SimpleTimetableFragment.java b/android/src/com/mapswithme/maps/editor/SimpleTimetableFragment.java index e1e5dcf532..d5a8cac325 100644 --- a/android/src/com/mapswithme/maps/editor/SimpleTimetableFragment.java +++ b/android/src/com/mapswithme/maps/editor/SimpleTimetableFragment.java @@ -12,7 +12,7 @@ import com.mapswithme.maps.base.BaseMwmRecyclerFragment; import com.mapswithme.maps.editor.data.HoursMinutes; import com.mapswithme.maps.editor.data.Timetable; -public class SimpleTimetableFragment extends BaseMwmRecyclerFragment +public class SimpleTimetableFragment extends BaseMwmRecyclerFragment implements TimetableFragment.TimetableProvider, HoursMinutesPickerFragment.OnPickListener { @@ -26,7 +26,7 @@ public class SimpleTimetableFragment extends BaseMwmRecyclerFragment } @Override - protected RecyclerView.Adapter createAdapter() + protected SimpleTimetableAdapter createAdapter() { mAdapter = new SimpleTimetableAdapter(this); if (mInitTts != null) diff --git a/android/src/com/mapswithme/maps/editor/StreetFragment.java b/android/src/com/mapswithme/maps/editor/StreetFragment.java index 38b407d36d..707d131d3d 100644 --- a/android/src/com/mapswithme/maps/editor/StreetFragment.java +++ b/android/src/com/mapswithme/maps/editor/StreetFragment.java @@ -14,7 +14,7 @@ import com.mapswithme.maps.base.BaseMwmRecyclerFragment; import com.mapswithme.maps.dialog.EditTextDialogFragment; import com.mapswithme.maps.editor.data.LocalizedStreet; -public class StreetFragment extends BaseMwmRecyclerFragment +public class StreetFragment extends BaseMwmRecyclerFragment implements EditTextDialogFragment.EditTextDialogInterface { private LocalizedStreet mSelectedString; @@ -41,7 +41,7 @@ public class StreetFragment extends BaseMwmRecyclerFragment } @Override - protected RecyclerView.Adapter createAdapter() + protected StreetAdapter createAdapter() { return new StreetAdapter(this, Editor.nativeGetNearbyStreets(), mSelectedString); } diff --git a/android/src/com/mapswithme/maps/search/SearchCategoriesFragment.java b/android/src/com/mapswithme/maps/search/SearchCategoriesFragment.java index d2cdc9b737..7bce0498e9 100644 --- a/android/src/com/mapswithme/maps/search/SearchCategoriesFragment.java +++ b/android/src/com/mapswithme/maps/search/SearchCategoriesFragment.java @@ -7,11 +7,11 @@ import android.support.v7.widget.RecyclerView; import com.mapswithme.maps.R; import com.mapswithme.maps.base.BaseMwmRecyclerFragment; -public class SearchCategoriesFragment extends BaseMwmRecyclerFragment +public class SearchCategoriesFragment extends BaseMwmRecyclerFragment implements CategoriesAdapter.OnCategorySelectedListener { @Override - protected RecyclerView.Adapter createAdapter() + protected CategoriesAdapter createAdapter() { return new CategoriesAdapter(this); } diff --git a/android/src/com/mapswithme/maps/search/SearchHistoryFragment.java b/android/src/com/mapswithme/maps/search/SearchHistoryFragment.java index 4f22c99985..b90238a51f 100644 --- a/android/src/com/mapswithme/maps/search/SearchHistoryFragment.java +++ b/android/src/com/mapswithme/maps/search/SearchHistoryFragment.java @@ -14,7 +14,7 @@ import com.mapswithme.maps.widget.PlaceholderView; import com.mapswithme.maps.widget.SearchToolbarController; import com.mapswithme.util.UiUtils; -public class SearchHistoryFragment extends BaseMwmRecyclerFragment +public class SearchHistoryFragment extends BaseMwmRecyclerFragment { private PlaceholderView mPlaceHolder; @@ -24,7 +24,7 @@ public class SearchHistoryFragment extends BaseMwmRecyclerFragment } @Override - protected RecyclerView.Adapter createAdapter() + protected SearchHistoryAdapter createAdapter() { return new SearchHistoryAdapter(((SearchToolbarController.Container) getParentFragment()).getController()); }