mirror of
https://github.com/SimpleMobileTools/Simple-Contacts.git
synced 2025-02-17 03:51:03 +01:00
move the contacts fragment handling in the shared parent fragment
This commit is contained in:
parent
67155fd1a5
commit
488a5d9025
@ -2,141 +2,11 @@ package com.simplemobiletools.contacts.fragments
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.graphics.Paint
|
||||
import android.util.AttributeSet
|
||||
import com.simplemobiletools.commons.extensions.beVisibleIf
|
||||
import com.simplemobiletools.commons.extensions.isActivityDestroyed
|
||||
import com.simplemobiletools.commons.extensions.toast
|
||||
import com.simplemobiletools.commons.extensions.updateTextColors
|
||||
import com.simplemobiletools.commons.helpers.SORT_BY_FIRST_NAME
|
||||
import com.simplemobiletools.commons.helpers.SORT_BY_SURNAME
|
||||
import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener
|
||||
import com.simplemobiletools.contacts.R
|
||||
import com.simplemobiletools.contacts.activities.ContactActivity
|
||||
import com.simplemobiletools.contacts.activities.MainActivity
|
||||
import com.simplemobiletools.contacts.activities.SimpleActivity
|
||||
import com.simplemobiletools.contacts.adapters.ContactsAdapter
|
||||
import com.simplemobiletools.contacts.extensions.openContact
|
||||
import com.simplemobiletools.contacts.extensions.tryStartCall
|
||||
import com.simplemobiletools.contacts.helpers.ContactsHelper
|
||||
import com.simplemobiletools.contacts.models.Contact
|
||||
import kotlinx.android.synthetic.main.fragment_contacts.view.*
|
||||
|
||||
class ContactsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet), RefreshRecyclerViewListener {
|
||||
var activity: MainActivity? = null
|
||||
|
||||
override fun initFragment(activity: MainActivity) {
|
||||
if (this.activity == null) {
|
||||
this.activity = activity
|
||||
contacts_fab.setOnClickListener {
|
||||
addNewContact()
|
||||
}
|
||||
|
||||
contacts_placeholder_2.setOnClickListener {
|
||||
activity.showFilterDialog()
|
||||
}
|
||||
|
||||
contacts_placeholder_2.paintFlags = contacts_placeholder_2.paintFlags or Paint.UNDERLINE_TEXT_FLAG
|
||||
updateViewStuff()
|
||||
}
|
||||
|
||||
initContacts()
|
||||
}
|
||||
|
||||
override fun textColorChanged(color: Int) {
|
||||
(contacts_list.adapter as ContactsAdapter).apply {
|
||||
updateTextColor(color)
|
||||
initDrawables()
|
||||
}
|
||||
}
|
||||
|
||||
override fun primaryColorChanged(color: Int) {
|
||||
contacts_fastscroller.updatePrimaryColor()
|
||||
contacts_fab.setColors(config.textColor, color, config.backgroundColor)
|
||||
contacts_fastscroller.updateBubblePrimaryColor()
|
||||
}
|
||||
|
||||
override fun startNameWithSurnameChanged(startNameWithSurname: Boolean) {
|
||||
(contacts_list.adapter as ContactsAdapter).apply {
|
||||
config.sorting = if (startNameWithSurname) SORT_BY_SURNAME else SORT_BY_FIRST_NAME
|
||||
initContacts()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onActivityResume() {
|
||||
updateViewStuff()
|
||||
}
|
||||
|
||||
private fun updateViewStuff() {
|
||||
context.updateTextColors(contacts_fragment)
|
||||
contacts_fastscroller.updateBubbleColors()
|
||||
contacts_fastscroller.allowBubbleDisplay = config.showInfoBubble
|
||||
contacts_placeholder_2.setTextColor(config.primaryColor)
|
||||
}
|
||||
|
||||
fun initContacts() {
|
||||
if (activity == null || activity!!.isActivityDestroyed()) {
|
||||
return
|
||||
}
|
||||
|
||||
ContactsHelper(activity!!).getContacts {
|
||||
if (activity == null || activity!!.isActivityDestroyed()) {
|
||||
return@getContacts
|
||||
}
|
||||
|
||||
if (config.lastUsedContactSource.isEmpty()) {
|
||||
val grouped = it.groupBy { it.source }.maxWith(compareBy { it.value.size })
|
||||
config.lastUsedContactSource = grouped?.key ?: ""
|
||||
}
|
||||
|
||||
Contact.sorting = config.sorting
|
||||
it.sort()
|
||||
|
||||
if (it.hashCode() != (contacts_list.adapter as? ContactsAdapter)?.contactItems?.hashCode()) {
|
||||
activity!!.runOnUiThread {
|
||||
setupContacts(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupContacts(contacts: ArrayList<Contact>) {
|
||||
contacts_placeholder_2.beVisibleIf(contacts.isEmpty())
|
||||
contacts_placeholder.beVisibleIf(contacts.isEmpty())
|
||||
|
||||
val currAdapter = contacts_list.adapter
|
||||
if (currAdapter == null) {
|
||||
ContactsAdapter(activity as SimpleActivity, contacts, this, contacts_list) {
|
||||
if (config.callContact) {
|
||||
val contact = it as Contact
|
||||
if (contact.phoneNumbers.isNotEmpty()) {
|
||||
(activity as SimpleActivity).tryStartCall(it)
|
||||
} else {
|
||||
activity!!.toast(R.string.no_phone_number_found)
|
||||
}
|
||||
} else {
|
||||
context!!.openContact(it as Contact)
|
||||
}
|
||||
}.apply {
|
||||
setupDragListener(true)
|
||||
addVerticalDividers(true)
|
||||
contacts_list.adapter = this
|
||||
}
|
||||
|
||||
contacts_fastscroller.setViews(contacts_list) {
|
||||
val item = (contacts_list.adapter as ContactsAdapter).contactItems.getOrNull(it)
|
||||
contacts_fastscroller.updateBubbleText(item?.getBubbleText() ?: "")
|
||||
}
|
||||
} else {
|
||||
(currAdapter as ContactsAdapter).apply {
|
||||
startNameWithSurname = config.startNameWithSurname
|
||||
showPhoneNumbers = config.showPhoneNumbers
|
||||
updateItems(contacts)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun addNewContact() {
|
||||
class ContactsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet) {
|
||||
override fun fabClicked() {
|
||||
Intent(context, ContactActivity::class.java).apply {
|
||||
context.startActivity(this)
|
||||
}
|
||||
|
@ -2,42 +2,16 @@ package com.simplemobiletools.contacts.fragments
|
||||
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import com.simplemobiletools.contacts.activities.MainActivity
|
||||
import com.simplemobiletools.contacts.dialogs.AddFavoritesDialog
|
||||
import kotlinx.android.synthetic.main.fragment_favorites.view.*
|
||||
|
||||
class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet) {
|
||||
var activity: MainActivity? = null
|
||||
override fun initFragment(activity: MainActivity) {
|
||||
if (this.activity == null) {
|
||||
this.activity = activity
|
||||
favorites_fab.setOnClickListener {
|
||||
addNewFavorites()
|
||||
}
|
||||
}
|
||||
|
||||
initFavorites()
|
||||
}
|
||||
|
||||
override fun textColorChanged(color: Int) {
|
||||
}
|
||||
|
||||
override fun primaryColorChanged(color: Int) {
|
||||
}
|
||||
|
||||
override fun startNameWithSurnameChanged(startNameWithSurname: Boolean) {
|
||||
}
|
||||
|
||||
override fun onActivityResume() {
|
||||
}
|
||||
|
||||
private fun initFavorites() {
|
||||
|
||||
}
|
||||
|
||||
private fun addNewFavorites() {
|
||||
override fun fabClicked() {
|
||||
AddFavoritesDialog(activity!!) {
|
||||
initFavorites()
|
||||
initContacts()
|
||||
}
|
||||
}
|
||||
|
||||
override fun refreshItems() {
|
||||
initContacts()
|
||||
}
|
||||
}
|
||||
|
@ -1,27 +1,149 @@
|
||||
package com.simplemobiletools.contacts.fragments
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Paint
|
||||
import android.support.design.widget.CoordinatorLayout
|
||||
import android.util.AttributeSet
|
||||
import com.simplemobiletools.commons.extensions.beVisibleIf
|
||||
import com.simplemobiletools.commons.extensions.isActivityDestroyed
|
||||
import com.simplemobiletools.commons.extensions.toast
|
||||
import com.simplemobiletools.commons.extensions.updateTextColors
|
||||
import com.simplemobiletools.commons.helpers.SORT_BY_FIRST_NAME
|
||||
import com.simplemobiletools.commons.helpers.SORT_BY_SURNAME
|
||||
import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener
|
||||
import com.simplemobiletools.contacts.R
|
||||
import com.simplemobiletools.contacts.activities.MainActivity
|
||||
import com.simplemobiletools.contacts.activities.SimpleActivity
|
||||
import com.simplemobiletools.contacts.adapters.ContactsAdapter
|
||||
import com.simplemobiletools.contacts.extensions.config
|
||||
import com.simplemobiletools.contacts.extensions.openContact
|
||||
import com.simplemobiletools.contacts.extensions.tryStartCall
|
||||
import com.simplemobiletools.contacts.helpers.Config
|
||||
import com.simplemobiletools.contacts.helpers.ContactsHelper
|
||||
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), RefreshRecyclerViewListener {
|
||||
var activity: MainActivity? = null
|
||||
lateinit var config: Config
|
||||
|
||||
fun setupFragment(activity: MainActivity) {
|
||||
config = activity.config
|
||||
initFragment(activity)
|
||||
if (this.activity == null) {
|
||||
this.activity = activity
|
||||
fragment_fab.setOnClickListener {
|
||||
fabClicked()
|
||||
}
|
||||
|
||||
fragment_placeholder_2.setOnClickListener {
|
||||
activity.showFilterDialog()
|
||||
}
|
||||
|
||||
fragment_placeholder_2.paintFlags = fragment_placeholder_2.paintFlags or Paint.UNDERLINE_TEXT_FLAG
|
||||
updateViewStuff()
|
||||
}
|
||||
|
||||
initContacts()
|
||||
}
|
||||
|
||||
abstract fun initFragment(activity: MainActivity)
|
||||
fun textColorChanged(color: Int) {
|
||||
(fragment_list.adapter as ContactsAdapter).apply {
|
||||
updateTextColor(color)
|
||||
initDrawables()
|
||||
}
|
||||
}
|
||||
|
||||
abstract fun textColorChanged(color: Int)
|
||||
fun primaryColorChanged(color: Int) {
|
||||
fragment_fastscroller.updatePrimaryColor()
|
||||
fragment_fab.setColors(config.textColor, color, config.backgroundColor)
|
||||
fragment_fastscroller.updateBubblePrimaryColor()
|
||||
}
|
||||
|
||||
abstract fun primaryColorChanged(color: Int)
|
||||
fun startNameWithSurnameChanged(startNameWithSurname: Boolean) {
|
||||
(fragment_list.adapter as ContactsAdapter).apply {
|
||||
config.sorting = if (startNameWithSurname) SORT_BY_SURNAME else SORT_BY_FIRST_NAME
|
||||
initContacts()
|
||||
}
|
||||
}
|
||||
|
||||
abstract fun startNameWithSurnameChanged(startNameWithSurname: Boolean)
|
||||
fun initContacts() {
|
||||
if (activity == null || activity!!.isActivityDestroyed()) {
|
||||
return
|
||||
}
|
||||
|
||||
abstract fun onActivityResume()
|
||||
ContactsHelper(activity!!).getContacts {
|
||||
var contacts = it
|
||||
if (activity == null || activity!!.isActivityDestroyed()) {
|
||||
return@getContacts
|
||||
}
|
||||
|
||||
if (config.lastUsedContactSource.isEmpty()) {
|
||||
val grouped = contacts.groupBy { it.source }.maxWith(compareBy { it.value.size })
|
||||
config.lastUsedContactSource = grouped?.key ?: ""
|
||||
}
|
||||
|
||||
Contact.sorting = config.sorting
|
||||
contacts.sort()
|
||||
|
||||
if (this is FavoritesFragment) {
|
||||
val favorites = config.favorites
|
||||
contacts = contacts.filter { favorites.contains(it.id.toString()) } as ArrayList<Contact>
|
||||
}
|
||||
|
||||
if (contacts.hashCode() != (fragment_list.adapter as? ContactsAdapter)?.contactItems?.hashCode()) {
|
||||
activity!!.runOnUiThread {
|
||||
setupContacts(contacts)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupContacts(contacts: ArrayList<Contact>) {
|
||||
fragment_placeholder_2.beVisibleIf(contacts.isEmpty())
|
||||
fragment_placeholder.beVisibleIf(contacts.isEmpty())
|
||||
|
||||
val currAdapter = fragment_list.adapter
|
||||
if (currAdapter == null) {
|
||||
ContactsAdapter(activity as SimpleActivity, contacts, this, fragment_list) {
|
||||
if (config.callContact) {
|
||||
val contact = it as Contact
|
||||
if (contact.phoneNumbers.isNotEmpty()) {
|
||||
(activity as SimpleActivity).tryStartCall(it)
|
||||
} else {
|
||||
activity!!.toast(R.string.no_phone_number_found)
|
||||
}
|
||||
} else {
|
||||
context!!.openContact(it as Contact)
|
||||
}
|
||||
}.apply {
|
||||
setupDragListener(true)
|
||||
addVerticalDividers(true)
|
||||
fragment_list.adapter = this
|
||||
}
|
||||
|
||||
fragment_fastscroller.setViews(fragment_list) {
|
||||
val item = (fragment_list.adapter as ContactsAdapter).contactItems.getOrNull(it)
|
||||
fragment_fastscroller.updateBubbleText(item?.getBubbleText() ?: "")
|
||||
}
|
||||
} else {
|
||||
(currAdapter as ContactsAdapter).apply {
|
||||
startNameWithSurname = config.startNameWithSurname
|
||||
showPhoneNumbers = config.showPhoneNumbers
|
||||
updateItems(contacts)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun onActivityResume() {
|
||||
updateViewStuff()
|
||||
}
|
||||
|
||||
private fun updateViewStuff() {
|
||||
context.updateTextColors(fragment_wrapper)
|
||||
fragment_fastscroller.updateBubbleColors()
|
||||
fragment_fastscroller.allowBubbleDisplay = config.showInfoBubble
|
||||
fragment_placeholder_2.setTextColor(config.primaryColor)
|
||||
}
|
||||
|
||||
abstract fun fabClicked()
|
||||
}
|
||||
|
@ -1,68 +1,10 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<com.simplemobiletools.contacts.fragments.ContactsFragment
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:id="@+id/contacts_fragment"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<RelativeLayout
|
||||
android:id="@+id/contacts_wrapper"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<com.simplemobiletools.commons.views.MyTextView
|
||||
android:id="@+id/contacts_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:text="@string/no_items_found"
|
||||
android:textSize="@dimen/bigger_text_size"
|
||||
android:visibility="gone"/>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyTextView
|
||||
android:id="@+id/contacts_placeholder_2"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/contacts_placeholder"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:gravity="center"
|
||||
android:paddingBottom="@dimen/medium_margin"
|
||||
android:paddingTop="@dimen/medium_margin"
|
||||
android:text="@string/change_filter"
|
||||
android:textSize="@dimen/bigger_text_size"
|
||||
android:visibility="gone"/>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyRecyclerView
|
||||
android:id="@+id/contacts_list"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:clipToPadding="false"
|
||||
android:scrollbars="none"
|
||||
app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
|
||||
|
||||
<com.simplemobiletools.commons.views.FastScroller
|
||||
android:id="@+id/contacts_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/contacts_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.ContactsFragment>
|
||||
|
@ -5,19 +5,6 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<RelativeLayout
|
||||
android:id="@+id/favorites_wrapper"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyFloatingActionButton
|
||||
android:id="@+id/favorites_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.FavoritesFragment>
|
||||
|
65
app/src/main/res/layout/fragment_layout.xml
Normal file
65
app/src/main/res/layout/fragment_layout.xml
Normal file
@ -0,0 +1,65 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<merge
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<RelativeLayout
|
||||
android:id="@+id/fragment_wrapper"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<com.simplemobiletools.commons.views.MyTextView
|
||||
android:id="@+id/fragment_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:text="@string/no_items_found"
|
||||
android:textSize="@dimen/bigger_text_size"
|
||||
android:visibility="gone"/>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyTextView
|
||||
android:id="@+id/fragment_placeholder_2"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/fragment_placeholder"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:gravity="center"
|
||||
android:paddingBottom="@dimen/medium_margin"
|
||||
android:paddingTop="@dimen/medium_margin"
|
||||
android:text="@string/change_filter"
|
||||
android:textSize="@dimen/bigger_text_size"
|
||||
android:visibility="gone"/>
|
||||
|
||||
<com.simplemobiletools.commons.views.MyRecyclerView
|
||||
android:id="@+id/fragment_list"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:clipToPadding="false"
|
||||
android:scrollbars="none"
|
||||
app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
|
||||
|
||||
<com.simplemobiletools.commons.views.FastScroller
|
||||
android:id="@+id/fragment_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/fragment_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"/>
|
||||
|
||||
</merge>
|
Loading…
x
Reference in New Issue
Block a user