From 93edf90d51088f54c8ced574cecb34abb5cd8954 Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 16 Mar 2018 20:23:15 +0100 Subject: [PATCH] adding a tab for Groups --- .../contacts/activities/MainActivity.kt | 28 +++++++++++++------ .../contacts/adapters/ViewPagerAdapter.kt | 12 ++++++-- .../contacts/fragments/GroupsFragment.kt | 9 ++++++ app/src/main/res/layout/activity_main.xml | 6 ++++ app/src/main/res/layout/fragment_groups.xml | 8 ++++++ 5 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/contacts/fragments/GroupsFragment.kt create mode 100644 app/src/main/res/layout/fragment_groups.xml 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 eaebb9b8..d20e791b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/MainActivity.kt @@ -26,6 +26,7 @@ import com.simplemobiletools.contacts.dialogs.ImportContactsDialog import com.simplemobiletools.contacts.extensions.config import com.simplemobiletools.contacts.extensions.dbHelper import com.simplemobiletools.contacts.extensions.getTempFile +import com.simplemobiletools.contacts.fragments.MyViewPagerFragment import com.simplemobiletools.contacts.helpers.ContactsHelper import com.simplemobiletools.contacts.helpers.VcfExporter import com.simplemobiletools.contacts.interfaces.RefreshContactsListener @@ -33,6 +34,7 @@ import com.simplemobiletools.contacts.models.Contact import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.fragment_contacts.* import kotlinx.android.synthetic.main.fragment_favorites.* +import kotlinx.android.synthetic.main.fragment_groups.* import java.io.FileOutputStream class MainActivity : SimpleActivity(), RefreshContactsListener { @@ -97,7 +99,9 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { val configTextColor = config.textColor if (storedTextColor != configTextColor) { - main_tabs_holder.getTabAt(getOtherViewPagerItem(viewpager.currentItem))?.icon?.applyColorFilter(configTextColor) + getInactiveTabIndexes(viewpager.currentItem).forEach { + main_tabs_holder.getTabAt(it)?.icon?.applyColorFilter(configTextColor) + } contacts_fragment?.textColorChanged(configTextColor) favorites_fragment?.textColorChanged(configTextColor) } @@ -190,7 +194,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { override fun onQueryTextChange(newText: String): Boolean { if (isSearchOpen) { - getCurrentFragment()?.onSearchQueryChanged(newText) + (getCurrentFragment() as? MyViewPagerFragment)?.onSearchQueryChanged(newText) } return true } @@ -199,20 +203,24 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { MenuItemCompat.setOnActionExpandListener(searchMenuItem, object : MenuItemCompat.OnActionExpandListener { override fun onMenuItemActionExpand(item: MenuItem?): Boolean { - getCurrentFragment()?.onSearchOpened() + (getCurrentFragment() as? MyViewPagerFragment)?.onSearchOpened() isSearchOpen = true return true } override fun onMenuItemActionCollapse(item: MenuItem?): Boolean { - getCurrentFragment()?.onSearchClosed() + (getCurrentFragment() as? MyViewPagerFragment)?.onSearchClosed() isSearchOpen = false return true } }) } - private fun getCurrentFragment() = if (viewpager.currentItem == 0) contacts_fragment else favorites_fragment + private fun getCurrentFragment() = when (viewpager.currentItem) { + 0 -> contacts_fragment + 1 -> favorites_fragment + else -> groups_fragment + } private fun setupTabColors() { val lastUsedPage = config.lastUsedViewPagerPage @@ -221,7 +229,10 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { setSelectedTabIndicatorColor(getAdjustedPrimaryColor()) getTabAt(lastUsedPage)?.select() getTabAt(lastUsedPage)?.icon?.applyColorFilter(getAdjustedPrimaryColor()) - getTabAt(getOtherViewPagerItem(lastUsedPage))?.icon?.applyColorFilter(config.textColor) + + getInactiveTabIndexes(lastUsedPage).forEach { + getTabAt(it)?.icon?.applyColorFilter(config.textColor) + } } } @@ -253,14 +264,15 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { } } - private fun getOtherViewPagerItem(used: Int) = if (used == 1) 0 else 1 + 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) { if (isSearchOpen) { - getCurrentFragment().onSearchQueryChanged("") + (getCurrentFragment() as? MyViewPagerFragment)?.onSearchQueryChanged("") searchMenuItem?.collapseActionView() } } 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 bed555b9..644e71d5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/ViewPagerAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/ViewPagerAdapter.kt @@ -10,10 +10,10 @@ import com.simplemobiletools.contacts.fragments.MyViewPagerFragment class ViewPagerAdapter(val activity: MainActivity) : PagerAdapter() { override fun instantiateItem(container: ViewGroup, position: Int): Any { - val layout = if (position == 0) R.layout.fragment_contacts else R.layout.fragment_favorites + val layout = getFragment(position) val view = activity.layoutInflater.inflate(layout, container, false) container.addView(view) - (view as MyViewPagerFragment).setupFragment(activity) + (view as? MyViewPagerFragment)?.setupFragment(activity) return view } @@ -21,6 +21,12 @@ class ViewPagerAdapter(val activity: MainActivity) : PagerAdapter() { container.removeView(item as View) } - override fun getCount() = 2 + override fun getCount() = 3 override fun isViewFromObject(view: View, item: Any) = view == item + + private fun getFragment(position: Int) = when (position) { + 0 -> R.layout.fragment_contacts + 1 -> R.layout.fragment_favorites + else -> R.layout.fragment_groups + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/GroupsFragment.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/GroupsFragment.kt new file mode 100644 index 00000000..7fc1b8ad --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/GroupsFragment.kt @@ -0,0 +1,9 @@ +package com.simplemobiletools.contacts.fragments + +import android.content.Context +import android.support.design.widget.CoordinatorLayout +import android.util.AttributeSet + +class GroupsFragment(context: Context, attributeSet: AttributeSet) : CoordinatorLayout(context, attributeSet) { + +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index f3250f88..95c89e52 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -28,6 +28,12 @@ android:layout_height="wrap_content" android:icon="@drawable/ic_star_on"/> + + + + +