fetch contacts at app startup only once

This commit is contained in:
tibbi 2018-03-19 11:53:20 +01:00
parent f74376e8ac
commit 53b0632edd
9 changed files with 68 additions and 53 deletions

View File

@ -130,10 +130,9 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
initFragments()
}
contacts_fragment?.refreshContacts()
contacts_fragment?.onActivityResume()
favorites_fragment?.refreshContacts()
favorites_fragment?.onActivityResume()
refreshContacts(true, true)
}
if (hasPermission(PERMISSION_WRITE_CONTACTS)) {
@ -267,7 +266,6 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
private fun getInactiveTabIndexes(activeIndex: Int) = arrayListOf(0, 1, 2).filter { it != activeIndex }
private fun initFragments() {
viewpager.adapter = ViewPagerAdapter(this)
viewpager.offscreenPageLimit = 2
viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrollStateChanged(state: Int) {
@ -288,6 +286,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
}
})
viewpager.currentItem = config.lastUsedViewPagerPage
refreshContacts(true, true)
main_tabs_holder.onTabSelectionChanged(
tabUnselectedAction = {
@ -306,15 +305,14 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
private fun showSortingDialog() {
ChangeSortingDialog(this) {
contacts_fragment?.refreshContacts()
favorites_fragment?.refreshContacts()
refreshContacts(true, true)
}
}
fun showFilterDialog() {
FilterContactSourcesDialog(this) {
contacts_fragment?.forceListRedraw = true
contacts_fragment?.refreshContacts()
refreshContacts(true, false)
}
}
@ -336,7 +334,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
ImportContactsDialog(this, path) {
if (it) {
runOnUiThread {
refreshContacts()
refreshContacts(true, true)
}
}
}
@ -399,13 +397,32 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
BuildConfig.VERSION_NAME, faqItems)
}
override fun refreshContacts() {
contacts_fragment.refreshContacts()
favorites_fragment.refreshContacts()
override fun refreshContacts(refreshContactsTab: Boolean, refreshFavoritesTab: Boolean) {
if (isActivityDestroyed()) {
return
}
ContactsHelper(this).getContacts {
if (isActivityDestroyed()) {
return@getContacts
}
if (viewpager.adapter == null) {
viewpager.adapter = ViewPagerAdapter(this, it)
}
if (refreshContactsTab) {
contacts_fragment?.refreshContacts(it)
}
if (refreshFavoritesTab) {
favorites_fragment?.refreshContacts(it)
}
}
}
override fun refreshFavorites() {
favorites_fragment?.refreshContacts()
refreshContacts(false, true)
}
private fun checkWhatsNewDialog() {

View File

@ -130,7 +130,7 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: ArrayList<Cont
ContactsHelper(activity).deleteContacts(contactsToRemove)
if (contactItems.isEmpty()) {
listener?.refreshContacts()
listener?.refreshContacts(true, true)
finishActMode()
} else {
removeSelectedItems()

View File

@ -5,15 +5,19 @@ import android.view.View
import android.view.ViewGroup
import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.activities.MainActivity
import com.simplemobiletools.contacts.fragments.MyViewPagerFragment
import com.simplemobiletools.contacts.interfaces.FragmentInterface
import com.simplemobiletools.contacts.models.Contact
class ViewPagerAdapter(val activity: MainActivity) : PagerAdapter() {
class ViewPagerAdapter(val activity: MainActivity, val contacts: ArrayList<Contact>) : PagerAdapter() {
override fun instantiateItem(container: ViewGroup, position: Int): Any {
val layout = getFragment(position)
val view = activity.layoutInflater.inflate(layout, container, false)
container.addView(view)
(view as? MyViewPagerFragment)?.setupFragment(activity)
(view as FragmentInterface).apply {
setupFragment(activity)
refreshContacts(contacts)
}
return view
}

View File

@ -16,7 +16,7 @@ class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPa
private fun showAddFavoritesDialog() {
AddFavoritesDialog(activity!!) {
refreshContacts()
activity!!.refreshContacts(false, true)
}
}
}

View File

@ -5,6 +5,7 @@ import android.support.design.widget.CoordinatorLayout
import android.util.AttributeSet
import com.simplemobiletools.contacts.activities.MainActivity
import com.simplemobiletools.contacts.interfaces.FragmentInterface
import com.simplemobiletools.contacts.models.Contact
class GroupsFragment(context: Context, attributeSet: AttributeSet) : CoordinatorLayout(context, attributeSet), FragmentInterface {
override fun setupFragment(activity: MainActivity) {
@ -16,6 +17,6 @@ class GroupsFragment(context: Context, attributeSet: AttributeSet) : Coordinator
override fun primaryColorChanged(color: Int) {
}
override fun refreshContacts() {
override fun refreshContacts(contacts: ArrayList<Contact>) {
}
}

View File

@ -15,7 +15,10 @@ import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.extensions.editContact
import com.simplemobiletools.contacts.extensions.tryStartCall
import com.simplemobiletools.contacts.extensions.viewContact
import com.simplemobiletools.contacts.helpers.*
import com.simplemobiletools.contacts.helpers.Config
import com.simplemobiletools.contacts.helpers.ON_CLICK_CALL_CONTACT
import com.simplemobiletools.contacts.helpers.ON_CLICK_EDIT_CONTACT
import com.simplemobiletools.contacts.helpers.ON_CLICK_VIEW_CONTACT
import com.simplemobiletools.contacts.interfaces.FragmentInterface
import com.simplemobiletools.contacts.models.Contact
import kotlinx.android.synthetic.main.fragment_layout.view.*
@ -48,8 +51,6 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
fragment_placeholder_2.text = activity.getString(R.string.add_favorites)
}
}
refreshContacts()
}
override fun textColorChanged(color: Int) {
@ -67,45 +68,34 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
fun startNameWithSurnameChanged(startNameWithSurname: Boolean) {
(fragment_list.adapter as ContactsAdapter).apply {
config.sorting = if (startNameWithSurname) SORT_BY_SURNAME else SORT_BY_FIRST_NAME
refreshContacts()
this@MyViewPagerFragment.activity!!.refreshContacts(true, true)
}
}
override fun refreshContacts() {
if (activity == null || activity!!.isActivityDestroyed()) {
return
override fun refreshContacts(contacts: ArrayList<Contact>) {
if (config.lastUsedContactSource.isEmpty()) {
val grouped = contacts.groupBy { it.source }.maxWith(compareBy { it.value.size })
config.lastUsedContactSource = grouped?.key ?: ""
}
ContactsHelper(activity!!).getContacts {
var contacts = it
if (activity == null || activity!!.isActivityDestroyed()) {
return@getContacts
}
if (config.lastUsedContactSource.isEmpty()) {
val grouped = contacts.groupBy { it.source }.maxWith(compareBy { it.value.size })
config.lastUsedContactSource = grouped?.key ?: ""
}
contacts = if (this is FavoritesFragment) {
contacts.filter { it.starred == 1 } as ArrayList<Contact>
val filtered = if (this is FavoritesFragment) {
contacts.filter { it.starred == 1 } as ArrayList<Contact>
} else {
val contactSources = config.displayContactSources
if (config.showAllContacts()) {
contacts
} else {
val contactSources = config.displayContactSources
if (config.showAllContacts()) {
contacts
} else {
contacts.filter { contactSources.contains(it.source) } as ArrayList<Contact>
}
contacts.filter { contactSources.contains(it.source) } as ArrayList<Contact>
}
}
Contact.sorting = config.sorting
contacts.sort()
Contact.sorting = config.sorting
filtered.sort()
if (contacts.hashCode() != lastHashCode) {
lastHashCode = contacts.hashCode()
activity!!.runOnUiThread {
setupContacts(contacts)
}
if (filtered.hashCode() != lastHashCode) {
lastHashCode = filtered.hashCode()
activity?.runOnUiThread {
setupContacts(filtered)
}
}
}

View File

@ -116,7 +116,9 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
var resultContacts = ArrayList<Contact>(contactsSize)
(0 until contactsSize).mapTo(resultContacts) { contacts.valueAt(it) }
resultContacts = resultContacts.distinctBy { it.contactId } as ArrayList<Contact>
callback(resultContacts)
activity.runOnUiThread {
callback(resultContacts)
}
}.start()
}

View File

@ -1,6 +1,7 @@
package com.simplemobiletools.contacts.interfaces
import com.simplemobiletools.contacts.activities.MainActivity
import com.simplemobiletools.contacts.models.Contact
interface FragmentInterface {
fun setupFragment(activity: MainActivity)
@ -9,5 +10,5 @@ interface FragmentInterface {
fun primaryColorChanged(color: Int)
fun refreshContacts()
fun refreshContacts(contacts: ArrayList<Contact>)
}

View File

@ -1,7 +1,7 @@
package com.simplemobiletools.contacts.interfaces
interface RefreshContactsListener {
fun refreshContacts()
fun refreshContacts(refreshContactsTab: Boolean, refreshFavoritesTab: Boolean)
fun refreshFavorites()
}