From 17385892cbe4e0359c45a6221036dd5d6c9cb283 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sun, 19 Apr 2020 13:16:25 +0530 Subject: PasswordFragment: Replace fab options with descriptive bottom sheet Signed-off-by: Harsh Shandilya --- .../java/com/zeapo/pwdstore/PasswordFragment.kt | 28 ++-- .../pwdstore/ui/dialogs/ItemCreationBottomSheet.kt | 76 +++++++++++ .../fab/EmitExpandableTransformationBehaviour.kt | 141 --------------------- 3 files changed, 85 insertions(+), 160 deletions(-) create mode 100644 app/src/main/java/com/zeapo/pwdstore/ui/dialogs/ItemCreationBottomSheet.kt delete mode 100644 app/src/main/java/com/zeapo/pwdstore/widget/fab/EmitExpandableTransformationBehaviour.kt (limited to 'app/src/main/java') diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt index 5e12697d..2b1551fa 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt @@ -22,13 +22,13 @@ import androidx.lifecycle.observe import androidx.recyclerview.widget.FixOnItemTouchDispatchRecyclerView import androidx.recyclerview.widget.LinearLayoutManager import androidx.swiperefreshlayout.widget.SwipeRefreshLayout -import com.google.android.material.floatingactionbutton.FloatingActionButton import com.google.android.material.snackbar.Snackbar import com.zeapo.pwdstore.databinding.PasswordRecyclerViewBinding import com.zeapo.pwdstore.git.BaseGitActivity import com.zeapo.pwdstore.git.GitOperationActivity import com.zeapo.pwdstore.ui.OnOffItemAnimator import com.zeapo.pwdstore.ui.adapters.PasswordItemRecyclerAdapter +import com.zeapo.pwdstore.ui.dialogs.ItemCreationBottomSheet import com.zeapo.pwdstore.utils.PasswordItem import com.zeapo.pwdstore.utils.PasswordRepository import java.io.File @@ -57,17 +57,10 @@ class PasswordFragment : Fragment() { ): View? { _binding = PasswordRecyclerViewBinding.inflate(inflater, container, false) initializePasswordList() - val fab = binding.fab - fab.setOnClickListener { - toggleFabExpand(fab) - } - binding.createFolder.setOnClickListener { - requireStore().createFolder() - toggleFabExpand(fab) - } - binding.createPassword.setOnClickListener { - requireStore().createPassword() - toggleFabExpand(fab) + binding.fab.setOnClickListener { + ItemCreationBottomSheet().apply { + setTargetFragment(this@PasswordFragment, 1000) + }.show(parentFragmentManager, "BOTTOM_SHEET") } return binding.root } @@ -145,14 +138,7 @@ class PasswordFragment : Fragment() { super.onDestroyView() } - private fun toggleFabExpand(fab: FloatingActionButton) = with(fab) { - isExpanded = !isExpanded - isActivated = isExpanded - animate().rotationBy(if (isExpanded) -45f else 45f).setDuration(100).start() - } - private val actionModeCallback = object : ActionMode.Callback { - // Called when the action mode is created; startActionMode() was called override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean { // Inflate a menu resource providing context menu items @@ -282,6 +268,10 @@ class PasswordFragment : Fragment() { actionMode?.finish() } + fun createFolder() = requireStore().createFolder() + + fun createPassword() = requireStore().createPassword() + interface OnFragmentInteractionListener { fun onFragmentInteraction(item: PasswordItem) } diff --git a/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/ItemCreationBottomSheet.kt b/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/ItemCreationBottomSheet.kt new file mode 100644 index 00000000..55a767d8 --- /dev/null +++ b/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/ItemCreationBottomSheet.kt @@ -0,0 +1,76 @@ +/* + * Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ +package com.zeapo.pwdstore.ui.dialogs + +import android.graphics.drawable.GradientDrawable +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.view.ViewTreeObserver +import android.widget.FrameLayout +import androidx.fragment.app.Fragment +import com.google.android.material.bottomsheet.BottomSheetBehavior +import com.google.android.material.bottomsheet.BottomSheetDialog +import com.google.android.material.bottomsheet.BottomSheetDialogFragment +import com.zeapo.pwdstore.PasswordFragment +import com.zeapo.pwdstore.R +import com.zeapo.pwdstore.utils.resolveAttribute + +class ItemCreationBottomSheet : BottomSheetDialogFragment() { + private var behavior: BottomSheetBehavior? = null + private val bottomSheetCallback = object : BottomSheetBehavior.BottomSheetCallback() { + override fun onSlide(bottomSheet: View, slideOffset: Float) { + } + + override fun onStateChanged(bottomSheet: View, newState: Int) { + if (newState == BottomSheetBehavior.STATE_COLLAPSED) { + dismiss() + } + } + } + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { + if (savedInstanceState != null) dismiss() + return inflater.inflate(R.layout.item_create_sheet, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + view.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener { + override fun onGlobalLayout() { + view.viewTreeObserver.removeOnGlobalLayoutListener(this) + val dialog = dialog as BottomSheetDialog? ?: return + behavior = dialog.behavior + behavior?.apply { + state = BottomSheetBehavior.STATE_EXPANDED + peekHeight = 0 + addBottomSheetCallback(bottomSheetCallback) + } + dialog.findViewById(R.id.create_folder)?.setOnClickListener { + (requireTargetFragment() as PasswordFragment).createFolder() + dismiss() + } + dialog.findViewById(R.id.create_password)?.setOnClickListener { + (requireTargetFragment() as PasswordFragment).createPassword() + dismiss() + } + } + }) + val gradientDrawable = GradientDrawable().apply { + setColor(requireContext().resolveAttribute(android.R.attr.windowBackground)) + } + view.background = gradientDrawable + } + + override fun dismiss() { + super.dismiss() + behavior?.removeBottomSheetCallback(bottomSheetCallback) + } + + private fun requireTargetFragment(): Fragment = requireNotNull(targetFragment) { + "A target fragment must be set for $this" + } +} diff --git a/app/src/main/java/com/zeapo/pwdstore/widget/fab/EmitExpandableTransformationBehaviour.kt b/app/src/main/java/com/zeapo/pwdstore/widget/fab/EmitExpandableTransformationBehaviour.kt deleted file mode 100644 index 6cefda14..00000000 --- a/app/src/main/java/com/zeapo/pwdstore/widget/fab/EmitExpandableTransformationBehaviour.kt +++ /dev/null @@ -1,141 +0,0 @@ -/* - * Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved. - * SPDX-License-Identifier: GPL-3.0-only - */ -package com.zeapo.pwdstore.widget.fab - -import android.animation.Animator -import android.animation.AnimatorSet -import android.animation.ObjectAnimator -import android.animation.PropertyValuesHolder -import android.content.Context -import android.util.AttributeSet -import android.view.View -import android.view.ViewGroup -import androidx.coordinatorlayout.widget.CoordinatorLayout -import androidx.core.animation.addListener -import androidx.core.view.children -import androidx.core.view.isInvisible -import androidx.core.view.isVisible -import com.google.android.material.floatingactionbutton.FloatingActionButton -import com.google.android.material.transformation.ExpandableTransformationBehavior -import java.util.ArrayList - -/** - * Taken from Mao Yufeng's excellent example at https://git.io/Jvml9, all credits to him for this. - * It's hard to create per-file copyright rules for Spotless so I'm choosing to credit him here. - */ -class EmitExpandableTransformationBehavior @JvmOverloads constructor( - context: Context? = null, - attrs: AttributeSet? = null -) : ExpandableTransformationBehavior(context, attrs) { - - companion object { - private const val EXPAND_DELAY = 60L - private const val EXPAND_DURATION = 150L - private const val COLLAPSE_DELAY = 60L - private const val COLLAPSE_DURATION = 150L - } - - override fun layoutDependsOn(parent: CoordinatorLayout, child: View, dependency: View): Boolean { - return dependency is FloatingActionButton && child is ViewGroup - } - - override fun onCreateExpandedStateChangeAnimation( - dependency: View, - child: View, - expanded: Boolean, - isAnimating: Boolean - ): AnimatorSet { - - if (child !is ViewGroup) { - return AnimatorSet() - } - - val animations = ArrayList() - - if (expanded) { - createExpandAnimation(child, isAnimating, animations) - } else { - createCollapseAnimation(child, animations) - } - - val set = AnimatorSet() - set.playTogether(animations) - set.addListener( - onStart = { - if (expanded) { - child.isVisible = true - } - }, - onEnd = { - if (!expanded) { - child.isInvisible = true - } - } - ) - return set - } - - private fun createExpandAnimation( - child: ViewGroup, - currentlyAnimating: Boolean, - animations: MutableList - ) { - if (!currentlyAnimating) { - child.children.forEach { - it.alpha = 0f - it.scaleX = 0.4f - it.scaleY = 0.4f - } - } - val delays = List(child.childCount) { - it * EXPAND_DELAY - }.reversed().asSequence() - val scaleXHolder = PropertyValuesHolder.ofFloat(View.SCALE_X, 1f) - val scaleYHolder = PropertyValuesHolder.ofFloat(View.SCALE_Y, 1f) - val alphaHolder = PropertyValuesHolder.ofFloat(View.ALPHA, 1f) - val animators = child.children.zip(delays) { view, delay -> - ObjectAnimator.ofPropertyValuesHolder( - view, - scaleXHolder, - scaleYHolder, - alphaHolder - ).apply { - duration = EXPAND_DURATION - startDelay = delay - } - }.toList() - val animatorSet = AnimatorSet().apply { - playTogether(animators) - } - animations.add(animatorSet) - } - - private fun createCollapseAnimation( - child: ViewGroup, - animations: MutableList - ) { - val delays = List(child.childCount) { - it * COLLAPSE_DELAY - }.asSequence() - val scaleXHolder = PropertyValuesHolder.ofFloat(View.SCALE_X, 0.4f) - val scaleYHolder = PropertyValuesHolder.ofFloat(View.SCALE_Y, 0.4f) - val alphaHolder = PropertyValuesHolder.ofFloat(View.ALPHA, 0f) - val animators = child.children.zip(delays) { view, delay -> - ObjectAnimator.ofPropertyValuesHolder( - view, - scaleXHolder, - scaleYHolder, - alphaHolder - ).apply { - duration = COLLAPSE_DURATION - startDelay = delay - } - }.toList() - val animatorSet = AnimatorSet().apply { - playTogether(animators) - } - animations.add(animatorSet) - } -} -- cgit v1.2.3