From 514081ee7676b25165e8dc358291363c97d0e93f Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 30 Dec 2021 14:38:51 +0100 Subject: [PATCH] adding some bottom action menu related code style formatting --- .../commons/adapters/MyRecyclerViewAdapter.kt | 2 +- .../adapters/MyRecyclerViewListAdapter.kt | 2 +- .../BottomActionMenuCallback.kt | 6 +-- .../BottomActionMenuItemPopup.kt | 40 +++++++++---------- .../BottomActionMenuParser.kt | 8 +++- .../bottomactionmenu/BottomActionMenuPopup.kt | 2 - .../bottomactionmenu/BottomActionMenuView.kt | 39 +++--------------- .../res/layout/item_action_mode_popup.xml | 2 +- commons/src/main/res/values/dimens.xml | 3 -- 9 files changed, 37 insertions(+), 67 deletions(-) diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/adapters/MyRecyclerViewAdapter.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/adapters/MyRecyclerViewAdapter.kt index 5536e708d..8b9f57de9 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/adapters/MyRecyclerViewAdapter.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/adapters/MyRecyclerViewAdapter.kt @@ -16,10 +16,10 @@ import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor import com.simplemobiletools.commons.extensions.getContrastColor import com.simplemobiletools.commons.interfaces.MyActionModeCallback import com.simplemobiletools.commons.views.MyRecyclerView -import com.simplemobiletools.commons.views.bottomactionmenu.BottomActionMenuView import com.simplemobiletools.commons.views.bottomactionmenu.BottomActionMenuCallback import com.simplemobiletools.commons.views.bottomactionmenu.BottomActionMenuItem import com.simplemobiletools.commons.views.bottomactionmenu.BottomActionMenuPopup +import com.simplemobiletools.commons.views.bottomactionmenu.BottomActionMenuView import java.util.* abstract class MyRecyclerViewAdapter(val activity: BaseSimpleActivity, val recyclerView: MyRecyclerView, val itemClick: (Any) -> Unit) : diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/adapters/MyRecyclerViewListAdapter.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/adapters/MyRecyclerViewListAdapter.kt index dec0bb2c8..bd6be401f 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/adapters/MyRecyclerViewListAdapter.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/adapters/MyRecyclerViewListAdapter.kt @@ -18,10 +18,10 @@ import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor import com.simplemobiletools.commons.extensions.getContrastColor import com.simplemobiletools.commons.interfaces.MyActionModeCallback import com.simplemobiletools.commons.views.MyRecyclerView -import com.simplemobiletools.commons.views.bottomactionmenu.BottomActionMenuView import com.simplemobiletools.commons.views.bottomactionmenu.BottomActionMenuCallback import com.simplemobiletools.commons.views.bottomactionmenu.BottomActionMenuItem import com.simplemobiletools.commons.views.bottomactionmenu.BottomActionMenuPopup +import com.simplemobiletools.commons.views.bottomactionmenu.BottomActionMenuView import java.util.* abstract class MyRecyclerViewListAdapter( diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/views/bottomactionmenu/BottomActionMenuCallback.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/views/bottomactionmenu/BottomActionMenuCallback.kt index 27830c0b7..633793116 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/views/bottomactionmenu/BottomActionMenuCallback.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/views/bottomactionmenu/BottomActionMenuCallback.kt @@ -1,7 +1,7 @@ package com.simplemobiletools.commons.views.bottomactionmenu interface BottomActionMenuCallback { - fun onItemClicked(item: BottomActionMenuItem){} - fun onViewCreated(view: BottomActionMenuView){} - fun onViewDestroyed(){} + fun onItemClicked(item: BottomActionMenuItem) {} + fun onViewCreated(view: BottomActionMenuView) {} + fun onViewDestroyed() {} } diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/views/bottomactionmenu/BottomActionMenuItemPopup.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/views/bottomactionmenu/BottomActionMenuItemPopup.kt index 50a30c78c..a03d0b57e 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/views/bottomactionmenu/BottomActionMenuItemPopup.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/views/bottomactionmenu/BottomActionMenuItemPopup.kt @@ -4,7 +4,10 @@ import android.content.Context import android.graphics.Rect import android.view.* import android.view.View.MeasureSpec -import android.widget.* +import android.widget.ArrayAdapter +import android.widget.FrameLayout +import android.widget.ListView +import android.widget.PopupWindow import androidx.core.content.ContextCompat import androidx.core.widget.PopupWindowCompat import com.simplemobiletools.commons.R @@ -13,14 +16,12 @@ import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor import com.simplemobiletools.commons.extensions.windowManager import com.simplemobiletools.commons.helpers.isRPlus import kotlinx.android.synthetic.main.item_action_mode_popup.view.* -import kotlin.math.ceil class BottomActionMenuItemPopup( private val context: Context, private val items: List, private val onSelect: (BottomActionMenuItem) -> Unit ) { - private val popup = PopupWindow(context, null, android.R.attr.popupMenuStyle) private var anchorView: View? = null private var dropDownWidth = ViewGroup.LayoutParams.WRAP_CONTENT @@ -41,6 +42,7 @@ class BottomActionMenuItemPopup( if (view == null) { view = LayoutInflater.from(context).inflate(R.layout.item_action_mode_popup, parent, false) } + val item = items[position] view!!.cab_item.text = item.title if (item.icon != View.NO_ID) { @@ -48,10 +50,12 @@ class BottomActionMenuItemPopup( icon?.applyColorFilter(context.getAdjustedPrimaryColor()) view.cab_item.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null) } + view.setOnClickListener { onSelect.invoke(item) popup.dismiss() } + return view } } @@ -71,7 +75,8 @@ class BottomActionMenuItemPopup( PopupWindowCompat.setWindowLayoutType(popup, WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL) popup.isOutsideTouchable = true if (popup.isShowing) { - popup.update(anchorView, dropDownHorizontalOffset, dropDownVerticalOffset, dropDownWidth, if (dropDownHeight < 0) -1 else dropDownHeight) + val dropDownHeight = if (dropDownHeight < 0) -1 else dropDownHeight + popup.update(anchorView, dropDownHorizontalOffset, dropDownVerticalOffset, dropDownWidth, dropDownHeight) } else { popup.width = dropDownWidth popup.height = dropDownHeight @@ -84,14 +89,6 @@ class BottomActionMenuItemPopup( popup.contentView = null } - internal fun setOnDismissListener(listener: (() -> Unit)?) { - if (listener != null) { - popup.setOnDismissListener { listener.invoke() } - } else { - popup.setOnDismissListener(null) - } - } - private fun buildDropDown() { var otherHeights = 0 val dropDownList = ListView(context).apply { @@ -132,7 +129,7 @@ class BottomActionMenuItemPopup( padding = 0 } - if ((dropDownGravity and Gravity.BOTTOM) == Gravity.BOTTOM) { + if (dropDownGravity and Gravity.BOTTOM == Gravity.BOTTOM) { dropDownVerticalOffset += anchorView!!.height } @@ -161,7 +158,6 @@ class BottomActionMenuItemPopup( * @see androidx.appcompat.widget.DropDownListView.measureHeightOfChildrenCompat */ private fun measureHeightOfChildrenCompat(maxHeight: Int): Int { - val parent = FrameLayout(context) val widthMeasureSpec = MeasureSpec.makeMeasureSpec(dropDownWidth, MeasureSpec.EXACTLY) @@ -182,27 +178,27 @@ class BottomActionMenuItemPopup( // Compute child height spec val heightMeasureSpec: Int - var childLp: ViewGroup.LayoutParams? = child.layoutParams + var childLayoutParams: ViewGroup.LayoutParams? = child.layoutParams - if (childLp == null) { - childLp = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) - child.layoutParams = childLp + if (childLayoutParams == null) { + childLayoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) + child.layoutParams = childLayoutParams } - heightMeasureSpec = if (childLp.height > 0) { + heightMeasureSpec = if (childLayoutParams.height > 0) { MeasureSpec.makeMeasureSpec( - childLp.height, + childLayoutParams.height, MeasureSpec.EXACTLY ) } else { - MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED) + MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED) } child.measure(widthMeasureSpec, heightMeasureSpec) // Since this view was measured directly against the parent measure // spec, we must measure it again before reuse. child.forceLayout() - val marginLayoutParams = childLp as? ViewGroup.MarginLayoutParams + val marginLayoutParams = childLayoutParams as? ViewGroup.MarginLayoutParams val topMargin = marginLayoutParams?.topMargin ?: 0 val bottomMargin = marginLayoutParams?.bottomMargin ?: 0 val verticalMargin = topMargin + bottomMargin diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/views/bottomactionmenu/BottomActionMenuParser.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/views/bottomactionmenu/BottomActionMenuParser.kt index 2b4e92e49..0a8655946 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/views/bottomactionmenu/BottomActionMenuParser.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/views/bottomactionmenu/BottomActionMenuParser.kt @@ -74,6 +74,12 @@ internal class BottomActionMenuParser(private val context: Context) { val visible = typedArray.getBoolean(R.styleable.BottomActionMenuItem_android_visible, true) typedArray.recycle() parser.require(XmlPullParser.START_TAG, null, MENU_ITEM_TAG) - return BottomActionMenuItem(id, text, iconId, showAsAction == MenuItem.SHOW_AS_ACTION_ALWAYS || showAsAction == MenuItem.SHOW_AS_ACTION_IF_ROOM, visible) + return BottomActionMenuItem( + id, + text, + iconId, + showAsAction == MenuItem.SHOW_AS_ACTION_ALWAYS || showAsAction == MenuItem.SHOW_AS_ACTION_IF_ROOM, + visible + ) } } diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/views/bottomactionmenu/BottomActionMenuPopup.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/views/bottomactionmenu/BottomActionMenuPopup.kt index d7a6fb545..e920b75a6 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/views/bottomactionmenu/BottomActionMenuPopup.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/views/bottomactionmenu/BottomActionMenuPopup.kt @@ -10,14 +10,12 @@ import androidx.core.widget.PopupWindowCompat import com.google.android.material.floatingactionbutton.FloatingActionButton import com.simplemobiletools.commons.activities.BaseSimpleActivity - class BottomActionMenuPopup(private val activity: BaseSimpleActivity, items: List) { private val bottomActionMenuView = BottomActionMenuView(activity) private val popup = PopupWindow(activity, null, android.R.attr.popupMenuStyle) private var floatingActionButton: FloatingActionButton? = null private var callback: BottomActionMenuCallback? = null - constructor(activity: BaseSimpleActivity, @MenuRes menuResId: Int) : this(activity, BottomActionMenuParser(activity).inflate(menuResId)) init { diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/views/bottomactionmenu/BottomActionMenuView.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/views/bottomactionmenu/BottomActionMenuView.kt index cba0cc786..a0fca281a 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/views/bottomactionmenu/BottomActionMenuView.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/views/bottomactionmenu/BottomActionMenuView.kt @@ -11,18 +11,13 @@ import android.view.View import android.view.ViewPropertyAnimator import android.widget.ImageView import android.widget.LinearLayout -import androidx.annotation.DrawableRes import androidx.annotation.IdRes -import androidx.annotation.MenuRes -import androidx.annotation.StringRes import com.google.android.material.animation.AnimationUtils import com.simplemobiletools.commons.R import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.isRPlus - class BottomActionMenuView : LinearLayout { - companion object { private const val ENTER_ANIMATION_DURATION = 225 private const val EXIT_ANIMATION_DURATION = 175 @@ -37,7 +32,12 @@ class BottomActionMenuView : LinearLayout { private val inflater = LayoutInflater.from(context) private val itemsLookup = LinkedHashMap() private val items: List - get() = itemsLookup.values.toList().sortedWith(compareByDescending { it.showAsAction }.thenBy { it.icon != View.NO_ID }).filter { it.isVisible } + get() = itemsLookup.values.toList().sortedWith(compareByDescending { + it.showAsAction + }.thenBy { + it.icon != View.NO_ID + }).filter { it.isVisible } + private var currentAnimator: ViewPropertyAnimator? = null private var callback: BottomActionMenuCallback? = null @@ -99,14 +99,6 @@ class BottomActionMenuView : LinearLayout { }) } - fun createFromMenu(@MenuRes menuResId: Int) { - if (menuResId != View.NO_ID) { - val menuParser = BottomActionMenuParser(context) - val items = menuParser.inflate(menuResId) - setup(items) - } - } - fun setup(items: List) { items.forEach { itemsLookup[it.id] = it } init() @@ -116,25 +108,6 @@ class BottomActionMenuView : LinearLayout { setItem(item) } - fun setItemShowAsAction(@IdRes itemId: Int, showAsAction: Boolean) { - val item = itemsLookup[itemId] - setItem(item?.copy(showAsAction = showAsAction)) - } - - fun changeItemIcon(@IdRes itemId: Int, @DrawableRes iconRes: Int) { - val item = itemsLookup[itemId] - setItem(item?.copy(icon = iconRes)) - } - - fun changeItemTitle(@IdRes itemId: Int, @StringRes stringRes: Int) { - changeItemTitle(itemId, context.getString(stringRes)) - } - - fun changeItemTitle(@IdRes itemId: Int, title: String) { - val item = itemsLookup[itemId] - setItem(item?.copy(title = title)) - } - private fun setItem(item: BottomActionMenuItem?) { item?.let { itemsLookup[item.id] = item diff --git a/commons/src/main/res/layout/item_action_mode_popup.xml b/commons/src/main/res/layout/item_action_mode_popup.xml index 428c96317..3f4e9a29f 100644 --- a/commons/src/main/res/layout/item_action_mode_popup.xml +++ b/commons/src/main/res/layout/item_action_mode_popup.xml @@ -12,4 +12,4 @@ android:textSize="@dimen/bigger_text_size" tools:drawableStart="@drawable/ic_rename_vector" tools:drawableTint="@color/color_primary" - tools:text="@tools:sample/cities" /> + tools:text="@string/delete" /> diff --git a/commons/src/main/res/values/dimens.xml b/commons/src/main/res/values/dimens.xml index 2ea54f217..5092f5f2c 100644 --- a/commons/src/main/res/values/dimens.xml +++ b/commons/src/main/res/values/dimens.xml @@ -20,8 +20,6 @@ 70dp 48dp - 8dp - 40dp 72dp 56dp 76dp @@ -51,7 +49,6 @@ 20sp 22sp - 336dp 168dp 85dp