From 64db7ac0532b0c9742058d871e95da42660800fa Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 30 Nov 2018 15:28:57 +0100 Subject: [PATCH] list the blocked numbers at the particular activity --- .../ManageBlockedNumbersActivity.kt | 7 ++ .../adapters/ManageBlockedNumbersAdapter.kt | 81 +++++++++++++++++++ ...ber.xml => item_manage_blocked_number.xml} | 0 3 files changed, 88 insertions(+) create mode 100644 app/src/main/kotlin/com/simplemobiletools/contacts/pro/adapters/ManageBlockedNumbersAdapter.kt rename app/src/main/res/layout/{item_blocked_number.xml => item_manage_blocked_number.xml} (100%) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/ManageBlockedNumbersActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/ManageBlockedNumbersActivity.kt index c07285be..062964a4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/ManageBlockedNumbersActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/ManageBlockedNumbersActivity.kt @@ -9,6 +9,7 @@ import com.simplemobiletools.commons.extensions.underlineText import com.simplemobiletools.commons.extensions.updateTextColors import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener import com.simplemobiletools.contacts.pro.R +import com.simplemobiletools.contacts.pro.adapters.ManageBlockedNumbersAdapter import com.simplemobiletools.contacts.pro.extensions.getBlockedNumbers import kotlinx.android.synthetic.main.activity_manage_blocked_numbers.* @@ -31,6 +32,12 @@ class ManageBlockedNumbersActivity : SimpleActivity(), RefreshRecyclerViewListen private fun updateBlockedNumbers() { Thread { val blockedNumbers = getBlockedNumbers() + ManageBlockedNumbersAdapter(this, blockedNumbers, this, manage_blocked_numbers_list) { + + }.apply { + manage_blocked_numbers_list.adapter = this + } + runOnUiThread { manage_blocked_numbers_placeholder.beVisibleIf(blockedNumbers.isEmpty()) manage_blocked_numbers_placeholder_2.beVisibleIf(blockedNumbers.isEmpty()) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/adapters/ManageBlockedNumbersAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/adapters/ManageBlockedNumbersAdapter.kt new file mode 100644 index 00000000..f48e385a --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/adapters/ManageBlockedNumbersAdapter.kt @@ -0,0 +1,81 @@ +package com.simplemobiletools.contacts.pro.adapters + +import android.view.Menu +import android.view.View +import android.view.ViewGroup +import com.simplemobiletools.commons.activities.BaseSimpleActivity +import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter +import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener +import com.simplemobiletools.commons.views.MyRecyclerView +import com.simplemobiletools.contacts.pro.R +import com.simplemobiletools.contacts.pro.extensions.config +import com.simplemobiletools.contacts.pro.models.BlockedNumber +import kotlinx.android.synthetic.main.item_manage_blocked_number.view.* +import java.util.* + +class ManageBlockedNumbersAdapter(activity: BaseSimpleActivity, var blockedNumbers: ArrayList, val listener: RefreshRecyclerViewListener?, + recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, null, itemClick) { + + private val config = activity.config + + init { + setupDragListener(true) + } + + override fun getActionMenuId() = R.menu.cab_remove_only + + override fun prepareActionMode(menu: Menu) {} + + override fun actionItemPressed(id: Int) { + when (id) { + R.id.cab_remove -> removeSelection() + } + } + + override fun getSelectableItemCount() = blockedNumbers.size + + override fun getIsItemSelectable(position: Int) = true + + override fun getItemSelectionKey(position: Int) = blockedNumbers.getOrNull(position)?.id?.toInt() + + override fun getItemKeyPosition(key: Int) = blockedNumbers.indexOfFirst { it.id.toInt() == key } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = createViewHolder(R.layout.item_manage_blocked_number, parent) + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + val folder = blockedNumbers[position] + holder.bindView(folder, true, true) { itemView, adapterPosition -> + setupView(itemView, folder) + } + bindViewHolder(holder) + } + + override fun getItemCount() = blockedNumbers.size + + private fun getSelectedItems() = blockedNumbers.filter { selectedKeys.contains(it.id.toInt()) } as ArrayList + + private fun setupView(view: View, blockedNumber: BlockedNumber) { + view.apply { + manage_blocked_number_holder?.isSelected = selectedKeys.contains(blockedNumber.id.toInt()) + manage_blocked_number_title.apply { + text = blockedNumber.number + setTextColor(config.textColor) + } + } + } + + private fun removeSelection() { + val removeFolders = ArrayList(selectedKeys.size) + val positions = getSelectedItemPositions() + + getSelectedItems().forEach { + removeFolders.add(it) + } + + blockedNumbers.removeAll(removeFolders) + removeSelectedItems(positions) + if (blockedNumbers.isEmpty()) { + listener?.refreshItems() + } + } +} diff --git a/app/src/main/res/layout/item_blocked_number.xml b/app/src/main/res/layout/item_manage_blocked_number.xml similarity index 100% rename from app/src/main/res/layout/item_blocked_number.xml rename to app/src/main/res/layout/item_manage_blocked_number.xml