From f74376e8ac2812905d3715ae15589c8c605e58f9 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 19 Mar 2018 10:11:58 +0100 Subject: [PATCH] create an interface for all fragments --- .../contacts/activities/MainActivity.kt | 20 +++++++++---------- .../contacts/fragments/FavoritesFragment.kt | 2 +- .../contacts/fragments/GroupsFragment.kt | 14 ++++++++++++- .../contacts/fragments/MyViewPagerFragment.kt | 15 +++++++------- .../contacts/interfaces/FragmentInterface.kt | 13 ++++++++++++ 5 files changed, 45 insertions(+), 19 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/contacts/interfaces/FragmentInterface.kt 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 d20e791b..d7f3df30 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/MainActivity.kt @@ -115,8 +115,8 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { if (storedPrimaryColor != configPrimaryColor) { main_tabs_holder.setSelectedTabIndicatorColor(getAdjustedPrimaryColor()) main_tabs_holder.getTabAt(viewpager.currentItem)?.icon?.applyColorFilter(getAdjustedPrimaryColor()) - contacts_fragment?.primaryColorChanged() - favorites_fragment?.primaryColorChanged() + contacts_fragment?.primaryColorChanged(configPrimaryColor) + favorites_fragment?.primaryColorChanged(configPrimaryColor) } val configStartNameWithSurname = config.startNameWithSurname @@ -130,9 +130,9 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { initFragments() } - contacts_fragment?.initContacts() + contacts_fragment?.refreshContacts() contacts_fragment?.onActivityResume() - favorites_fragment?.initContacts() + favorites_fragment?.refreshContacts() favorites_fragment?.onActivityResume() } @@ -306,15 +306,15 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { private fun showSortingDialog() { ChangeSortingDialog(this) { - contacts_fragment?.initContacts() - favorites_fragment?.initContacts() + contacts_fragment?.refreshContacts() + favorites_fragment?.refreshContacts() } } fun showFilterDialog() { FilterContactSourcesDialog(this) { contacts_fragment?.forceListRedraw = true - contacts_fragment?.initContacts() + contacts_fragment?.refreshContacts() } } @@ -400,12 +400,12 @@ class MainActivity : SimpleActivity(), RefreshContactsListener { } override fun refreshContacts() { - contacts_fragment.initContacts() - favorites_fragment.initContacts() + contacts_fragment.refreshContacts() + favorites_fragment.refreshContacts() } override fun refreshFavorites() { - favorites_fragment?.initContacts() + favorites_fragment?.refreshContacts() } private fun checkWhatsNewDialog() { diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/FavoritesFragment.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/FavoritesFragment.kt index 3e42d3aa..f4881002 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/FavoritesFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/FavoritesFragment.kt @@ -16,7 +16,7 @@ class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPa private fun showAddFavoritesDialog() { AddFavoritesDialog(activity!!) { - initContacts() + refreshContacts() } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/GroupsFragment.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/GroupsFragment.kt index 7fc1b8ad..49103e3f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/GroupsFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/GroupsFragment.kt @@ -3,7 +3,19 @@ package com.simplemobiletools.contacts.fragments import android.content.Context import android.support.design.widget.CoordinatorLayout import android.util.AttributeSet +import com.simplemobiletools.contacts.activities.MainActivity +import com.simplemobiletools.contacts.interfaces.FragmentInterface -class GroupsFragment(context: Context, attributeSet: AttributeSet) : CoordinatorLayout(context, attributeSet) { +class GroupsFragment(context: Context, attributeSet: AttributeSet) : CoordinatorLayout(context, attributeSet), FragmentInterface { + override fun setupFragment(activity: MainActivity) { + } + override fun textColorChanged(color: Int) { + } + + override fun primaryColorChanged(color: Int) { + } + + override fun refreshContacts() { + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/MyViewPagerFragment.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/MyViewPagerFragment.kt index 7c767a66..5f34a546 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/MyViewPagerFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/fragments/MyViewPagerFragment.kt @@ -16,10 +16,11 @@ 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.interfaces.FragmentInterface import com.simplemobiletools.contacts.models.Contact import kotlinx.android.synthetic.main.fragment_layout.view.* -abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) : CoordinatorLayout(context, attributeSet) { +abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) : CoordinatorLayout(context, attributeSet), FragmentInterface { protected var activity: MainActivity? = null private var lastHashCode = 0 private var contactsIgnoringSearch = ArrayList() @@ -27,7 +28,7 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) var forceListRedraw = false - fun setupFragment(activity: MainActivity) { + override fun setupFragment(activity: MainActivity) { config = activity.config if (this.activity == null) { this.activity = activity @@ -48,17 +49,17 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) } } - initContacts() + refreshContacts() } - fun textColorChanged(color: Int) { + override fun textColorChanged(color: Int) { (fragment_list.adapter as ContactsAdapter).apply { updateTextColor(color) initDrawables() } } - fun primaryColorChanged() { + override fun primaryColorChanged(color: Int) { fragment_fastscroller.updatePrimaryColor() fragment_fastscroller.updateBubblePrimaryColor() } @@ -66,11 +67,11 @@ 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 - initContacts() + refreshContacts() } } - fun initContacts() { + override fun refreshContacts() { if (activity == null || activity!!.isActivityDestroyed()) { return } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/interfaces/FragmentInterface.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/interfaces/FragmentInterface.kt new file mode 100644 index 00000000..e4a1c2d3 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/interfaces/FragmentInterface.kt @@ -0,0 +1,13 @@ +package com.simplemobiletools.contacts.interfaces + +import com.simplemobiletools.contacts.activities.MainActivity + +interface FragmentInterface { + fun setupFragment(activity: MainActivity) + + fun textColorChanged(color: Int) + + fun primaryColorChanged(color: Int) + + fun refreshContacts() +}