mirror of
https://github.com/SimpleMobileTools/Simple-Contacts.git
synced 2025-06-05 21:59:27 +02:00
rewrite SpeedDial items into a recyclerview
This commit is contained in:
@ -3,8 +3,8 @@ package com.simplemobiletools.contacts.pro.activities
|
||||
import android.os.Bundle
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import com.simplemobiletools.commons.views.MyTextView
|
||||
import com.simplemobiletools.contacts.pro.R
|
||||
import com.simplemobiletools.contacts.pro.adapters.SpeedDialAdapter
|
||||
import com.simplemobiletools.contacts.pro.dialogs.SelectContactsDialog
|
||||
import com.simplemobiletools.contacts.pro.extensions.config
|
||||
import com.simplemobiletools.contacts.pro.helpers.ContactsHelper
|
||||
@ -20,32 +20,19 @@ class ManageSpeedDialActivity : SimpleActivity() {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_manage_speed_dial)
|
||||
|
||||
val views = HashMap<Int, MyTextView>().apply {
|
||||
put(1, speed_dial_1)
|
||||
put(2, speed_dial_2)
|
||||
put(3, speed_dial_3)
|
||||
put(4, speed_dial_4)
|
||||
put(5, speed_dial_5)
|
||||
put(6, speed_dial_6)
|
||||
put(7, speed_dial_7)
|
||||
put(8, speed_dial_8)
|
||||
put(9, speed_dial_9)
|
||||
}
|
||||
|
||||
val speedDialType = object : TypeToken<List<SpeedDial>>() {}.type
|
||||
speedDialValues = Gson().fromJson<ArrayList<SpeedDial>>(config.speedDial, speedDialType) ?: ArrayList(1)
|
||||
|
||||
speedDialValues.forEach {
|
||||
val view = views.get(it.id)
|
||||
view!!.text = "${it.id}. ${it.displayName}"
|
||||
for (i in 1..9) {
|
||||
val speedDial = SpeedDial(i, "", "")
|
||||
if (speedDialValues.firstOrNull { it.id == i } == null) {
|
||||
speedDialValues.add(speedDial)
|
||||
}
|
||||
}
|
||||
|
||||
updateAdapter()
|
||||
ContactsHelper(this).getContacts { contacts ->
|
||||
allContacts = contacts
|
||||
|
||||
for ((id, textView) in views) {
|
||||
setupView(id, textView)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -54,15 +41,23 @@ class ManageSpeedDialActivity : SimpleActivity() {
|
||||
config.speedDial = Gson().toJson(speedDialValues)
|
||||
}
|
||||
|
||||
private fun setupView(id: Int, textView: MyTextView) {
|
||||
textView.setOnClickListener {
|
||||
private fun updateAdapter() {
|
||||
SpeedDialAdapter(this, speedDialValues, speed_dial_list) {
|
||||
val clickedContact = it as SpeedDial
|
||||
if (allContacts.isEmpty()) {
|
||||
return@SpeedDialAdapter
|
||||
}
|
||||
|
||||
SelectContactsDialog(this, allContacts, false, true) { addedContacts, removedContacts ->
|
||||
val selectedContact = addedContacts.first()
|
||||
val speedDial = SpeedDial(id, selectedContact.phoneNumbers.first().toString(), selectedContact.getNameToDisplay())
|
||||
textView.text = "$id. ${speedDial.displayName}"
|
||||
speedDialValues = speedDialValues.filter { it.id != id }.toMutableList() as ArrayList<SpeedDial>
|
||||
speedDialValues.add(speedDial)
|
||||
speedDialValues.first { it.id == clickedContact.id }.apply {
|
||||
displayName = selectedContact.getNameToDisplay()
|
||||
number = selectedContact.phoneNumbers.first().toString()
|
||||
}
|
||||
updateAdapter()
|
||||
}
|
||||
}.apply {
|
||||
speed_dial_list.adapter = this
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -33,7 +33,6 @@ class GroupsAdapter(activity: SimpleActivity, var groups: ArrayList<Group>, val
|
||||
|
||||
var adjustedPrimaryColor = activity.getAdjustedPrimaryColor()
|
||||
var showContactThumbnails = activity.config.showContactThumbnails
|
||||
var showPhoneNumbers = activity.config.showPhoneNumbers
|
||||
|
||||
init {
|
||||
setupDragListener(true)
|
||||
|
@ -0,0 +1,63 @@
|
||||
package com.simplemobiletools.contacts.pro.adapters
|
||||
|
||||
import android.view.Menu
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
|
||||
import com.simplemobiletools.commons.views.MyRecyclerView
|
||||
import com.simplemobiletools.contacts.pro.R
|
||||
import com.simplemobiletools.contacts.pro.activities.SimpleActivity
|
||||
import com.simplemobiletools.contacts.pro.models.SpeedDial
|
||||
import kotlinx.android.synthetic.main.item_speed_dial.view.*
|
||||
import java.util.*
|
||||
|
||||
class SpeedDialAdapter(activity: SimpleActivity, var speedDialValues: ArrayList<SpeedDial>, recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) :
|
||||
MyRecyclerViewAdapter(activity, recyclerView, null, itemClick) {
|
||||
|
||||
init {
|
||||
setupDragListener(true)
|
||||
}
|
||||
|
||||
override fun getActionMenuId() = R.menu.cab_speed_dial
|
||||
|
||||
override fun prepareActionMode(menu: Menu) {}
|
||||
|
||||
override fun actionItemPressed(id: Int) {
|
||||
if (selectedKeys.isEmpty()) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
override fun getSelectableItemCount() = speedDialValues.size
|
||||
|
||||
override fun getIsItemSelectable(position: Int) = speedDialValues[position].isValid()
|
||||
|
||||
override fun getItemSelectionKey(position: Int) = speedDialValues.getOrNull(position)?.hashCode()
|
||||
|
||||
override fun getItemKeyPosition(key: Int) = speedDialValues.indexOfFirst { it.hashCode() == key }
|
||||
|
||||
override fun onActionModeCreated() {}
|
||||
|
||||
override fun onActionModeDestroyed() {}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = createViewHolder(R.layout.item_speed_dial, parent)
|
||||
|
||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||
val speedDial = speedDialValues[position]
|
||||
holder.bindView(speedDial, true, true) { itemView, layoutPosition ->
|
||||
setupView(itemView, speedDial)
|
||||
}
|
||||
bindViewHolder(holder)
|
||||
}
|
||||
|
||||
override fun getItemCount() = speedDialValues.size
|
||||
|
||||
private fun setupView(view: View, speedDial: SpeedDial) {
|
||||
view.apply {
|
||||
var text = "${speedDial.id}. "
|
||||
text += if (speedDial.isValid()) speedDial.displayName else ""
|
||||
speed_dial_label.text = text
|
||||
speed_dial_label.isSelected = selectedKeys.contains(speedDial.hashCode())
|
||||
}
|
||||
}
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
package com.simplemobiletools.contacts.pro.models
|
||||
|
||||
data class SpeedDial(val id: Int, var number: String, var displayName: String)
|
||||
data class SpeedDial(val id: Int, var number: String, var displayName: String) {
|
||||
fun isValid() = number.trim().isNotEmpty()
|
||||
}
|
||||
|
Reference in New Issue
Block a user