119 lines
4.0 KiB
Kotlin
119 lines
4.0 KiB
Kotlin
/* Copyright 2017 Andrew Dawson
|
|
*
|
|
* This file is a part of Tusky.
|
|
*
|
|
* This program 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.
|
|
*
|
|
* Tusky 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 Tusky; if not,
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
package com.keylesspalace.tusky.components.search
|
|
|
|
import android.app.SearchManager
|
|
import android.content.Context
|
|
import android.content.Intent
|
|
import android.os.Bundle
|
|
import android.view.Menu
|
|
import androidx.activity.viewModels
|
|
import androidx.appcompat.widget.SearchView
|
|
import com.google.android.material.tabs.TabLayoutMediator
|
|
import com.keylesspalace.tusky.BottomSheetActivity
|
|
import com.keylesspalace.tusky.R
|
|
import com.keylesspalace.tusky.components.search.adapter.SearchPagerAdapter
|
|
import com.keylesspalace.tusky.databinding.ActivitySearchBinding
|
|
import com.keylesspalace.tusky.di.ViewModelFactory
|
|
import com.keylesspalace.tusky.util.viewBinding
|
|
import dagger.android.DispatchingAndroidInjector
|
|
import dagger.android.HasAndroidInjector
|
|
import javax.inject.Inject
|
|
|
|
class SearchActivity : BottomSheetActivity(), HasAndroidInjector {
|
|
@Inject
|
|
lateinit var androidInjector: DispatchingAndroidInjector<Any>
|
|
|
|
@Inject
|
|
lateinit var viewModelFactory: ViewModelFactory
|
|
|
|
private val viewModel: SearchViewModel by viewModels { viewModelFactory }
|
|
|
|
private val binding by viewBinding(ActivitySearchBinding::inflate)
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
setContentView(binding.root)
|
|
setSupportActionBar(binding.toolbar)
|
|
supportActionBar?.apply {
|
|
setDisplayHomeAsUpEnabled(true)
|
|
setDisplayShowHomeEnabled(true)
|
|
setDisplayShowTitleEnabled(false)
|
|
}
|
|
setupPages()
|
|
handleIntent(intent)
|
|
}
|
|
|
|
private fun setupPages() {
|
|
binding.pages.adapter = SearchPagerAdapter(this)
|
|
|
|
TabLayoutMediator(binding.tabs, binding.pages) {
|
|
tab, position ->
|
|
tab.text = getPageTitle(position)
|
|
}.attach()
|
|
}
|
|
|
|
override fun onNewIntent(intent: Intent) {
|
|
super.onNewIntent(intent)
|
|
handleIntent(intent)
|
|
}
|
|
|
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
|
super.onCreateOptionsMenu(menu)
|
|
|
|
menuInflater.inflate(R.menu.search_toolbar, menu)
|
|
val searchView = menu.findItem(R.id.action_search)
|
|
.actionView as SearchView
|
|
setupSearchView(searchView)
|
|
|
|
searchView.setQuery(viewModel.currentQuery, false)
|
|
|
|
return true
|
|
}
|
|
|
|
private fun getPageTitle(position: Int): CharSequence {
|
|
return when (position) {
|
|
0 -> getString(R.string.title_statuses)
|
|
1 -> getString(R.string.title_accounts)
|
|
2 -> getString(R.string.title_hashtags_dialog)
|
|
else -> throw IllegalArgumentException("Unknown page index: $position")
|
|
}
|
|
}
|
|
|
|
private fun handleIntent(intent: Intent) {
|
|
if (Intent.ACTION_SEARCH == intent.action) {
|
|
viewModel.currentQuery = intent.getStringExtra(SearchManager.QUERY) ?: ""
|
|
viewModel.search(viewModel.currentQuery)
|
|
}
|
|
}
|
|
|
|
private fun setupSearchView(searchView: SearchView) {
|
|
searchView.setIconifiedByDefault(false)
|
|
|
|
searchView.setSearchableInfo((getSystemService(Context.SEARCH_SERVICE) as? SearchManager)?.getSearchableInfo(componentName))
|
|
|
|
searchView.requestFocus()
|
|
|
|
searchView.maxWidth = Integer.MAX_VALUE
|
|
}
|
|
|
|
override fun androidInjector() = androidInjector
|
|
|
|
companion object {
|
|
fun getIntent(context: Context) = Intent(context, SearchActivity::class.java)
|
|
}
|
|
}
|