[android] Upgraded bottom sheet lib, supported changed interfaces

This commit is contained in:
Dmitry Donskoy 2018-10-01 14:39:16 +03:00 committed by Aleksandr Zatsepin
parent c0be2a972d
commit 6509024eb8
10 changed files with 99 additions and 159 deletions

View file

@ -87,7 +87,7 @@ dependencies {
}
implementation fileTree(dir: '3rd_party', include: '*.jar')
// BottomSheet
implementation project(":3rd_party:BottomSheet")
implementation 'com.cocosw:bottomsheet:1.3.1@aar'
// LinearLayoutManager allowing wrap_content of RecyclerView-s
// TODO remove this library when default LinearLayoutManager will be fixed.
implementation 'org.solovyev.android.views:linear-layout-manager:0.5@aar'

View file

@ -1,54 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<com.cocosw.bottomsheet.ClosableSlidingLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?bs_dialogBackground"
tools:ignore="Overdraw"
android:layout_gravity="bottom">
android:id="@+id/bs_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?bs_dialogBackground"
android:orientation="vertical"
tools:ignore="Overdraw">
<View
style="@style/BottomSheet.TopDivider" />
<!--<include layout="?bs_headerLayout" />-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
tools:ignore="UseCompoundDrawables">
<ImageView
android:id="@+id/bottom_sheet_title_image"
android:visibility="gone"
tools:ignore="ContentDescription"
style="@style/BottomSheet.Icon" />
<com.cocosw.bottomsheet.PinnedSectionGridView
android:id="@+id/bottom_sheet_gridview"
style="?bs_listStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fadingEdge="none"
android:numColumns="?bs_numColumns"
tools:listitem="@layout/bs_grid_entry" />
<TextView
android:id="@+id/bottom_sheet_title"
android:visibility="gone"
style="@style/BottomSheet.Title" />
</LinearLayout>
<FrameLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.cocosw.bottomsheet.PinnedSectionGridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bottom_sheet_gridview"
android:numColumns="?bs_numColumns"
android:fadingEdge="none"
style="?bs_listStyle"
tools:listitem="@layout/bs_grid_entry" />
<include layout="@layout/shadow_top"/>
</FrameLayout>
</LinearLayout>
</com.cocosw.bottomsheet.ClosableSlidingLayout>
</com.cocosw.bottomsheet.ClosableSlidingLayout>

View file

@ -12,6 +12,7 @@ import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import android.view.View;
import com.cocosw.bottomsheet.BottomSheet;
import com.mapswithme.maps.R;
import com.mapswithme.maps.base.BaseMwmRecyclerFragment;
import com.mapswithme.maps.bookmarks.data.BookmarkCategory;
@ -161,16 +162,18 @@ public abstract class BaseBookmarkCategoriesFragment extends BaseMwmRecyclerFrag
.sheet(getCategoryMenuResId())
.listener(this);
bs.getItemByIndex(0)
.setIcon(item.isVisible() ? R.drawable.ic_hide : R.drawable.ic_show)
.setTitle(item.isVisible() ? R.string.hide : R.string.show);
prepareBottomMenuItems(bs);
bs.tint().show();
BottomSheet bottomSheet = bs.build();
prepareBottomMenuItems(bottomSheet);
bottomSheet
.getMenu().getItem(0)
.setIcon(item.isVisible() ? R.drawable.ic_hide : R.drawable.ic_show)
.setTitle(item.isVisible() ? R.string.hide : R.string.show);
BottomSheetHelper.tint(bottomSheet);
bottomSheet.show();
}
protected abstract void prepareBottomMenuItems(@NonNull BottomSheetHelper.Builder builder);
protected abstract void prepareBottomMenuItems(@NonNull BottomSheet bottomSheet);
@MenuRes
protected int getCategoryMenuResId()
@ -285,12 +288,13 @@ public abstract class BaseBookmarkCategoriesFragment extends BaseMwmRecyclerFrag
showBottomMenu(category);
}
static void setEnableForMenuItem(@IdRes int id, @NonNull BottomSheetHelper.Builder builder,
static void setEnableForMenuItem(@IdRes int id, @NonNull BottomSheet bottomSheet,
boolean enable)
{
builder.getItemById(id)
.setVisible(enable)
.setEnabled(enable);
BottomSheetHelper
.findItemById(bottomSheet, id)
.setVisible(enable)
.setEnabled(enable);
}
private void onSaveText(@NonNull String text)

View file

@ -5,12 +5,12 @@ import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
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.BookmarkManager;
import com.mapswithme.maps.widget.BookmarkBackupView;
import com.mapswithme.util.BottomSheetHelper;
import com.mapswithme.util.UiUtils;
public class BookmarkCategoriesFragment extends BaseBookmarkCategoriesFragment
@ -66,9 +66,9 @@ public class BookmarkCategoriesFragment extends BaseBookmarkCategoriesFragment
}
@Override
protected void prepareBottomMenuItems(@NonNull BottomSheetHelper.Builder builder)
protected void prepareBottomMenuItems(@NonNull BottomSheet bottomSheet)
{
boolean isMultipleItems = getAdapter().getBookmarkCategories().size() > 1;
setEnableForMenuItem(R.id.set_delete, builder, isMultipleItems);
setEnableForMenuItem(R.id.set_delete, bottomSheet, isMultipleItems);
}
}

