Skip to content

Commit 9ba58c1

Browse files
committed
rewriting insert/edit activity to new search
1 parent 86089c9 commit 9ba58c1

File tree

4 files changed

+62
-105
lines changed

4 files changed

+62
-105
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,6 @@
4242
android:configChanges="orientation"
4343
android:exported="true">
4444

45-
<meta-data
46-
android:name="android.app.default_searchable"
47-
android:resource="@xml/searchable" />
48-
49-
<intent-filter>
50-
<action android:name="android.intent.action.SEARCH" />
51-
</intent-filter>
52-
5345
<intent-filter>
5446
<action android:name="android.intent.action.VIEW" />
5547

@@ -166,14 +158,6 @@
166158
android:exported="true"
167159
android:label="@string/select_contact">
168160

169-
<meta-data
170-
android:name="android.app.default_searchable"
171-
android:resource="@xml/searchable" />
172-
173-
<intent-filter>
174-
<action android:name="android.intent.action.SEARCH" />
175-
</intent-filter>
176-
177161
<intent-filter>
178162
<action android:name="android.intent.action.INSERT_OR_EDIT" />
179163
<category android:name="android.intent.category.DEFAULT" />

app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/InsertOrEditContactActivity.kt

Lines changed: 57 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
package com.simplemobiletools.contacts.pro.activities
22

33
import android.app.Activity
4-
import android.app.SearchManager
54
import android.content.ActivityNotFoundException
6-
import android.content.Context
75
import android.content.Intent
86
import android.net.Uri
97
import android.os.Bundle
108
import android.provider.ContactsContract
119
import android.provider.ContactsContract.CommonDataKinds.Email
1210
import android.provider.ContactsContract.CommonDataKinds.Phone
13-
import android.view.Menu
1411
import android.view.MenuItem
1512
import android.widget.ImageView
1613
import android.widget.TextView
17-
import androidx.appcompat.widget.SearchView
18-
import androidx.core.view.MenuItemCompat
1914
import androidx.viewpager.widget.ViewPager
2015
import com.simplemobiletools.commons.extensions.*
2116
import com.simplemobiletools.commons.helpers.*
@@ -55,6 +50,7 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
5550
setContentView(R.layout.activity_insert_edit_contact)
5651
setupOptionsMenu()
5752
isSelectContactIntent = intent.action == Intent.ACTION_PICK
53+
updateMaterialActivityViews(insert_edit_coordinator, insert_edit_contact_holder, useTransparentNavigation = false, useTopSearchMenu = true)
5854

