show a list of contacts for managing favorites at FAB press

This commit is contained in:
tibbi 2017-12-30 13:22:18 +01:00
parent 88b624bbc5
commit d807df4f97
6 changed files with 277 additions and 0 deletions

View File

@ -0,0 +1,110 @@
package com.simplemobiletools.contacts.adapters
import android.graphics.drawable.Drawable
import android.support.v7.widget.RecyclerView
import android.util.SparseArray
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.signature.ObjectKey
import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor
import com.simplemobiletools.commons.interfaces.MyAdapterListener
import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.activities.SimpleActivity
import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.helpers.Config
import com.simplemobiletools.contacts.models.Contact
import kotlinx.android.synthetic.main.item_add_favorite_with_number.view.*
import java.util.*
class AddFavoritesAdapter(val activity: SimpleActivity, val contacts: List<Contact>) : RecyclerView.Adapter<AddFavoritesAdapter.ViewHolder>() {
private val itemViews = SparseArray<View>()
private val selectedPositions = HashSet<Int>()
private val config = activity.config
private val textColor = config.textColor
private val contactDrawable = activity.resources.getColoredDrawableWithColor(R.drawable.ic_person, textColor)
private val startNameWithSurname = config.startNameWithSurname
private val itemLayout = if (config.showPhoneNumbers) R.layout.item_add_favorite_with_number else R.layout.item_add_favorite_without_number
private fun toggleItemSelection(select: Boolean, pos: Int) {
if (select) {
if (itemViews[pos] != null) {
selectedPositions.add(pos)
}
} else {
selectedPositions.remove(pos)
}
itemViews[pos]?.contact_checkbox?.isChecked = select
}
private val adapterListener = object : MyAdapterListener {
override fun toggleItemSelectionAdapter(select: Boolean, position: Int) {
toggleItemSelection(select, position)
}
override fun getSelectedPositions() = selectedPositions
override fun itemLongClicked(position: Int) {}
}
fun getSelectedItemsSet(): HashSet<String> {
val selectedItemsSet = HashSet<String>(selectedPositions.size)
selectedPositions.forEach { selectedItemsSet.add(contacts[it].id.toString()) }
return selectedItemsSet
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
val view = activity.layoutInflater.inflate(itemLayout, parent, false)
return ViewHolder(view, adapterListener, activity)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val eventType = contacts[position]
itemViews.put(position, holder.bindView(eventType, startNameWithSurname, contactDrawable, config))
toggleItemSelection(selectedPositions.contains(position), position)
}
override fun getItemCount() = contacts.size
class ViewHolder(view: View, val adapterListener: MyAdapterListener, val activity: SimpleActivity) : RecyclerView.ViewHolder(view) {
fun bindView(contact: Contact, startNameWithSurname: Boolean, contactDrawable: Drawable, config: Config): View {
itemView.apply {
contact_checkbox.setColors(config.textColor, config.primaryColor, config.backgroundColor)
val textColor = config.textColor
contact_name.text = contact.getFullName(startNameWithSurname)
contact_name.setTextColor(textColor)
contact_number?.text = contact.phoneNumbers.firstOrNull()?.value ?: ""
contact_number?.setTextColor(textColor)
contact_frame.setOnClickListener { viewClicked(!contact_checkbox.isChecked) }
if (contact.photoUri.isNotEmpty()) {
val options = RequestOptions()
.signature(ObjectKey(contact.photoUri))
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.error(contactDrawable)
.centerCrop()
Glide.with(activity).load(contact.photoUri).transition(DrawableTransitionOptions.withCrossFade()).apply(options).into(contact_tmb)
} else {
contact_tmb.setImageDrawable(contactDrawable)
}
}
return itemView
}
private fun viewClicked(select: Boolean) {
adapterListener.toggleItemSelectionAdapter(select, adapterPosition)
}
}
override fun onViewRecycled(holder: ViewHolder?) {
super.onViewRecycled(holder)
Glide.with(activity).clear(holder?.itemView?.contact_tmb)
}
}

View File

@ -0,0 +1,38 @@
package com.simplemobiletools.contacts.dialogs
import android.support.v7.app.AlertDialog
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.activities.SimpleActivity
import com.simplemobiletools.contacts.adapters.AddFavoritesAdapter
import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.helpers.ContactsHelper
import com.simplemobiletools.contacts.models.Contact
import kotlinx.android.synthetic.main.dialog_add_favorites.view.*
class AddFavoritesDialog(val activity: SimpleActivity, val callback: () -> Unit) {
var dialog: AlertDialog? = null
private var view = activity.layoutInflater.inflate(R.layout.dialog_add_favorites, null)
init {
ContactsHelper(activity).getContacts {
Contact.sorting = activity.config.sorting
it.sort()
view.add_favorites_list.adapter = AddFavoritesAdapter(activity, it)
activity.runOnUiThread {
dialog = AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok, { dialog, which -> dialogConfirmed() })
.setNegativeButton(R.string.cancel, null)
.create().apply {
activity.setupDialogStuff(view, this)
}
}
}
}
private fun dialogConfirmed() {
val selectedItems = (view.add_favorites_list.adapter as AddFavoritesAdapter).getSelectedItemsSet()
dialog?.dismiss()
}
}

View File

@ -3,6 +3,7 @@ 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) {
@ -29,6 +30,8 @@ class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPa
}
private fun addNewFavorites() {
AddFavoritesDialog(activity!!) {
}
}
}

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<com.simplemobiletools.commons.views.MyRecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/add_favorites_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingTop="@dimen/medium_margin"
android:scrollbars="vertical"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>

View File

@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/contact_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true">
<RelativeLayout
android:id="@+id/contact_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="@dimen/medium_margin">
<ImageView
android:id="@+id/contact_tmb"
android:layout_width="@dimen/normal_icon_size"
android:layout_height="@dimen/normal_icon_size"
android:padding="@dimen/medium_margin"
android:src="@drawable/ic_person"/>
<TextView
android:id="@+id/contact_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/contact_tmb"
android:layout_toLeftOf="@+id/contact_checkbox"
android:layout_toRightOf="@+id/contact_tmb"
android:ellipsize="end"
android:maxLines="1"
android:paddingLeft="@dimen/small_margin"
android:paddingRight="@dimen/small_margin"
android:paddingTop="@dimen/small_margin"
android:textSize="@dimen/normal_text_size"
tools:text="John Doe"/>
<TextView
android:id="@+id/contact_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/contact_name"
android:layout_toLeftOf="@+id/contact_checkbox"
android:layout_toRightOf="@+id/contact_tmb"
android:maxLines="1"
android:paddingLeft="@dimen/small_margin"
android:paddingRight="@dimen/small_margin"
android:textSize="@dimen/normal_text_size"
tools:text="0123 456 789"/>
<com.simplemobiletools.commons.views.MyAppCompatCheckbox
android:id="@+id/contact_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/contact_tmb"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/contact_tmb"
android:gravity="center_vertical"/>
</RelativeLayout>
</FrameLayout>

View File

@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/contact_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true">
<RelativeLayout
android:id="@+id/contact_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="@dimen/medium_margin">
<ImageView
android:id="@+id/contact_tmb"
android:layout_width="@dimen/normal_icon_size"
android:layout_height="@dimen/normal_icon_size"
android:padding="@dimen/medium_margin"
android:src="@drawable/ic_person"/>
<TextView
android:id="@+id/contact_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/contact_tmb"
android:layout_alignTop="@+id/contact_tmb"
android:layout_toLeftOf="@+id/contact_checkbox"
android:layout_toRightOf="@+id/contact_tmb"
android:ellipsize="end"
android:gravity="center_vertical"
android:maxLines="1"
android:paddingLeft="@dimen/small_margin"
android:paddingRight="@dimen/small_margin"
android:paddingTop="@dimen/small_margin"
android:textSize="@dimen/bigger_text_size"
tools:text="John Doe"/>
<com.simplemobiletools.commons.views.MyAppCompatCheckbox
android:id="@+id/contact_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/contact_tmb"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/contact_tmb"
android:gravity="center_vertical"/>
</RelativeLayout>
</FrameLayout>