View file

@ -16,6 +16,7 @@ import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.cocosw.bottomsheet.BottomSheet;
import com.crashlytics.android.Crashlytics;
import com.mapswithme.maps.MwmActivity;
import com.mapswithme.maps.R;
@ -200,26 +201,34 @@ public class BookmarksListFragment extends BaseMwmRecyclerFragment<BookmarkListA
case BookmarkListAdapter.TYPE_BOOKMARK:
final Bookmark bookmark = (Bookmark) adapter.getItem(mSelectedPosition);
int menuResId = isCatalogCategory() ? R.menu.menu_bookmarks_catalog : R.menu.menu_bookmarks;
BottomSheetHelper.Builder bs = BottomSheetHelper.create(getActivity(), bookmark.getTitle())
.sheet(menuResId)
.listener(this);
bs.tint().show();
BottomSheet bs = BottomSheetHelper.create(getActivity(), bookmark.getTitle())
.sheet(menuResId)
.listener(this)
.build();
BottomSheetHelper.tint(bs);
bs.show();
break;
case BookmarkListAdapter.TYPE_TRACK:
final Track track = (Track) adapter.getItem(mSelectedPosition);
BottomSheetHelper.create(getActivity(), track.getName())
.sheet(Menu.NONE, R.drawable.ic_delete, R.string.delete)
.listener(new MenuItem.OnMenuItemClickListener()
{
@Override
public boolean onMenuItemClick(MenuItem menuItem)
{
BookmarkManager.INSTANCE.deleteTrack(track.getTrackId());
adapter.notifyDataSetChanged();
return false;
}
}).tint().show();
BottomSheet bottomSheet = BottomSheetHelper.create(getActivity(), track.getName())
.sheet(Menu.NONE, R.drawable.ic_delete,
R.string.delete)
.listener(new MenuItem.OnMenuItemClickListener()
{
@Override
public boolean onMenuItemClick(MenuItem
menuItem)
{
BookmarkManager.INSTANCE.deleteTrack(track
.getTrackId());
adapter.notifyDataSetChanged();
return false;
}
}).build();
BottomSheetHelper.tint(bottomSheet);
bottomSheet.show();
break;
}
}

View file

@ -10,10 +10,10 @@ import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.cocosw.bottomsheet.BottomSheet;
import com.mapswithme.maps.R;
import com.mapswithme.maps.bookmarks.data.BookmarkCategory;
import com.mapswithme.maps.bookmarks.data.BookmarkManager;
import com.mapswithme.util.BottomSheetHelper;
import com.mapswithme.util.SharedPropertiesUtils;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.sharing.TargetUtils;
@ -157,10 +157,10 @@ public class CachedBookmarkCategoriesFragment extends BaseBookmarkCategoriesFrag
}
@Override
protected void prepareBottomMenuItems(@NonNull BottomSheetHelper.Builder builder)
protected void prepareBottomMenuItems(@NonNull BottomSheet bottomSheet)
{
setEnableForMenuItem(R.id.delete_list, builder, true);
setEnableForMenuItem(R.id.share_list, builder, false);
setEnableForMenuItem(R.id.delete_list, bottomSheet, true);
setEnableForMenuItem(R.id.share_list, bottomSheet, false);
}
private class CloseHeaderClickListener implements View.OnClickListener

View file

@ -23,6 +23,7 @@ import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.cocosw.bottomsheet.BottomSheet;
import com.mapswithme.maps.MwmActivity;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.R;
@ -472,7 +473,7 @@ class DownloaderAdapter extends RecyclerView.Adapter<DownloaderAdapter.ViewHolde
for (MenuItem item: items)
bs.sheet(item.ordinal(), item.icon, item.title);
bs.listener(new android.view.MenuItem.OnMenuItemClickListener()
BottomSheet bottomSheet = bs.listener(new android.view.MenuItem.OnMenuItemClickListener()
{
@Override
public boolean onMenuItemClick(android.view.MenuItem item)
@ -480,7 +481,9 @@ class DownloaderAdapter extends RecyclerView.Adapter<DownloaderAdapter.ViewHolde
MenuItem.values()[item.getItemId()].invoke(mItem, DownloaderAdapter.this);
return false;
}
}).tint().show();
}).build();
BottomSheetHelper.tint(bottomSheet);
bottomSheet.show();
}
ItemViewHolder(View frame)

