From 6ea7994a083f939b3af118b8f8c3caeebe6fca2b Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 6 Dec 2019 20:49:12 +0100 Subject: [PATCH] move the contact click handling in the proper activities --- .../pro/activities/GroupContactsActivity.kt | 4 ++ .../activities/InsertOrEditContactActivity.kt | 40 ++++++++++++++++++- .../contacts/pro/activities/MainActivity.kt | 8 +++- .../contacts/pro/adapters/ViewPagerAdapter.kt | 9 ++--- .../contacts/pro/extensions/Activity.kt | 2 +- .../pro/fragments/ContactsFragment.kt | 3 +- .../pro/fragments/FavoritesFragment.kt | 3 +- .../contacts/pro/fragments/GroupsFragment.kt | 3 +- .../pro/fragments/MyViewPagerFragment.kt | 14 +++---- .../pro/interfaces/RefreshContactsListener.kt | 4 ++ 10 files changed, 71 insertions(+), 19 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/GroupContactsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/GroupContactsActivity.kt index e2a2814d..8d034e14 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/GroupContactsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/GroupContactsActivity.kt @@ -123,6 +123,10 @@ class GroupContactsActivity : SimpleActivity(), RemoveFromGroupListener, Refresh refreshContacts() } + override fun contactClicked(contact: Contact) { + handleGenericContactClick(contact) + } + override fun removeFromGroup(contacts: ArrayList) { ensureBackgroundThread { removeContactsFromGroup(contacts, group.id!!) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/InsertOrEditContactActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/InsertOrEditContactActivity.kt index 26c1ab93..2c2c9f18 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/InsertOrEditContactActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/InsertOrEditContactActivity.kt @@ -9,12 +9,18 @@ import com.simplemobiletools.commons.helpers.PERMISSION_GET_ACCOUNTS import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CONTACTS import com.simplemobiletools.contacts.pro.R +import com.simplemobiletools.contacts.pro.adapters.ViewPagerAdapter import com.simplemobiletools.contacts.pro.extensions.config import com.simplemobiletools.contacts.pro.helpers.CONTACTS_TAB_MASK +import com.simplemobiletools.contacts.pro.helpers.ContactsHelper import com.simplemobiletools.contacts.pro.helpers.FAVORITES_TAB_MASK +import com.simplemobiletools.contacts.pro.interfaces.RefreshContactsListener +import com.simplemobiletools.contacts.pro.models.Contact import kotlinx.android.synthetic.main.activity_insert_edit_contact.* +import kotlinx.android.synthetic.main.fragment_contacts.* +import kotlinx.android.synthetic.main.fragment_favorites.* -class InsertOrEditContactActivity : SimpleActivity() { +class InsertOrEditContactActivity : SimpleActivity(), RefreshContactsListener { private val START_INSERT_ACTIVITY = 1 private val START_EDIT_ACTIVITY = 2 @@ -63,6 +69,10 @@ class InsertOrEditContactActivity : SimpleActivity() { } }) + viewpager.onGlobalLayout { + refreshContacts(CONTACTS_TAB_MASK or FAVORITES_TAB_MASK) + } + insert_or_edit_tabs_holder.onTabSelectionChanged( tabUnselectedAction = { it.icon?.applyColorFilter(config.textColor) @@ -93,4 +103,32 @@ class InsertOrEditContactActivity : SimpleActivity() { setSelectedTabIndicatorColor(getAdjustedPrimaryColor()) } } + + override fun refreshContacts(refreshTabsMask: Int) { + if (isDestroyed || isFinishing) { + return + } + + if (viewpager.adapter == null) { + viewpager.adapter = ViewPagerAdapter(this, contactsFavoritesList, CONTACTS_TAB_MASK or FAVORITES_TAB_MASK) + } + + ContactsHelper(this).getContacts { contacts -> + if (isDestroyed || isFinishing) { + return@getContacts + } + + if (refreshTabsMask and CONTACTS_TAB_MASK != 0) { + contacts_fragment?.refreshContacts(contacts) + } + + if (refreshTabsMask and FAVORITES_TAB_MASK != 0) { + favorites_fragment?.refreshContacts(contacts) + } + } + } + + override fun contactClicked(contact: Contact) { + + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/MainActivity.kt index ce8fcd4e..b052eed5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/MainActivity.kt @@ -33,9 +33,11 @@ import com.simplemobiletools.contacts.pro.dialogs.FilterContactSourcesDialog import com.simplemobiletools.contacts.pro.dialogs.ImportContactsDialog import com.simplemobiletools.contacts.pro.extensions.config import com.simplemobiletools.contacts.pro.extensions.getTempFile +import com.simplemobiletools.contacts.pro.extensions.handleGenericContactClick import com.simplemobiletools.contacts.pro.fragments.MyViewPagerFragment import com.simplemobiletools.contacts.pro.helpers.* import com.simplemobiletools.contacts.pro.interfaces.RefreshContactsListener +import com.simplemobiletools.contacts.pro.models.Contact import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.fragment_contacts.* import kotlinx.android.synthetic.main.fragment_favorites.* @@ -530,7 +532,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { isGettingContacts = true if (viewpager.adapter == null) { - viewpager.adapter = ViewPagerAdapter(this) + viewpager.adapter = ViewPagerAdapter(this, tabsList, config.showTabs) viewpager.currentItem = config.lastUsedViewPagerPage } @@ -557,6 +559,10 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { } } + override fun contactClicked(contact: Contact) { + handleGenericContactClick(contact) + } + private fun getAllFragments() = arrayListOf(contacts_fragment, favorites_fragment, groups_fragment) private fun checkWhatsNewDialog() { diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/adapters/ViewPagerAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/adapters/ViewPagerAdapter.kt index 12d0edb3..ab141e80 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/adapters/ViewPagerAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/adapters/ViewPagerAdapter.kt @@ -4,16 +4,13 @@ import android.view.View import android.view.ViewGroup import androidx.viewpager.widget.PagerAdapter import com.simplemobiletools.contacts.pro.R -import com.simplemobiletools.contacts.pro.activities.MainActivity -import com.simplemobiletools.contacts.pro.extensions.config +import com.simplemobiletools.contacts.pro.activities.SimpleActivity import com.simplemobiletools.contacts.pro.fragments.MyViewPagerFragment import com.simplemobiletools.contacts.pro.helpers.CONTACTS_TAB_MASK import com.simplemobiletools.contacts.pro.helpers.FAVORITES_TAB_MASK import com.simplemobiletools.contacts.pro.helpers.GROUPS_TAB_MASK -import com.simplemobiletools.contacts.pro.helpers.tabsList -class ViewPagerAdapter(val activity: MainActivity) : PagerAdapter() { - private val showTabs = activity.config.showTabs +class ViewPagerAdapter(val activity: SimpleActivity, val currTabsList: ArrayList, val showTabs: Int) : PagerAdapter() { override fun instantiateItem(container: ViewGroup, position: Int): Any { val layout = getFragment(position) @@ -31,7 +28,7 @@ class ViewPagerAdapter(val activity: MainActivity) : PagerAdapter() { container.removeView(item as View) } - override fun getCount() = tabsList.filter { it and showTabs != 0 }.size + override fun getCount() = currTabsList.filter { it and showTabs != 0 }.size override fun isViewFromObject(view: View, item: Any) = view == item diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/Activity.kt index c576b080..9995e35c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/Activity.kt @@ -101,7 +101,7 @@ fun BaseSimpleActivity.shareContacts(contacts: ArrayList) { } } -fun SimpleActivity.contactClicked(contact: Contact) { +fun SimpleActivity.handleGenericContactClick(contact: Contact) { when (config.onContactClick) { ON_CLICK_CALL_CONTACT -> callContact(contact) ON_CLICK_VIEW_CONTACT -> viewContact(contact) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/ContactsFragment.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/ContactsFragment.kt index dd0009d5..17a1b53c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/ContactsFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/ContactsFragment.kt @@ -4,6 +4,7 @@ import android.content.Context import android.content.Intent import android.util.AttributeSet import com.simplemobiletools.contacts.pro.activities.EditContactActivity +import com.simplemobiletools.contacts.pro.activities.MainActivity class ContactsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet) { override fun fabClicked() { @@ -13,6 +14,6 @@ class ContactsFragment(context: Context, attributeSet: AttributeSet) : MyViewPag } override fun placeholderClicked() { - activity!!.showFilterDialog() + (activity as? MainActivity)?.showFilterDialog() } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/FavoritesFragment.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/FavoritesFragment.kt index eb18ae40..66be621f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/FavoritesFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/FavoritesFragment.kt @@ -2,6 +2,7 @@ package com.simplemobiletools.contacts.pro.fragments import android.content.Context import android.util.AttributeSet +import com.simplemobiletools.contacts.pro.activities.MainActivity import com.simplemobiletools.contacts.pro.activities.SimpleActivity import com.simplemobiletools.contacts.pro.dialogs.SelectContactsDialog import com.simplemobiletools.contacts.pro.helpers.ContactsHelper @@ -24,7 +25,7 @@ class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPa removeFavorites(removedContacts) } - activity!!.refreshContacts(FAVORITES_TAB_MASK) + (activity as? MainActivity)?.refreshContacts(FAVORITES_TAB_MASK) } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/GroupsFragment.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/GroupsFragment.kt index 87178c5f..22ba261a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/GroupsFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/GroupsFragment.kt @@ -2,6 +2,7 @@ package com.simplemobiletools.contacts.pro.fragments import android.content.Context import android.util.AttributeSet +import com.simplemobiletools.contacts.pro.activities.MainActivity import com.simplemobiletools.contacts.pro.activities.SimpleActivity import com.simplemobiletools.contacts.pro.dialogs.CreateNewGroupDialog import com.simplemobiletools.contacts.pro.helpers.GROUPS_TAB_MASK @@ -18,7 +19,7 @@ class GroupsFragment(context: Context, attributeSet: AttributeSet) : MyViewPager private fun showNewGroupsDialog() { CreateNewGroupDialog(activity as SimpleActivity) { - activity!!.refreshContacts(GROUPS_TAB_MASK) + (activity as? MainActivity)?.refreshContacts(GROUPS_TAB_MASK) } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/MyViewPagerFragment.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/MyViewPagerFragment.kt index a64d93c9..5371be75 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/MyViewPagerFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/fragments/MyViewPagerFragment.kt @@ -16,15 +16,15 @@ import com.simplemobiletools.contacts.pro.activities.SimpleActivity import com.simplemobiletools.contacts.pro.adapters.ContactsAdapter import com.simplemobiletools.contacts.pro.adapters.GroupsAdapter import com.simplemobiletools.contacts.pro.extensions.config -import com.simplemobiletools.contacts.pro.extensions.contactClicked import com.simplemobiletools.contacts.pro.extensions.getVisibleContactSources import com.simplemobiletools.contacts.pro.helpers.* +import com.simplemobiletools.contacts.pro.interfaces.RefreshContactsListener import com.simplemobiletools.contacts.pro.models.Contact import com.simplemobiletools.contacts.pro.models.Group import kotlinx.android.synthetic.main.fragment_layout.view.* abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) : CoordinatorLayout(context, attributeSet) { - protected var activity: MainActivity? = null + protected var activity: SimpleActivity? = null protected var allContacts = ArrayList() private var lastHashCode = 0 @@ -35,7 +35,7 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) var skipHashComparing = false var forceListRedraw = false - fun setupFragment(activity: MainActivity) { + fun setupFragment(activity: SimpleActivity) { config = activity.config if (this.activity == null) { this.activity = activity @@ -89,7 +89,7 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) if (this !is GroupsFragment) { (fragment_list.adapter as? ContactsAdapter)?.apply { config.sorting = if (startNameWithSurname) SORT_BY_SURNAME else SORT_BY_FIRST_NAME - this@MyViewPagerFragment.activity!!.refreshContacts(CONTACTS_TAB_MASK or FAVORITES_TAB_MASK) + (this@MyViewPagerFragment.activity!! as MainActivity).refreshContacts(CONTACTS_TAB_MASK or FAVORITES_TAB_MASK) } } } @@ -156,7 +156,7 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) val currAdapter = fragment_list.adapter if (currAdapter == null) { - GroupsAdapter(activity as SimpleActivity, storedGroups, activity, fragment_list, fragment_fastscroller) { + GroupsAdapter(activity as SimpleActivity, storedGroups, activity as RefreshContactsListener, fragment_list, fragment_fastscroller) { Intent(activity, GroupContactsActivity::class.java).apply { putExtra(GROUP, it as Group) activity!!.startActivity(this) @@ -187,8 +187,8 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) if (currAdapter == null || forceListRedraw) { forceListRedraw = false val location = if (this is FavoritesFragment) LOCATION_FAVORITES_TAB else LOCATION_CONTACTS_TAB - ContactsAdapter(activity as SimpleActivity, contacts, activity, location, null, fragment_list, fragment_fastscroller) { - activity?.contactClicked(it as Contact) + ContactsAdapter(activity as SimpleActivity, contacts, activity as RefreshContactsListener, location, null, fragment_list, fragment_fastscroller) { + (activity as RefreshContactsListener).contactClicked(it as Contact) }.apply { fragment_list.adapter = this } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/interfaces/RefreshContactsListener.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/interfaces/RefreshContactsListener.kt index 0d59b444..1a917f56 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/interfaces/RefreshContactsListener.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/interfaces/RefreshContactsListener.kt @@ -1,5 +1,9 @@ package com.simplemobiletools.contacts.pro.interfaces +import com.simplemobiletools.contacts.pro.models.Contact + interface RefreshContactsListener { fun refreshContacts(refreshTabsMask: Int) + + fun contactClicked(contact: Contact) }