allow renaming local private groups

This commit is contained in:
tibbi 2018-03-21 18:07:05 +01:00
parent 30a7c1770d
commit bd8dffb549
6 changed files with 20 additions and 12 deletions

View file

@ -15,7 +15,6 @@ import com.simplemobiletools.contacts.dialogs.RenameGroupDialog
import com.simplemobiletools.contacts.extensions.config import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.extensions.dbHelper import com.simplemobiletools.contacts.extensions.dbHelper
import com.simplemobiletools.contacts.helpers.ContactsHelper import com.simplemobiletools.contacts.helpers.ContactsHelper
import com.simplemobiletools.contacts.helpers.FIRST_GROUP_ID
import com.simplemobiletools.contacts.helpers.GROUPS_TAB_MASK import com.simplemobiletools.contacts.helpers.GROUPS_TAB_MASK
import com.simplemobiletools.contacts.interfaces.RefreshContactsListener import com.simplemobiletools.contacts.interfaces.RefreshContactsListener
import com.simplemobiletools.contacts.models.Group import com.simplemobiletools.contacts.models.Group
@ -99,7 +98,7 @@ class GroupsAdapter(activity: SimpleActivity, var groups: ArrayList<Group>, val
selectedPositions.sortedDescending().forEach { selectedPositions.sortedDescending().forEach {
val group = groups[it] val group = groups[it]
groupsToRemove.add(group) groupsToRemove.add(group)
if (group.id >= FIRST_GROUP_ID) { if (group.isPrivateSecretGroup()) {
activity.dbHelper.deleteGroup(group.id) activity.dbHelper.deleteGroup(group.id)
} else { } else {
ContactsHelper(activity).deleteGroup(group.id) ContactsHelper(activity).deleteGroup(group.id)

View file

@ -4,6 +4,7 @@ import android.support.v7.app.AlertDialog
import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.contacts.R import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.extensions.dbHelper
import com.simplemobiletools.contacts.helpers.ContactsHelper import com.simplemobiletools.contacts.helpers.ContactsHelper
import com.simplemobiletools.contacts.models.Group import com.simplemobiletools.contacts.models.Group
import kotlinx.android.synthetic.main.dialog_rename_group.view.* import kotlinx.android.synthetic.main.dialog_rename_group.view.*
@ -12,7 +13,7 @@ class RenameGroupDialog(val activity: BaseSimpleActivity, val group: Group, val
init { init {
val view = activity.layoutInflater.inflate(R.layout.dialog_rename_group, null).apply { val view = activity.layoutInflater.inflate(R.layout.dialog_rename_group, null).apply {
rename_group_name.setText(group.title) rename_group_title.setText(group.title)
} }
AlertDialog.Builder(activity) AlertDialog.Builder(activity)
@ -20,20 +21,25 @@ class RenameGroupDialog(val activity: BaseSimpleActivity, val group: Group, val
.setNegativeButton(R.string.cancel, null) .setNegativeButton(R.string.cancel, null)
.create().apply { .create().apply {
activity.setupDialogStuff(view, this, R.string.rename) { activity.setupDialogStuff(view, this, R.string.rename) {
showKeyboard(view.rename_group_name) showKeyboard(view.rename_group_title)
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener { getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
val newName = view.rename_group_name.value val newTitle = view.rename_group_title.value
if (newName.isEmpty()) { if (newTitle.isEmpty()) {
activity.toast(R.string.empty_name) activity.toast(R.string.empty_name)
return@setOnClickListener return@setOnClickListener
} }
if (!newName.isAValidFilename()) { if (!newTitle.isAValidFilename()) {
activity.toast(R.string.invalid_name) activity.toast(R.string.invalid_name)
return@setOnClickListener return@setOnClickListener
} }
ContactsHelper(activity).renameGroup(group, newName) group.title = newTitle
if (group.isPrivateSecretGroup()) {
activity.dbHelper.renameGroup(group)
} else {
ContactsHelper(activity).renameGroup(group)
}
callback() callback()
dismiss() dismiss()
} }

View file

@ -418,13 +418,13 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
return null return null
} }
fun renameGroup(group: Group, newTitle: String) { fun renameGroup(group: Group) {
val operations = ArrayList<ContentProviderOperation>() val operations = ArrayList<ContentProviderOperation>()
ContentProviderOperation.newUpdate(ContactsContract.Groups.CONTENT_URI).apply { ContentProviderOperation.newUpdate(ContactsContract.Groups.CONTENT_URI).apply {
val selection = "${ContactsContract.Groups._ID} = ?" val selection = "${ContactsContract.Groups._ID} = ?"
val selectionArgs = arrayOf(group.id.toString()) val selectionArgs = arrayOf(group.id.toString())
withSelection(selection, selectionArgs) withSelection(selection, selectionArgs)
withValue(ContactsContract.Groups.TITLE, newTitle) withValue(ContactsContract.Groups.TITLE, group.title)
operations.add(build()) operations.add(build())
} }

View file

@ -155,7 +155,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
} }
} }
fun updateGroup(group: Group): Boolean { fun renameGroup(group: Group): Boolean {
val contactValues = fillGroupValues(group) val contactValues = fillGroupValues(group)
val selection = "$COL_ID = ?" val selection = "$COL_ID = ?"
val selectionArgs = arrayOf(group.id.toString()) val selectionArgs = arrayOf(group.id.toString())

View file

@ -1,5 +1,6 @@
package com.simplemobiletools.contacts.models package com.simplemobiletools.contacts.models
import com.simplemobiletools.contacts.helpers.FIRST_GROUP_ID
import java.io.Serializable import java.io.Serializable
data class Group(var id: Long, var title: String, var contactsCount: Int = 0) : Serializable { data class Group(var id: Long, var title: String, var contactsCount: Int = 0) : Serializable {
@ -10,4 +11,6 @@ data class Group(var id: Long, var title: String, var contactsCount: Int = 0) :
fun addContact() = contactsCount++ fun addContact() = contactsCount++
fun getBubbleText() = title fun getBubbleText() = title
fun isPrivateSecretGroup() = id >= FIRST_GROUP_ID
} }

View file

@ -8,7 +8,7 @@
android:padding="@dimen/activity_margin"> android:padding="@dimen/activity_margin">
<com.simplemobiletools.commons.views.MyEditText <com.simplemobiletools.commons.views.MyEditText
android:id="@+id/rename_group_name" android:id="@+id/rename_group_title"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:inputType="textCapWords" android:inputType="textCapWords"