5955
if (isSelectContactIntent) {
6056
specialMimeType = when (intent.data) {
@@ -89,8 +85,8 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
8985

9086
override fun onResume() {
9187
super.onResume()
88+
updateMenuColors()
9289
setupTabColors()
93-
setupToolbar(insert_edit_toolbar, NavigationIcon.None, searchMenuItem = searchMenuItem)
9490
}
9591

9692
override fun onStop() {
@@ -99,8 +95,21 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
9995
}
10096

10197
private fun setupOptionsMenu() {
102-
setupSearch(insert_edit_toolbar.menu)
103-
insert_edit_toolbar.setOnMenuItemClickListener { menuItem ->
98+
insert_edit_menu.getToolbar().inflateMenu(R.menu.menu_insert_or_edit)
99+
insert_edit_menu.toggleHideOnScroll(false)
100+
insert_edit_menu.setupMenu()
101+
102+
insert_edit_menu.onSearchClosedListener = {
103+
getAllFragments().forEach {
104+
it?.onSearchClosed()
105+
}
106+
}
107+
108+
insert_edit_menu.onSearchTextChangedListener = { text ->
109+
getCurrentFragment()?.onSearchQueryChanged(text)
110+
}
111+
112+
insert_edit_menu.getToolbar().setOnMenuItemClickListener { menuItem ->
104113
when (menuItem.itemId) {
105114
R.id.sort -> showSortingDialog()
106115
R.id.filter -> showFilterDialog()
@@ -110,6 +119,11 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
110119
}
111120
}
112121

122+
private fun updateMenuColors() {
123+
updateStatusbarColor(getProperBackgroundColor())
124+
insert_edit_menu.updateColors()
125+
}
126+
113127
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
114128
super.onActivityResult(requestCode, resultCode, resultData)
115129
if (resultCode == Activity.RESULT_OK) {
@@ -118,14 +132,22 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
118132
}
119133
}
120134

135+
override fun onBackPressed() {
136+
if (insert_edit_menu.isSearchOpen) {
137+
insert_edit_menu.closeSearch()
138+
} else {
139+
super.onBackPressed()
140+
}
141+
}
142+
121143
private fun initFragments() {
122144
view_pager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
123145
override fun onPageScrollStateChanged(state: Int) {}
124146

125147
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
126148

127149
override fun onPageSelected(position: Int) {
128-
insert_or_edit_tabs_holder.getTabAt(position)?.select()
150+
insert_edit_tabs_holder.getTabAt(position)?.select()
129151
getAllFragments().forEach {
130152
it?.finishActMode()
131153
}
@@ -145,69 +167,29 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
145167
}
146168

147169
private fun setupTabs() {
148-
insert_or_edit_tabs_holder.removeAllTabs()
170+
insert_edit_tabs_holder.removeAllTabs()
149171
contactsFavoritesList.forEachIndexed { index, value ->
150172
if (config.showTabs and value != 0) {
151-
insert_or_edit_tabs_holder.newTab().setCustomView(R.layout.bottom_tablayout_item).apply {
173+
insert_edit_tabs_holder.newTab().setCustomView(R.layout.bottom_tablayout_item).apply {
152174
customView?.findViewById<ImageView>(R.id.tab_item_icon)?.setImageDrawable(getTabIcon(index))
153175
customView?.findViewById<TextView>(R.id.tab_item_label)?.text = getTabLabel(index)
154-
insert_or_edit_tabs_holder.addTab(this)
176+
insert_edit_tabs_holder.addTab(this)
155177
}
156178
}
157179
}
158180

159-
insert_or_edit_tabs_holder.onTabSelectionChanged(
181+
insert_edit_tabs_holder.onTabSelectionChanged(
160182
tabUnselectedAction = {
161-
updateBottomTabItemColors(it.customView, false)
183+
updateBottomTabItemColors(it.customView, false, getDeselectedTabDrawableIds()[it.position])
162184
},
163185
tabSelectedAction = {
164-
closeSearch()
186+
insert_edit_menu.closeSearch()
165187
view_pager.currentItem = it.position
166-
updateBottomTabItemColors(it.customView, true)
188+
updateBottomTabItemColors(it.customView, true, getSelectedTabDrawableIds()[it.position])
167189
}
168190
)
169191

170-
insert_or_edit_tabs_holder.beGoneIf(insert_or_edit_tabs_holder.tabCount == 1)
171-
}
172-
173-
private fun setupSearch(menu: Menu) {
174-
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
175-
searchMenuItem = menu.findItem(R.id.search)
176-
(searchMenuItem!!.actionView as SearchView).apply {
177-
setSearchableInfo(searchManager.getSearchableInfo(componentName))
178-
isSubmitButtonEnabled = false
179-
queryHint = getString(getSearchString())
180-
setOnQueryTextListener(object : SearchView.OnQueryTextListener {
181-
override fun onQueryTextSubmit(query: String) = false
182-
183-
override fun onQueryTextChange(newText: String): Boolean {
184-
if (isSearchOpen) {
185-
getCurrentFragment()?.onSearchQueryChanged(newText)
186-
}
187-
return true
188-
}
189-
})
190-
}
191-
192-
MenuItemCompat.setOnActionExpandListener(searchMenuItem, object : MenuItemCompat.OnActionExpandListener {
193-
override fun onMenuItemActionExpand(item: MenuItem?): Boolean {
194-
isSearchOpen = true
195-
return true
196-
}
197-
198-
override fun onMenuItemActionCollapse(item: MenuItem?): Boolean {
199-
getCurrentFragment()?.onSearchClosed()
200-
isSearchOpen = false
201-
return true
202-
}
203-
})
204-
}
205-
206-
private fun getSearchString(): Int {
207-
return when (getCurrentFragment()) {
208-
favorites_fragment -> R.string.search_favorites
209-
else -> R.string.search_contacts
210-
}
192+
insert_edit_tabs_holder.beGoneIf(insert_edit_tabs_holder.tabCount == 1)
211193
}
212194

213195
private fun getCurrentFragment(): MyViewPagerFragment? {
@@ -221,20 +203,30 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
221203
private fun getAllFragments() = arrayListOf(contacts_fragment, favorites_fragment)
222204

223205
private fun setupTabColors() {
224-
val activeView = insert_or_edit_tabs_holder.getTabAt(view_pager.currentItem)?.customView
225-
updateBottomTabItemColors(activeView, true)
206+
val activeView = insert_edit_tabs_holder.getTabAt(view_pager.currentItem)?.customView
207+
updateBottomTabItemColors(activeView, true, getSelectedTabDrawableIds()[view_pager.currentItem])
226208

227209
getInactiveTabIndexes(view_pager.currentItem).forEach { index ->
228-
val inactiveView = insert_or_edit_tabs_holder.getTabAt(index)?.customView
229-
updateBottomTabItemColors(inactiveView, false)
210+
val inactiveView = insert_edit_tabs_holder.getTabAt(index)?.customView
211+
updateBottomTabItemColors(inactiveView, false, getDeselectedTabDrawableIds()[index])
230212
}
231213

232214
val bottomBarColor = getBottomNavigationBackgroundColor()
233-
insert_or_edit_tabs_holder.setBackgroundColor(bottomBarColor)
215+
insert_edit_tabs_holder.setBackgroundColor(bottomBarColor)
234216
updateNavigationBarColor(bottomBarColor)
235217
}
236218

237-
private fun getInactiveTabIndexes(activeIndex: Int) = (0 until contactsFavoritesList.size).filter { it != activeIndex }
219+
private fun getInactiveTabIndexes(activeIndex: Int) = (0 until insert_edit_tabs_holder.tabCount).filter { it != activeIndex }
220+
221+
private fun getSelectedTabDrawableIds() = arrayOf(
222+
R.drawable.ic_person_vector,
223+
R.drawable.ic_star_vector
224+
)
225+
226+
private fun getDeselectedTabDrawableIds() = arrayOf(
227+
R.drawable.ic_person_outline_vector,
228+
R.drawable.ic_star_outline_vector
229+
)
238230

239231
override fun refreshContacts(refreshTabsMask: Int) {
240232
if (isDestroyed || isFinishing) {
@@ -270,10 +262,12 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
270262
}
271263

272264
if (refreshTabsMask and TAB_CONTACTS != 0) {
265+
contacts_fragment?.skipHashComparing = true
273266
contacts_fragment?.refreshContacts(contacts, placeholderText)
274267
}
275268

276269
if (refreshTabsMask and TAB_FAVORITES != 0) {
270+
favorites_fragment?.skipHashComparing = true
277271
favorites_fragment?.refreshContacts(contacts, placeholderText)
278272
}
279273
}
@@ -350,10 +344,6 @@ class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener {
350344
}
351345
}
352346

353-
fun fabClicked() {
354-
createNewContact()
355-
}
356-
357347
private fun closeSearch() {
358348
if (isSearchOpen) {
359349
getAllFragments().forEach {

app/src/main/res/layout/activity_insert_edit_contact.xml

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,10 @@
55
android:layout_width="match_parent"
66
android:layout_height="match_parent">
77

8-
<com.google.android.material.appbar.AppBarLayout
9-
android:id="@+id/insert_edit_app_bar_layout"
8+
<com.simplemobiletools.commons.views.MySearchMenu
9+
android:id="@+id/insert_edit_menu"
1010
android:layout_width="match_parent"
11-
android:layout_height="wrap_content">
12-
13-
<com.google.android.material.appbar.MaterialToolbar
14-
android:id="@+id/insert_edit_toolbar"
15-
android:layout_width="match_parent"
16-
android:layout_height="?attr/actionBarSize"
17-
android:background="@color/color_primary"
18-
app:menu="@menu/menu_insert_or_edit"
19-
app:title="@string/select_contact"
20-
app:titleTextAppearance="@style/AppTheme.ActionBar.TitleTextStyle" />
21-
22-
</com.google.android.material.appbar.AppBarLayout>
11+
android:layout_height="wrap_content" />
2312

2413
<RelativeLayout
2514
android:id="@+id/insert_edit_contact_holder"
@@ -77,11 +66,11 @@
7766
android:id="@+id/view_pager"
7867
android:layout_width="match_parent"
7968
android:layout_height="match_parent"
80-
android:layout_above="@+id/insert_or_edit_tabs_holder"
69+
android:layout_above="@+id/insert_edit_tabs_holder"
8170
android:layout_below="@+id/select_contact_label" />
8271

8372
<com.google.android.material.tabs.TabLayout
84-
android:id="@+id/insert_or_edit_tabs_holder"
73+
android:id="@+id/insert_edit_tabs_holder"
8574
android:layout_width="match_parent"
8675
android:layout_height="wrap_content"
8776
android:layout_alignParentBottom="true"

app/src/main/res/menu/menu_insert_or_edit.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
xmlns:tools="http://schemas.android.com/tools"
55
tools:ignore="AppCompatResource,AlwaysShowAction">
6-
<item
7-
android:id="@+id/search"
8-
android:icon="@drawable/ic_search_vector"
9-
android:title="@string/search"
10-
app:actionViewClass="androidx.appcompat.widget.SearchView"
11-
app:showAsAction="collapseActionView|always" />
126
<item
137
android:id="@+id/sort"
148
android:icon="@drawable/ic_sort_vector"

0 commit comments

Comments
 (0)