From 86b46d14426ec806dad0a29b4ae0388af558cc31 Mon Sep 17 00:00:00 2001 From: Onuray Sahin Date: Mon, 21 Sep 2020 17:01:27 +0300 Subject: [PATCH] Configure SearchView as activity toolbar. --- .../home/room/detail/search/SearchActivity.kt | 31 ++++++++++++- .../home/room/detail/search/SearchFragment.kt | 5 +++ .../src/main/res/layout/activity_search.xml | 43 +++++++++++++++++++ .../src/main/res/layout/fragment_search.xml | 3 ++ 4 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 vector/src/main/res/layout/activity_search.xml diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/search/SearchActivity.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/search/SearchActivity.kt index bedc4cb881..70205eed62 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/search/SearchActivity.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/search/SearchActivity.kt @@ -18,29 +18,56 @@ package im.vector.app.features.home.room.detail.search import android.content.Context import android.content.Intent +import android.os.Bundle +import androidx.appcompat.widget.SearchView import com.airbnb.mvrx.MvRx import im.vector.app.R import im.vector.app.core.di.ScreenComponent import im.vector.app.core.extensions.addFragment import im.vector.app.core.platform.VectorBaseActivity +import kotlinx.android.synthetic.main.activity_search.* class SearchActivity : VectorBaseActivity() { - override fun getLayoutRes() = R.layout.activity_simple + private val searchFragment: SearchFragment? + get() { + return supportFragmentManager.findFragmentByTag(FRAGMENT_TAG) as? SearchFragment + } + + override fun getLayoutRes() = R.layout.activity_search override fun injectWith(injector: ScreenComponent) { super.injectWith(injector) injector.inject(this) } + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + configureToolbar(searchToolbar) + } + override fun initUiAndData() { if (isFirstCreation()) { val fragmentArgs: SearchArgs = intent?.extras?.getParcelable(MvRx.KEY_ARG) ?: return - addFragment(R.id.simpleFragmentContainer, SearchFragment::class.java, fragmentArgs) + addFragment(R.id.searchFragmentContainer, SearchFragment::class.java, fragmentArgs, FRAGMENT_TAG) } + searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener { + override fun onQueryTextSubmit(query: String): Boolean { + searchFragment?.search(query) + return true + } + + override fun onQueryTextChange(newText: String): Boolean { + return true + } + }) + // Open the keyboard immediately + searchView.requestFocus() } companion object { + private const val FRAGMENT_TAG = "SearchFragment" + fun newIntent(context: Context, args: SearchArgs): Intent { return Intent(context, SearchActivity::class.java).apply { putExtra(MvRx.KEY_ARG, args) diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/search/SearchFragment.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/search/SearchFragment.kt index f5b8440b58..0a0604eb38 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/search/SearchFragment.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/search/SearchFragment.kt @@ -24,6 +24,7 @@ import com.airbnb.mvrx.fragmentViewModel import im.vector.app.R import im.vector.app.core.platform.VectorBaseFragment import kotlinx.android.parcel.Parcelize +import timber.log.Timber import javax.inject.Inject @Parcelize @@ -43,4 +44,8 @@ class SearchFragment @Inject constructor( override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) } + + fun search(query: String) { + Timber.d(query) + } } diff --git a/vector/src/main/res/layout/activity_search.xml b/vector/src/main/res/layout/activity_search.xml new file mode 100644 index 0000000000..a3aa21eecc --- /dev/null +++ b/vector/src/main/res/layout/activity_search.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/vector/src/main/res/layout/fragment_search.xml b/vector/src/main/res/layout/fragment_search.xml index 77d9ef65f8..2916089675 100644 --- a/vector/src/main/res/layout/fragment_search.xml +++ b/vector/src/main/res/layout/fragment_search.xml @@ -1,6 +1,9 @@ + + \ No newline at end of file