From 77cbceccb8a7e0da2239b6d108dabe5c6042ca51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Lapi=C5=A1?= Date: Sat, 2 Oct 2021 20:16:21 +0200 Subject: [PATCH 1/8] Changes and fixes for next version --- README.md | 124 ++---------- build.gradle | 12 +- .../search/internal/ClippableFrameLayout.kt | 0 .../internal/TouchObserverFrameLayout.kt | 16 -- .../lapism/search/widget/MaterialSearchBar.kt | 64 +++++-- .../search/widget/MaterialSearchLayout.kt | 8 +- .../search/widget/MaterialSearchToolbar.kt | 29 +-- .../search/widget/MaterialSearchView.kt | 180 ++++++++++++------ .../res/layout/material_search_toolbar.xml | 3 +- src/main/res/layout/material_search_view.xml | 4 +- src/main/res/values/attrs.xml | 2 + src/main/res/values/public.xml | 1 + 12 files changed, 209 insertions(+), 234 deletions(-) mode change 100755 => 100644 src/main/kotlin/com/lapism/search/internal/ClippableFrameLayout.kt delete mode 100644 src/main/kotlin/com/lapism/search/internal/TouchObserverFrameLayout.kt diff --git a/README.md b/README.md index 2206fbe0..dc1531b0 100755 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ![API](https://img.shields.io/badge/API-26%2B-brightgreen.svg?style=flat) [![Kotlin Version](https://img.shields.io/badge/Kotlin-1.5.31-blue.svg)](https://kotlinlang.org) -[![Download](https://img.shields.io/maven-metadata/v?metadataUrl=https%3A%2F%2Frepo1.maven.org%2Fmaven2%2Fio%2Fgithub%2Flapism%2Fsearch%2Fmaven-metadata.xml) ](https://repo1.maven.org/maven2/io/github/lapism/search/1.0.0/) +[![Download](https://img.shields.io/maven-metadata/v?metadataUrl=https%3A%2F%2Frepo1.maven.org%2Fmaven2%2Fio%2Fgithub%2Flapism%2Fsearch%2Fmaven-metadata.xml) ](https://repo1.maven.org/maven2/io/github/lapism/search/) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) # Search @@ -29,133 +29,31 @@ Add the dependency to your gradle file: } dependencies { - implementation 'io.github.lapism:search:1.0.0' + implementation 'io.github.lapism:search:1.1.0' } ``` ## Usage - private fun clearFocusOnSearch() { - binding.materialSearchView.clearFocus() - binding.materialSearchView.visibility = View.GONE - } - - private fun requestFocusOnSearch() { - binding.materialSearchView.visibility = View.VISIBLE - binding.materialSearchView.requestFocus() - } +```java + binding.materialSearchView.requestFocus() + binding.materialSearchView.clearFocus() +``` ### MaterialSearchBar ```java - val toolbar = binding.materialSearchBar.getToolbar() - setSupportActionBar(toolbar) - - val actionBar = supportActionBar - actionBar?.setIcon(R.drawable.new_ic_outline_search_24) - - binding.materialSearchBar.apply { - setHint(getString(R.string.search)) - setOnClickListener { - requestFocusOnSearch() - } - setNavigationOnClickListener { - requestFocusOnSearch() - } - } + ``` ### MaterialSearchView ```java - binding.materialSearchView.apply { - visibility = View.GONE - addView(recyclerView) - setNavigationIcon( - ContextCompat.getDrawable( - this@MainActivity, - R.drawable.search_ic_outline_arrow_back_24 - ) - ) - setNavigationOnClickListener { - clearFocusOnSearch() - } - setHint(getString(R.string.search)) - setBackgroundColor( - ContextCompat.getColor( - this@MainActivity, - R.color.color_surface - ) - ) - setOnQueryTextListener(object : MaterialSearchView.OnQueryTextListener { - override fun onQueryTextChange(newText: CharSequence): Boolean { - adapter.filter(newText) - return true - } - - override fun onQueryTextSubmit(query: CharSequence): Boolean { - return true - } - }) - setOnFocusChangeListener(object : MaterialSearchView.OnFocusChangeListener { - override fun onFocusChange(hasFocus: Boolean) { - - } - }) - } + ``` -### Layout +### Layout You have to use app theme Theme.Material3.* or Theme.MaterialComponents.*. ```xml - - - - - - - - - - - - - - - - - + ``` ### XML attributes @@ -167,6 +65,7 @@ You have to use app theme Theme.Material3.* or Theme.MaterialComponents.*. + @@ -181,6 +80,7 @@ You have to use app theme Theme.Material3.* or Theme.MaterialComponents.*. + diff --git a/build.gradle b/build.gradle index c99d90a1..77562a0b 100644 --- a/build.gradle +++ b/build.gradle @@ -44,14 +44,14 @@ android { dependencies { // Core - implementation 'androidx.core:core-ktx:1.7.0-alpha02' - implementation 'androidx.appcompat:appcompat:1.4.0-alpha03' - implementation 'com.google.android.material:material:1.5.0-alpha03' + implementation 'androidx.core:core-ktx:1.7.0-beta02' + implementation 'androidx.appcompat:appcompat:1.4.0-beta01' + implementation 'com.google.android.material:material:1.5.0-alpha04' } ext { PUBLISH_GROUP_ID = 'io.github.lapism' - PUBLISH_VERSION = '1.0.0' + PUBLISH_VERSION = '1.1.0' PUBLISH_ARTIFACT_ID = 'search' PUBLISH_DESCRIPTION = 'Material You Search component for Android, SearchView' PUBLISH_URL = 'https://github.com/lapism/search' @@ -65,6 +65,4 @@ ext { PUBLISH_SCM_URL = 'https://github.com/lapism/search/tree/master' } -apply from: "${rootProject.projectDir}/scripts/publish-module.gradle" - -// ./gradlew search:publishReleasePublicationToSonatypeRepository \ No newline at end of file +apply from: "${rootProject.projectDir}/scripts/publish-module.gradle" \ No newline at end of file diff --git a/src/main/kotlin/com/lapism/search/internal/ClippableFrameLayout.kt b/src/main/kotlin/com/lapism/search/internal/ClippableFrameLayout.kt old mode 100755 new mode 100644 diff --git a/src/main/kotlin/com/lapism/search/internal/TouchObserverFrameLayout.kt b/src/main/kotlin/com/lapism/search/internal/TouchObserverFrameLayout.kt deleted file mode 100644 index 5409488c..00000000 --- a/src/main/kotlin/com/lapism/search/internal/TouchObserverFrameLayout.kt +++ /dev/null @@ -1,16 +0,0 @@ -package com.lapism.search.internal - -import android.content.Context -import android.util.AttributeSet -import android.widget.FrameLayout - - -class TouchObserverFrameLayout @JvmOverloads constructor( - context: Context, - attrs: AttributeSet? = null, - defStyleAttr: Int = 0, - defStyleRes: Int = 0 -) : FrameLayout(context, attrs, defStyleAttr, defStyleRes) { - - -} \ No newline at end of file diff --git a/src/main/kotlin/com/lapism/search/widget/MaterialSearchBar.kt b/src/main/kotlin/com/lapism/search/widget/MaterialSearchBar.kt index 1178bb43..90442b26 100755 --- a/src/main/kotlin/com/lapism/search/widget/MaterialSearchBar.kt +++ b/src/main/kotlin/com/lapism/search/widget/MaterialSearchBar.kt @@ -9,6 +9,7 @@ import android.view.View import androidx.annotation.ColorInt import androidx.annotation.Nullable import androidx.coordinatorlayout.widget.CoordinatorLayout +import androidx.core.view.ViewCompat import com.google.android.material.appbar.AppBarLayout import com.google.android.material.card.MaterialCardView import com.lapism.search.R @@ -28,11 +29,6 @@ class MaterialSearchBar @JvmOverloads constructor( init { View.inflate(context, R.layout.material_search_bar, this) - /* TODO BINDING ? pruhlednost, edittext ztraci focus - private lateinit var binding: ViewSearchBinding - val inflater = LayoutInflater.from(context) - val binding = MaterialSearchBarBinding.inflate(inflater, this)*/ - card = findViewById(R.id.search_bar_card) toolbar = findViewById(R.id.search_bar_toolbar) @@ -41,19 +37,25 @@ class MaterialSearchBar @JvmOverloads constructor( ) when { - a?.hasValue(R.styleable.MaterialSearchView_search_navigationIconCompat)!! -> { + a?.hasValue(R.styleable.MaterialSearchBar_search_navigationIconCompat)!! -> { navigationIconCompat = a?.getInt( - R.styleable.MaterialSearchView_search_navigationIconCompat, + R.styleable.MaterialSearchBar_search_navigationIconCompat, NavigationIconCompat.NONE )!! } - a?.hasValue(R.styleable.MaterialSearchView_search_navigationIcon)!! -> { - setNavigationIcon(a?.getDrawable(R.styleable.MaterialSearchView_search_navigationIcon)) + a?.hasValue(R.styleable.MaterialSearchBar_search_navigationIcon)!! -> { + setNavigationIcon(a?.getDrawable(R.styleable.MaterialSearchBar_search_navigationIcon)) } } - if (a?.hasValue(R.styleable.MaterialSearchView_search_backgroundColor)!!) { - val color = a?.getInt(R.styleable.MaterialSearchView_search_backgroundColor, 0) + if (a?.hasValue(R.styleable.MaterialSearchBar_search_navigationContentDescription)!!) { + val description = + a?.getText(R.styleable.MaterialSearchBar_search_navigationContentDescription) + setNavigationContentDescription(description) + } + + if (a?.hasValue(R.styleable.MaterialSearchBar_search_backgroundColor)!!) { + val color = a?.getInt(R.styleable.MaterialSearchBar_search_backgroundColor, 0) setBackgroundColor(color!!) } @@ -96,6 +98,14 @@ class MaterialSearchBar @JvmOverloads constructor( toolbar?.setNavigationIcon(resId) } + override fun setNavigationContentDescription(resId: Int) { + toolbar?.setNavigationContentDescription(resId) + } + + override fun setNavigationContentDescription(description: CharSequence?) { + toolbar?.navigationContentDescription = description + } + override fun setOnClickListener(@Nullable l: OnClickListener?) { toolbar?.setOnClickListener(l) } @@ -108,6 +118,24 @@ class MaterialSearchBar @JvmOverloads constructor( return card?.elevation!! } +/* + fun setForegroundColor(){ + + } + + fun setBackgroundColor(){ + + } + + fun setStrokeWidth(){ + + } + + fun setStrokeColor{ + card?.setStrokeColor() + } +*/ + override fun setBackgroundColor(@ColorInt color: Int) { card?.setCardBackgroundColor(color) } @@ -161,19 +189,19 @@ class MaterialSearchBar @JvmOverloads constructor( constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet) - override fun layoutDependsOn( + override fun onDependentViewChanged( parent: CoordinatorLayout, child: View, dependency: View ): Boolean { - return if (dependency is AppBarLayout) { + super.onDependentViewChanged(parent, child, dependency) + if (dependency is AppBarLayout) { dependency.setBackgroundColor(Color.TRANSPARENT) - dependency.elevation = 0.0f - dependency.stateListAnimator = null - true - } else { - false + // dependency.elevation = 0.0f + // dependency.stateListAnimator = null + ViewCompat.setElevation(dependency, 0.0f) } + return false } } diff --git a/src/main/kotlin/com/lapism/search/widget/MaterialSearchLayout.kt b/src/main/kotlin/com/lapism/search/widget/MaterialSearchLayout.kt index bc25f76c..c64bb1c2 100644 --- a/src/main/kotlin/com/lapism/search/widget/MaterialSearchLayout.kt +++ b/src/main/kotlin/com/lapism/search/widget/MaterialSearchLayout.kt @@ -24,7 +24,7 @@ abstract class MaterialSearchLayout @JvmOverloads constructor( when (navigationIconCompat) { NavigationIconCompat.NONE -> { - setNavigationIcon(null) + setNavigationIcon(0) } NavigationIconCompat.ARROW -> { setNavigationIcon( @@ -45,9 +45,13 @@ abstract class MaterialSearchLayout @JvmOverloads constructor( } } + abstract fun setNavigationIcon(resId: Int) + abstract fun setNavigationIcon(@Nullable drawable: Drawable?) - abstract fun setNavigationIcon(resId: Int) + abstract fun setNavigationContentDescription(resId: Int) + + abstract fun setNavigationContentDescription(description: CharSequence?) abstract fun setNavigationOnClickListener(listener: OnClickListener) diff --git a/src/main/kotlin/com/lapism/search/widget/MaterialSearchToolbar.kt b/src/main/kotlin/com/lapism/search/widget/MaterialSearchToolbar.kt index 33dfe175..b02c5f87 100755 --- a/src/main/kotlin/com/lapism/search/widget/MaterialSearchToolbar.kt +++ b/src/main/kotlin/com/lapism/search/widget/MaterialSearchToolbar.kt @@ -1,34 +1,33 @@ package com.lapism.search.widget import android.content.Context +import android.graphics.drawable.Drawable import android.text.TextUtils import android.util.AttributeSet -import android.view.View +import android.view.LayoutInflater import android.view.accessibility.AccessibilityNodeInfo -import androidx.annotation.AttrRes import com.google.android.material.appbar.MaterialToolbar import com.google.android.material.textview.MaterialTextView -import com.lapism.search.R +import com.lapism.search.databinding.MaterialSearchToolbarBinding class MaterialSearchToolbar : MaterialToolbar { - private var textView: MaterialTextView? = null + private var binding: MaterialSearchToolbarBinding constructor(context: Context) : super(context) constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) - constructor(context: Context, attrs: AttributeSet?, @AttrRes defStyleAttr: Int) : super( + constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super( context, attrs, defStyleAttr ) init { - View.inflate(context, R.layout.material_search_toolbar, this) - - textView = findViewById(R.id.search_toolbar_text_view) + val inflater = LayoutInflater.from(context) + binding = MaterialSearchToolbarBinding.inflate(inflater, this, true) } override fun setTitle(title: CharSequence?) { @@ -51,6 +50,12 @@ class MaterialSearchToolbar : MaterialToolbar { } + override fun setNavigationIcon(drawable: Drawable?) { + if (drawable != null) { + super.setNavigationIcon(drawable) + } + } + override fun onInitializeAccessibilityNodeInfo(info: AccessibilityNodeInfo?) { super.onInitializeAccessibilityNodeInfo(info) info?.className = MaterialTextView::class.java.canonicalName @@ -65,19 +70,19 @@ class MaterialSearchToolbar : MaterialToolbar { } fun setText(text: CharSequence?) { - textView?.text = text + binding.searchToolbarTextView.text = text } private fun getText(): CharSequence? { - return textView?.text + return binding.searchToolbarTextView.text } private fun getHint(): CharSequence? { - return textView?.hint + return binding.searchToolbarTextView.hint } fun setHint(hint: CharSequence?) { - textView?.hint = hint + binding.searchToolbarTextView.hint = hint } } \ No newline at end of file diff --git a/src/main/kotlin/com/lapism/search/widget/MaterialSearchView.kt b/src/main/kotlin/com/lapism/search/widget/MaterialSearchView.kt index 4c7ddb60..da4089e4 100755 --- a/src/main/kotlin/com/lapism/search/widget/MaterialSearchView.kt +++ b/src/main/kotlin/com/lapism/search/widget/MaterialSearchView.kt @@ -1,29 +1,30 @@ package com.lapism.search.widget +import android.animation.LayoutTransition import android.content.Context import android.graphics.Rect +import android.graphics.Typeface import android.graphics.drawable.Drawable +import android.os.Build import android.os.Parcel import android.os.Parcelable import android.text.Editable import android.text.TextUtils import android.text.TextWatcher import android.util.AttributeSet +import android.view.LayoutInflater import android.view.View +import android.view.ViewGroup import android.view.inputmethod.InputMethodManager -import android.widget.FrameLayout -import android.widget.ImageButton import android.widget.LinearLayout import androidx.annotation.* import androidx.core.content.ContextCompat import androidx.customview.view.AbsSavedState import androidx.transition.* -import com.google.android.material.appbar.MaterialToolbar import com.lapism.search.R -import com.lapism.search.internal.FocusEditText +import com.lapism.search.databinding.MaterialSearchViewBinding -@Suppress("MemberVisibilityCanBePrivate") class MaterialSearchView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, @@ -31,29 +32,16 @@ class MaterialSearchView @JvmOverloads constructor( defStyleRes: Int = 0 ) : MaterialSearchLayout(context, attrs, defStyleAttr, defStyleRes) { - private var scrim: View? = null - private var background: LinearLayout? = null - private var toolbar: MaterialToolbar? = null - private var editText: FocusEditText? = null - private var clear: ImageButton? = null - private var divider: View? = null - private var container: FrameLayout? = null - + private var binding: MaterialSearchViewBinding private var focusListener: OnFocusChangeListener? = null private var queryListener: OnQueryTextListener? = null private var clearClickListener: OnClearClickListener? = null init { - View.inflate(context, R.layout.material_search_view, this) - - scrim = findViewById(R.id.search_view_scrim) - - background = findViewById(R.id.search_view_background) + val inflater = LayoutInflater.from(getContext()) + binding = MaterialSearchViewBinding.inflate(inflater, this) - toolbar = findViewById(R.id.search_view_toolbar) - - editText = findViewById(R.id.search_view_edit_text) - editText?.addTextChangedListener(object : TextWatcher { + binding.searchViewEditText.addTextChangedListener(object : TextWatcher { override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) { } @@ -66,39 +54,38 @@ class MaterialSearchView @JvmOverloads constructor( } }) - editText?.setOnEditorActionListener { _, _, _ -> + binding.searchViewEditText.setOnEditorActionListener { _, _, _ -> onSubmitQuery() return@setOnEditorActionListener true // true } - editText?.setOnFocusChangeListener { _, hasFocus -> - if (hasFocus) { + binding.searchViewEditText.setOnFocusChangeListener { _, hasFocus -> + visibility = if (hasFocus) { showAnimation() showKeyboard() + View.VISIBLE } else { hideKeyboard() hideAnimation() + View.GONE } focusListener?.onFocusChange(hasFocus) } val left = context.resources.getDimensionPixelSize(R.dimen.search_dp_8) - val params = editText?.layoutParams as? LinearLayout.LayoutParams + val params = binding.searchViewEditText.layoutParams as? MarginLayoutParams params?.setMargins(left, 0, 0, 0) - editText?.layoutParams = params - editText?.isFocusable = true - editText?.isFocusableInTouchMode = true + binding.searchViewEditText.layoutParams = params + + binding.searchViewEditText.isFocusable = true + binding.searchViewEditText.isFocusableInTouchMode = true - clear = findViewById(R.id.search_view_clear_button) - clear?.visibility = View.GONE - clear?.setOnClickListener { + binding.searchViewClearButton.visibility = View.GONE + binding.searchViewClearButton.setOnClickListener { clearClickListener?.onClearClick() - editText?.text?.clear() + binding.searchViewEditText.text?.clear() } - divider = findViewById(R.id.search_view_divider) - container = findViewById(R.id.search_view_content_container) - val a = context.obtainStyledAttributes( attrs, R.styleable.MaterialSearchView, defStyleAttr, defStyleRes ) @@ -115,6 +102,12 @@ class MaterialSearchView @JvmOverloads constructor( } } + if (a.hasValue(R.styleable.MaterialSearchView_search_navigationContentDescription)) { + val description = + a.getText(R.styleable.MaterialSearchView_search_navigationContentDescription) + setNavigationContentDescription(description) + } + if (a.hasValue(R.styleable.MaterialSearchView_search_backgroundColor)) { val color = a.getInt(R.styleable.MaterialSearchView_search_backgroundColor, 0) setBackgroundColor(color) @@ -160,66 +153,97 @@ class MaterialSearchView @JvmOverloads constructor( } a.recycle() + + setTransition() + + visibility = View.GONE + } + + private fun setTransition() { + val mTransition = LayoutTransition() + mTransition.enableTransitionType(LayoutTransition.CHANGING) + mTransition.setDuration(300L) + + binding.searchViewBackground.layoutTransition = mTransition } // TODO ANOTACE A NAZVY PROMENNCYH + @Nullable override fun addView(child: View?) { - container?.addView(child) + binding.searchViewContentContainer.addView(child) } - fun setTextQuery(query: CharSequence?, submit: Boolean) { - editText?.setText(query) + fun setTextQuery(@Nullable query: CharSequence?, submit: Boolean) { + binding.searchViewEditText.setText(query) + if (query != null) { - editText?.setSelection(editText?.length()!!) + binding.searchViewEditText.setSelection(binding.searchViewEditText.length()) } + if (submit && !TextUtils.isEmpty(query)) { onSubmitQuery() } } private fun onSubmitQuery() { - val query = editText?.text + val query = binding.searchViewEditText.text if (query != null && TextUtils.getTrimmedLength(query) > 0) { if (queryListener == null || !queryListener!!.onQueryTextSubmit(query.toString())) { - editText?.text = query + hideKeyboard() } } } + override fun setNavigationContentDescription(resId: Int) { + binding.searchViewToolbar.setNavigationContentDescription(resId) + } + + override fun setNavigationContentDescription(description: CharSequence?) { + binding.searchViewToolbar.navigationContentDescription = description + } + override fun setNavigationOnClickListener(listener: OnClickListener) { - toolbar?.setNavigationOnClickListener(listener) + binding.searchViewToolbar.setNavigationOnClickListener(listener) } override fun setNavigationIcon(drawable: Drawable?) { - toolbar?.navigationIcon = drawable + binding.searchViewToolbar.navigationIcon = drawable } override fun setNavigationIcon(resId: Int) { - toolbar?.setNavigationIcon(resId) + binding.searchViewToolbar.setNavigationIcon(resId) } fun setClearIcon(drawable: Drawable?) { - clear?.setImageDrawable(drawable) + binding.searchViewClearButton.setImageDrawable(drawable) } fun setImeOptions(imeOptions: Int) { - editText?.imeOptions = imeOptions + binding.searchViewEditText.imeOptions = imeOptions } fun setInputType(type: Int) { - editText?.inputType = type + binding.searchViewEditText.inputType = type } fun setTextClearOnBackPressed(clear: Boolean) { - editText?.setTextClearOnBackPressed(clear) + binding.searchViewEditText.setTextClearOnBackPressed(clear) } fun getTextQuery(): Editable? { - return editText?.text + return binding.searchViewEditText.text + } + + fun setTextTypeface(@Nullable typeface: Typeface?) { + binding.searchViewEditText.typeface = typeface + } + + @Nullable + fun getTextTypeface(): Typeface? { + return binding.searchViewEditText.typeface } fun setHint(hint: CharSequence?) { - editText?.hint = hint + binding.searchViewEditText.hint = hint } fun setOnFocusChangeListener(listener: OnFocusChangeListener) { @@ -231,11 +255,19 @@ class MaterialSearchView @JvmOverloads constructor( } private fun showAnimation() { - // TODO + val params = LinearLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.MATCH_PARENT + ) + binding.searchViewContentContainer.layoutParams = params } private fun hideAnimation() { - // TODO + val params = LinearLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.WRAP_CONTENT + ) + binding.searchViewContentContainer.layoutParams = params } fun showKeyboard() { @@ -243,7 +275,7 @@ class MaterialSearchView @JvmOverloads constructor( val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager inputMethodManager.showSoftInput( - editText, + binding.searchViewEditText, InputMethodManager.RESULT_UNCHANGED_SHOWN ) } @@ -254,7 +286,8 @@ class MaterialSearchView @JvmOverloads constructor( val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager inputMethodManager.hideSoftInputFromWindow( - editText?.windowToken, + //TODO binding.searchViewEditText.windowToken, + windowToken, InputMethodManager.RESULT_UNCHANGED_SHOWN ) } @@ -262,32 +295,40 @@ class MaterialSearchView @JvmOverloads constructor( private fun onTextChanged(s: CharSequence) { if (s.isNotEmpty()) { - clear?.visibility = View.VISIBLE + binding.searchViewClearButton.visibility = View.VISIBLE } else { - clear?.visibility = View.GONE + binding.searchViewClearButton.visibility = View.GONE } queryListener?.onQueryTextChange(s) } override fun setBackgroundColor(@ColorInt color: Int) { - background?.setBackgroundColor(color) + binding.searchViewBackground.setBackgroundColor(color) } fun setDividerColor(@ColorInt color: Int) { - divider?.setBackgroundColor(color) + binding.searchViewDivider.setBackgroundColor(color) } fun setScrimColor(@ColorInt color: Int) { - scrim?.setBackgroundColor(color) + binding.searchViewScrim.setBackgroundColor(color) } override fun requestFocus(direction: Int, previouslyFocusedRect: Rect?): Boolean { - return editText?.requestFocus(direction, previouslyFocusedRect)!! + return binding.searchViewEditText.requestFocus(direction, previouslyFocusedRect) } override fun clearFocus() { - editText?.clearFocus() + binding.searchViewEditText.clearFocus() + } + + fun setTextHintColor(color: Int) { + binding.searchViewEditText.setHintTextColor(color) + } + + fun setTextColor(color: Int) { + binding.searchViewEditText.setTextColor(color) } override fun onSaveInstanceState(): Parcelable? { @@ -295,6 +336,7 @@ class MaterialSearchView @JvmOverloads constructor( superState?.let { val state = SavedState(it) state.text = getTextQuery().toString() + state.focus = binding.searchViewEditText.hasFocus() return state } ?: run { return superState @@ -305,6 +347,9 @@ class MaterialSearchView @JvmOverloads constructor( if (state is SavedState) { super.onRestoreInstanceState(state.superState) setTextQuery(state.text, false) + if (state.focus) { + binding.searchViewEditText.requestFocus() + } } else { super.onRestoreInstanceState(state) } @@ -331,16 +376,27 @@ class MaterialSearchView @JvmOverloads constructor( class SavedState : AbsSavedState { var text: String? = null + var focus: Boolean = false constructor(superState: Parcelable) : super(superState) constructor(source: Parcel, loader: ClassLoader? = null) : super(source, loader) { text = source.readString() + focus = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + source.readBoolean() + } else { + source.readInt() == 1 + } } override fun writeToParcel(dest: Parcel, flags: Int) { super.writeToParcel(dest, flags) dest.writeString(text) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + dest.writeBoolean(focus) + } else { + dest.writeInt(if (focus) 1 else 0) + } } companion object { diff --git a/src/main/res/layout/material_search_toolbar.xml b/src/main/res/layout/material_search_toolbar.xml index 2eaa342e..8538bc0a 100755 --- a/src/main/res/layout/material_search_toolbar.xml +++ b/src/main/res/layout/material_search_toolbar.xml @@ -1,9 +1,8 @@ - + android:clipChildren="false"> + @@ -22,6 +23,7 @@ + diff --git a/src/main/res/values/public.xml b/src/main/res/values/public.xml index 5276bb6d..b8a443c7 100755 --- a/src/main/res/values/public.xml +++ b/src/main/res/values/public.xml @@ -3,6 +3,7 @@ + From 554c7a5c41869353bdf222f06a5960b5cde8b1b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Lapi=C5=A1?= Date: Wed, 6 Oct 2021 22:13:17 +0200 Subject: [PATCH 2/8] Many many fixes --- README.md | 96 ++++++++++++++++++- ...out.kt => ClippableRoundedCornerLayout.kt} | 2 +- .../lapism/search/widget/MaterialSearchBar.kt | 83 ++++++++-------- .../search/widget/MaterialSearchView.kt | 31 ------ src/main/res/layout/material_search_bar.xml | 11 ++- src/main/res/layout/material_search_view.xml | 16 ++-- src/main/res/values/attrs.xml | 2 - src/main/res/values/dimens.xml | 17 +--- src/main/res/values/styles.xml | 12 --- 9 files changed, 151 insertions(+), 119 deletions(-) rename src/main/kotlin/com/lapism/search/internal/{ClippableFrameLayout.kt => ClippableRoundedCornerLayout.kt} (91%) delete mode 100755 src/main/res/values/styles.xml diff --git a/README.md b/README.md index dc1531b0..a4771e07 100755 --- a/README.md +++ b/README.md @@ -41,19 +41,107 @@ Add the dependency to your gradle file: ### MaterialSearchBar ```java - + val toolbar = binding.materialSearchBar.getToolbar() + setSupportActionBar(toolbar) + + binding.materialSearchBar.apply { + navigationIconCompat = NavigationIconCompat.SEARCH + setHint(getString(R.string.search)) + setOnClickListener { + binding.materialSearchView.requestFocus() + } + setNavigationOnClickListener { + binding.materialSearchView.requestFocus() + } + } ``` ### MaterialSearchView ```java - + binding.materialSearchView.apply { + addView(recyclerView) + navigationIconCompat = NavigationIconCompat.ARROW + setNavigationOnClickListener { + binding.materialSearchView.clearFocus() + } + setHint(getString(R.string.search)) + setBackgroundColor( + ContextCompat.getColor( + this@MainActivity, + R.color.color_surface + ) + ) + setOnQueryTextListener(object : MaterialSearchView.OnQueryTextListener { + override fun onQueryTextChange(newText: CharSequence): Boolean { + adapter.filter(newText) + return true + } + + override fun onQueryTextSubmit(query: CharSequence): Boolean { + return true + } + }) + setOnFocusChangeListener(object : MaterialSearchView.OnFocusChangeListener { + override fun onFocusChange(hasFocus: Boolean) { + + } + }) + } ``` ### Layout You have to use app theme Theme.Material3.* or Theme.MaterialComponents.*. ```xml - + + + + + + + + + + + + + + + + + ``` ### XML attributes @@ -68,7 +156,6 @@ You have to use app theme Theme.Material3.* or Theme.MaterialComponents.*. - @@ -85,7 +172,6 @@ You have to use app theme Theme.Material3.* or Theme.MaterialComponents.*. - diff --git a/src/main/kotlin/com/lapism/search/internal/ClippableFrameLayout.kt b/src/main/kotlin/com/lapism/search/internal/ClippableRoundedCornerLayout.kt similarity index 91% rename from src/main/kotlin/com/lapism/search/internal/ClippableFrameLayout.kt rename to src/main/kotlin/com/lapism/search/internal/ClippableRoundedCornerLayout.kt index 12281509..732bf69d 100644 --- a/src/main/kotlin/com/lapism/search/internal/ClippableFrameLayout.kt +++ b/src/main/kotlin/com/lapism/search/internal/ClippableRoundedCornerLayout.kt @@ -7,7 +7,7 @@ import android.util.AttributeSet import android.widget.FrameLayout -class ClippableFrameLayout @JvmOverloads constructor( +class ClippableRoundedCornerLayout @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, diff --git a/src/main/kotlin/com/lapism/search/widget/MaterialSearchBar.kt b/src/main/kotlin/com/lapism/search/widget/MaterialSearchBar.kt index 90442b26..d1c8d4bd 100755 --- a/src/main/kotlin/com/lapism/search/widget/MaterialSearchBar.kt +++ b/src/main/kotlin/com/lapism/search/widget/MaterialSearchBar.kt @@ -5,14 +5,15 @@ import android.content.res.TypedArray import android.graphics.Color import android.graphics.drawable.Drawable import android.util.AttributeSet +import android.view.LayoutInflater import android.view.View import androidx.annotation.ColorInt import androidx.annotation.Nullable import androidx.coordinatorlayout.widget.CoordinatorLayout import androidx.core.view.ViewCompat import com.google.android.material.appbar.AppBarLayout -import com.google.android.material.card.MaterialCardView import com.lapism.search.R +import com.lapism.search.databinding.MaterialSearchBarBinding class MaterialSearchBar @JvmOverloads constructor( @@ -22,15 +23,12 @@ class MaterialSearchBar @JvmOverloads constructor( defStyleRes: Int = 0 ) : MaterialSearchLayout(context, attrs, defStyleAttr, defStyleRes) { - private var card: MaterialCardView? = null - private var toolbar: MaterialSearchToolbar? = null + private var binding: MaterialSearchBarBinding private var a: TypedArray? = null init { - View.inflate(context, R.layout.material_search_bar, this) - - card = findViewById(R.id.search_bar_card) - toolbar = findViewById(R.id.search_bar_toolbar) + val inflater = LayoutInflater.from(getContext()) + binding = MaterialSearchBarBinding.inflate(inflater, this) a = context.obtainStyledAttributes( attrs, R.styleable.MaterialSearchBar, defStyleAttr, defStyleRes @@ -59,11 +57,11 @@ class MaterialSearchBar @JvmOverloads constructor( setBackgroundColor(color!!) } - val defaultRadius = context.resources.getDimensionPixelSize(R.dimen.search_bar_radius) + val defaultRadius = context.resources.getDimensionPixelSize(R.dimen.search_dp_24) val customRadius = a?.getInt(R.styleable.MaterialSearchBar_search_radius, defaultRadius) setRadius(customRadius?.toFloat()!!) - val defaultElevation = context.resources.getDimensionPixelSize(R.dimen.search_bar_elevation) + val defaultElevation = context.resources.getDimensionPixelSize(R.dimen.search_dp_0) val customElevation = a?.getInt(R.styleable.MaterialSearchBar_android_elevation, defaultElevation) elevation = customElevation?.toFloat()!! @@ -74,91 +72,91 @@ class MaterialSearchBar @JvmOverloads constructor( } } - fun getToolbar(): MaterialSearchToolbar? { - return toolbar + fun getToolbar(): MaterialSearchToolbar { + return binding.searchBarToolbar } fun setText(text: CharSequence?) { - toolbar?.setText(text) + binding.searchBarToolbar.setText(text) } fun setHint(hint: CharSequence?) { - toolbar?.setHint(hint) + binding.searchBarToolbar.setHint(hint) } override fun setNavigationOnClickListener(listener: OnClickListener) { - toolbar?.setNavigationOnClickListener(listener) + binding.searchBarToolbar.setNavigationOnClickListener(listener) } override fun setNavigationIcon(@Nullable drawable: Drawable?) { - toolbar?.navigationIcon = drawable + binding.searchBarToolbar.navigationIcon = drawable } override fun setNavigationIcon(resId: Int) { - toolbar?.setNavigationIcon(resId) + binding.searchBarToolbar.setNavigationIcon(resId) } override fun setNavigationContentDescription(resId: Int) { - toolbar?.setNavigationContentDescription(resId) + binding.searchBarToolbar.setNavigationContentDescription(resId) } override fun setNavigationContentDescription(description: CharSequence?) { - toolbar?.navigationContentDescription = description + binding.searchBarToolbar.navigationContentDescription = description } override fun setOnClickListener(@Nullable l: OnClickListener?) { - toolbar?.setOnClickListener(l) + binding.searchBarToolbar.setOnClickListener(l) } - override fun setElevation(elevation: Float) { - card?.cardElevation = elevation - } - override fun getElevation(): Float { - return card?.elevation!! - } + /* + fun setForegroundColor(){ -/* - fun setForegroundColor(){ + } - } + fun setBackgroundColor(){ - fun setBackgroundColor(){ + } - } + fun setStrokeWidth(){ - fun setStrokeWidth(){ + } + fun setStrokeColor{ + card?.setStrokeColor() + } + */ + override fun setElevation(elevation: Float) { + binding.searchBarCard.cardElevation = elevation } - fun setStrokeColor{ - card?.setStrokeColor() + override fun getElevation(): Float { + return binding.searchBarCard.elevation!! } -*/ override fun setBackgroundColor(@ColorInt color: Int) { - card?.setCardBackgroundColor(color) + binding.searchBarCard.setCardBackgroundColor(color) } private fun setMargins(left: Int, top: Int, right: Int, bottom: Int) { - if (card?.layoutParams is MarginLayoutParams) { - val params = card?.layoutParams as? MarginLayoutParams + if (binding.searchBarCard.layoutParams is MarginLayoutParams) { + val params = binding.searchBarCard.layoutParams as? MarginLayoutParams params?.setMargins(left, top, right, bottom) - card?.layoutParams = params + binding.searchBarCard.layoutParams = params } } private fun setRadius(radius: Float) { - card?.radius = radius + binding.searchBarCard.radius = radius } override fun onAttachedToWindow() { super.onAttachedToWindow() val defaultMarginsStartEnd = - context.resources.getDimensionPixelSize(R.dimen.search_bar_margins_start_end) + context.resources.getDimensionPixelSize(R.dimen.search_dp_16) val defaultMarginsTopBottom = - context.resources.getDimensionPixelSize(R.dimen.search_bar_margins_top_bottom) + context.resources.getDimensionPixelSize(R.dimen.search_dp_8) val customMarginsStart = a?.getDimensionPixelSize( R.styleable.MaterialSearchBar_android_layout_marginStart, @@ -197,8 +195,7 @@ class MaterialSearchBar @JvmOverloads constructor( super.onDependentViewChanged(parent, child, dependency) if (dependency is AppBarLayout) { dependency.setBackgroundColor(Color.TRANSPARENT) - // dependency.elevation = 0.0f - // dependency.stateListAnimator = null + dependency.stateListAnimator = null ViewCompat.setElevation(dependency, 0.0f) } return false diff --git a/src/main/kotlin/com/lapism/search/widget/MaterialSearchView.kt b/src/main/kotlin/com/lapism/search/widget/MaterialSearchView.kt index da4089e4..e7246bf3 100755 --- a/src/main/kotlin/com/lapism/search/widget/MaterialSearchView.kt +++ b/src/main/kotlin/com/lapism/search/widget/MaterialSearchView.kt @@ -1,6 +1,5 @@ package com.lapism.search.widget -import android.animation.LayoutTransition import android.content.Context import android.graphics.Rect import android.graphics.Typeface @@ -14,9 +13,7 @@ import android.text.TextWatcher import android.util.AttributeSet import android.view.LayoutInflater import android.view.View -import android.view.ViewGroup import android.view.inputmethod.InputMethodManager -import android.widget.LinearLayout import androidx.annotation.* import androidx.core.content.ContextCompat import androidx.customview.view.AbsSavedState @@ -60,12 +57,10 @@ class MaterialSearchView @JvmOverloads constructor( } binding.searchViewEditText.setOnFocusChangeListener { _, hasFocus -> visibility = if (hasFocus) { - showAnimation() showKeyboard() View.VISIBLE } else { hideKeyboard() - hideAnimation() View.GONE } @@ -154,19 +149,9 @@ class MaterialSearchView @JvmOverloads constructor( a.recycle() - setTransition() - visibility = View.GONE } - private fun setTransition() { - val mTransition = LayoutTransition() - mTransition.enableTransitionType(LayoutTransition.CHANGING) - mTransition.setDuration(300L) - - binding.searchViewBackground.layoutTransition = mTransition - } - // TODO ANOTACE A NAZVY PROMENNCYH + @Nullable override fun addView(child: View?) { binding.searchViewContentContainer.addView(child) @@ -254,22 +239,6 @@ class MaterialSearchView @JvmOverloads constructor( queryListener = listener } - private fun showAnimation() { - val params = LinearLayout.LayoutParams( - ViewGroup.LayoutParams.MATCH_PARENT, - ViewGroup.LayoutParams.MATCH_PARENT - ) - binding.searchViewContentContainer.layoutParams = params - } - - private fun hideAnimation() { - val params = LinearLayout.LayoutParams( - ViewGroup.LayoutParams.MATCH_PARENT, - ViewGroup.LayoutParams.WRAP_CONTENT - ) - binding.searchViewContentContainer.layoutParams = params - } - fun showKeyboard() { if (!isInEditMode) { val inputMethodManager = diff --git a/src/main/res/layout/material_search_bar.xml b/src/main/res/layout/material_search_bar.xml index 0554379a..bfd76fa9 100755 --- a/src/main/res/layout/material_search_bar.xml +++ b/src/main/res/layout/material_search_bar.xml @@ -1,5 +1,6 @@ - + + android:layout_height="@dimen/search_dp_48" + android:foreground="?attr/selectableItemBackground" + android:minHeight="@dimen/search_dp_48" + android:paddingStart="@dimen/search_dp_4" + android:paddingEnd="@dimen/search_dp_0" + app:maxButtonHeight="@dimen/search_dp_48" /> diff --git a/src/main/res/layout/material_search_view.xml b/src/main/res/layout/material_search_view.xml index cb93df2c..910b25d3 100755 --- a/src/main/res/layout/material_search_view.xml +++ b/src/main/res/layout/material_search_view.xml @@ -1,5 +1,6 @@ - + - @@ -39,14 +40,15 @@ android:layout_weight="1" android:background="@null" android:gravity="start|center_vertical" - android:hint="@null" android:imeOptions="actionSearch|flagNoExtractUi" + android:importantForAutofill="no" android:inputType="textNoSuggestions" android:privateImeOptions="nm" android:textColor="?android:attr/textColorPrimary" android:textColorHint="?android:attr/textColorSecondary" android:textDirection="firstStrong" - android:textSize="@dimen/search_sp_16" /> + android:textSize="@dimen/search_sp_16" + tools:ignore="LabelFor" /> - + \ No newline at end of file diff --git a/src/main/res/values/attrs.xml b/src/main/res/values/attrs.xml index 7e4f11d2..eb9c52c0 100755 --- a/src/main/res/values/attrs.xml +++ b/src/main/res/values/attrs.xml @@ -11,7 +11,6 @@ - @@ -28,7 +27,6 @@ - diff --git a/src/main/res/values/dimens.xml b/src/main/res/values/dimens.xml index afe65c37..95a44343 100755 --- a/src/main/res/values/dimens.xml +++ b/src/main/res/values/dimens.xml @@ -2,6 +2,8 @@ 0dp + 1dp + 4dp 8dp 16dp 24dp @@ -9,19 +11,4 @@ 16sp - 16dp - 8dp - - 0dp - 48dp - 24dp - 16dp - 8dp - - 1dp - - - 4dp - 16dp - \ No newline at end of file diff --git a/src/main/res/values/styles.xml b/src/main/res/values/styles.xml deleted file mode 100755 index 0eb097e7..00000000 --- a/src/main/res/values/styles.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - \ No newline at end of file From cff927026d31171660b81f24874602674180e5ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Lapi=C5=A1?= Date: Wed, 6 Oct 2021 22:14:48 +0200 Subject: [PATCH 3/8] Readme fix --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a4771e07..210d465f 100755 --- a/README.md +++ b/README.md @@ -35,8 +35,8 @@ Add the dependency to your gradle file: ## Usage ```java - binding.materialSearchView.requestFocus() - binding.materialSearchView.clearFocus() + binding.materialSearchView.requestFocus() + binding.materialSearchView.clearFocus() ``` ### MaterialSearchBar From ae067d3ced1d0c56f0ccce362878d1a314586743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Lapi=C5=A1?= Date: Wed, 6 Oct 2021 22:15:22 +0200 Subject: [PATCH 4/8] Readme fix --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 210d465f..012f8ff1 100755 --- a/README.md +++ b/README.md @@ -23,14 +23,14 @@ Add the dependency to your gradle file: ```groovy - repositories { - google() - mavenCentral() - } - - dependencies { - implementation 'io.github.lapism:search:1.1.0' - } + repositories { + google() + mavenCentral() + } + + dependencies { + implementation 'io.github.lapism:search:1.1.0' + } ``` ## Usage From 4fa7a98862a67853b6923716d32ea128b1eb0565 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Lapi=C5=A1?= Date: Wed, 13 Oct 2021 13:14:40 +0200 Subject: [PATCH 5/8] MaterialSearchLayout --- README.md | 2 +- build.gradle | 2 - .../lapism/search/widget/MaterialSearchBar.kt | 4 +- .../search/widget/MaterialSearchLayout.kt | 8 +- .../search/widget/MaterialSearchToolbar.kt | 15 +- .../search/widget/MaterialSearchView.kt | 161 +++++++++++------- .../search/widget/NavigationIconCompat.kt | 11 +- .../search_ic_outline_arrow_back_24.xml | 0 .../drawable/search_ic_outline_clear_24.xml | 0 .../drawable/search_ic_outline_search_24.xml | 0 10 files changed, 115 insertions(+), 88 deletions(-) mode change 100755 => 100644 src/main/res/drawable/search_ic_outline_arrow_back_24.xml mode change 100755 => 100644 src/main/res/drawable/search_ic_outline_clear_24.xml mode change 100755 => 100644 src/main/res/drawable/search_ic_outline_search_24.xml diff --git a/README.md b/README.md index 012f8ff1..43228ab8 100755 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ Add the dependency to your gradle file: } ``` -### Layout +### Layout You have to use app theme Theme.Material3.* or Theme.MaterialComponents.*. ```xml diff --git a/build.gradle b/build.gradle index 77562a0b..dbbf8ae2 100644 --- a/build.gradle +++ b/build.gradle @@ -10,8 +10,6 @@ android { defaultConfig { minSdk 26 targetSdk 31 - versionCode 1010000 - versionName "1.1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles "consumer-rules.pro" } diff --git a/src/main/kotlin/com/lapism/search/widget/MaterialSearchBar.kt b/src/main/kotlin/com/lapism/search/widget/MaterialSearchBar.kt index d1c8d4bd..71bc2c55 100755 --- a/src/main/kotlin/com/lapism/search/widget/MaterialSearchBar.kt +++ b/src/main/kotlin/com/lapism/search/widget/MaterialSearchBar.kt @@ -108,7 +108,6 @@ class MaterialSearchBar @JvmOverloads constructor( binding.searchBarToolbar.setOnClickListener(l) } - /* fun setForegroundColor(){ @@ -126,12 +125,13 @@ class MaterialSearchBar @JvmOverloads constructor( card?.setStrokeColor() } */ + override fun setElevation(elevation: Float) { binding.searchBarCard.cardElevation = elevation } override fun getElevation(): Float { - return binding.searchBarCard.elevation!! + return binding.searchBarCard.elevation } override fun setBackgroundColor(@ColorInt color: Int) { diff --git a/src/main/kotlin/com/lapism/search/widget/MaterialSearchLayout.kt b/src/main/kotlin/com/lapism/search/widget/MaterialSearchLayout.kt index c64bb1c2..0a579225 100644 --- a/src/main/kotlin/com/lapism/search/widget/MaterialSearchLayout.kt +++ b/src/main/kotlin/com/lapism/search/widget/MaterialSearchLayout.kt @@ -4,7 +4,9 @@ import android.content.Context import android.graphics.drawable.Drawable import android.util.AttributeSet import android.widget.FrameLayout +import androidx.annotation.DrawableRes import androidx.annotation.Nullable +import androidx.annotation.StringRes import androidx.core.content.ContextCompat import com.lapism.search.R @@ -45,13 +47,13 @@ abstract class MaterialSearchLayout @JvmOverloads constructor( } } - abstract fun setNavigationIcon(resId: Int) + abstract fun setNavigationIcon(@DrawableRes resId: Int) abstract fun setNavigationIcon(@Nullable drawable: Drawable?) - abstract fun setNavigationContentDescription(resId: Int) + abstract fun setNavigationContentDescription(@StringRes resId: Int) - abstract fun setNavigationContentDescription(description: CharSequence?) + abstract fun setNavigationContentDescription(@Nullable description: CharSequence?) abstract fun setNavigationOnClickListener(listener: OnClickListener) diff --git a/src/main/kotlin/com/lapism/search/widget/MaterialSearchToolbar.kt b/src/main/kotlin/com/lapism/search/widget/MaterialSearchToolbar.kt index b02c5f87..eef35482 100755 --- a/src/main/kotlin/com/lapism/search/widget/MaterialSearchToolbar.kt +++ b/src/main/kotlin/com/lapism/search/widget/MaterialSearchToolbar.kt @@ -6,8 +6,8 @@ import android.text.TextUtils import android.util.AttributeSet import android.view.LayoutInflater import android.view.accessibility.AccessibilityNodeInfo +import android.widget.TextView import com.google.android.material.appbar.MaterialToolbar -import com.google.android.material.textview.MaterialTextView import com.lapism.search.databinding.MaterialSearchToolbarBinding @@ -30,19 +30,19 @@ class MaterialSearchToolbar : MaterialToolbar { binding = MaterialSearchToolbarBinding.inflate(inflater, this, true) } - override fun setTitle(title: CharSequence?) { + override fun setTitle(resId: Int) { } - override fun setTitle(resId: Int) { + override fun setTitle(title: CharSequence?) { } - override fun setSubtitle(subtitle: CharSequence?) { + override fun setSubtitle(resId: Int) { } - override fun setSubtitle(resId: Int) { + override fun setSubtitle(subtitle: CharSequence?) { } @@ -58,7 +58,7 @@ class MaterialSearchToolbar : MaterialToolbar { override fun onInitializeAccessibilityNodeInfo(info: AccessibilityNodeInfo?) { super.onInitializeAccessibilityNodeInfo(info) - info?.className = MaterialTextView::class.java.canonicalName + info?.className = TextView::class.java.canonicalName var text: CharSequence? = getText() val isEmpty = TextUtils.isEmpty(text) info?.hintText = getHint() @@ -69,6 +69,9 @@ class MaterialSearchToolbar : MaterialToolbar { info?.text = text } + + + // todo DODELAT **************************************************************************************** fun setText(text: CharSequence?) { binding.searchToolbarTextView.text = text } diff --git a/src/main/kotlin/com/lapism/search/widget/MaterialSearchView.kt b/src/main/kotlin/com/lapism/search/widget/MaterialSearchView.kt index e7246bf3..577d20bb 100755 --- a/src/main/kotlin/com/lapism/search/widget/MaterialSearchView.kt +++ b/src/main/kotlin/com/lapism/search/widget/MaterialSearchView.kt @@ -152,37 +152,20 @@ class MaterialSearchView @JvmOverloads constructor( visibility = View.GONE } - // TODO ANOTACE A NAZVY PROMENNCYH + @Nullable - override fun addView(child: View?) { - binding.searchViewContentContainer.addView(child) - } - - fun setTextQuery(@Nullable query: CharSequence?, submit: Boolean) { - binding.searchViewEditText.setText(query) - - if (query != null) { - binding.searchViewEditText.setSelection(binding.searchViewEditText.length()) - } - - if (submit && !TextUtils.isEmpty(query)) { - onSubmitQuery() - } + // ********************************************************************************************* + override fun setNavigationIcon(@DrawableRes resId: Int) { + binding.searchViewToolbar.setNavigationIcon(resId) } - private fun onSubmitQuery() { - val query = binding.searchViewEditText.text - if (query != null && TextUtils.getTrimmedLength(query) > 0) { - if (queryListener == null || !queryListener!!.onQueryTextSubmit(query.toString())) { - hideKeyboard() - } - } + override fun setNavigationIcon(@Nullable drawable: Drawable?) { + binding.searchViewToolbar.navigationIcon = drawable } - override fun setNavigationContentDescription(resId: Int) { + override fun setNavigationContentDescription(@StringRes resId: Int) { binding.searchViewToolbar.setNavigationContentDescription(resId) } - override fun setNavigationContentDescription(description: CharSequence?) { + override fun setNavigationContentDescription(@Nullable description: CharSequence?) { binding.searchViewToolbar.navigationContentDescription = description } @@ -190,14 +173,76 @@ class MaterialSearchView @JvmOverloads constructor( binding.searchViewToolbar.setNavigationOnClickListener(listener) } - override fun setNavigationIcon(drawable: Drawable?) { - binding.searchViewToolbar.navigationIcon = drawable + // ********************************************************************************************* + override fun addView(child: View?) { + binding.searchViewContentContainer.addView(child) } - override fun setNavigationIcon(resId: Int) { - binding.searchViewToolbar.setNavigationIcon(resId) + override fun setBackgroundColor(color: Int) { + binding.searchViewBackground.setBackgroundColor(color) + } + + override fun requestFocus(direction: Int, previouslyFocusedRect: Rect?): Boolean { + return binding.searchViewEditText.requestFocus(direction, previouslyFocusedRect) + } + + override fun clearFocus() { + binding.searchViewEditText.clearFocus() + } + + + + + + + + + // TODO ANOTACE A NAZVY PROMENNCYH + @Nullable + + + + + + + + + + + + + + + override fun onSaveInstanceState(): Parcelable? { + val superState: Parcelable? = super.onSaveInstanceState() + superState?.let { + val state = SavedState(it) + state.text = getTextQuery().toString() + state.focus = binding.searchViewEditText.hasFocus() + return state + } ?: run { + return superState + } + } + + override fun onRestoreInstanceState(state: Parcelable?) { + if (state is SavedState) { + super.onRestoreInstanceState(state.superState) + setTextQuery(state.text, false) + if (state.focus) { + binding.searchViewEditText.requestFocus() + } + } else { + super.onRestoreInstanceState(state) + } } + + + + + + + // ********************************************************************************************* fun setClearIcon(drawable: Drawable?) { binding.searchViewClearButton.setImageDrawable(drawable) } @@ -218,6 +263,27 @@ class MaterialSearchView @JvmOverloads constructor( return binding.searchViewEditText.text } + fun setTextQuery(@Nullable query: CharSequence?, submit: Boolean) { + binding.searchViewEditText.setText(query) + + if (query != null) { + binding.searchViewEditText.setSelection(binding.searchViewEditText.length()) + } + + if (submit && !TextUtils.isEmpty(query)) { + onSubmitQuery() + } + } + + private fun onSubmitQuery() { + val query = binding.searchViewEditText.text + if (query != null && TextUtils.getTrimmedLength(query) > 0) { + if (queryListener == null || !queryListener!!.onQueryTextSubmit(query.toString())) { + hideKeyboard() + } + } + } + fun setTextTypeface(@Nullable typeface: Typeface?) { binding.searchViewEditText.typeface = typeface } @@ -255,7 +321,7 @@ class MaterialSearchView @JvmOverloads constructor( val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager inputMethodManager.hideSoftInputFromWindow( - //TODO binding.searchViewEditText.windowToken, + // TODO binding.searchViewEditText.windowToken ? windowToken, InputMethodManager.RESULT_UNCHANGED_SHOWN ) @@ -272,10 +338,6 @@ class MaterialSearchView @JvmOverloads constructor( queryListener?.onQueryTextChange(s) } - override fun setBackgroundColor(@ColorInt color: Int) { - binding.searchViewBackground.setBackgroundColor(color) - } - fun setDividerColor(@ColorInt color: Int) { binding.searchViewDivider.setBackgroundColor(color) } @@ -284,13 +346,6 @@ class MaterialSearchView @JvmOverloads constructor( binding.searchViewScrim.setBackgroundColor(color) } - override fun requestFocus(direction: Int, previouslyFocusedRect: Rect?): Boolean { - return binding.searchViewEditText.requestFocus(direction, previouslyFocusedRect) - } - - override fun clearFocus() { - binding.searchViewEditText.clearFocus() - } fun setTextHintColor(color: Int) { binding.searchViewEditText.setHintTextColor(color) @@ -300,30 +355,7 @@ class MaterialSearchView @JvmOverloads constructor( binding.searchViewEditText.setTextColor(color) } - override fun onSaveInstanceState(): Parcelable? { - val superState: Parcelable? = super.onSaveInstanceState() - superState?.let { - val state = SavedState(it) - state.text = getTextQuery().toString() - state.focus = binding.searchViewEditText.hasFocus() - return state - } ?: run { - return superState - } - } - - override fun onRestoreInstanceState(state: Parcelable?) { - if (state is SavedState) { - super.onRestoreInstanceState(state.superState) - setTextQuery(state.text, false) - if (state.focus) { - binding.searchViewEditText.requestFocus() - } - } else { - super.onRestoreInstanceState(state) - } - } - + // ********************************************************************************************* interface OnFocusChangeListener { fun onFocusChange(hasFocus: Boolean) @@ -341,6 +373,7 @@ class MaterialSearchView @JvmOverloads constructor( fun onClearClick() } + // ********************************************************************************************* @Suppress("unused") class SavedState : AbsSavedState { diff --git a/src/main/kotlin/com/lapism/search/widget/NavigationIconCompat.kt b/src/main/kotlin/com/lapism/search/widget/NavigationIconCompat.kt index 6fa0f4b9..cfed76dd 100644 --- a/src/main/kotlin/com/lapism/search/widget/NavigationIconCompat.kt +++ b/src/main/kotlin/com/lapism/search/widget/NavigationIconCompat.kt @@ -15,13 +15,4 @@ annotation class NavigationIconCompat { const val ARROW = 1 const val SEARCH = 2 } -} - -/* TODO - ANOTACE, - NOVE IKONKY, - PORADEK V KNIHOVNE - - function: () -> Unit - function: Function - */ \ No newline at end of file +} \ No newline at end of file diff --git a/src/main/res/drawable/search_ic_outline_arrow_back_24.xml b/src/main/res/drawable/search_ic_outline_arrow_back_24.xml old mode 100755 new mode 100644 diff --git a/src/main/res/drawable/search_ic_outline_clear_24.xml b/src/main/res/drawable/search_ic_outline_clear_24.xml old mode 100755 new mode 100644 diff --git a/src/main/res/drawable/search_ic_outline_search_24.xml b/src/main/res/drawable/search_ic_outline_search_24.xml old mode 100755 new mode 100644 From 762075c6d248050905ee9073fa3dddcc46d4d189 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Lapi=C5=A1?= Date: Fri, 15 Oct 2021 14:56:08 +0200 Subject: [PATCH 6/8] Code cleanup 1 --- .../internal/ClippableRoundedCornerLayout.kt | 2 +- .../lapism/search/widget/MaterialSearchBar.kt | 122 ++++++++++-------- .../search/widget/MaterialSearchView.kt | 58 +++------ 3 files changed, 85 insertions(+), 97 deletions(-) diff --git a/src/main/kotlin/com/lapism/search/internal/ClippableRoundedCornerLayout.kt b/src/main/kotlin/com/lapism/search/internal/ClippableRoundedCornerLayout.kt index 732bf69d..b4d9bb4a 100644 --- a/src/main/kotlin/com/lapism/search/internal/ClippableRoundedCornerLayout.kt +++ b/src/main/kotlin/com/lapism/search/internal/ClippableRoundedCornerLayout.kt @@ -14,7 +14,7 @@ class ClippableRoundedCornerLayout @JvmOverloads constructor( defStyleRes: Int = 0 ) : FrameLayout(context, attrs, defStyleAttr, defStyleRes) { - private var path: Path? = null + var path: Path? = null override fun dispatchDraw(canvas: Canvas?) { if (path == null) { diff --git a/src/main/kotlin/com/lapism/search/widget/MaterialSearchBar.kt b/src/main/kotlin/com/lapism/search/widget/MaterialSearchBar.kt index 71bc2c55..ea13ee93 100755 --- a/src/main/kotlin/com/lapism/search/widget/MaterialSearchBar.kt +++ b/src/main/kotlin/com/lapism/search/widget/MaterialSearchBar.kt @@ -8,7 +8,9 @@ import android.util.AttributeSet import android.view.LayoutInflater import android.view.View import androidx.annotation.ColorInt +import androidx.annotation.DrawableRes import androidx.annotation.Nullable +import androidx.annotation.StringRes import androidx.coordinatorlayout.widget.CoordinatorLayout import androidx.core.view.ViewCompat import com.google.android.material.appbar.AppBarLayout @@ -23,9 +25,11 @@ class MaterialSearchBar @JvmOverloads constructor( defStyleRes: Int = 0 ) : MaterialSearchLayout(context, attrs, defStyleAttr, defStyleRes) { + // ********************************************************************************************* private var binding: MaterialSearchBarBinding private var a: TypedArray? = null + // ********************************************************************************************* init { val inflater = LayoutInflater.from(getContext()) binding = MaterialSearchBarBinding.inflate(inflater, this) @@ -72,59 +76,35 @@ class MaterialSearchBar @JvmOverloads constructor( } } - fun getToolbar(): MaterialSearchToolbar { - return binding.searchBarToolbar - } - - fun setText(text: CharSequence?) { - binding.searchBarToolbar.setText(text) - } - - fun setHint(hint: CharSequence?) { - binding.searchBarToolbar.setHint(hint) - } - - override fun setNavigationOnClickListener(listener: OnClickListener) { - binding.searchBarToolbar.setNavigationOnClickListener(listener) + // ********************************************************************************************* + override fun setNavigationIcon(@DrawableRes resId: Int) { + binding.searchBarToolbar.setNavigationIcon(resId) } override fun setNavigationIcon(@Nullable drawable: Drawable?) { binding.searchBarToolbar.navigationIcon = drawable } - override fun setNavigationIcon(resId: Int) { - binding.searchBarToolbar.setNavigationIcon(resId) - } - - override fun setNavigationContentDescription(resId: Int) { + override fun setNavigationContentDescription(@StringRes resId: Int) { binding.searchBarToolbar.setNavigationContentDescription(resId) } - override fun setNavigationContentDescription(description: CharSequence?) { + override fun setNavigationContentDescription(@Nullable description: CharSequence?) { binding.searchBarToolbar.navigationContentDescription = description } - override fun setOnClickListener(@Nullable l: OnClickListener?) { - binding.searchBarToolbar.setOnClickListener(l) + override fun setNavigationOnClickListener(listener: OnClickListener) { + binding.searchBarToolbar.setNavigationOnClickListener(listener) } - /* - fun setForegroundColor(){ - - } - - fun setBackgroundColor(){ - - } - - fun setStrokeWidth(){ - - } + // ********************************************************************************************* + override fun setBackgroundColor(@ColorInt color: Int) { + binding.searchBarCard.setCardBackgroundColor(color) + } - fun setStrokeColor{ - card?.setStrokeColor() - } - */ + override fun setOnClickListener(@Nullable l: OnClickListener?) { + binding.searchBarToolbar.setOnClickListener(l) + } override fun setElevation(elevation: Float) { binding.searchBarCard.cardElevation = elevation @@ -134,22 +114,6 @@ class MaterialSearchBar @JvmOverloads constructor( return binding.searchBarCard.elevation } - override fun setBackgroundColor(@ColorInt color: Int) { - binding.searchBarCard.setCardBackgroundColor(color) - } - - private fun setMargins(left: Int, top: Int, right: Int, bottom: Int) { - if (binding.searchBarCard.layoutParams is MarginLayoutParams) { - val params = binding.searchBarCard.layoutParams as? MarginLayoutParams - params?.setMargins(left, top, right, bottom) - binding.searchBarCard.layoutParams = params - } - } - - private fun setRadius(radius: Float) { - binding.searchBarCard.radius = radius - } - override fun onAttachedToWindow() { super.onAttachedToWindow() @@ -180,6 +144,54 @@ class MaterialSearchBar @JvmOverloads constructor( a?.recycle() } + // ********************************************************************************************* + + + + + fun getToolbar(): MaterialSearchToolbar { + return binding.searchBarToolbar + } + + fun setText(text: CharSequence?) { + binding.searchBarToolbar.setText(text) + } + + fun setHint(hint: CharSequence?) { + binding.searchBarToolbar.setHint(hint) + } + + /* + fun setForegroundColor(){ + + } + + fun setBackgroundColor(){ + + } + + fun setStrokeWidth(){ + + } + + fun setStrokeColor{ + card?.setStrokeColor() + } + */ + + private fun setMargins(left: Int, top: Int, right: Int, bottom: Int) { + if (binding.searchBarCard.layoutParams is MarginLayoutParams) { + val params = binding.searchBarCard.layoutParams as? MarginLayoutParams + params?.setMargins(left, top, right, bottom) + binding.searchBarCard.layoutParams = params + } + } + + private fun setRadius(radius: Float) { + binding.searchBarCard.radius = radius + } + + // ********************************************************************************************* @Suppress("unused") class ScrollingViewBehavior : AppBarLayout.ScrollingViewBehavior { @@ -203,4 +215,4 @@ class MaterialSearchBar @JvmOverloads constructor( } -} +} \ No newline at end of file diff --git a/src/main/kotlin/com/lapism/search/widget/MaterialSearchView.kt b/src/main/kotlin/com/lapism/search/widget/MaterialSearchView.kt index 577d20bb..9ef3c7d2 100755 --- a/src/main/kotlin/com/lapism/search/widget/MaterialSearchView.kt +++ b/src/main/kotlin/com/lapism/search/widget/MaterialSearchView.kt @@ -22,6 +22,7 @@ import com.lapism.search.R import com.lapism.search.databinding.MaterialSearchViewBinding +//@Suppress("MemberVisibilityCanBePrivate", "unused") class MaterialSearchView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, @@ -29,11 +30,13 @@ class MaterialSearchView @JvmOverloads constructor( defStyleRes: Int = 0 ) : MaterialSearchLayout(context, attrs, defStyleAttr, defStyleRes) { + // ********************************************************************************************* private var binding: MaterialSearchViewBinding private var focusListener: OnFocusChangeListener? = null private var queryListener: OnQueryTextListener? = null private var clearClickListener: OnClearClickListener? = null + // ********************************************************************************************* init { val inflater = LayoutInflater.from(getContext()) binding = MaterialSearchViewBinding.inflate(inflater, this) @@ -53,7 +56,7 @@ class MaterialSearchView @JvmOverloads constructor( }) binding.searchViewEditText.setOnEditorActionListener { _, _, _ -> onSubmitQuery() - return@setOnEditorActionListener true // true + return@setOnEditorActionListener true // same as ,,true" :-) } binding.searchViewEditText.setOnFocusChangeListener { _, hasFocus -> visibility = if (hasFocus) { @@ -174,12 +177,12 @@ class MaterialSearchView @JvmOverloads constructor( } // ********************************************************************************************* - override fun addView(child: View?) { - binding.searchViewContentContainer.addView(child) + override fun setBackgroundColor(@ColorInt color: Int) { + binding.searchViewBackground.setBackgroundColor(color) } - override fun setBackgroundColor(color: Int) { - binding.searchViewBackground.setBackgroundColor(color) + override fun addView(child: View?) { + binding.searchViewContentContainer.addView(child) } override fun requestFocus(direction: Int, previouslyFocusedRect: Rect?): Boolean { @@ -190,28 +193,6 @@ class MaterialSearchView @JvmOverloads constructor( binding.searchViewEditText.clearFocus() } - - - - - - - - // TODO ANOTACE A NAZVY PROMENNCYH + @Nullable - - - - - - - - - - - - - - override fun onSaveInstanceState(): Parcelable? { val superState: Parcelable? = super.onSaveInstanceState() superState?.let { @@ -236,14 +217,9 @@ class MaterialSearchView @JvmOverloads constructor( } } - - - - - - // ********************************************************************************************* - fun setClearIcon(drawable: Drawable?) { + // TODO ANOTACE, NAZVY PROMENNCYH, @Nullable + fun setClearIcon(@Nullable drawable: Drawable?) { binding.searchViewClearButton.setImageDrawable(drawable) } @@ -259,6 +235,7 @@ class MaterialSearchView @JvmOverloads constructor( binding.searchViewEditText.setTextClearOnBackPressed(clear) } + @Nullable fun getTextQuery(): Editable? { return binding.searchViewEditText.text } @@ -293,15 +270,15 @@ class MaterialSearchView @JvmOverloads constructor( return binding.searchViewEditText.typeface } - fun setHint(hint: CharSequence?) { + fun setHint(@Nullable hint: CharSequence?) { binding.searchViewEditText.hint = hint } - fun setOnFocusChangeListener(listener: OnFocusChangeListener) { + fun setOnFocusChangeListener(@Nullable listener: OnFocusChangeListener?) { focusListener = listener } - fun setOnQueryTextListener(listener: OnQueryTextListener) { + fun setOnQueryTextListener(@Nullable listener: OnQueryTextListener?) { queryListener = listener } @@ -321,8 +298,7 @@ class MaterialSearchView @JvmOverloads constructor( val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager inputMethodManager.hideSoftInputFromWindow( - // TODO binding.searchViewEditText.windowToken ? - windowToken, + binding.searchViewEditText.windowToken, InputMethodManager.RESULT_UNCHANGED_SHOWN ) } @@ -347,11 +323,11 @@ class MaterialSearchView @JvmOverloads constructor( } - fun setTextHintColor(color: Int) { + fun setTextHintColor(@ColorInt color: Int) { binding.searchViewEditText.setHintTextColor(color) } - fun setTextColor(color: Int) { + fun setTextColor(@ColorInt color: Int) { binding.searchViewEditText.setTextColor(color) } From 81f42727ae5d21cadce799406062b0bda4dbf557 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Lapi=C5=A1?= Date: Thu, 21 Oct 2021 11:56:57 +0200 Subject: [PATCH 7/8] Update --- .github/FUNDING.yml | 0 .gitignore | 0 build.gradle | 2 +- consumer-rules.pro | 0 proguard-rules.pro | 0 src/main/AndroidManifest.xml | 0 .../internal/ClippableRoundedCornerLayout.kt | 1 + .../lapism/search/widget/MaterialSearchBar.kt | 13 ++---- .../search/widget/MaterialSearchLayout.kt | 0 .../search/widget/MaterialSearchToolbar.kt | 18 ++++---- .../search/widget/MaterialSearchView.kt | 46 +++++++++---------- .../search/widget/NavigationIconCompat.kt | 0 .../search_ic_outline_arrow_back_24.xml | 0 .../drawable/search_ic_outline_clear_24.xml | 0 .../drawable/search_ic_outline_search_24.xml | 0 src/main/res/layout/material_search_view.xml | 1 + 16 files changed, 39 insertions(+), 42 deletions(-) mode change 100644 => 100755 .github/FUNDING.yml mode change 100644 => 100755 .gitignore mode change 100644 => 100755 build.gradle mode change 100644 => 100755 consumer-rules.pro mode change 100644 => 100755 proguard-rules.pro mode change 100644 => 100755 src/main/AndroidManifest.xml mode change 100644 => 100755 src/main/kotlin/com/lapism/search/internal/ClippableRoundedCornerLayout.kt mode change 100644 => 100755 src/main/kotlin/com/lapism/search/widget/MaterialSearchLayout.kt mode change 100644 => 100755 src/main/kotlin/com/lapism/search/widget/NavigationIconCompat.kt mode change 100644 => 100755 src/main/res/drawable/search_ic_outline_arrow_back_24.xml mode change 100644 => 100755 src/main/res/drawable/search_ic_outline_clear_24.xml mode change 100644 => 100755 src/main/res/drawable/search_ic_outline_search_24.xml diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml old mode 100644 new mode 100755 diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/build.gradle b/build.gradle old mode 100644 new mode 100755 index dbbf8ae2..0b3cd58b --- a/build.gradle +++ b/build.gradle @@ -42,7 +42,7 @@ android { dependencies { // Core - implementation 'androidx.core:core-ktx:1.7.0-beta02' + implementation 'androidx.core:core-ktx:1.7.0-rc01' implementation 'androidx.appcompat:appcompat:1.4.0-beta01' implementation 'com.google.android.material:material:1.5.0-alpha04' } diff --git a/consumer-rules.pro b/consumer-rules.pro old mode 100644 new mode 100755 diff --git a/proguard-rules.pro b/proguard-rules.pro old mode 100644 new mode 100755 diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml old mode 100644 new mode 100755 diff --git a/src/main/kotlin/com/lapism/search/internal/ClippableRoundedCornerLayout.kt b/src/main/kotlin/com/lapism/search/internal/ClippableRoundedCornerLayout.kt old mode 100644 new mode 100755 index b4d9bb4a..bd87a3e5 --- a/src/main/kotlin/com/lapism/search/internal/ClippableRoundedCornerLayout.kt +++ b/src/main/kotlin/com/lapism/search/internal/ClippableRoundedCornerLayout.kt @@ -14,6 +14,7 @@ class ClippableRoundedCornerLayout @JvmOverloads constructor( defStyleRes: Int = 0 ) : FrameLayout(context, attrs, defStyleAttr, defStyleRes) { + // TODO CLIP ANIMATION var path: Path? = null override fun dispatchDraw(canvas: Canvas?) { diff --git a/src/main/kotlin/com/lapism/search/widget/MaterialSearchBar.kt b/src/main/kotlin/com/lapism/search/widget/MaterialSearchBar.kt index ea13ee93..56872973 100755 --- a/src/main/kotlin/com/lapism/search/widget/MaterialSearchBar.kt +++ b/src/main/kotlin/com/lapism/search/widget/MaterialSearchBar.kt @@ -145,19 +145,15 @@ class MaterialSearchBar @JvmOverloads constructor( } // ********************************************************************************************* - - - - fun getToolbar(): MaterialSearchToolbar { return binding.searchBarToolbar } - fun setText(text: CharSequence?) { + fun setText(@Nullable text: CharSequence?) { binding.searchBarToolbar.setText(text) } - fun setHint(hint: CharSequence?) { + fun setHint(@Nullable hint: CharSequence?) { binding.searchBarToolbar.setHint(hint) } @@ -179,15 +175,16 @@ class MaterialSearchBar @JvmOverloads constructor( } */ - private fun setMargins(left: Int, top: Int, right: Int, bottom: Int) { + fun setMargins(left: Int, top: Int, right: Int, bottom: Int) { if (binding.searchBarCard.layoutParams is MarginLayoutParams) { val params = binding.searchBarCard.layoutParams as? MarginLayoutParams params?.setMargins(left, top, right, bottom) binding.searchBarCard.layoutParams = params + // requestLayout() TODO } } - private fun setRadius(radius: Float) { + fun setRadius(radius: Float) { binding.searchBarCard.radius = radius } diff --git a/src/main/kotlin/com/lapism/search/widget/MaterialSearchLayout.kt b/src/main/kotlin/com/lapism/search/widget/MaterialSearchLayout.kt old mode 100644 new mode 100755 diff --git a/src/main/kotlin/com/lapism/search/widget/MaterialSearchToolbar.kt b/src/main/kotlin/com/lapism/search/widget/MaterialSearchToolbar.kt index eef35482..4ed9c440 100755 --- a/src/main/kotlin/com/lapism/search/widget/MaterialSearchToolbar.kt +++ b/src/main/kotlin/com/lapism/search/widget/MaterialSearchToolbar.kt @@ -7,6 +7,7 @@ import android.util.AttributeSet import android.view.LayoutInflater import android.view.accessibility.AccessibilityNodeInfo import android.widget.TextView +import androidx.annotation.Nullable import com.google.android.material.appbar.MaterialToolbar import com.lapism.search.databinding.MaterialSearchToolbarBinding @@ -51,6 +52,7 @@ class MaterialSearchToolbar : MaterialToolbar { } override fun setNavigationIcon(drawable: Drawable?) { + // Fix for Navigation Library if (drawable != null) { super.setNavigationIcon(drawable) } @@ -69,23 +71,23 @@ class MaterialSearchToolbar : MaterialToolbar { info?.text = text } - - - // todo DODELAT **************************************************************************************** - fun setText(text: CharSequence?) { + // ********************************************************************************************* + fun setText(@Nullable text: CharSequence?) { binding.searchToolbarTextView.text = text } + @Nullable private fun getText(): CharSequence? { return binding.searchToolbarTextView.text } - private fun getHint(): CharSequence? { - return binding.searchToolbarTextView.hint + fun setHint(@Nullable hint: CharSequence?) { + binding.searchToolbarTextView.hint = hint } - fun setHint(hint: CharSequence?) { - binding.searchToolbarTextView.hint = hint + @Nullable + private fun getHint(): CharSequence? { + return binding.searchToolbarTextView.hint } } \ No newline at end of file diff --git a/src/main/kotlin/com/lapism/search/widget/MaterialSearchView.kt b/src/main/kotlin/com/lapism/search/widget/MaterialSearchView.kt index 9ef3c7d2..6f5aa3c0 100755 --- a/src/main/kotlin/com/lapism/search/widget/MaterialSearchView.kt +++ b/src/main/kotlin/com/lapism/search/widget/MaterialSearchView.kt @@ -22,7 +22,6 @@ import com.lapism.search.R import com.lapism.search.databinding.MaterialSearchViewBinding -//@Suppress("MemberVisibilityCanBePrivate", "unused") class MaterialSearchView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, @@ -56,7 +55,7 @@ class MaterialSearchView @JvmOverloads constructor( }) binding.searchViewEditText.setOnEditorActionListener { _, _, _ -> onSubmitQuery() - return@setOnEditorActionListener true // same as ,,true" :-) + return@setOnEditorActionListener true // same as ,,true" :) } binding.searchViewEditText.setOnFocusChangeListener { _, hasFocus -> visibility = if (hasFocus) { @@ -152,6 +151,8 @@ class MaterialSearchView @JvmOverloads constructor( a.recycle() + // TODO ANIMATE binding.seachViewClip.path + visibility = View.GONE } @@ -218,7 +219,7 @@ class MaterialSearchView @JvmOverloads constructor( } // ********************************************************************************************* - // TODO ANOTACE, NAZVY PROMENNCYH, @Nullable + // TODO ANOTACE, NAZVY PROMENNCYH fun setClearIcon(@Nullable drawable: Drawable?) { binding.searchViewClearButton.setImageDrawable(drawable) } @@ -265,15 +266,26 @@ class MaterialSearchView @JvmOverloads constructor( binding.searchViewEditText.typeface = typeface } - @Nullable - fun getTextTypeface(): Typeface? { - return binding.searchViewEditText.typeface - } - fun setHint(@Nullable hint: CharSequence?) { binding.searchViewEditText.hint = hint } + fun setDividerColor(@ColorInt color: Int) { + binding.searchViewDivider.setBackgroundColor(color) + } + + fun setScrimColor(@ColorInt color: Int) { + binding.searchViewScrim.setBackgroundColor(color) + } + + fun setTextHintColor(@ColorInt color: Int) { + binding.searchViewEditText.setHintTextColor(color) + } + + fun setTextColor(@ColorInt color: Int) { + binding.searchViewEditText.setTextColor(color) + } + fun setOnFocusChangeListener(@Nullable listener: OnFocusChangeListener?) { focusListener = listener } @@ -304,6 +316,7 @@ class MaterialSearchView @JvmOverloads constructor( } } + // ********************************************************************************************* private fun onTextChanged(s: CharSequence) { if (s.isNotEmpty()) { binding.searchViewClearButton.visibility = View.VISIBLE @@ -314,23 +327,6 @@ class MaterialSearchView @JvmOverloads constructor( queryListener?.onQueryTextChange(s) } - fun setDividerColor(@ColorInt color: Int) { - binding.searchViewDivider.setBackgroundColor(color) - } - - fun setScrimColor(@ColorInt color: Int) { - binding.searchViewScrim.setBackgroundColor(color) - } - - - fun setTextHintColor(@ColorInt color: Int) { - binding.searchViewEditText.setHintTextColor(color) - } - - fun setTextColor(@ColorInt color: Int) { - binding.searchViewEditText.setTextColor(color) - } - // ********************************************************************************************* interface OnFocusChangeListener { diff --git a/src/main/kotlin/com/lapism/search/widget/NavigationIconCompat.kt b/src/main/kotlin/com/lapism/search/widget/NavigationIconCompat.kt old mode 100644 new mode 100755 diff --git a/src/main/res/drawable/search_ic_outline_arrow_back_24.xml b/src/main/res/drawable/search_ic_outline_arrow_back_24.xml old mode 100644 new mode 100755 diff --git a/src/main/res/drawable/search_ic_outline_clear_24.xml b/src/main/res/drawable/search_ic_outline_clear_24.xml old mode 100644 new mode 100755 diff --git a/src/main/res/drawable/search_ic_outline_search_24.xml b/src/main/res/drawable/search_ic_outline_search_24.xml old mode 100644 new mode 100755 diff --git a/src/main/res/layout/material_search_view.xml b/src/main/res/layout/material_search_view.xml index 910b25d3..6ee8a075 100755 --- a/src/main/res/layout/material_search_view.xml +++ b/src/main/res/layout/material_search_view.xml @@ -10,6 +10,7 @@ android:background="?scrimBackground" /> Date: Fri, 29 Oct 2021 16:27:17 +0200 Subject: [PATCH 8/8] Update --- .../internal/ClippableRoundedCornerLayout.kt | 2 + .../lapism/search/internal/FocusEditText.kt | 3 ++ .../lapism/search/widget/MaterialSearchBar.kt | 42 ++++++++----------- .../search/widget/MaterialSearchLayout.kt | 2 + .../search/widget/MaterialSearchToolbar.kt | 14 ++++--- .../search/widget/MaterialSearchView.kt | 24 +++++------ 6 files changed, 46 insertions(+), 41 deletions(-) diff --git a/src/main/kotlin/com/lapism/search/internal/ClippableRoundedCornerLayout.kt b/src/main/kotlin/com/lapism/search/internal/ClippableRoundedCornerLayout.kt index bd87a3e5..d51f7eec 100755 --- a/src/main/kotlin/com/lapism/search/internal/ClippableRoundedCornerLayout.kt +++ b/src/main/kotlin/com/lapism/search/internal/ClippableRoundedCornerLayout.kt @@ -14,9 +14,11 @@ class ClippableRoundedCornerLayout @JvmOverloads constructor( defStyleRes: Int = 0 ) : FrameLayout(context, attrs, defStyleAttr, defStyleRes) { + // ********************************************************************************************* // TODO CLIP ANIMATION var path: Path? = null + // ********************************************************************************************* override fun dispatchDraw(canvas: Canvas?) { if (path == null) { super.dispatchDraw(canvas) diff --git a/src/main/kotlin/com/lapism/search/internal/FocusEditText.kt b/src/main/kotlin/com/lapism/search/internal/FocusEditText.kt index 13e3a86f..0a957d11 100755 --- a/src/main/kotlin/com/lapism/search/internal/FocusEditText.kt +++ b/src/main/kotlin/com/lapism/search/internal/FocusEditText.kt @@ -12,8 +12,10 @@ class FocusEditText @JvmOverloads constructor( defStyleAttr: Int = 0 ) : AppCompatEditText(context, attrs, defStyleAttr) { + // ********************************************************************************************* private var textClear: Boolean = false + // ********************************************************************************************* override fun onKeyPreIme(keyCode: Int, event: KeyEvent?): Boolean { if (keyCode == KeyEvent.KEYCODE_BACK && event?.action == KeyEvent.ACTION_UP && textClear) { if (hasFocus()) { @@ -29,6 +31,7 @@ class FocusEditText @JvmOverloads constructor( text?.clear() } + // ********************************************************************************************* fun setTextClearOnBackPressed(clear: Boolean) { textClear = clear } diff --git a/src/main/kotlin/com/lapism/search/widget/MaterialSearchBar.kt b/src/main/kotlin/com/lapism/search/widget/MaterialSearchBar.kt index 56872973..440b2c8e 100755 --- a/src/main/kotlin/com/lapism/search/widget/MaterialSearchBar.kt +++ b/src/main/kotlin/com/lapism/search/widget/MaterialSearchBar.kt @@ -1,16 +1,14 @@ package com.lapism.search.widget import android.content.Context +import android.content.res.ColorStateList import android.content.res.TypedArray import android.graphics.Color import android.graphics.drawable.Drawable import android.util.AttributeSet import android.view.LayoutInflater import android.view.View -import androidx.annotation.ColorInt -import androidx.annotation.DrawableRes -import androidx.annotation.Nullable -import androidx.annotation.StringRes +import androidx.annotation.* import androidx.coordinatorlayout.widget.CoordinatorLayout import androidx.core.view.ViewCompat import com.google.android.material.appbar.AppBarLayout @@ -18,6 +16,7 @@ import com.lapism.search.R import com.lapism.search.databinding.MaterialSearchBarBinding +@Suppress("unused", "MemberVisibilityCanBePrivate") class MaterialSearchBar @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, @@ -157,37 +156,32 @@ class MaterialSearchBar @JvmOverloads constructor( binding.searchBarToolbar.setHint(hint) } - /* - fun setForegroundColor(){ - - } - - fun setBackgroundColor(){ - - } + fun setForegroundColor(@Nullable foregroundColor: ColorStateList?) { + binding.searchBarCard.setCardForegroundColor(foregroundColor) + } - fun setStrokeWidth(){ + fun setStrokeWidth(@Dimension strokeWidth: Int) { + binding.searchBarCard.strokeWidth = strokeWidth + } - } + fun setStrokeColor(strokeColor: ColorStateList) { + binding.searchBarCard.setStrokeColor(strokeColor) + } - fun setStrokeColor{ - card?.setStrokeColor() - } - */ + fun setRadius(radius: Float) { + binding.searchBarCard.radius = radius + } - fun setMargins(left: Int, top: Int, right: Int, bottom: Int) { + // ********************************************************************************************* + // TODO PUBLIC ? and requestLayout() + private fun setMargins(left: Int, top: Int, right: Int, bottom: Int) { if (binding.searchBarCard.layoutParams is MarginLayoutParams) { val params = binding.searchBarCard.layoutParams as? MarginLayoutParams params?.setMargins(left, top, right, bottom) binding.searchBarCard.layoutParams = params - // requestLayout() TODO } } - fun setRadius(radius: Float) { - binding.searchBarCard.radius = radius - } - // ********************************************************************************************* @Suppress("unused") class ScrollingViewBehavior : AppBarLayout.ScrollingViewBehavior { diff --git a/src/main/kotlin/com/lapism/search/widget/MaterialSearchLayout.kt b/src/main/kotlin/com/lapism/search/widget/MaterialSearchLayout.kt index 0a579225..ec64593e 100755 --- a/src/main/kotlin/com/lapism/search/widget/MaterialSearchLayout.kt +++ b/src/main/kotlin/com/lapism/search/widget/MaterialSearchLayout.kt @@ -18,6 +18,7 @@ abstract class MaterialSearchLayout @JvmOverloads constructor( defStyleRes: Int = 0 ) : FrameLayout(context, attrs, defStyleAttr, defStyleRes) { + // ********************************************************************************************* @NavigationIconCompat @get:NavigationIconCompat var navigationIconCompat: Int = 0 @@ -47,6 +48,7 @@ abstract class MaterialSearchLayout @JvmOverloads constructor( } } + // ********************************************************************************************* abstract fun setNavigationIcon(@DrawableRes resId: Int) abstract fun setNavigationIcon(@Nullable drawable: Drawable?) diff --git a/src/main/kotlin/com/lapism/search/widget/MaterialSearchToolbar.kt b/src/main/kotlin/com/lapism/search/widget/MaterialSearchToolbar.kt index 4ed9c440..55becad9 100755 --- a/src/main/kotlin/com/lapism/search/widget/MaterialSearchToolbar.kt +++ b/src/main/kotlin/com/lapism/search/widget/MaterialSearchToolbar.kt @@ -14,8 +14,10 @@ import com.lapism.search.databinding.MaterialSearchToolbarBinding class MaterialSearchToolbar : MaterialToolbar { + // ********************************************************************************************* private var binding: MaterialSearchToolbarBinding + // ********************************************************************************************* constructor(context: Context) : super(context) constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) @@ -26,11 +28,13 @@ class MaterialSearchToolbar : MaterialToolbar { defStyleAttr ) + // ********************************************************************************************* init { val inflater = LayoutInflater.from(context) binding = MaterialSearchToolbarBinding.inflate(inflater, this, true) } + // ********************************************************************************************* override fun setTitle(resId: Int) { } @@ -58,17 +62,17 @@ class MaterialSearchToolbar : MaterialToolbar { } } - override fun onInitializeAccessibilityNodeInfo(info: AccessibilityNodeInfo?) { + override fun onInitializeAccessibilityNodeInfo(info: AccessibilityNodeInfo) { super.onInitializeAccessibilityNodeInfo(info) - info?.className = TextView::class.java.canonicalName + info.className = TextView::class.java.canonicalName var text: CharSequence? = getText() val isEmpty = TextUtils.isEmpty(text) - info?.hintText = getHint() - info?.isShowingHintText = isEmpty + info.hintText = getHint() + info.isShowingHintText = isEmpty if (isEmpty) { text = getHint() } - info?.text = text + info.text = text } // ********************************************************************************************* diff --git a/src/main/kotlin/com/lapism/search/widget/MaterialSearchView.kt b/src/main/kotlin/com/lapism/search/widget/MaterialSearchView.kt index 6f5aa3c0..b6d00828 100755 --- a/src/main/kotlin/com/lapism/search/widget/MaterialSearchView.kt +++ b/src/main/kotlin/com/lapism/search/widget/MaterialSearchView.kt @@ -22,6 +22,7 @@ import com.lapism.search.R import com.lapism.search.databinding.MaterialSearchViewBinding +@Suppress("MemberVisibilityCanBePrivate", "unused") class MaterialSearchView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, @@ -33,7 +34,6 @@ class MaterialSearchView @JvmOverloads constructor( private var binding: MaterialSearchViewBinding private var focusListener: OnFocusChangeListener? = null private var queryListener: OnQueryTextListener? = null - private var clearClickListener: OnClearClickListener? = null // ********************************************************************************************* init { @@ -79,7 +79,6 @@ class MaterialSearchView @JvmOverloads constructor( binding.searchViewClearButton.visibility = View.GONE binding.searchViewClearButton.setOnClickListener { - clearClickListener?.onClearClick() binding.searchViewEditText.text?.clear() } @@ -151,11 +150,18 @@ class MaterialSearchView @JvmOverloads constructor( a.recycle() - // TODO ANIMATE binding.seachViewClip.path - visibility = View.GONE } + /* TODO styles + codes, ANIMATE binding.searchViewClip.path, SCALE, path + private fun setTransition() { + val mTransition = LayoutTransition() + mTransition.enableTransitionType(LayoutTransition.CHANGING) + mTransition.setDuration(3000L) + + binding.searchViewBackground.layoutTransition = mTransition + }*/ + // ********************************************************************************************* override fun setNavigationIcon(@DrawableRes resId: Int) { binding.searchViewToolbar.setNavigationIcon(resId) @@ -182,7 +188,7 @@ class MaterialSearchView @JvmOverloads constructor( binding.searchViewBackground.setBackgroundColor(color) } - override fun addView(child: View?) { + override fun addView(child: View) { binding.searchViewContentContainer.addView(child) } @@ -219,7 +225,6 @@ class MaterialSearchView @JvmOverloads constructor( } // ********************************************************************************************* - // TODO ANOTACE, NAZVY PROMENNCYH fun setClearIcon(@Nullable drawable: Drawable?) { binding.searchViewClearButton.setImageDrawable(drawable) } @@ -340,14 +345,9 @@ class MaterialSearchView @JvmOverloads constructor( fun onQueryTextSubmit(query: CharSequence): Boolean } - interface OnClearClickListener { - - fun onClearClick() - } - // ********************************************************************************************* @Suppress("unused") - class SavedState : AbsSavedState { + private class SavedState : AbsSavedState { var text: String? = null var focus: Boolean = false