View file

@ -17,6 +17,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import com.cocosw.bottomsheet.BottomSheet;
import com.mapswithme.maps.R;
import com.mapswithme.maps.editor.data.UserStats;
import com.mapswithme.util.BottomSheetHelper;
@ -179,7 +180,7 @@ public class ProfileFragment extends AuthFragment implements View.OnClickListene
for (MenuItem item: items)
bs.sheet(item.ordinal(), item.icon, item.title);
bs.listener(new android.view.MenuItem.OnMenuItemClickListener()
BottomSheet bottomSheet = bs.listener(new android.view.MenuItem.OnMenuItemClickListener()
{
@Override
public boolean onMenuItemClick(android.view.MenuItem item)
@ -187,6 +188,8 @@ public class ProfileFragment extends AuthFragment implements View.OnClickListene
MenuItem.values()[item.getItemId()].invoke(ProfileFragment.this);
return false;
}
}).tint().show();
}).build();
BottomSheetHelper.tint(bottomSheet);
bottomSheet.show();
}
}

View file

@ -10,6 +10,7 @@ import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.cocosw.bottomsheet.BottomSheet;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.R;
import com.mapswithme.maps.routing.RoutingController;
@ -468,7 +469,7 @@ public final class PlacePageButtons
bs.sheet(i, iconRes, bsItem.getTitle());
}
bs.listener(new MenuItem.OnMenuItemClickListener()
BottomSheet bottomSheet = bs.listener(new MenuItem.OnMenuItemClickListener()
{
@Override
public boolean onMenuItemClick(MenuItem item)
@ -476,9 +477,9 @@ public final class PlacePageButtons
mItemListener.onItemClick(buttons.get(item.getItemId()));
return true;
}
});
bs.tint().show();
}).build();
BottomSheetHelper.tint(bottomSheet);
bottomSheet.show();
}
private View createButton(@NonNull final List<PlacePageButton> items,

View file

@ -1,8 +1,6 @@
package com.mapswithme.util;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
@ -13,46 +11,11 @@ import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;
import android.view.MenuItem;
import java.lang.ref.WeakReference;
import com.cocosw.bottomsheet.BottomSheet;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.widget.ListShadowController;
public final class BottomSheetHelper
{
private static class ShadowedBottomSheet extends BottomSheet
{
private ListShadowController mShadowController;
ShadowedBottomSheet(Context context, int theme)
{
super(context, theme);
}
@Override
protected void init(Context context)
{
super.init(context);
mShadowController = new ListShadowController(list);
}
@Override
protected void showFullItems()
{
super.showFullItems();
mShadowController.attach();
}
@Override
protected void showShortItems()
{
super.showShortItems();
mShadowController.detach();
}
}
public static class Builder extends BottomSheet.Builder
{
public Builder(@NonNull Activity context)
@ -63,12 +26,6 @@ public final class BottomSheetHelper
darkTheme();
}
@Override
protected BottomSheet createDialog(Context context, int theme)
{
return new ShadowedBottomSheet(context, theme);
}
@Override
public BottomSheet build()
{
@ -141,41 +98,28 @@ public final class BottomSheetHelper
super.listener(listener);
return this;
}
}
public Builder tint()
public static void tint(@NonNull BottomSheet bottomSheet)
{
for (int i = 0; i < bottomSheet.getMenu().size(); i++)
{
for (int i = 0; i < getMenu().size(); i++)
{
MenuItem mi = getMenu().getItem(i);
Drawable icon = mi.getIcon();
if (icon != null)
mi.setIcon(Graphics.tint(context, icon));
}
return this;
MenuItem mi = bottomSheet.getMenu().getItem(i);
Drawable icon = mi.getIcon();
if (icon != null)
mi.setIcon(Graphics.tint(bottomSheet.getContext(), icon));
}
}
@NonNull
public MenuItem getItemByIndex(int index)
{
MenuItem item = getMenu().getItem(index);
@NonNull
public static MenuItem findItemById(@NonNull BottomSheet bottomSheet, @IdRes int id)
{
MenuItem item = bottomSheet.getMenu().findItem(id);
if (item == null)
throw new AssertionError("Can not find bottom sheet item with index: " + index);
if (item == null)
throw new AssertionError("Can not find bottom sheet item with id: " + id);
return item;
}
@NonNull
public MenuItem getItemById(@IdRes int id)
{
MenuItem item = getMenu().findItem(id);
if (item == null)
throw new AssertionError("Can not find bottom sheet item with id: " + id);
return item;
}
return item;
}
private BottomSheetHelper()