Added clear button in search

This commit is contained in:
h4h13 2019-11-30 16:42:47 +05:30
parent 778ec942a5
commit 4da1f68617
2 changed files with 198 additions and 171 deletions

View file

@ -1,24 +1,35 @@
package code.name.monkey.retromusic.activities package code.name.monkey.retromusic.activities
import android.app.* import android.app.Activity
import android.content.* import android.app.Service
import android.content.ActivityNotFoundException
import android.content.Intent
import android.content.res.ColorStateList import android.content.res.ColorStateList
import android.os.Bundle import android.os.Bundle
import android.speech.RecognizerIntent import android.speech.RecognizerIntent
import android.text.* import android.text.Editable
import android.text.TextWatcher
import android.view.View import android.view.View
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import android.widget.TextView.BufferType import android.widget.TextView.BufferType
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.widget.SearchView.OnQueryTextListener import androidx.appcompat.widget.SearchView.OnQueryTextListener
import androidx.recyclerview.widget.* import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.transition.TransitionManager
import code.name.monkey.appthemehelper.ThemeStore import code.name.monkey.appthemehelper.ThemeStore
import code.name.monkey.appthemehelper.util.* import code.name.monkey.appthemehelper.util.ATHUtil
import code.name.monkey.retromusic.* import code.name.monkey.appthemehelper.util.ColorUtil
import code.name.monkey.appthemehelper.util.MaterialValueHelper
import code.name.monkey.retromusic.App
import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.activities.base.AbsMusicServiceActivity import code.name.monkey.retromusic.activities.base.AbsMusicServiceActivity
import code.name.monkey.retromusic.adapter.SearchAdapter import code.name.monkey.retromusic.adapter.SearchAdapter
import code.name.monkey.retromusic.mvp.presenter.* import code.name.monkey.retromusic.mvp.presenter.SearchPresenter
import code.name.monkey.retromusic.util.* import code.name.monkey.retromusic.mvp.presenter.SearchView
import code.name.monkey.retromusic.util.RetroColorUtil
import code.name.monkey.retromusic.util.RetroUtil
import com.google.android.material.textfield.TextInputEditText
import kotlinx.android.synthetic.main.activity_search.* import kotlinx.android.synthetic.main.activity_search.*
import java.util.* import java.util.*
import javax.inject.Inject import javax.inject.Inject
@ -53,7 +64,7 @@ class SearchActivity : AbsMusicServiceActivity(), OnQueryTextListener, TextWatch
back.setOnClickListener { onBackPressed() } back.setOnClickListener { onBackPressed() }
voiceSearch.setOnClickListener { startMicSearch() } voiceSearch.setOnClickListener { startMicSearch() }
clearText.setOnClickListener { searchView.clearText() }
searchContainer.setCardBackgroundColor(RetroColorUtil.toolbarColor(this)) searchContainer.setCardBackgroundColor(RetroColorUtil.toolbarColor(this))
keyboardPopup.setOnClickListener { keyboardPopup.setOnClickListener {
@ -63,14 +74,7 @@ class SearchActivity : AbsMusicServiceActivity(), OnQueryTextListener, TextWatch
keyboardPopup.backgroundTintList = ColorStateList.valueOf(ThemeStore.accentColor(this)) keyboardPopup.backgroundTintList = ColorStateList.valueOf(ThemeStore.accentColor(this))
ColorStateList.valueOf( ColorStateList.valueOf(
MaterialValueHelper.getPrimaryTextColor( MaterialValueHelper.getPrimaryTextColor(this, ColorUtil.isColorLight(ThemeStore.accentColor(this)))
this,
ColorUtil.isColorLight(
ThemeStore.accentColor(
this
)
)
)
).apply { ).apply {
keyboardPopup.setTextColor(this) keyboardPopup.setTextColor(this)
keyboardPopup.iconTint = this keyboardPopup.iconTint = this
@ -83,7 +87,7 @@ class SearchActivity : AbsMusicServiceActivity(), OnQueryTextListener, TextWatch
private fun setupRecyclerView() { private fun setupRecyclerView() {
searchAdapter = SearchAdapter(this, emptyList()) searchAdapter = SearchAdapter(this, emptyList())
searchAdapter!!.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() { searchAdapter?.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onChanged() { override fun onChanged() {
super.onChanged() super.onChanged()
empty.visibility = if (searchAdapter!!.itemCount < 1) View.VISIBLE else View.GONE empty.visibility = if (searchAdapter!!.itemCount < 1) View.VISIBLE else View.GONE
@ -126,7 +130,9 @@ class SearchActivity : AbsMusicServiceActivity(), OnQueryTextListener, TextWatch
private fun search(query: String) { private fun search(query: String) {
this.query = query this.query = query
TransitionManager.beginDelayedTransition(appBarLayout)
voiceSearch.visibility = if (query.isNotEmpty()) View.GONE else View.VISIBLE voiceSearch.visibility = if (query.isNotEmpty()) View.GONE else View.VISIBLE
clearText.visibility = if (query.isNotEmpty()) View.VISIBLE else View.GONE
searchPresenter.search(query) searchPresenter.search(query)
} }
@ -213,3 +219,7 @@ class SearchActivity : AbsMusicServiceActivity(), OnQueryTextListener, TextWatch
private const val REQ_CODE_SPEECH_INPUT = 9002 private const val REQ_CODE_SPEECH_INPUT = 9002
} }
} }
fun TextInputEditText.clearText() {
text = null
}

View file

@ -55,16 +55,33 @@
<requestFocus /> <requestFocus />
</com.google.android.material.textfield.TextInputEditText> </com.google.android.material.textfield.TextInputEditText>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0">
<androidx.appcompat.widget.AppCompatImageView <androidx.appcompat.widget.AppCompatImageView
android:id="@+id/voiceSearch" android:id="@+id/voiceSearch"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="0"
android:background="?roundSelector" android:background="?roundSelector"
android:padding="12dp" android:padding="12dp"
app:srcCompat="@drawable/ic_mic_white_24dp" app:srcCompat="@drawable/ic_mic_white_24dp"
app:tint="?colorOnSurface" app:tint="?colorOnSurface"
tools:visibility="visible" /> tools:visibility="visible" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/clearText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?roundSelector"
android:padding="12dp"
app:srcCompat="@drawable/ic_close_white_24dp"
app:tint="?colorOnSurface"
android:visibility="gone"
tools:visibility="visible" />
</FrameLayout>
</LinearLayout> </LinearLayout>
</com.google.android.material.card.MaterialCardView> </com.google.android.material.card.MaterialCardView>
</com.google.android.material.appbar.AppBarLayout> </com.google.android.material.appbar.AppBarLayout>