move the contact click handling in the proper activities

This commit is contained in:
tibbi 2019-12-06 20:49:12 +01:00
parent 9aac2414fd
commit 6ea7994a08
10 changed files with 71 additions and 19 deletions

View File

@ -123,6 +123,10 @@ class GroupContactsActivity : SimpleActivity(), RemoveFromGroupListener, Refresh
refreshContacts()
}
override fun contactClicked(contact: Contact) {
handleGenericContactClick(contact)
}
override fun removeFromGroup(contacts: ArrayList<Contact>) {
ensureBackgroundThread {
removeContactsFromGroup(contacts, group.id!!)

View File

@ -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) {
}
}

View File

@ -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() {

View File

@ -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<Int>, 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

View File

@ -101,7 +101,7 @@ fun BaseSimpleActivity.shareContacts(contacts: ArrayList<Contact>) {
}
}
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)

View File

@ -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()
}
}

View File

@ -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)
}
}
}

View File

@ -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)
}
}
}

View File

@ -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<Contact>()
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
}

View File

@ -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)
}