Utilize TextWatcher KTX extensions

This commit is contained in:
TacoTheDank 2022-04-07 17:20:16 -04:00
parent eac16c64e4
commit 3a84650cd1
4 changed files with 28 additions and 58 deletions

View file

@ -22,12 +22,11 @@ import android.graphics.Color
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.transition.Slide import android.transition.Slide
import android.view.LayoutInflater import android.view.LayoutInflater
import android.widget.ImageView import android.widget.ImageView
import android.widget.Toast import android.widget.Toast
import androidx.core.widget.doAfterTextChanged
import code.name.monkey.appthemehelper.util.MaterialValueHelper import code.name.monkey.appthemehelper.util.MaterialValueHelper
import code.name.monkey.retromusic.R import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.databinding.ActivityAlbumTagEditorBinding import code.name.monkey.retromusic.databinding.ActivityAlbumTagEditorBinding
@ -50,7 +49,7 @@ import com.google.android.material.shape.MaterialShapeDrawable
import org.jaudiotagger.tag.FieldKey import org.jaudiotagger.tag.FieldKey
import java.util.* import java.util.*
class AlbumTagEditorActivity : AbsTagEditorActivity<ActivityAlbumTagEditorBinding>(), TextWatcher { class AlbumTagEditorActivity : AbsTagEditorActivity<ActivityAlbumTagEditorBinding>() {
override val bindingInflater: (LayoutInflater) -> ActivityAlbumTagEditorBinding = override val bindingInflater: (LayoutInflater) -> ActivityAlbumTagEditorBinding =
ActivityAlbumTagEditorBinding::inflate ActivityAlbumTagEditorBinding::inflate
@ -91,10 +90,10 @@ class AlbumTagEditorActivity : AbsTagEditorActivity<ActivityAlbumTagEditorBindin
binding.albumTitleContainer.setTint(false) binding.albumTitleContainer.setTint(false)
binding.albumArtistContainer.setTint(false) binding.albumArtistContainer.setTint(false)
binding.albumText.appHandleColor().addTextChangedListener(this) binding.albumText.appHandleColor().doAfterTextChanged { dataChanged() }
binding.albumArtistText.appHandleColor().addTextChangedListener(this) binding.albumArtistText.appHandleColor().doAfterTextChanged { dataChanged() }
binding.genreTitle.appHandleColor().addTextChangedListener(this) binding.genreTitle.appHandleColor().doAfterTextChanged { dataChanged() }
binding.yearTitle.appHandleColor().addTextChangedListener(this) binding.yearTitle.appHandleColor().doAfterTextChanged { dataChanged() }
} }
private fun fillViewsWithFileTags() { private fun fillViewsWithFileTags() {
@ -198,17 +197,6 @@ class AlbumTagEditorActivity : AbsTagEditorActivity<ActivityAlbumTagEditorBindin
MusicUtil.getSongFileUri(it.id) MusicUtil.getSongFileUri(it.id)
} }
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
}
override fun afterTextChanged(s: Editable) {
dataChanged()
}
override fun setColors(color: Int) { override fun setColors(color: Int) {
super.setColors(color) super.setColors(color)
saveFab.backgroundTintList = ColorStateList.valueOf(color) saveFab.backgroundTintList = ColorStateList.valueOf(color)

View file

@ -23,11 +23,10 @@ import android.graphics.Color
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater import android.view.LayoutInflater
import android.widget.ImageView import android.widget.ImageView
import android.widget.Toast import android.widget.Toast
import androidx.core.widget.doAfterTextChanged
import code.name.monkey.appthemehelper.util.MaterialValueHelper import code.name.monkey.appthemehelper.util.MaterialValueHelper
import code.name.monkey.retromusic.R import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.databinding.ActivitySongTagEditorBinding import code.name.monkey.retromusic.databinding.ActivitySongTagEditorBinding
@ -50,7 +49,7 @@ import org.jaudiotagger.tag.FieldKey
import org.koin.android.ext.android.inject import org.koin.android.ext.android.inject
import java.util.* import java.util.*
class SongTagEditorActivity : AbsTagEditorActivity<ActivitySongTagEditorBinding>(), TextWatcher { class SongTagEditorActivity : AbsTagEditorActivity<ActivitySongTagEditorBinding>() {
override val bindingInflater: (LayoutInflater) -> ActivitySongTagEditorBinding = override val bindingInflater: (LayoutInflater) -> ActivitySongTagEditorBinding =
ActivitySongTagEditorBinding::inflate ActivitySongTagEditorBinding::inflate
@ -83,16 +82,16 @@ class SongTagEditorActivity : AbsTagEditorActivity<ActivitySongTagEditorBinding>
binding.discNumberContainer.setTint(false) binding.discNumberContainer.setTint(false)
binding.lyricsContainer.setTint(false) binding.lyricsContainer.setTint(false)
binding.songText.appHandleColor().addTextChangedListener(this) binding.songText.appHandleColor().doAfterTextChanged { dataChanged() }
binding.albumText.appHandleColor().addTextChangedListener(this) binding.albumText.appHandleColor().doAfterTextChanged { dataChanged() }
binding.albumArtistText.appHandleColor().addTextChangedListener(this) binding.albumArtistText.appHandleColor().doAfterTextChanged { dataChanged() }
binding.artistText.appHandleColor().addTextChangedListener(this) binding.artistText.appHandleColor().doAfterTextChanged { dataChanged() }
binding.genreText.appHandleColor().addTextChangedListener(this) binding.genreText.appHandleColor().doAfterTextChanged { dataChanged() }
binding.yearText.appHandleColor().addTextChangedListener(this) binding.yearText.appHandleColor().doAfterTextChanged { dataChanged() }
binding.trackNumberText.appHandleColor().addTextChangedListener(this) binding.trackNumberText.appHandleColor().doAfterTextChanged { dataChanged() }
binding.discNumberText.appHandleColor().addTextChangedListener(this) binding.discNumberText.appHandleColor().doAfterTextChanged { dataChanged() }
binding.lyricsText.appHandleColor().addTextChangedListener(this) binding.lyricsText.appHandleColor().doAfterTextChanged { dataChanged() }
binding.songComposerText.appHandleColor().addTextChangedListener(this) binding.songComposerText.appHandleColor().doAfterTextChanged { dataChanged() }
} }
private fun fillViewsWithFileTags() { private fun fillViewsWithFileTags() {
@ -205,16 +204,6 @@ class SongTagEditorActivity : AbsTagEditorActivity<ActivitySongTagEditorBinding>
}) })
} }
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
}
override fun afterTextChanged(s: Editable) {
dataChanged()
}
companion object { companion object {
val TAG: String = SongTagEditorActivity::class.java.simpleName val TAG: String = SongTagEditorActivity::class.java.simpleName
} }

