allow deleting Contacts at the first tab

This commit is contained in:
tibbi 2020-05-12 12:55:40 +02:00
parent ba34830449
commit f680535c99
11 changed files with 93 additions and 32 deletions

View File

@ -36,6 +36,6 @@ android {
}
dependencies {
implementation 'com.simplemobiletools:commons:5.27.24'
implementation 'com.simplemobiletools:commons:5.27.26'
implementation 'com.github.tibbi:IndicatorFastScroll:08f512858a'
}

View File

@ -5,6 +5,7 @@
android:installLocation="auto">
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />

View File

@ -194,7 +194,7 @@ class DialpadActivity : SimpleActivity() {
}
})
ContactsAdapter(this, filtered, dialpad_list, text) {
ContactsAdapter(this, filtered, dialpad_list, null, text) {
startCallIntent((it as SimpleContact).phoneNumber)
}.apply {
dialpad_list.adapter = this

View File

@ -15,6 +15,7 @@ import com.simplemobiletools.dialer.BuildConfig
import com.simplemobiletools.dialer.R
import com.simplemobiletools.dialer.adapters.ViewPagerAdapter
import com.simplemobiletools.dialer.extensions.config
import com.simplemobiletools.dialer.fragments.MyViewPagerFragment
import com.simplemobiletools.dialer.helpers.tabsList
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_contacts.*
@ -56,7 +57,7 @@ class MainActivity : SimpleActivity() {
}
getAllFragments().forEach {
it?.textColorChanged(configTextColor)
it.textColorChanged(configTextColor)
}
}
@ -65,7 +66,7 @@ class MainActivity : SimpleActivity() {
main_tabs_holder.setSelectedTabIndicatorColor(getAdjustedPrimaryColor())
main_tabs_holder.getTabAt(viewpager.currentItem)?.icon?.applyColorFilter(getAdjustedPrimaryColor())
getAllFragments().forEach {
it?.primaryColorChanged(configPrimaryColor)
it.primaryColorChanged(configPrimaryColor)
}
}
@ -154,7 +155,7 @@ class MainActivity : SimpleActivity() {
override fun onPageSelected(position: Int) {
main_tabs_holder.getTabAt(position)?.select()
getAllFragments().forEach {
it?.finishActMode()
it.finishActMode()
}
invalidateOptionsMenu()
}
@ -210,11 +211,11 @@ class MainActivity : SimpleActivity() {
viewpager.currentItem = config.lastUsedViewPagerPage
}
contacts_fragment.refreshContacts()
recents_fragment.refreshRecents()
contacts_fragment.refreshItems()
recents_fragment.refreshItems()
}
private fun getAllFragments() = arrayListOf(contacts_fragment, recents_fragment)
private fun getAllFragments() = arrayListOf(contacts_fragment, recents_fragment).toMutableList() as ArrayList<MyViewPagerFragment>
private fun launchAbout() {
val licenses = LICENSE_GLIDE or LICENSE_INDICATOR_FAST_SCROLL

View File

@ -4,33 +4,48 @@ import android.util.TypedValue
import android.view.Menu
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor
import com.simplemobiletools.commons.extensions.getTextSize
import com.simplemobiletools.commons.extensions.highlightTextFromNumbers
import com.simplemobiletools.commons.extensions.highlightTextPart
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CONTACTS
import com.simplemobiletools.commons.helpers.SimpleContactsHelper
import com.simplemobiletools.commons.models.SimpleContact
import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.dialer.R
import com.simplemobiletools.dialer.activities.SimpleActivity
import java.util.*
import com.simplemobiletools.dialer.interfaces.RefreshItemsListener
class ContactsAdapter(activity: SimpleActivity, var contacts: ArrayList<SimpleContact>, recyclerView: MyRecyclerView, highlightText: String = "",
itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, null, itemClick) {
class ContactsAdapter(activity: SimpleActivity, var contacts: ArrayList<SimpleContact>, recyclerView: MyRecyclerView, val refreshItemsListener: RefreshItemsListener? = null,
highlightText: String = "", itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, null, itemClick) {
private var textToHighlight = highlightText
private var adjustedPrimaryColor = activity.getAdjustedPrimaryColor()
private var fontSize = activity.getTextSize()
override fun getActionMenuId() = 0
init {
setupDragListener(true)
}
override fun getActionMenuId() = R.menu.cab_delete_only
override fun prepareActionMode(menu: Menu) {}
override fun actionItemPressed(id: Int) {}
override fun actionItemPressed(id: Int) {
if (selectedKeys.isEmpty()) {
return
}
when (id) {
R.id.cab_delete -> askConfirmDelete()
}
}
override fun getSelectableItemCount() = contacts.size
@ -48,7 +63,7 @@ class ContactsAdapter(activity: SimpleActivity, var contacts: ArrayList<SimpleCo
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val contact = contacts[position]
holder.bindView(contact, true, false) { itemView, layoutPosition ->
holder.bindView(contact, true, true) { itemView, layoutPosition ->
setupView(itemView, contact)
}
bindViewHolder(holder)
@ -68,6 +83,49 @@ class ContactsAdapter(activity: SimpleActivity, var contacts: ArrayList<SimpleCo
}
}
private fun askConfirmDelete() {
val itemsCnt = selectedKeys.size
val firstItem = getSelectedItems().first()
val items = if (itemsCnt == 1) {
"\"${firstItem.name}\""
} else {
resources.getQuantityString(R.plurals.delete_contacts, itemsCnt, itemsCnt)
}
val baseString = R.string.deletion_confirmation
val question = String.format(resources.getString(baseString), items)
ConfirmationDialog(activity, question) {
activity.handlePermission(PERMISSION_WRITE_CONTACTS) {
deleteContacts()
}
}
}
private fun deleteContacts() {
if (selectedKeys.isEmpty()) {
return
}
val contactsToRemove = getSelectedItems()
val positions = getSelectedItemPositions()
contacts.removeAll(contactsToRemove)
val idsToRemove = contactsToRemove.map { it.rawId }.toMutableList() as ArrayList<Int>
SimpleContactsHelper(activity).deleteContactRawIDs(idsToRemove) {
activity.runOnUiThread {
if (contacts.isEmpty()) {
refreshItemsListener?.refreshItems()
finishActMode()
} else {
removeSelectedItems(positions)
}
}
}
}
private fun getSelectedItems() = contacts.filter { selectedKeys.contains(it.rawId) } as ArrayList<SimpleContact>
override fun onViewRecycled(holder: ViewHolder) {
super.onViewRecycled(holder)
if (!activity.isDestroyed && !activity.isFinishing) {
@ -77,6 +135,7 @@ class ContactsAdapter(activity: SimpleActivity, var contacts: ArrayList<SimpleCo
private fun setupView(view: View, contact: SimpleContact) {
view.apply {
findViewById<FrameLayout>(R.id.item_contact_frame).isSelected = selectedKeys.contains(contact.rawId)
findViewById<TextView>(R.id.item_contact_name).apply {
setTextColor(textColor)
setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize)

View File

@ -20,12 +20,12 @@ import com.simplemobiletools.dialer.extensions.areMultipleSIMsAvailable
import com.simplemobiletools.dialer.extensions.config
import com.simplemobiletools.dialer.helpers.KEY_PHONE
import com.simplemobiletools.dialer.helpers.RecentsHelper
import com.simplemobiletools.dialer.interfaces.RefreshRecentsListener
import com.simplemobiletools.dialer.interfaces.RefreshItemsListener
import com.simplemobiletools.dialer.models.RecentCall
import kotlinx.android.synthetic.main.item_recent_call.view.*
import java.util.*
class RecentCallsAdapter(activity: SimpleActivity, var recentCalls: ArrayList<RecentCall>, recyclerView: MyRecyclerView, val refreshRecentsListener: RefreshRecentsListener,
class RecentCallsAdapter(activity: SimpleActivity, var recentCalls: ArrayList<RecentCall>, recyclerView: MyRecyclerView, val refreshItemsListener: RefreshItemsListener,
itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, null, itemClick) {
private lateinit var incomingCallIcon: Drawable
@ -133,7 +133,7 @@ class RecentCallsAdapter(activity: SimpleActivity, var recentCalls: ArrayList<Re
recentCalls.removeAll(callsToRemove)
activity.runOnUiThread {
if (recentCalls.isEmpty()) {
refreshRecentsListener.refreshRecents()
refreshItemsListener.refreshItems()
finishActMode()
} else {
removeSelectedItems(positions)

View File

@ -10,16 +10,17 @@ import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS
import com.simplemobiletools.commons.helpers.SimpleContactsHelper
import com.simplemobiletools.commons.helpers.mydebug
import com.simplemobiletools.commons.models.SimpleContact
import com.simplemobiletools.dialer.R
import com.simplemobiletools.dialer.activities.SimpleActivity
import com.simplemobiletools.dialer.adapters.ContactsAdapter
import com.simplemobiletools.dialer.extensions.config
import kotlinx.android.synthetic.main.fragment_contacts.*
import com.simplemobiletools.dialer.interfaces.RefreshItemsListener
import kotlinx.android.synthetic.main.fragment_letters_layout.view.*
import java.util.*
class ContactsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet) {
class ContactsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet), RefreshItemsListener {
override fun setupFragment() {
val placeholderResId = if (context.hasPermission(PERMISSION_READ_CONTACTS)) {
R.string.no_contacts_found
@ -72,7 +73,7 @@ class ContactsFragment(context: Context, attributeSet: AttributeSet) : MyViewPag
fragment_fab.background.applyColorFilter(context.getAdjustedPrimaryColor())
}
fun refreshContacts() {
override fun refreshItems() {
SimpleContactsHelper(context).getAvailableContacts { contacts ->
activity?.runOnUiThread {
gotContacts(contacts)
@ -93,7 +94,8 @@ class ContactsFragment(context: Context, attributeSet: AttributeSet) : MyViewPag
val currAdapter = fragment_list.adapter
if (currAdapter == null) {
ContactsAdapter(activity as SimpleActivity, contacts, fragment_list) {
ContactsAdapter(activity as SimpleActivity, contacts, fragment_list, this) {
mydebug("clicked $it")
val lookupKey = SimpleContactsHelper(activity!!).getContactLookupKey((it as SimpleContact).rawId.toString())
val publicUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey)
activity!!.launchViewContactIntent(publicUri)

View File

@ -9,11 +9,11 @@ import com.simplemobiletools.dialer.activities.SimpleActivity
import com.simplemobiletools.dialer.adapters.RecentCallsAdapter
import com.simplemobiletools.dialer.extensions.config
import com.simplemobiletools.dialer.helpers.RecentsHelper
import com.simplemobiletools.dialer.interfaces.RefreshRecentsListener
import com.simplemobiletools.dialer.interfaces.RefreshItemsListener
import com.simplemobiletools.dialer.models.RecentCall
import kotlinx.android.synthetic.main.fragment_recents.view.*
class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet), RefreshRecentsListener {
class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet), RefreshItemsListener {
override fun setupFragment() {
val placeholderResId = if (context.hasPermission(PERMISSION_READ_CALL_LOG)) {
R.string.no_previous_calls
@ -40,7 +40,7 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
override fun primaryColorChanged(color: Int) {}
override fun refreshRecents() {
override fun refreshItems() {
RecentsHelper(context).getRecentCalls { recents ->
activity?.runOnUiThread {
gotRecents(recents)

View File

@ -6,7 +6,7 @@ import android.provider.CallLog.Calls
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CALL_LOG
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.overloads.times
import com.simplemobiletools.commons.helpers.getQuestionMarks
import com.simplemobiletools.dialer.extensions.getAvailableSIMCardLabels
import com.simplemobiletools.dialer.models.RecentCall
@ -79,6 +79,4 @@ class RecentsHelper(private val context: Context) {
callback()
}
}
private fun getQuestionMarks(size: Int) = ("?," * size).trimEnd(',')
}

View File

@ -0,0 +1,5 @@
package com.simplemobiletools.dialer.interfaces
interface RefreshItemsListener {
fun refreshItems()
}

View File

@ -1,5 +0,0 @@
package com.simplemobiletools.dialer.interfaces
interface RefreshRecentsListener {
fun refreshRecents()
}