mirror of
https://github.com/SimpleMobileTools/Simple-Contacts.git
synced 2025-06-05 21:59:27 +02:00
fetch contacts at app startup only once
This commit is contained in:
@ -130,10 +130,9 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
|
|||||||
initFragments()
|
initFragments()
|
||||||
}
|
}
|
||||||
|
|
||||||
contacts_fragment?.refreshContacts()
|
|
||||||
contacts_fragment?.onActivityResume()
|
contacts_fragment?.onActivityResume()
|
||||||
favorites_fragment?.refreshContacts()
|
|
||||||
favorites_fragment?.onActivityResume()
|
favorites_fragment?.onActivityResume()
|
||||||
|
refreshContacts(true, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasPermission(PERMISSION_WRITE_CONTACTS)) {
|
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 getInactiveTabIndexes(activeIndex: Int) = arrayListOf(0, 1, 2).filter { it != activeIndex }
|
||||||
|
|
||||||
private fun initFragments() {
|
private fun initFragments() {
|
||||||
viewpager.adapter = ViewPagerAdapter(this)
|
|
||||||
viewpager.offscreenPageLimit = 2
|
viewpager.offscreenPageLimit = 2
|
||||||
viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
|
viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
|
||||||
override fun onPageScrollStateChanged(state: Int) {
|
override fun onPageScrollStateChanged(state: Int) {
|
||||||
@ -288,6 +286,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
viewpager.currentItem = config.lastUsedViewPagerPage
|
viewpager.currentItem = config.lastUsedViewPagerPage
|
||||||
|
refreshContacts(true, true)
|
||||||
|
|
||||||
main_tabs_holder.onTabSelectionChanged(
|
main_tabs_holder.onTabSelectionChanged(
|
||||||
tabUnselectedAction = {
|
tabUnselectedAction = {
|
||||||
@ -306,15 +305,14 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
|
|||||||
|
|
||||||
private fun showSortingDialog() {
|
private fun showSortingDialog() {
|
||||||
ChangeSortingDialog(this) {
|
ChangeSortingDialog(this) {
|
||||||
contacts_fragment?.refreshContacts()
|
refreshContacts(true, true)
|
||||||
favorites_fragment?.refreshContacts()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun showFilterDialog() {
|
fun showFilterDialog() {
|
||||||
FilterContactSourcesDialog(this) {
|
FilterContactSourcesDialog(this) {
|
||||||
contacts_fragment?.forceListRedraw = true
|
contacts_fragment?.forceListRedraw = true
|
||||||
contacts_fragment?.refreshContacts()
|
refreshContacts(true, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -336,7 +334,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
|
|||||||
ImportContactsDialog(this, path) {
|
ImportContactsDialog(this, path) {
|
||||||
if (it) {
|
if (it) {
|
||||||
runOnUiThread {
|
runOnUiThread {
|
||||||
refreshContacts()
|
refreshContacts(true, true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -399,13 +397,32 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
|
|||||||
BuildConfig.VERSION_NAME, faqItems)
|
BuildConfig.VERSION_NAME, faqItems)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun refreshContacts() {
|
override fun refreshContacts(refreshContactsTab: Boolean, refreshFavoritesTab: Boolean) {
|
||||||
contacts_fragment.refreshContacts()
|
if (isActivityDestroyed()) {
|
||||||
favorites_fragment.refreshContacts()
|
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() {
|
override fun refreshFavorites() {
|
||||||
favorites_fragment?.refreshContacts()
|
refreshContacts(false, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkWhatsNewDialog() {
|
private fun checkWhatsNewDialog() {
|
||||||
|
@ -130,7 +130,7 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: ArrayList<Cont
|
|||||||
|
|
||||||
ContactsHelper(activity).deleteContacts(contactsToRemove)
|
ContactsHelper(activity).deleteContacts(contactsToRemove)
|
||||||
if (contactItems.isEmpty()) {
|
if (contactItems.isEmpty()) {
|
||||||
listener?.refreshContacts()
|
listener?.refreshContacts(true, true)
|
||||||
finishActMode()
|
finishActMode()
|
||||||
} else {
|
} else {
|
||||||
removeSelectedItems()
|
removeSelectedItems()
|
||||||
|
@ -5,15 +5,19 @@ import android.view.View
|
|||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import com.simplemobiletools.contacts.R
|
import com.simplemobiletools.contacts.R
|
||||||
import com.simplemobiletools.contacts.activities.MainActivity
|
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 {
|
override fun instantiateItem(container: ViewGroup, position: Int): Any {
|
||||||
val layout = getFragment(position)
|
val layout = getFragment(position)
|
||||||
val view = activity.layoutInflater.inflate(layout, container, false)
|
val view = activity.layoutInflater.inflate(layout, container, false)
|
||||||
container.addView(view)
|
container.addView(view)
|
||||||
(view as? MyViewPagerFragment)?.setupFragment(activity)
|
(view as FragmentInterface).apply {
|
||||||
|
setupFragment(activity)
|
||||||
|
refreshContacts(contacts)
|
||||||
|
}
|
||||||
return view
|
return view
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPa
|
|||||||
|
|
||||||
private fun showAddFavoritesDialog() {
|
private fun showAddFavoritesDialog() {
|
||||||
AddFavoritesDialog(activity!!) {
|
AddFavoritesDialog(activity!!) {
|
||||||
refreshContacts()
|
activity!!.refreshContacts(false, true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import android.support.design.widget.CoordinatorLayout
|
|||||||
import android.util.AttributeSet
|
import android.util.AttributeSet
|
||||||
import com.simplemobiletools.contacts.activities.MainActivity
|
import com.simplemobiletools.contacts.activities.MainActivity
|
||||||
import com.simplemobiletools.contacts.interfaces.FragmentInterface
|
import com.simplemobiletools.contacts.interfaces.FragmentInterface
|
||||||
|
import com.simplemobiletools.contacts.models.Contact
|
||||||
|
|
||||||
class GroupsFragment(context: Context, attributeSet: AttributeSet) : CoordinatorLayout(context, attributeSet), FragmentInterface {
|
class GroupsFragment(context: Context, attributeSet: AttributeSet) : CoordinatorLayout(context, attributeSet), FragmentInterface {
|
||||||
override fun setupFragment(activity: MainActivity) {
|
override fun setupFragment(activity: MainActivity) {
|
||||||
@ -16,6 +17,6 @@ class GroupsFragment(context: Context, attributeSet: AttributeSet) : Coordinator
|
|||||||
override fun primaryColorChanged(color: Int) {
|
override fun primaryColorChanged(color: Int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun refreshContacts() {
|
override fun refreshContacts(contacts: ArrayList<Contact>) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,10 @@ import com.simplemobiletools.contacts.extensions.config
|
|||||||
import com.simplemobiletools.contacts.extensions.editContact
|
import com.simplemobiletools.contacts.extensions.editContact
|
||||||
import com.simplemobiletools.contacts.extensions.tryStartCall
|
import com.simplemobiletools.contacts.extensions.tryStartCall
|
||||||
import com.simplemobiletools.contacts.extensions.viewContact
|
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.interfaces.FragmentInterface
|
||||||
import com.simplemobiletools.contacts.models.Contact
|
import com.simplemobiletools.contacts.models.Contact
|
||||||
import kotlinx.android.synthetic.main.fragment_layout.view.*
|
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)
|
fragment_placeholder_2.text = activity.getString(R.string.add_favorites)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
refreshContacts()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun textColorChanged(color: Int) {
|
override fun textColorChanged(color: Int) {
|
||||||
@ -67,45 +68,34 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
|
|||||||
fun startNameWithSurnameChanged(startNameWithSurname: Boolean) {
|
fun startNameWithSurnameChanged(startNameWithSurname: Boolean) {
|
||||||
(fragment_list.adapter as ContactsAdapter).apply {
|
(fragment_list.adapter as ContactsAdapter).apply {
|
||||||
config.sorting = if (startNameWithSurname) SORT_BY_SURNAME else SORT_BY_FIRST_NAME
|
config.sorting = if (startNameWithSurname) SORT_BY_SURNAME else SORT_BY_FIRST_NAME
|
||||||
refreshContacts()
|
this@MyViewPagerFragment.activity!!.refreshContacts(true, true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun refreshContacts() {
|
override fun refreshContacts(contacts: ArrayList<Contact>) {
|
||||||
if (activity == null || activity!!.isActivityDestroyed()) {
|
if (config.lastUsedContactSource.isEmpty()) {
|
||||||
return
|
val grouped = contacts.groupBy { it.source }.maxWith(compareBy { it.value.size })
|
||||||
|
config.lastUsedContactSource = grouped?.key ?: ""
|
||||||
}
|
}
|
||||||
|
|
||||||
ContactsHelper(activity!!).getContacts {
|
val filtered = if (this is FavoritesFragment) {
|
||||||
var contacts = it
|
contacts.filter { it.starred == 1 } as ArrayList<Contact>
|
||||||
if (activity == null || activity!!.isActivityDestroyed()) {
|
} else {
|
||||||
return@getContacts
|
val contactSources = config.displayContactSources
|
||||||
}
|
if (config.showAllContacts()) {
|
||||||
|
contacts
|
||||||
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>
|
|
||||||
} else {
|
} else {
|
||||||
val contactSources = config.displayContactSources
|
contacts.filter { contactSources.contains(it.source) } as ArrayList<Contact>
|
||||||
if (config.showAllContacts()) {
|
|
||||||
contacts
|
|
||||||
} else {
|
|
||||||
contacts.filter { contactSources.contains(it.source) } as ArrayList<Contact>
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Contact.sorting = config.sorting
|
Contact.sorting = config.sorting
|
||||||
contacts.sort()
|
filtered.sort()
|
||||||
|
|
||||||
if (contacts.hashCode() != lastHashCode) {
|
if (filtered.hashCode() != lastHashCode) {
|
||||||
lastHashCode = contacts.hashCode()
|
lastHashCode = filtered.hashCode()
|
||||||
activity!!.runOnUiThread {
|
activity?.runOnUiThread {
|
||||||
setupContacts(contacts)
|
setupContacts(filtered)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -116,7 +116,9 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
|
|||||||
var resultContacts = ArrayList<Contact>(contactsSize)
|
var resultContacts = ArrayList<Contact>(contactsSize)
|
||||||
(0 until contactsSize).mapTo(resultContacts) { contacts.valueAt(it) }
|
(0 until contactsSize).mapTo(resultContacts) { contacts.valueAt(it) }
|
||||||
resultContacts = resultContacts.distinctBy { it.contactId } as ArrayList<Contact>
|
resultContacts = resultContacts.distinctBy { it.contactId } as ArrayList<Contact>
|
||||||
callback(resultContacts)
|
activity.runOnUiThread {
|
||||||
|
callback(resultContacts)
|
||||||
|
}
|
||||||
}.start()
|
}.start()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.simplemobiletools.contacts.interfaces
|
package com.simplemobiletools.contacts.interfaces
|
||||||
|
|
||||||
import com.simplemobiletools.contacts.activities.MainActivity
|
import com.simplemobiletools.contacts.activities.MainActivity
|
||||||
|
import com.simplemobiletools.contacts.models.Contact
|
||||||
|
|
||||||
interface FragmentInterface {
|
interface FragmentInterface {
|
||||||
fun setupFragment(activity: MainActivity)
|
fun setupFragment(activity: MainActivity)
|
||||||
@ -9,5 +10,5 @@ interface FragmentInterface {
|
|||||||
|
|
||||||
fun primaryColorChanged(color: Int)
|
fun primaryColorChanged(color: Int)
|
||||||
|
|
||||||
fun refreshContacts()
|
fun refreshContacts(contacts: ArrayList<Contact>)
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package com.simplemobiletools.contacts.interfaces
|
package com.simplemobiletools.contacts.interfaces
|
||||||
|
|
||||||
interface RefreshContactsListener {
|
interface RefreshContactsListener {
|
||||||
fun refreshContacts()
|
fun refreshContacts(refreshContactsTab: Boolean, refreshFavoritesTab: Boolean)
|
||||||
|
|
||||||
fun refreshFavorites()
|
fun refreshFavorites()
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user