make GroupsFragment inherit from MyViewPagerFragment too

This commit is contained in:
tibbi 2018-03-20 17:30:13 +01:00
parent 1e49b8e019
commit d2daf6efdb
6 changed files with 111 additions and 244 deletions

View File

@ -28,7 +28,6 @@ import com.simplemobiletools.contacts.extensions.dbHelper
import com.simplemobiletools.contacts.extensions.getTempFile
import com.simplemobiletools.contacts.helpers.ContactsHelper
import com.simplemobiletools.contacts.helpers.VcfExporter
import com.simplemobiletools.contacts.interfaces.FragmentInterface
import com.simplemobiletools.contacts.interfaces.RefreshContactsListener
import com.simplemobiletools.contacts.models.Contact
import kotlinx.android.synthetic.main.activity_main.*
@ -94,7 +93,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
val configShowContactThumbnails = config.showContactThumbnails
if (storedShowContactThumbnails != configShowContactThumbnails) {
getAllFragments().forEach {
(it as? FragmentInterface)?.showContactThumbnailsChanged(configShowContactThumbnails)
it?.showContactThumbnailsChanged(configShowContactThumbnails)
}
}
@ -104,7 +103,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
main_tabs_holder.getTabAt(it)?.icon?.applyColorFilter(configTextColor)
}
getAllFragments().forEach {
(it as? FragmentInterface)?.textColorChanged(configTextColor)
it?.textColorChanged(configTextColor)
}
}
@ -118,7 +117,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
main_tabs_holder.setSelectedTabIndicatorColor(getAdjustedPrimaryColor())
main_tabs_holder.getTabAt(viewpager.currentItem)?.icon?.applyColorFilter(getAdjustedPrimaryColor())
getAllFragments().forEach {
(it as? FragmentInterface)?.primaryColorChanged(configPrimaryColor)
it?.primaryColorChanged(configPrimaryColor)
}
}
@ -134,7 +133,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
}
getAllFragments().forEach {
(it as? FragmentInterface)?.onActivityResume()
it?.onActivityResume()
}
refreshContacts(true, true)
}
@ -219,7 +218,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
})
}
private fun getCurrentFragment(): FragmentInterface? = when (viewpager.currentItem) {
private fun getCurrentFragment() = when (viewpager.currentItem) {
0 -> contacts_fragment
1 -> favorites_fragment
else -> groups_fragment
@ -286,7 +285,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
override fun onPageSelected(position: Int) {
main_tabs_holder.getTabAt(position)?.select()
getAllFragments().forEach {
(it as? FragmentInterface)?.finishActMode()
it?.finishActMode()
}
invalidateOptionsMenu()
}

View File

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

View File

@ -1,139 +1,23 @@
package com.simplemobiletools.contacts.fragments
import android.content.Context
import android.content.Intent
import android.support.design.widget.CoordinatorLayout
import android.util.AttributeSet
import android.view.ViewGroup
import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor
import com.simplemobiletools.commons.extensions.underlineText
import com.simplemobiletools.commons.extensions.updateTextColors
import com.simplemobiletools.contacts.activities.GroupContactsActivity
import com.simplemobiletools.contacts.activities.MainActivity
import com.simplemobiletools.contacts.activities.SimpleActivity
import com.simplemobiletools.contacts.adapters.GroupsAdapter
import com.simplemobiletools.contacts.dialogs.CreateNewGroupDialog
import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.helpers.ContactsHelper
import com.simplemobiletools.contacts.helpers.GROUP
import com.simplemobiletools.contacts.interfaces.FragmentInterface
import com.simplemobiletools.contacts.models.Contact
import com.simplemobiletools.contacts.models.Group
import kotlinx.android.synthetic.main.fragment_groups.view.*
class GroupsFragment(context: Context, attributeSet: AttributeSet) : CoordinatorLayout(context, attributeSet), FragmentInterface {
private var activity: MainActivity? = null
private var lastContacts = ArrayList<Contact>()
override fun setupFragment(activity: MainActivity) {
if (this.activity == null) {
this.activity = activity
groups_fab.setOnClickListener {
fabClicked()
}
groups_placeholder_2.setOnClickListener {
fabClicked()
}
groups_placeholder_2.underlineText()
updateViewStuff()
}
class GroupsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet) {
override fun fabClicked() {
finishActMode()
showNewGroupsDialog()
}
private fun fabClicked() {
override fun placeholderClicked() {
showNewGroupsDialog()
}
private fun showNewGroupsDialog() {
CreateNewGroupDialog(activity as SimpleActivity) {
refreshContacts(lastContacts)
refreshContacts(allContacts)
}
}
override fun textColorChanged(color: Int) {
(groups_list.adapter as GroupsAdapter).updateTextColor(color)
}
override fun primaryColorChanged(color: Int) {
groups_fastscroller.apply {
updatePrimaryColor()
updateBubblePrimaryColor()
}
}
override fun refreshContacts(contacts: ArrayList<Contact>) {
if (activity == null) {
return
}
lastContacts = contacts
var storedGroups = ContactsHelper(activity!!).getStoredGroups()
contacts.forEach {
it.groups.forEach {
val group = it
val storedGroup = storedGroups.firstOrNull { it.id == group.id }
storedGroup?.addContact()
}
}
storedGroups = storedGroups.sortedWith(compareBy { it.title }).toList() as? ArrayList<Group> ?: ArrayList()
groups_placeholder_2.beVisibleIf(storedGroups.isEmpty())
groups_placeholder.beVisibleIf(storedGroups.isEmpty())
groups_list.beVisibleIf(storedGroups.isNotEmpty())
val currAdapter = groups_list.adapter
if (currAdapter == null) {
GroupsAdapter(activity as SimpleActivity, storedGroups, groups_list, groups_fastscroller) {
Intent(activity, GroupContactsActivity::class.java).apply {
putExtra(GROUP, it as Group)
activity!!.startActivity(this)
}
}.apply {
setupDragListener(true)
addVerticalDividers(true)
groups_list.adapter = this
}
groups_fastscroller.setScrollTo(0)
groups_fastscroller.setViews(groups_list) {
val item = (groups_list.adapter as GroupsAdapter).groups.getOrNull(it)
groups_fastscroller.updateBubbleText(item?.getBubbleText() ?: "")
}
} else {
(currAdapter as GroupsAdapter).apply {
showContactThumbnails = activity.config.showContactThumbnails
updateItems(storedGroups)
}
}
}
override fun showContactThumbnailsChanged(showThumbnails: Boolean) {
(groups_list.adapter as? GroupsAdapter)?.apply {
showContactThumbnails = showThumbnails
notifyDataSetChanged()
}
}
override fun onActivityResume() {
updateViewStuff()
}
override fun finishActMode() {
(groups_list.adapter as? GroupsAdapter)?.finishActMode()
}
override fun onSearchQueryChanged(text: String) {
}
override fun onSearchOpened() {
}
override fun onSearchClosed() {
}
private fun updateViewStuff() {
context.updateTextColors(groups_wrapper.parent as ViewGroup)
groups_fastscroller.updateBubbleColors()
groups_fastscroller.allowBubbleDisplay = context.config.showInfoBubble
groups_placeholder_2.setTextColor(context.getAdjustedPrimaryColor())
}
}

View File

@ -1,26 +1,30 @@
package com.simplemobiletools.contacts.fragments
import android.content.Context
import android.content.Intent
import android.support.design.widget.CoordinatorLayout
import android.util.AttributeSet
import android.view.ViewGroup
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.SORT_BY_FIRST_NAME
import com.simplemobiletools.commons.helpers.SORT_BY_SURNAME
import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.activities.GroupContactsActivity
import com.simplemobiletools.contacts.activities.MainActivity
import com.simplemobiletools.contacts.activities.SimpleActivity
import com.simplemobiletools.contacts.adapters.ContactsAdapter
import com.simplemobiletools.contacts.adapters.GroupsAdapter
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.interfaces.FragmentInterface
import com.simplemobiletools.contacts.models.Contact
import com.simplemobiletools.contacts.models.Group
import kotlinx.android.synthetic.main.fragment_layout.view.*
abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) : CoordinatorLayout(context, attributeSet), FragmentInterface {
abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) : CoordinatorLayout(context, attributeSet) {
protected var activity: MainActivity? = null
protected var allContacts = ArrayList<Contact>()
@ -30,7 +34,7 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
var forceListRedraw = false
override fun setupFragment(activity: MainActivity) {
fun setupFragment(activity: MainActivity) {
config = activity.config
if (this.activity == null) {
this.activity = activity
@ -48,30 +52,39 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
if (this is FavoritesFragment) {
fragment_placeholder.text = activity.getString(R.string.no_favorites)
fragment_placeholder_2.text = activity.getString(R.string.add_favorites)
} else if (this is GroupsFragment) {
fragment_placeholder.text = activity.getString(R.string.no_group_created)
fragment_placeholder_2.text = activity.getString(R.string.create_group)
}
}
}
override fun textColorChanged(color: Int) {
(fragment_list.adapter as ContactsAdapter).apply {
updateTextColor(color)
initDrawables()
fun textColorChanged(color: Int) {
if (this is GroupsFragment) {
(fragment_list.adapter as GroupsAdapter).updateTextColor(color)
} else {
(fragment_list.adapter as ContactsAdapter).apply {
updateTextColor(color)
initDrawables()
}
}
}
override fun primaryColorChanged(color: Int) {
fun primaryColorChanged(color: Int) {
fragment_fastscroller.updatePrimaryColor()
fragment_fastscroller.updateBubblePrimaryColor()
}
fun startNameWithSurnameChanged(startNameWithSurname: Boolean) {
(fragment_list.adapter as ContactsAdapter).apply {
config.sorting = if (startNameWithSurname) SORT_BY_SURNAME else SORT_BY_FIRST_NAME
this@MyViewPagerFragment.activity!!.refreshContacts(true, true)
if (this !is GroupsFragment) {
(fragment_list.adapter as ContactsAdapter).apply {
config.sorting = if (startNameWithSurname) SORT_BY_SURNAME else SORT_BY_FIRST_NAME
this@MyViewPagerFragment.activity!!.refreshContacts(true, true)
}
}
}
override fun refreshContacts(contacts: ArrayList<Contact>) {
fun refreshContacts(contacts: ArrayList<Contact>) {
if (config.lastUsedContactSource.isEmpty()) {
val grouped = contacts.groupBy { it.source }.maxWith(compareBy { it.value.size })
config.lastUsedContactSource = grouped?.key ?: ""
@ -101,6 +114,56 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
}
private fun setupContacts(contacts: ArrayList<Contact>) {
if (this is GroupsFragment) {
setupGroupsAdapter(contacts)
} else {
setupContactsFavoritesAdapter(contacts)
}
}
private fun setupGroupsAdapter(contacts: ArrayList<Contact>) {
var storedGroups = ContactsHelper(activity!!).getStoredGroups()
contacts.forEach {
it.groups.forEach {
val group = it
val storedGroup = storedGroups.firstOrNull { it.id == group.id }
storedGroup?.addContact()
}
}
storedGroups = storedGroups.sortedWith(compareBy { it.title }).toList() as? ArrayList<Group> ?: ArrayList()
fragment_placeholder_2.beVisibleIf(storedGroups.isEmpty())
fragment_placeholder.beVisibleIf(storedGroups.isEmpty())
fragment_list.beVisibleIf(storedGroups.isNotEmpty())
val currAdapter = fragment_list.adapter
if (currAdapter == null) {
GroupsAdapter(activity as SimpleActivity, storedGroups, fragment_list, fragment_fastscroller) {
Intent(activity, GroupContactsActivity::class.java).apply {
putExtra(GROUP, it as Group)
activity!!.startActivity(this)
}
}.apply {
setupDragListener(true)
addVerticalDividers(true)
fragment_list.adapter = this
}
fragment_fastscroller.setScrollTo(0)
fragment_fastscroller.setViews(fragment_list) {
val item = (fragment_list.adapter as GroupsAdapter).groups.getOrNull(it)
fragment_fastscroller.updateBubbleText(item?.getBubbleText() ?: "")
}
} else {
(currAdapter as GroupsAdapter).apply {
showContactThumbnails = activity.config.showContactThumbnails
updateItems(storedGroups)
}
}
}
private fun setupContactsFavoritesAdapter(contacts: ArrayList<Contact>) {
fragment_placeholder_2.beVisibleIf(contacts.isEmpty())
fragment_placeholder.beVisibleIf(contacts.isEmpty())
fragment_list.beVisibleIf(contacts.isNotEmpty())
@ -143,22 +206,29 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
}
}
override fun showContactThumbnailsChanged(showThumbnails: Boolean) {
(fragment_list.adapter as? ContactsAdapter)?.apply {
showContactThumbnails = showThumbnails
notifyDataSetChanged()
fun showContactThumbnailsChanged(showThumbnails: Boolean) {
if (this is GroupsFragment) {
(fragment_list.adapter as? GroupsAdapter)?.apply {
showContactThumbnails = showThumbnails
notifyDataSetChanged()
}
} else {
(fragment_list.adapter as? ContactsAdapter)?.apply {
showContactThumbnails = showThumbnails
notifyDataSetChanged()
}
}
}
override fun onActivityResume() {
fun onActivityResume() {
updateViewStuff()
}
override fun finishActMode() {
(fragment_list.adapter as? ContactsAdapter)?.finishActMode()
fun finishActMode() {
(fragment_list.adapter as? MyRecyclerViewAdapter)?.finishActMode()
}
override fun onSearchQueryChanged(text: String) {
fun onSearchQueryChanged(text: String) {
(fragment_list.adapter as? ContactsAdapter)?.apply {
val filtered = contactsIgnoringSearch.filter {
it.getFullName(startNameWithSurname).contains(text, true) ||
@ -181,11 +251,11 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
}
}
override fun onSearchOpened() {
fun onSearchOpened() {
contactsIgnoringSearch = (fragment_list?.adapter as? ContactsAdapter)?.contactItems ?: ArrayList()
}
override fun onSearchClosed() {
fun onSearchClosed() {
(fragment_list.adapter as? ContactsAdapter)?.updateItems(contactsIgnoringSearch)
if (this is FavoritesFragment) {
fragment_placeholder.text = activity?.getString(R.string.no_favorites)

View File

@ -1,26 +0,0 @@
package com.simplemobiletools.contacts.interfaces
import com.simplemobiletools.contacts.activities.MainActivity
import com.simplemobiletools.contacts.models.Contact
interface FragmentInterface {
fun setupFragment(activity: MainActivity)
fun onActivityResume()
fun textColorChanged(color: Int)
fun primaryColorChanged(color: Int)
fun refreshContacts(contacts: ArrayList<Contact>)
fun showContactThumbnailsChanged(showThumbnails: Boolean)
fun finishActMode()
fun onSearchQueryChanged(text: String)
fun onSearchOpened()
fun onSearchClosed()
}

View File

@ -1,70 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<com.simplemobiletools.contacts.fragments.GroupsFragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/groups_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/groups_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/groups_placeholder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/activity_margin"
android:gravity="center"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:text="@string/no_group_created"
android:textSize="@dimen/bigger_text_size"
android:visibility="gone"/>
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/groups_placeholder_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/groups_placeholder"
android:layout_centerHorizontal="true"
android:gravity="center"
android:paddingBottom="@dimen/medium_margin"
android:paddingTop="@dimen/medium_margin"
android:text="@string/create_group"
android:textSize="@dimen/bigger_text_size"
android:visibility="gone"/>
<com.simplemobiletools.commons.views.MyRecyclerView
android:id="@+id/groups_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:scrollbars="none"
app:layoutManager="com.simplemobiletools.commons.views.MyLinearLayoutManager"/>
<com.simplemobiletools.commons.views.FastScroller
android:id="@+id/groups_fastscroller"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:paddingLeft="@dimen/normal_margin"
android:paddingStart="@dimen/normal_margin">
<include layout="@layout/fastscroller_handle_vertical"/>
</com.simplemobiletools.commons.views.FastScroller>
</RelativeLayout>
<com.simplemobiletools.commons.views.MyFloatingActionButton
android:id="@+id/groups_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/activity_margin"
android:src="@drawable/ic_plus"/>
<include layout="@layout/fragment_layout"/>
</com.simplemobiletools.contacts.fragments.GroupsFragment>