From 3257dc7c799acf4563550889fc3bc4e0660b6a3e Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 4 Aug 2018 19:42:33 +0200 Subject: [PATCH] adding some performance improvements at initial app loading --- .../contacts/activities/MainActivity.kt | 27 ++++++++++--------- .../contacts/adapters/ViewPagerAdapter.kt | 7 +++-- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/MainActivity.kt index fd99a331..79477df7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/MainActivity.kt @@ -283,7 +283,6 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { private fun getInactiveTabIndexes(activeIndex: Int) = arrayListOf(0, 1, 2).filter { it != activeIndex } private fun initFragments() { - refreshContacts(ALL_TABS_MASK) viewpager.offscreenPageLimit = 3 viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener { override fun onPageScrollStateChanged(state: Int) { @@ -305,6 +304,10 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { } }) + viewpager.onGlobalLayout { + refreshContacts(ALL_TABS_MASK) + } + main_tabs_holder.onTabSelectionChanged( tabUnselectedAction = { it.icon?.applyColorFilter(config.textColor) @@ -451,19 +454,19 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { if (isActivityDestroyed() || isGettingContacts) { return } - isGettingContacts = true + + if (viewpager.adapter == null) { + viewpager.adapter = ViewPagerAdapter(this) + viewpager.currentItem = config.lastUsedViewPagerPage + } + ContactsHelper(this).getContacts { isGettingContacts = false if (isActivityDestroyed()) { return@getContacts } - if (viewpager.adapter == null) { - viewpager.adapter = ViewPagerAdapter(this, it) - viewpager.currentItem = config.lastUsedViewPagerPage - } - if (refreshTabsMask and CONTACTS_TAB_MASK != 0) { contacts_fragment?.refreshContacts(it) } @@ -478,12 +481,12 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { } groups_fragment?.refreshContacts(it) } + } - if (refreshTabsMask and RECENTS_TAB_MASK != 0) { - ContactsHelper(this).getRecents { - runOnUiThread { - recents_fragment?.updateRecentCalls(it) - } + if (refreshTabsMask and RECENTS_TAB_MASK != 0) { + ContactsHelper(this).getRecents { + runOnUiThread { + recents_fragment?.updateRecentCalls(it) } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/ViewPagerAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/ViewPagerAdapter.kt index 5a61132d..c78ffaef 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/ViewPagerAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/ViewPagerAdapter.kt @@ -8,10 +8,9 @@ import com.simplemobiletools.contacts.activities.MainActivity import com.simplemobiletools.contacts.extensions.config import com.simplemobiletools.contacts.fragments.MyViewPagerFragment import com.simplemobiletools.contacts.helpers.* -import com.simplemobiletools.contacts.models.Contact -class ViewPagerAdapter(val activity: MainActivity, val contacts: ArrayList) : PagerAdapter() { - val showTabs = activity.config.showTabs +class ViewPagerAdapter(val activity: MainActivity) : PagerAdapter() { + private val showTabs = activity.config.showTabs override fun instantiateItem(container: ViewGroup, position: Int): Any { val layout = getFragment(position) @@ -20,8 +19,8 @@ class ViewPagerAdapter(val activity: MainActivity, val contacts: ArrayList