move some search bar related code to a custom view
This commit is contained in:
parent
7bce8ab31b
commit
f02348dc6c
4 changed files with 137 additions and 86 deletions
|
|
@ -33,6 +33,7 @@ import com.simplemobiletools.smsmessenger.models.Events
|
||||||
import com.simplemobiletools.smsmessenger.models.Message
|
import com.simplemobiletools.smsmessenger.models.Message
|
||||||
import com.simplemobiletools.smsmessenger.models.SearchResult
|
import com.simplemobiletools.smsmessenger.models.SearchResult
|
||||||
import kotlinx.android.synthetic.main.activity_main.*
|
import kotlinx.android.synthetic.main.activity_main.*
|
||||||
|
import kotlinx.android.synthetic.main.menu_search.view.*
|
||||||
import org.greenrobot.eventbus.EventBus
|
import org.greenrobot.eventbus.EventBus
|
||||||
import org.greenrobot.eventbus.Subscribe
|
import org.greenrobot.eventbus.Subscribe
|
||||||
import org.greenrobot.eventbus.ThreadMode
|
import org.greenrobot.eventbus.ThreadMode
|
||||||
|
|
@ -93,7 +94,6 @@ class MainActivity : SimpleActivity() {
|
||||||
|
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
super.onResume()
|
super.onResume()
|
||||||
setupToolbar(main_toolbar)
|
|
||||||
updateMenuColors()
|
updateMenuColors()
|
||||||
|
|
||||||
getOrCreateConversationsAdapter().apply {
|
getOrCreateConversationsAdapter().apply {
|
||||||
|
|
@ -131,35 +131,29 @@ class MainActivity : SimpleActivity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBackPressed() {
|
override fun onBackPressed() {
|
||||||
if (search_holder.isVisible()) {
|
if (main_menu.isSearchOpen) {
|
||||||
closeSearch()
|
main_menu.closeSearch()
|
||||||
} else {
|
} else {
|
||||||
super.onBackPressed()
|
super.onBackPressed()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setupOptionsMenu() {
|
private fun setupOptionsMenu() {
|
||||||
updateMenuColors()
|
main_menu.top_toolbar.inflateMenu(R.menu.menu_main)
|
||||||
|
main_menu.setupMenu()
|
||||||
main_toolbar_search_icon.setOnClickListener {
|
main_menu.onSearchOpenListener = {
|
||||||
if (search_holder.isVisible()) {
|
search_holder.beVisible()
|
||||||
closeSearch()
|
|
||||||
} else {
|
|
||||||
main_toolbar_search.requestFocus()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
main_toolbar_search.setOnFocusChangeListener { v, hasFocus ->
|
main_menu.onSearchClosedListener = {
|
||||||
if (hasFocus) {
|
search_holder.beGone()
|
||||||
openSearch()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
main_toolbar_search.onTextChangeListener {
|
main_menu.onSearchTextChangedListener = { text ->
|
||||||
searchTextChanged(it)
|
searchTextChanged(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
main_toolbar.setOnMenuItemClickListener { menuItem ->
|
main_menu.top_toolbar.setOnMenuItemClickListener { menuItem ->
|
||||||
when (menuItem.itemId) {
|
when (menuItem.itemId) {
|
||||||
R.id.import_messages -> tryImportMessages()
|
R.id.import_messages -> tryImportMessages()
|
||||||
R.id.export_messages -> tryToExportMessages()
|
R.id.export_messages -> tryToExportMessages()
|
||||||
|
|
@ -173,7 +167,7 @@ class MainActivity : SimpleActivity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun refreshMenuItems() {
|
private fun refreshMenuItems() {
|
||||||
main_toolbar.menu.apply {
|
main_menu.top_toolbar.menu.apply {
|
||||||
findItem(R.id.more_apps_from_us).isVisible = !resources.getBoolean(R.bool.hide_google_relations)
|
findItem(R.id.more_apps_from_us).isVisible = !resources.getBoolean(R.bool.hide_google_relations)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -200,15 +194,8 @@ class MainActivity : SimpleActivity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateMenuColors() {
|
private fun updateMenuColors() {
|
||||||
val backgroundColor = getProperBackgroundColor()
|
updateStatusbarColor(getProperBackgroundColor())
|
||||||
val contrastColor = backgroundColor.getContrastColor()
|
main_menu.updateColors()
|
||||||
|
|
||||||
updateStatusbarColor(backgroundColor)
|
|
||||||
main_app_bar_layout.setBackgroundColor(backgroundColor)
|
|
||||||
main_toolbar_search_icon.applyColorFilter(contrastColor)
|
|
||||||
main_toolbar_holder.background?.applyColorFilter(getProperPrimaryColor().adjustAlpha(LOWER_ALPHA))
|
|
||||||
main_toolbar_search.setTextColor(contrastColor)
|
|
||||||
main_toolbar_search.setHintTextColor(contrastColor.adjustAlpha(MEDIUM_ALPHA))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// while SEND_SMS and READ_SMS permissions are mandatory, READ_CONTACTS is optional. If we don't have it, we just won't be able to show the contact name in some cases
|
// while SEND_SMS and READ_SMS permissions are mandatory, READ_CONTACTS is optional. If we don't have it, we just won't be able to show the contact name in some cases
|
||||||
|
|
@ -440,18 +427,6 @@ class MainActivity : SimpleActivity() {
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun openSearch() {
|
|
||||||
search_holder.beVisible()
|
|
||||||
main_toolbar_search_icon.setImageResource(R.drawable.ic_arrow_left_vector)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun closeSearch() {
|
|
||||||
hideKeyboard()
|
|
||||||
main_toolbar_search.setText("")
|
|
||||||
search_holder.beGone()
|
|
||||||
main_toolbar_search_icon.setImageResource(R.drawable.ic_search_vector)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun searchTextChanged(text: String) {
|
private fun searchTextChanged(text: String) {
|
||||||
lastSearchedText = text
|
lastSearchedText = text
|
||||||
search_placeholder_2.beGoneIf(text.length >= 2)
|
search_placeholder_2.beGoneIf(text.length >= 2)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,71 @@
|
||||||
|
package com.simplemobiletools.smsmessenger.views
|
||||||
|
|
||||||
|
import android.app.Activity
|
||||||
|
import android.content.Context
|
||||||
|
import android.util.AttributeSet
|
||||||
|
import com.google.android.material.appbar.AppBarLayout
|
||||||
|
import com.simplemobiletools.commons.extensions.*
|
||||||
|
import com.simplemobiletools.commons.helpers.LOWER_ALPHA
|
||||||
|
import com.simplemobiletools.commons.helpers.MEDIUM_ALPHA
|
||||||
|
import com.simplemobiletools.smsmessenger.R
|
||||||
|
import com.simplemobiletools.smsmessenger.activities.SimpleActivity
|
||||||
|
import kotlinx.android.synthetic.main.menu_search.view.*
|
||||||
|
|
||||||
|
class MySearchMenu(context: Context, attrs: AttributeSet) : AppBarLayout(context, attrs) {
|
||||||
|
var isSearchOpen = false
|
||||||
|
var onSearchOpenListener: (() -> Unit)? = null
|
||||||
|
var onSearchClosedListener: (() -> Unit)? = null
|
||||||
|
var onSearchTextChangedListener: ((text: String) -> Unit)? = null
|
||||||
|
|
||||||
|
init {
|
||||||
|
inflate(context, R.layout.menu_search, this)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setupMenu() {
|
||||||
|
top_toolbar_search_icon.setOnClickListener {
|
||||||
|
if (isSearchOpen) {
|
||||||
|
closeSearch()
|
||||||
|
} else {
|
||||||
|
top_toolbar_search.requestFocus()
|
||||||
|
(context as? Activity)?.showKeyboard(top_toolbar_search)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
top_toolbar_search.setOnFocusChangeListener { v, hasFocus ->
|
||||||
|
if (hasFocus) {
|
||||||
|
openSearch()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
top_toolbar_search.onTextChangeListener {
|
||||||
|
onSearchTextChangedListener?.invoke(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun openSearch() {
|
||||||
|
isSearchOpen = true
|
||||||
|
onSearchOpenListener?.invoke()
|
||||||
|
top_toolbar_search_icon.setImageResource(R.drawable.ic_arrow_left_vector)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun closeSearch() {
|
||||||
|
isSearchOpen = false
|
||||||
|
onSearchClosedListener?.invoke()
|
||||||
|
top_toolbar_search.setText("")
|
||||||
|
top_toolbar_search_icon.setImageResource(R.drawable.ic_search_vector)
|
||||||
|
(context as? Activity)?.hideKeyboard()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updateColors() {
|
||||||
|
val backgroundColor = context.getProperBackgroundColor()
|
||||||
|
val contrastColor = backgroundColor.getContrastColor()
|
||||||
|
|
||||||
|
setBackgroundColor(backgroundColor)
|
||||||
|
top_app_bar_layout.setBackgroundColor(backgroundColor)
|
||||||
|
top_toolbar_search_icon.applyColorFilter(contrastColor)
|
||||||
|
top_toolbar_holder.background?.applyColorFilter(context.getProperPrimaryColor().adjustAlpha(LOWER_ALPHA))
|
||||||
|
top_toolbar_search.setTextColor(contrastColor)
|
||||||
|
top_toolbar_search.setHintTextColor(contrastColor.adjustAlpha(MEDIUM_ALPHA))
|
||||||
|
(context as? SimpleActivity)?.updateTopBarColors(top_toolbar, backgroundColor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,53 +5,10 @@
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent">
|
android:layout_height="match_parent">
|
||||||
|
|
||||||
<com.google.android.material.appbar.AppBarLayout
|
<com.simplemobiletools.smsmessenger.views.MySearchMenu
|
||||||
android:id="@+id/main_app_bar_layout"
|
android:id="@+id/main_menu"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="?attr/actionBarSize"
|
android:layout_height="wrap_content" />
|
||||||
android:layout_marginStart="@dimen/activity_margin"
|
|
||||||
android:layout_marginEnd="@dimen/activity_margin">
|
|
||||||
|
|
||||||
<RelativeLayout
|
|
||||||
android:id="@+id/main_toolbar_holder"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_marginTop="@dimen/small_margin"
|
|
||||||
android:layout_marginBottom="@dimen/small_margin"
|
|
||||||
android:background="@drawable/search_menu_background">
|
|
||||||
|
|
||||||
<ImageView
|
|
||||||
android:id="@+id/main_toolbar_search_icon"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="match_parent"
|
|
||||||
android:layout_centerVertical="true"
|
|
||||||
android:paddingStart="@dimen/activity_margin"
|
|
||||||
android:src="@drawable/ic_search_vector" />
|
|
||||||
|
|
||||||
<EditText
|
|
||||||
android:id="@+id/main_toolbar_search"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="match_parent"
|
|
||||||
android:layout_centerVertical="true"
|
|
||||||
android:layout_toStartOf="@+id/main_toolbar"
|
|
||||||
android:layout_toEndOf="@+id/main_toolbar_search_icon"
|
|
||||||
android:background="@null"
|
|
||||||
android:gravity="start|center_vertical"
|
|
||||||
android:hint="@string/search"
|
|
||||||
android:paddingStart="@dimen/activity_margin"
|
|
||||||
android:textSize="@dimen/big_text_size" />
|
|
||||||
|
|
||||||
<com.google.android.material.appbar.MaterialToolbar
|
|
||||||
android:id="@+id/main_toolbar"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="match_parent"
|
|
||||||
android:layout_alignParentEnd="true"
|
|
||||||
android:layout_marginEnd="@dimen/small_margin"
|
|
||||||
app:menu="@menu/menu_main"
|
|
||||||
app:titleTextAppearance="@style/AppTheme.ActionBar.TitleTextStyle" />
|
|
||||||
|
|
||||||
</RelativeLayout>
|
|
||||||
</com.google.android.material.appbar.AppBarLayout>
|
|
||||||
|
|
||||||
<RelativeLayout
|
<RelativeLayout
|
||||||
android:id="@+id/main_nested_scrollview"
|
android:id="@+id/main_nested_scrollview"
|
||||||
|
|
|
||||||
48
app/src/main/res/layout/menu_search.xml
Normal file
48
app/src/main/res/layout/menu_search.xml
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<com.google.android.material.appbar.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
android:id="@+id/top_app_bar_layout"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="?attr/actionBarSize"
|
||||||
|
android:layout_marginStart="@dimen/activity_margin"
|
||||||
|
android:layout_marginEnd="@dimen/activity_margin">
|
||||||
|
|
||||||
|
<RelativeLayout
|
||||||
|
android:id="@+id/top_toolbar_holder"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="@dimen/small_margin"
|
||||||
|
android:layout_marginBottom="@dimen/small_margin"
|
||||||
|
android:background="@drawable/search_menu_background">
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/top_toolbar_search_icon"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:layout_centerVertical="true"
|
||||||
|
android:paddingStart="@dimen/activity_margin"
|
||||||
|
android:src="@drawable/ic_search_vector" />
|
||||||
|
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/top_toolbar_search"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:layout_centerVertical="true"
|
||||||
|
android:layout_toStartOf="@+id/top_toolbar"
|
||||||
|
android:layout_toEndOf="@+id/top_toolbar_search_icon"
|
||||||
|
android:background="@null"
|
||||||
|
android:gravity="start|center_vertical"
|
||||||
|
android:hint="@string/search"
|
||||||
|
android:paddingStart="@dimen/activity_margin"
|
||||||
|
android:textSize="@dimen/big_text_size" />
|
||||||
|
|
||||||
|
<com.google.android.material.appbar.MaterialToolbar
|
||||||
|
android:id="@+id/top_toolbar"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:layout_alignParentEnd="true"
|
||||||
|
android:layout_marginEnd="@dimen/small_margin"
|
||||||
|
app:titleTextAppearance="@style/AppTheme.ActionBar.TitleTextStyle" />
|
||||||
|
|
||||||
|
</RelativeLayout>
|
||||||
|
</com.google.android.material.appbar.AppBarLayout>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue