Contacts favorites grid implemented

This commit is contained in:
merkost 2023-07-17 15:13:41 +10:00
parent ffd89252c7
commit ce337a31f5
7 changed files with 240 additions and 81 deletions

View File

@ -10,16 +10,18 @@ import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ContactsHelper
import com.simplemobiletools.commons.helpers.NavigationIcon
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.models.contacts.Contact
import com.simplemobiletools.commons.models.contacts.Group
import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.adapters.ContactsAdapter
import com.simplemobiletools.contacts.pro.dialogs.SelectContactsDialog
import com.simplemobiletools.contacts.pro.extensions.*
import com.simplemobiletools.contacts.pro.extensions.handleGenericContactClick
import com.simplemobiletools.contacts.pro.helpers.GROUP
import com.simplemobiletools.contacts.pro.helpers.LOCATION_GROUP_CONTACTS
import com.simplemobiletools.contacts.pro.interfaces.RefreshContactsListener
import com.simplemobiletools.contacts.pro.interfaces.RemoveFromGroupListener
import com.simplemobiletools.commons.models.contacts.*
import kotlinx.android.synthetic.main.activity_group_contacts.*
import kotlinx.android.synthetic.main.fragment_layout.fragment_list
class GroupContactsActivity : SimpleActivity(), RemoveFromGroupListener, RefreshContactsListener {
private var allContacts = ArrayList<Contact>()
@ -146,7 +148,14 @@ class GroupContactsActivity : SimpleActivity(), RemoveFromGroupListener, Refresh
private fun updateContacts(contacts: ArrayList<Contact>) {
val currAdapter = group_contacts_list.adapter
if (currAdapter == null) {
ContactsAdapter(this, contacts, this, LOCATION_GROUP_CONTACTS, this, group_contacts_list) {
ContactsAdapter(
this,
contactItems = contacts,
recyclerView = fragment_list,
location = LOCATION_GROUP_CONTACTS,
removeListener = this,
refreshListener = this
) {
contactClicked(it as Contact)
}.apply {
group_contacts_list.adapter = this

View File

@ -15,6 +15,7 @@ import android.widget.TextView
import android.widget.Toast
import androidx.viewpager.widget.ViewPager
import com.simplemobiletools.commons.databases.ContactsDatabase
import com.simplemobiletools.commons.dialogs.ChangeViewTypeDialog
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
import com.simplemobiletools.commons.dialogs.FilePickerDialog
import com.simplemobiletools.commons.extensions.*
@ -198,6 +199,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
findItem(R.id.sort).isVisible = currentFragment != groups_fragment
findItem(R.id.filter).isVisible = currentFragment != groups_fragment
findItem(R.id.dialpad).isVisible = !config.showDialpadButton
findItem(R.id.change_view_type).isVisible = currentFragment == favorites_fragment
findItem(R.id.more_apps_from_us).isVisible = !resources.getBoolean(R.bool.hide_google_relations)
}
}
@ -225,6 +227,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
R.id.import_contacts -> tryImportContacts()
R.id.export_contacts -> tryExportContacts()
R.id.more_apps_from_us -> launchMoreAppsFromUsIntent()
R.id.change_view_type -> changeViewType()
R.id.settings -> launchSettings()
R.id.about -> launchAbout()
else -> return@setOnMenuItemClickListener false
@ -661,4 +664,10 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
checkWhatsNew(this, BuildConfig.VERSION_CODE)
}
}
private fun changeViewType() {
ChangeViewTypeDialog(this) {
favorites_fragment?.updateFavoritesColumnCount()
}
}
}

View File

@ -16,6 +16,7 @@ import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
@ -46,15 +47,17 @@ import java.util.*
class ContactsAdapter(
activity: SimpleActivity,
var contactItems: ArrayList<Contact>,
var contactItems: MutableList<Contact>,
recyclerView: MyRecyclerView,
highlightText: String = "",
var viewType: Int = VIEW_TYPE_LIST,
private val refreshListener: RefreshContactsListener?,
private val location: Int,
private val removeListener: RemoveFromGroupListener?,
recyclerView: MyRecyclerView,
highlightText: String = "",
private val enableDrag: Boolean = false,
itemClick: (Any) -> Unit
) : MyRecyclerViewAdapter(activity, recyclerView, itemClick), RecyclerViewFastScroller.OnPopupTextUpdate, ItemTouchHelperContract {
) : MyRecyclerViewAdapter(activity, recyclerView, itemClick), RecyclerViewFastScroller.OnPopupTextUpdate, ItemTouchHelperContract,
MyRecyclerView.MyZoomListener {
private val NEW_GROUP_ID = -1
@ -66,13 +69,16 @@ class ContactsAdapter(
var showPhoneNumbers = config.showPhoneNumbers
var fontSize = activity.getTextSize()
var onDragEndListener: (() -> Unit)? = null
var onColumnCountListener: (Int) -> Unit = {}
private val itemLayout = if (showPhoneNumbers) R.layout.item_contact_with_number else R.layout.item_contact_without_number
private var touchHelper: ItemTouchHelper? = null
private var startReorderDragListener: StartReorderDragListener? = null
init {
setupDragListener(true)
if (recyclerView.layoutManager is GridLayoutManager) {
setupZoomListener(this)
}
if (enableDrag) {
touchHelper = ItemTouchHelper(ItemMoveCallback(this))
@ -143,7 +149,21 @@ class ContactsAdapter(
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = createViewHolder(itemLayout, parent)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val layout = when (viewType) {
VIEW_TYPE_GRID -> {
if (showPhoneNumbers) R.layout.item_contact_with_number_grid else R.layout.item_contact_without_number_grid
}
else -> {
if (showPhoneNumbers) R.layout.item_contact_with_number else R.layout.item_contact_without_number
}
}
return createViewHolder(layout, parent)
}
override fun getItemViewType(position: Int): Int {
return viewType
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val contact = contactItems[position]
@ -158,9 +178,9 @@ class ContactsAdapter(
private fun getItemWithKey(key: Int): Contact? = contactItems.firstOrNull { it.id == key }
fun updateItems(newItems: ArrayList<Contact>, highlightText: String = "") {
fun updateItems(newItems: List<Contact>, highlightText: String = "") {
if (newItems.hashCode() != contactItems.hashCode()) {
contactItems = newItems.clone() as ArrayList<Contact>
contactItems = newItems.toMutableList()
textToHighlight = highlightText
notifyDataSetChanged()
finishActMode()
@ -471,4 +491,26 @@ class ContactsAdapter(
override fun onRowClear(myViewHolder: ViewHolder?) {
onDragEndListener?.invoke()
}
override fun zoomIn() {
val layoutManager = recyclerView.layoutManager
if (layoutManager is GridLayoutManager) {
val currentSpanCount = layoutManager.spanCount
val newSpanCount = (currentSpanCount - 1).coerceIn(CONTACTS_GRID_MIN_COLUMNS_COUNT, CONTACTS_GRID_MAX_COLUMNS_COUNT)
layoutManager.spanCount = newSpanCount
recyclerView.requestLayout()
onColumnCountListener(newSpanCount)
}
}
override fun zoomOut() {
val layoutManager = recyclerView.layoutManager
if (layoutManager is GridLayoutManager) {
val currentSpanCount = layoutManager.spanCount
val newSpanCount = (currentSpanCount + 1).coerceIn(CONTACTS_GRID_MIN_COLUMNS_COUNT, CONTACTS_GRID_MAX_COLUMNS_COUNT)
layoutManager.spanCount = newSpanCount
recyclerView.requestLayout()
onColumnCountListener(newSpanCount)
}
}
}

View File

@ -3,10 +3,19 @@ package com.simplemobiletools.contacts.pro.fragments
import android.content.Context
import android.content.Intent
import android.util.AttributeSet
import com.simplemobiletools.commons.extensions.areSystemAnimationsEnabled
import com.simplemobiletools.commons.extensions.hideKeyboard
import com.simplemobiletools.commons.models.contacts.Contact
import com.simplemobiletools.contacts.pro.activities.EditContactActivity
import com.simplemobiletools.contacts.pro.activities.InsertOrEditContactActivity
import com.simplemobiletools.contacts.pro.activities.MainActivity
import com.simplemobiletools.contacts.pro.activities.SimpleActivity
import com.simplemobiletools.contacts.pro.adapters.ContactsAdapter
import com.simplemobiletools.contacts.pro.extensions.config
import com.simplemobiletools.contacts.pro.helpers.LOCATION_CONTACTS_TAB
import com.simplemobiletools.contacts.pro.interfaces.RefreshContactsListener
import kotlinx.android.synthetic.main.fragment_layout.view.fragment_list
import java.util.ArrayList
class ContactsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet) {
override fun fabClicked() {
@ -23,4 +32,39 @@ class ContactsFragment(context: Context, attributeSet: AttributeSet) : MyViewPag
(activity as InsertOrEditContactActivity).showFilterDialog()
}
}
fun setupContactsAdapter(contacts: List<Contact>) {
setupViewVisibility(contacts.isNotEmpty())
val currAdapter = fragment_list.adapter
if (currAdapter == null || forceListRedraw) {
forceListRedraw = false
val location = LOCATION_CONTACTS_TAB
ContactsAdapter(
activity = activity as SimpleActivity,
contactItems = contacts.toMutableList(),
refreshListener = activity as RefreshContactsListener,
location = location,
removeListener = null,
recyclerView = fragment_list,
enableDrag = false,
) {
(activity as RefreshContactsListener).contactClicked(it as Contact)
}.apply {
fragment_list.adapter = this
}
if (context.areSystemAnimationsEnabled) {
fragment_list.scheduleLayoutAnimation()
}
} else {
(currAdapter as ContactsAdapter).apply {
startNameWithSurname = context.config.startNameWithSurname
showPhoneNumbers = context.config.showPhoneNumbers
showContactThumbnails = context.config.showContactThumbnails
updateItems(contacts)
}
}
}
}

View File

@ -2,11 +2,26 @@ package com.simplemobiletools.contacts.pro.fragments
import android.content.Context
import android.util.AttributeSet
import com.google.gson.Gson
import com.simplemobiletools.commons.extensions.areSystemAnimationsEnabled
import com.simplemobiletools.commons.extensions.beGone
import com.simplemobiletools.commons.extensions.beVisible
import com.simplemobiletools.commons.helpers.ContactsHelper
import com.simplemobiletools.commons.helpers.TAB_FAVORITES
import com.simplemobiletools.commons.helpers.VIEW_TYPE_GRID
import com.simplemobiletools.commons.models.contacts.Contact
import com.simplemobiletools.commons.views.MyGridLayoutManager
import com.simplemobiletools.commons.views.MyLinearLayoutManager
import com.simplemobiletools.contacts.pro.activities.MainActivity
import com.simplemobiletools.contacts.pro.activities.SimpleActivity
import com.simplemobiletools.contacts.pro.adapters.ContactsAdapter
import com.simplemobiletools.contacts.pro.dialogs.SelectContactsDialog
import com.simplemobiletools.contacts.pro.extensions.config
import com.simplemobiletools.contacts.pro.helpers.LOCATION_FAVORITES_TAB
import com.simplemobiletools.contacts.pro.interfaces.RefreshContactsListener
import kotlinx.android.synthetic.main.dialog_select_contact.view.letter_fastscroller
import kotlinx.android.synthetic.main.fragment_favorites.view.favorites_fragment
import kotlinx.android.synthetic.main.fragment_layout.view.fragment_list
class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet) {
override fun fabClicked() {
@ -28,4 +43,82 @@ class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPa
(activity as? MainActivity)?.refreshContacts(TAB_FAVORITES)
}
}
fun setupContactsFavoritesAdapter(contacts: ArrayList<Contact>) {
setupViewVisibility(contacts.isNotEmpty())
val currAdapter = fragment_list.adapter
val viewType = context.config.viewType
setFavoritesViewType(viewType)
if (currAdapter == null || forceListRedraw) {
forceListRedraw = false
val location = LOCATION_FAVORITES_TAB
ContactsAdapter(
activity = activity as SimpleActivity,
contactItems = contacts,
refreshListener = activity as RefreshContactsListener,
location = location,
viewType = viewType,
removeListener = null,
recyclerView = fragment_list,
enableDrag = true,
) {
(activity as RefreshContactsListener).contactClicked(it as Contact)
}.apply {
fragment_list.adapter = this
onDragEndListener = {
val adapter = fragment_list?.adapter
if (adapter is ContactsAdapter) {
val items = adapter.contactItems
saveCustomOrderToPrefs(items)
setupLetterFastscroller(items)
}
}
onColumnCountListener = { newColumnCount ->
context.config.contactsGridColumnCnt = newColumnCount
}
}
if (context.areSystemAnimationsEnabled) {
fragment_list.scheduleLayoutAnimation()
}
} else {
(currAdapter as ContactsAdapter).apply {
startNameWithSurname = context.config.startNameWithSurname
showPhoneNumbers = context.config.showPhoneNumbers
showContactThumbnails = context.config.showContactThumbnails
this.viewType = viewType
updateItems(contacts)
}
}
}
private fun setFavoritesViewType(viewType: Int) {
val spanCount = context.config.contactsGridColumnCnt
val layoutManager = if (viewType == VIEW_TYPE_GRID) {
favorites_fragment.letter_fastscroller.beGone()
MyGridLayoutManager(context, spanCount)
} else {
favorites_fragment.letter_fastscroller.beVisible()
MyLinearLayoutManager(context)
}
fragment_list.layoutManager = layoutManager
}
fun updateFavoritesColumnCount() {
setupContactsFavoritesAdapter(allContacts)
}
private fun saveCustomOrderToPrefs(items: List<Contact>) {
activity?.apply {
val orderIds = items.map { it.id }
val orderGsonString = Gson().toJson(orderIds)
config.favoritesContactsOrder = orderGsonString
}
}
}

View File

@ -21,7 +21,12 @@ import com.simplemobiletools.commons.helpers.Converters
import com.simplemobiletools.contacts.pro.helpers.*
import com.simplemobiletools.contacts.pro.interfaces.RefreshContactsListener
import com.simplemobiletools.commons.models.contacts.*
import com.simplemobiletools.commons.views.MyGridLayoutManager
import com.simplemobiletools.commons.views.MyLinearLayoutManager
import com.simplemobiletools.contacts.pro.extensions.config
import kotlinx.android.synthetic.main.dialog_select_contact.view.letter_fastscroller
import kotlinx.android.synthetic.main.fragment_contacts.view.contacts_fragment
import kotlinx.android.synthetic.main.fragment_favorites.view.favorites_fragment
import kotlinx.android.synthetic.main.fragment_layout.view.*
import kotlinx.android.synthetic.main.fragment_layout.view.fragment_fab
import kotlinx.android.synthetic.main.fragment_layout.view.fragment_list
@ -30,14 +35,15 @@ import kotlinx.android.synthetic.main.fragment_layout.view.fragment_placeholder_
import kotlinx.android.synthetic.main.fragment_layout.view.fragment_wrapper
import kotlinx.android.synthetic.main.fragment_letters_layout.view.*
import java.util.*
import kotlin.collections.ArrayList
abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet) : CoordinatorLayout(context, attributeSet) {
protected var activity: SimpleActivity? = null
protected var allContacts = ArrayList<Contact>()
private var lastHashCode = 0
private var contactsIgnoringSearch = ArrayList<Contact>()
private var groupsIgnoringSearch = ArrayList<Group>()
private var contactsIgnoringSearch = listOf<Contact>()
private var groupsIgnoringSearch = listOf<Group>()
private lateinit var config: Config
var skipHashComparing = false
@ -172,17 +178,26 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
}
private fun setupContacts(contacts: ArrayList<Contact>) {
if (this is GroupsFragment) {
when (this) {
is GroupsFragment -> {
setupGroupsAdapter(contacts) {
groupsIgnoringSearch = (fragment_list?.adapter as? GroupsAdapter)?.groups ?: ArrayList()
}
} else {
setupContactsFavoritesAdapter(contacts)
}
is FavoritesFragment -> {
favorites_fragment.setupContactsFavoritesAdapter(contacts)
contactsIgnoringSearch = (fragment_list?.adapter as? ContactsAdapter)?.contactItems ?: listOf()
setupLetterFastscroller(contacts)
letter_fastscroller_thumb.setupWithFastScroller(letter_fastscroller)
}
else -> {
contacts_fragment.setupContactsAdapter(contacts)
contactsIgnoringSearch = (fragment_list?.adapter as? ContactsAdapter)?.contactItems ?: ArrayList()
setupLetterFastscroller(contacts)
letter_fastscroller_thumb.setupWithFastScroller(letter_fastscroller)
}
}
}
private fun setupGroupsAdapter(contacts: ArrayList<Contact>, callback: () -> Unit) {
ContactsHelper(activity!!).getStoredGroups {
@ -227,63 +242,6 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
}
}
private fun setupContactsFavoritesAdapter(contacts: ArrayList<Contact>) {
setupViewVisibility(contacts.isNotEmpty())
val currAdapter = fragment_list.adapter
if (currAdapter == null || forceListRedraw) {
forceListRedraw = false
val location = when {
activity is InsertOrEditContactActivity -> LOCATION_INSERT_OR_EDIT
this is FavoritesFragment -> LOCATION_FAVORITES_TAB
else -> LOCATION_CONTACTS_TAB
}
val enableDragReorder = this is FavoritesFragment
ContactsAdapter(
activity = activity as SimpleActivity,
contactItems = contacts,
refreshListener = activity as RefreshContactsListener,
location = location,
removeListener = null,
recyclerView = fragment_list,
enableDrag = enableDragReorder,
) {
(activity as RefreshContactsListener).contactClicked(it as Contact)
}.apply {
fragment_list.adapter = this
if (enableDragReorder) {
onDragEndListener = {
val adapter = fragment_list?.adapter
if (adapter is ContactsAdapter) {
val items = adapter.contactItems
saveCustomOrderToPrefs(items)
setupLetterFastscroller(items)
}
}
}
}
if (context.areSystemAnimationsEnabled) {
fragment_list.scheduleLayoutAnimation()
}
} else {
(currAdapter as ContactsAdapter).apply {
startNameWithSurname = config.startNameWithSurname
showPhoneNumbers = config.showPhoneNumbers
showContactThumbnails = config.showContactThumbnails
updateItems(contacts)
}
}
}
private fun saveCustomOrderToPrefs(items: ArrayList<Contact>) {
activity?.apply {
val orderIds = items.map { it.id }
val orderGsonString = Gson().toJson(orderIds)
config.favoritesContactsOrder = orderGsonString
}
}
fun showContactThumbnailsChanged(showThumbnails: Boolean) {
if (this is GroupsFragment) {
(fragment_list.adapter as? GroupsAdapter)?.apply {
@ -298,7 +256,7 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
}
}
private fun setupLetterFastscroller(contacts: ArrayList<Contact>) {
fun setupLetterFastscroller(contacts: List<Contact>) {
val sorting = context.config.sorting
letter_fastscroller.setupWithRecyclerView(fragment_list, { position ->
try {
@ -396,7 +354,7 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
setupLetterFastscroller(contactsIgnoringSearch)
setupViewVisibility(contactsIgnoringSearch.isNotEmpty())
} else if (fragment_list.adapter is GroupsAdapter) {
(fragment_list.adapter as? GroupsAdapter)?.updateItems(groupsIgnoringSearch)
(fragment_list.adapter as? GroupsAdapter)?.updateItems(ArrayList(groupsIgnoringSearch))
setupViewVisibility(groupsIgnoringSearch.isNotEmpty())
}
@ -405,7 +363,7 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
}
}
private fun setupViewVisibility(hasItemsToShow: Boolean) {
fun setupViewVisibility(hasItemsToShow: Boolean) {
if (fragment_placeholder_2.tag != AVOID_CHANGING_VISIBILITY_TAG) {
fragment_placeholder_2?.beVisibleIf(!hasItemsToShow)
}

View File

@ -18,6 +18,10 @@
android:icon="@drawable/ic_dialpad_vector"
android:title="@string/dialpad"
app:showAsAction="ifRoom" />
<item
android:id="@+id/change_view_type"
android:title="@string/change_view_type"
app:showAsAction="never" />
<item
android:id="@+id/import_contacts"
android:showAsAction="never"