Use ActionView for main search view

This commit is contained in:
Moez Bhatti
2018-08-14 19:10:41 +02:00
parent 7be398ee06
commit 25580e88e8
7 changed files with 155 additions and 27 deletions
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.common.widget
import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.LinearLayout
import androidx.appcompat.view.CollapsibleActionView
import androidx.appcompat.widget.Toolbar
import com.jakewharton.rxbinding2.widget.textChanges
import com.moez.QKSMS.R
import com.moez.QKSMS.common.util.extensions.dismissKeyboard
import io.reactivex.Observable
import kotlinx.android.synthetic.main.search_view.view.*
class SearchView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : LinearLayout(context, attrs), CollapsibleActionView {
val queryChanged: Observable<CharSequence>
init {
View.inflate(context, R.layout.search_view, this)
orientation = LinearLayout.HORIZONTAL
layoutParams = Toolbar.LayoutParams(Toolbar.LayoutParams.MATCH_PARENT, Toolbar.LayoutParams.MATCH_PARENT)
queryChanged = query.textChanges()
}
override fun onActionViewExpanded() {
// Focus on the query field and display the keyboard
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
query.requestFocus()
imm?.showSoftInput(query, 0)
}
override fun onActionViewCollapsed() {
// Reset the query
query.setText("")
// Dismiss the keyboard
(context as? Activity)?.dismissKeyboard()
}
}
@@ -25,7 +25,6 @@ import android.os.Bundle
import android.view.Gravity
import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.core.app.ActivityCompat
import androidx.core.view.isVisible
@@ -35,7 +34,6 @@ import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.snackbar.Snackbar
import com.jakewharton.rxbinding2.view.clicks
import com.jakewharton.rxbinding2.widget.textChanges
import com.moez.QKSMS.R
import com.moez.QKSMS.common.Navigator
import com.moez.QKSMS.common.androidxcompat.drawerOpen
@@ -48,6 +46,7 @@ import com.moez.QKSMS.common.util.extensions.scrapViews
import com.moez.QKSMS.common.util.extensions.setBackgroundTint
import com.moez.QKSMS.common.util.extensions.setTint
import com.moez.QKSMS.common.util.extensions.setVisible
import com.moez.QKSMS.common.widget.SearchView
import com.moez.QKSMS.feature.conversations.ConversationItemTouchCallback
import com.moez.QKSMS.feature.conversations.ConversationsAdapter
import com.moez.QKSMS.repository.SyncRepository
@@ -70,7 +69,7 @@ class MainActivity : QkThemedActivity(), MainView {
@Inject lateinit var viewModelFactory: ViewModelProvider.Factory
override val activityResumedIntent: Subject<Unit> = PublishSubject.create()
override val queryChangedIntent by lazy { toolbarSearch.textChanges() }
override val queryChangedIntent: Subject<CharSequence> = PublishSubject.create()
override val composeIntent by lazy { compose.clicks() }
override val drawerOpenIntent: Observable<Boolean> by lazy {
drawerLayout
@@ -184,9 +183,7 @@ class MainActivity : QkThemedActivity(), MainView {
else -> 0
}
toolbarSearch.setVisible(state.page is Inbox && state.page.selected == 0 || state.page is Searching)
toolbarTitle.setVisible(toolbarSearch.visibility != View.VISIBLE)
toolbar.menu.findItem(R.id.search)?.isVisible = state.page is Inbox && state.page.selected == 0 || state.page is Searching
toolbar.menu.findItem(R.id.archive)?.isVisible = state.page is Inbox && selectedConversations != 0
toolbar.menu.findItem(R.id.unarchive)?.isVisible = state.page is Archived && selectedConversations != 0
toolbar.menu.findItem(R.id.delete)?.isVisible = selectedConversations != 0
@@ -207,7 +204,10 @@ class MainActivity : QkThemedActivity(), MainView {
when (state.page) {
is Inbox -> {
showBackButton(state.page.showClearButton)
title = getString(R.string.main_title_selected, state.page.selected)
title = when (state.page.selected != 0) {
true -> getString(R.string.main_title_selected, state.page.selected)
false -> getString(R.string.main_title)
}
if (recyclerView.adapter !== conversationsAdapter) recyclerView.adapter = conversationsAdapter
conversationsAdapter.updateData(state.page.data)
itemTouchHelper.attachToRecyclerView(recyclerView)
@@ -280,8 +280,7 @@ class MainActivity : QkThemedActivity(), MainView {
}
override fun clearSearch() {
dismissKeyboard()
toolbarSearch.text = null
toolbar.menu.findItem(R.id.search)?.collapseActionView()
}
override fun clearSelection() {
@@ -304,6 +303,13 @@ class MainActivity : QkThemedActivity(), MainView {
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.main, menu)
// Query changes
menu?.findItem(R.id.search)
?.let { search -> search.actionView as? SearchView }
?.queryChanged
?.subscribe(queryChangedIntent)
return super.onCreateOptionsMenu(menu)
}
@@ -0,0 +1,27 @@
<!--
~ Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
~
~ This file is part of QKSMS.
~
~ QKSMS is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by
~ the Free Software Foundation, either version 3 of the License, or
~ (at your option) any later version.
~
~ QKSMS is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU General Public License for more details.
~
~ You should have received a copy of the GNU General Public License
~ along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"/>
</vector>
@@ -32,7 +32,7 @@
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
style="@style/Toolbar"
android:animateLayoutChanges="true"
android:elevation="8dp"
android:focusable="true"
android:focusableInTouchMode="true"
app:layout_constraintEnd_toEndOf="parent"
@@ -41,16 +41,8 @@
<com.moez.QKSMS.common.widget.QkTextView
android:id="@+id/toolbarTitle"
style="@style/ToolbarText" />
<com.moez.QKSMS.common.widget.QkEditText
android:id="@+id/toolbarSearch"
style="@style/ToolbarText"
android:layout_marginEnd="16dp"
android:hint="@string/title_conversations"
android:textColorHint="?android:attr/textColorTertiary"
app:textSize="primary"
tools:textSize="16sp" />
tools:text="Conversations" />
</androidx.appcompat.widget.Toolbar>
@@ -93,14 +85,6 @@
app:layout_constraintEnd_toEndOf="parent"
tools:backgroundTint="@color/tools_theme" />
<View
android:layout_width="0dp"
android:layout_height="8dp"
android:background="@drawable/ab_shadow"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar" />
<View
android:id="@+id/bottom"
android:layout_width="match_parent"
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
~
~ This file is part of QKSMS.
~
~ QKSMS is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by
~ the Free Software Foundation, either version 3 of the License, or
~ (at your option) any later version.
~
~ QKSMS is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU General Public License for more details.
~
~ You should have received a copy of the GNU General Public License
~ along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
-->
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:parentTag="com.moez.QKSMS.common.widget.SearchView">
<com.moez.QKSMS.common.widget.QkEditText
android:id="@+id/query"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@null"
android:hint="@string/title_conversations"
android:textColor="?android:attr/textColorPrimary"
android:textColorHint="?android:attr/textColorTertiary"
app:textSize="primary" />
</merge>
+7
View File
@@ -19,6 +19,13 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/search"
android:icon="@drawable/ic_search_black_24dp"
android:title="@string/main_menu_search"
app:actionViewClass="com.moez.QKSMS.common.widget.SearchView"
app:showAsAction="always|collapseActionView" />
<item
android:id="@+id/archive"
android:icon="@drawable/ic_archive_black_24dp"
@@ -35,8 +35,10 @@
<string name="menu_info">Details</string>
<string name="menu_save">Save to gallery</string>
<string name="main_title">Inbox</string>
<string name="main_drawer_open_cd">Open navigation drawer</string>
<string name="main_title_selected">%d selected</string>
<string name="main_menu_search">Search</string>
<string name="main_menu_clear">Clear</string>
<string name="main_menu_archive">Archive</string>
<string name="main_menu_unarchive">Unarchive</string>