Skip to content

Commit

Permalink
Improved controls of search view
Browse files Browse the repository at this point in the history
  • Loading branch information
Faltenreich committed Oct 11, 2020
1 parent 2495e63 commit 60b2c0a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.faltenreich.release.domain.release.discover
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import androidx.activity.OnBackPressedCallback
import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
Expand All @@ -21,6 +22,7 @@ import com.faltenreich.release.domain.release.list.ReleaseProvider
import com.faltenreich.release.domain.release.search.SearchListAdapter
import com.faltenreich.release.framework.android.fragment.BaseFragment
import com.faltenreich.release.framework.android.view.hideKeyboard
import com.faltenreich.release.framework.android.view.showKeyboard
import com.faltenreich.release.framework.skeleton.SkeletonFactory
import kotlinx.android.synthetic.main.fragment_discover.*
import org.threeten.bp.LocalDate
Expand All @@ -39,6 +41,7 @@ class DiscoverFragment : BaseFragment(R.layout.fragment_discover, R.menu.main),
}

private lateinit var searchListAdapter: SearchListAdapter
private lateinit var onBackPressedCallback: OnBackPressedCallback

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -104,10 +107,18 @@ class DiscoverFragment : BaseFragment(R.layout.fragment_discover, R.menu.main),
searchView.doOnTextChanged { text, _, _, _ -> viewModel.query = text?.toString() }
searchView.setOnClickListener { motionLayout.transitionToEnd() }
searchButton.setOnClickListener {
// TODO: Toggle depending on state
motionLayout.transitionToStart()
if (searchView.hasFocus()) motionLayout.transitionToStart()
else motionLayout.transitionToEnd()
}

onBackPressedCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (searchView.hasFocus()) motionLayout.transitionToStart()
}
}
onBackPressedCallback.isEnabled = false
requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, onBackPressedCallback)

motionLayout.setTransitionListener(object : MotionLayout.TransitionListener {

override fun onTransitionStarted(motionLayout: MotionLayout?, startId: Int, endId: Int) = Unit
Expand All @@ -118,11 +129,16 @@ class DiscoverFragment : BaseFragment(R.layout.fragment_discover, R.menu.main),
when (currentId) {
R.id.start -> {
searchButton.icon = ContextCompat.getDrawable(context, R.drawable.ic_search)
searchView.text = null
searchView.hideKeyboard()
view?.requestFocus()
onBackPressedCallback.isEnabled = false
}
R.id.end -> {
searchButton.icon = ContextCompat.getDrawable(context, R.drawable.ic_arrow_back)
searchView.requestFocus()
searchView.showKeyboard()
onBackPressedCallback.isEnabled = true
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_discover.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
app:cardBackgroundColor="?backgroundColorSecondary"
app:cardCornerRadius="8dp"
app:layout_constraintBottom_toBottomOf="@id/topBar"
app:layout_constraintEnd_toEndOf="@id/topBar"
app:layout_constraintEnd_toStartOf="@id/clearButton"
app:layout_constraintStart_toStartOf="@id/topBar"
app:layout_constraintTop_toTopOf="@id/topBar" />

Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/xml/motion_discover.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
app:layout_constraintEnd_toEndOf="@id/topBar"
app:layout_constraintStart_toStartOf="@id/topBar"
app:layout_constraintTop_toTopOf="@id/topBar" />
<Constraint android:id="@+id/clearButton">
<PropertySet android:visibility="gone" />
</Constraint>
</ConstraintSet>

<ConstraintSet android:id="@+id/end">
Expand All @@ -29,6 +32,9 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Constraint android:id="@+id/clearButton">
<PropertySet android:visibility="visible" />
</Constraint>
</ConstraintSet>

</MotionScene>

0 comments on commit 60b2c0a

Please sign in to comment.