rewriting insert/edit activity to new search

This commit is contained in:
tibbi 2023-01-04 21:16:39 +01:00
parent 86089c9a2a
commit 9ba58c1cc3
4 changed files with 62 additions and 105 deletions

View File

@ -42,14 +42,6 @@
android:configChanges="orientation"
android:exported="true">
<meta-data
android:name="android.app.default_searchable"
android:resource="@xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
@ -166,14 +158,6 @@
android:exported="true"
android:label="@string/select_contact">
<meta-data
android:name="android.app.default_searchable"
android:resource="@xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.INSERT_OR_EDIT" />
<category android:name="android.intent.category.DEFAULT" />

View File

@ -1,21 +1,16 @@
package com.simplemobiletools.contacts.pro.activities
import android.app.Activity
import android.app.SearchManager
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.ContactsContract
import android.provider.ContactsContract.CommonDataKinds.Email
import android.provider.ContactsContract.CommonDataKinds.Phone
import android.view.Menu
import android.view.MenuItem
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.widget.SearchView
import androidx.core.view.MenuItemCompat
import androidx.viewpager.widget.ViewPager
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.*
@ -55,6 +50,7 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
setContentView(R.layout.activity_insert_edit_contact)
setupOptionsMenu()
isSelectContactIntent = intent.action == Intent.ACTION_PICK
updateMaterialActivityViews(insert_edit_coordinator, insert_edit_contact_holder, useTransparentNavigation = false, useTopSearchMenu = true)
if (isSelectContactIntent) {
specialMimeType = when (intent.data) {
@ -89,8 +85,8 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
override fun onResume() {
super.onResume()
updateMenuColors()
setupTabColors()
setupToolbar(insert_edit_toolbar, NavigationIcon.None, searchMenuItem = searchMenuItem)
}
override fun onStop() {
@ -99,8 +95,21 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
}
private fun setupOptionsMenu() {
setupSearch(insert_edit_toolbar.menu)
insert_edit_toolbar.setOnMenuItemClickListener { menuItem ->
insert_edit_menu.getToolbar().inflateMenu(R.menu.menu_insert_or_edit)
insert_edit_menu.toggleHideOnScroll(false)
insert_edit_menu.setupMenu()
insert_edit_menu.onSearchClosedListener = {
getAllFragments().forEach {
it?.onSearchClosed()
}
}
insert_edit_menu.onSearchTextChangedListener = { text ->
getCurrentFragment()?.onSearchQueryChanged(text)
}
insert_edit_menu.getToolbar().setOnMenuItemClickListener { menuItem ->
when (menuItem.itemId) {
R.id.sort -> showSortingDialog()
R.id.filter -> showFilterDialog()
@ -110,6 +119,11 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
}
}
private fun updateMenuColors() {
updateStatusbarColor(getProperBackgroundColor())
insert_edit_menu.updateColors()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
super.onActivityResult(requestCode, resultCode, resultData)
if (resultCode == Activity.RESULT_OK) {
@ -118,6 +132,14 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
}
}
override fun onBackPressed() {
if (insert_edit_menu.isSearchOpen) {
insert_edit_menu.closeSearch()
} else {
super.onBackPressed()
}
}
private fun initFragments() {
view_pager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrollStateChanged(state: Int) {}
@ -125,7 +147,7 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
override fun onPageSelected(position: Int) {
insert_or_edit_tabs_holder.getTabAt(position)?.select()
insert_edit_tabs_holder.getTabAt(position)?.select()
getAllFragments().forEach {
it?.finishActMode()
}
@ -145,69 +167,29 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
}
private fun setupTabs() {
insert_or_edit_tabs_holder.removeAllTabs()
insert_edit_tabs_holder.removeAllTabs()
contactsFavoritesList.forEachIndexed { index, value ->
if (config.showTabs and value != 0) {
insert_or_edit_tabs_holder.newTab().setCustomView(R.layout.bottom_tablayout_item).apply {
insert_edit_tabs_holder.newTab().setCustomView(R.layout.bottom_tablayout_item).apply {
customView?.findViewById<ImageView>(R.id.tab_item_icon)?.setImageDrawable(getTabIcon(index))
customView?.findViewById<TextView>(R.id.tab_item_label)?.text = getTabLabel(index)
insert_or_edit_tabs_holder.addTab(this)
insert_edit_tabs_holder.addTab(this)
}
}
}
insert_or_edit_tabs_holder.onTabSelectionChanged(
insert_edit_tabs_holder.onTabSelectionChanged(
tabUnselectedAction = {
updateBottomTabItemColors(it.customView, false)
updateBottomTabItemColors(it.customView, false, getDeselectedTabDrawableIds()[it.position])
},
tabSelectedAction = {
closeSearch()
insert_edit_menu.closeSearch()
view_pager.currentItem = it.position
updateBottomTabItemColors(it.customView, true)
updateBottomTabItemColors(it.customView, true, getSelectedTabDrawableIds()[it.position])
}
)
insert_or_edit_tabs_holder.beGoneIf(insert_or_edit_tabs_holder.tabCount == 1)
}
private fun setupSearch(menu: Menu) {
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
searchMenuItem = menu.findItem(R.id.search)
(searchMenuItem!!.actionView as SearchView).apply {
setSearchableInfo(searchManager.getSearchableInfo(componentName))
isSubmitButtonEnabled = false
queryHint = getString(getSearchString())
setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String) = false
override fun onQueryTextChange(newText: String): Boolean {
if (isSearchOpen) {
getCurrentFragment()?.onSearchQueryChanged(newText)
}
return true
}
})
}
MenuItemCompat.setOnActionExpandListener(searchMenuItem, object : MenuItemCompat.OnActionExpandListener {
override fun onMenuItemActionExpand(item: MenuItem?): Boolean {
isSearchOpen = true
return true
}
override fun onMenuItemActionCollapse(item: MenuItem?): Boolean {
getCurrentFragment()?.onSearchClosed()
isSearchOpen = false
return true
}
})
}
private fun getSearchString(): Int {
return when (getCurrentFragment()) {
favorites_fragment -> R.string.search_favorites
else -> R.string.search_contacts
}
insert_edit_tabs_holder.beGoneIf(insert_edit_tabs_holder.tabCount == 1)
}
private fun getCurrentFragment(): MyViewPagerFragment? {
@ -221,20 +203,30 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
private fun getAllFragments() = arrayListOf(contacts_fragment, favorites_fragment)
private fun setupTabColors() {
val activeView = insert_or_edit_tabs_holder.getTabAt(view_pager.currentItem)?.customView
updateBottomTabItemColors(activeView, true)
val activeView = insert_edit_tabs_holder.getTabAt(view_pager.currentItem)?.customView
updateBottomTabItemColors(activeView, true, getSelectedTabDrawableIds()[view_pager.currentItem])
getInactiveTabIndexes(view_pager.currentItem).forEach { index ->
val inactiveView = insert_or_edit_tabs_holder.getTabAt(index)?.customView
updateBottomTabItemColors(inactiveView, false)
val inactiveView = insert_edit_tabs_holder.getTabAt(index)?.customView
updateBottomTabItemColors(inactiveView, false, getDeselectedTabDrawableIds()[index])
}
val bottomBarColor = getBottomNavigationBackgroundColor()
insert_or_edit_tabs_holder.setBackgroundColor(bottomBarColor)
insert_edit_tabs_holder.setBackgroundColor(bottomBarColor)
updateNavigationBarColor(bottomBarColor)
}
private fun getInactiveTabIndexes(activeIndex: Int) = (0 until contactsFavoritesList.size).filter { it != activeIndex }
private fun getInactiveTabIndexes(activeIndex: Int) = (0 until insert_edit_tabs_holder.tabCount).filter { it != activeIndex }
private fun getSelectedTabDrawableIds() = arrayOf(
R.drawable.ic_person_vector,
R.drawable.ic_star_vector
)
private fun getDeselectedTabDrawableIds() = arrayOf(
R.drawable.ic_person_outline_vector,
R.drawable.ic_star_outline_vector
)
override fun refreshContacts(refreshTabsMask: Int) {
if (isDestroyed || isFinishing) {
@ -270,10 +262,12 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
}
if (refreshTabsMask and TAB_CONTACTS != 0) {
contacts_fragment?.skipHashComparing = true
contacts_fragment?.refreshContacts(contacts, placeholderText)
}
if (refreshTabsMask and TAB_FAVORITES != 0) {
favorites_fragment?.skipHashComparing = true
favorites_fragment?.refreshContacts(contacts, placeholderText)
}
}
@ -350,10 +344,6 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
}
}
fun fabClicked() {
createNewContact()
}
private fun closeSearch() {
if (isSearchOpen) {
getAllFragments().forEach {

View File

@ -5,21 +5,10 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/insert_edit_app_bar_layout"
<com.simplemobiletools.commons.views.MySearchMenu
android:id="@+id/insert_edit_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/insert_edit_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/color_primary"
app:menu="@menu/menu_insert_or_edit"
app:title="@string/select_contact"
app:titleTextAppearance="@style/AppTheme.ActionBar.TitleTextStyle" />
</com.google.android.material.appbar.AppBarLayout>
android:layout_height="wrap_content" />
<RelativeLayout
android:id="@+id/insert_edit_contact_holder"
@ -77,11 +66,11 @@
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/insert_or_edit_tabs_holder"
android:layout_above="@+id/insert_edit_tabs_holder"
android:layout_below="@+id/select_contact_label" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/insert_or_edit_tabs_holder"
android:id="@+id/insert_edit_tabs_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"

View File

@ -3,12 +3,6 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="AppCompatResource,AlwaysShowAction">
<item
android:id="@+id/search"
android:icon="@drawable/ic_search_vector"
android:title="@string/search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="collapseActionView|always" />
<item
android:id="@+id/sort"
android:icon="@drawable/ic_sort_vector"