rewrite SpeedDial items into a recyclerview

This commit is contained in:
tibbi 2020-01-20 12:22:10 +01:00
parent 3d0b8d73ff
commit 021d87c89f
7 changed files with 120 additions and 108 deletions

View file

@ -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
}
}
}

View file

@ -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)

View file

@ -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())
}
}
}

View file

@ -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()
}

View file

@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/manage_speed_dial_scrollview"
android:layout_width="match_parent"
@ -20,86 +22,13 @@
android:text="@string/speed_dial_label"
android:textSize="@dimen/bigger_text_size" />
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/speed_dial_1"
<com.simplemobiletools.commons.views.MyRecyclerView
android:id="@+id/speed_dial_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:padding="@dimen/activity_margin"
android:text="1."
android:textSize="@dimen/bigger_text_size" />
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/speed_dial_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:padding="@dimen/activity_margin"
android:text="2."
android:textSize="@dimen/bigger_text_size" />
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/speed_dial_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:padding="@dimen/activity_margin"
android:text="3."
android:textSize="@dimen/bigger_text_size" />
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/speed_dial_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:padding="@dimen/activity_margin"
android:text="4."
android:textSize="@dimen/bigger_text_size" />
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/speed_dial_5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:padding="@dimen/activity_margin"
android:text="5."
android:textSize="@dimen/bigger_text_size" />
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/speed_dial_6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:padding="@dimen/activity_margin"
android:text="6."
android:textSize="@dimen/bigger_text_size" />
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/speed_dial_7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:padding="@dimen/activity_margin"
android:text="7."
android:textSize="@dimen/bigger_text_size" />
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/speed_dial_8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:padding="@dimen/activity_margin"
android:text="8."
android:textSize="@dimen/bigger_text_size" />
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/speed_dial_9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:padding="@dimen/activity_margin"
android:text="9."
android:textSize="@dimen/bigger_text_size" />
android:layout_height="match_parent"
android:clipToPadding="false"
android:scrollbars="none"
app:layoutManager="com.simplemobiletools.commons.views.MyLinearLayoutManager" />
</LinearLayout>
</ScrollView>

View file

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<com.simplemobiletools.commons.views.MyTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/speed_dial_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="?attr/selectableItemBackground"
android:ellipsize="end"
android:foreground="@drawable/selector"
android:lines="1"
android:maxLines="1"
android:padding="@dimen/activity_margin"
android:singleLine="true"
android:textSize="@dimen/bigger_text_size" />

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/cab_delete"
android:icon="@drawable/ic_delete_vector"
android:title="@string/delete"
app:showAsAction="ifRoom" />
</menu>