View file

@ -31,6 +31,7 @@ import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.ViewCompat import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updatePadding import androidx.core.view.updatePadding
import androidx.fragment.app.commit
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
@ -303,9 +304,9 @@ class GradientPlayerFragment : AbsPlayerFragment(R.layout.fragment_gradient_play
private fun hideVolumeIfAvailable() { private fun hideVolumeIfAvailable() {
if (PreferenceUtil.isVolumeVisibilityMode) { if (PreferenceUtil.isVolumeVisibilityMode) {
childFragmentManager.beginTransaction() childFragmentManager.commit {
.replace(R.id.volumeFragmentContainer, VolumeFragment.newInstance()) replace(R.id.volumeFragmentContainer, VolumeFragment.newInstance())
.commit() }
childFragmentManager.executePendingTransactions() childFragmentManager.executePendingTransactions()
volumeFragment = volumeFragment =
childFragmentManager.findFragmentById(R.id.volumeFragmentContainer) as VolumeFragment? childFragmentManager.findFragmentById(R.id.volumeFragmentContainer) as VolumeFragment?

View file

@ -20,13 +20,12 @@ 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.Editable
import android.text.TextWatcher
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import androidx.core.view.* import androidx.core.view.*
import androidx.core.widget.doAfterTextChanged
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import androidx.transition.TransitionManager import androidx.transition.TransitionManager
@ -51,7 +50,7 @@ import net.yslibrary.android.keyboardvisibilityevent.KeyboardVisibilityEvent
import java.util.* import java.util.*
class SearchFragment : AbsMainActivityFragment(R.layout.fragment_search), TextWatcher, class SearchFragment : AbsMainActivityFragment(R.layout.fragment_search),
ChipGroup.OnCheckedStateChangeListener { ChipGroup.OnCheckedStateChangeListener {
companion object { companion object {
const val QUERY = "query" const val QUERY = "query"
@ -81,7 +80,10 @@ class SearchFragment : AbsMainActivityFragment(R.layout.fragment_search), TextWa
searchAdapter.swapDataSet(listOf()) searchAdapter.swapDataSet(listOf())
} }
binding.searchView.apply { binding.searchView.apply {
addTextChangedListener(this@SearchFragment) doAfterTextChanged {
if (!it.isNullOrEmpty())
search(it.toString())
}
focusAndShowKeyboard() focusAndShowKeyboard()
} }
binding.keyboardPopup.apply { binding.keyboardPopup.apply {
@ -171,16 +173,6 @@ class SearchFragment : AbsMainActivityFragment(R.layout.fragment_search), TextWa
} }
} }
override fun afterTextChanged(newText: Editable?) {
if (!newText.isNullOrEmpty()) search(newText.toString())
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
private fun search(query: String) { private fun search(query: String) {
this.query = query this.query = query
TransitionManager.beginDelayedTransition(binding.appBarLayout) TransitionManager.beginDelayedTransition(binding.appBarLayout)