diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/GroupsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/GroupsAdapter.kt index d4fb8d63..e1e78718 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/GroupsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/GroupsAdapter.kt @@ -11,6 +11,7 @@ import com.simplemobiletools.commons.views.FastScroller import com.simplemobiletools.commons.views.MyRecyclerView import com.simplemobiletools.contacts.R import com.simplemobiletools.contacts.activities.SimpleActivity +import com.simplemobiletools.contacts.dialogs.RenameGroupDialog import com.simplemobiletools.contacts.extensions.config import com.simplemobiletools.contacts.helpers.ContactsHelper import com.simplemobiletools.contacts.helpers.GROUPS_TAB_MASK @@ -75,7 +76,10 @@ class GroupsAdapter(activity: SimpleActivity, var groups: ArrayList, val } private fun editGroup() { - + RenameGroupDialog(activity, groups[selectedPositions.first()]) { + finishActMode() + refreshListener?.refreshContacts(GROUPS_TAB_MASK) + } } private fun askConfirmDelete() { diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/RenameGroupDialog.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/RenameGroupDialog.kt new file mode 100644 index 00000000..95c31224 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/RenameGroupDialog.kt @@ -0,0 +1,43 @@ +package com.simplemobiletools.contacts.dialogs + +import android.support.v7.app.AlertDialog +import com.simplemobiletools.commons.activities.BaseSimpleActivity +import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.contacts.R +import com.simplemobiletools.contacts.helpers.ContactsHelper +import com.simplemobiletools.contacts.models.Group +import kotlinx.android.synthetic.main.dialog_rename_group.view.* + +class RenameGroupDialog(val activity: BaseSimpleActivity, val group: Group, val callback: () -> Unit) { + init { + + val view = activity.layoutInflater.inflate(R.layout.dialog_rename_group, null).apply { + rename_group_name.setText(group.title) + } + + AlertDialog.Builder(activity) + .setPositiveButton(R.string.ok, null) + .setNegativeButton(R.string.cancel, null) + .create().apply { + activity.setupDialogStuff(view, this, R.string.rename) { + showKeyboard(view.rename_group_name) + getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener { + val newName = view.rename_group_name.value + if (newName.isEmpty()) { + activity.toast(R.string.empty_name) + return@setOnClickListener + } + + if (!newName.isAValidFilename()) { + activity.toast(R.string.invalid_name) + return@setOnClickListener + } + + ContactsHelper(activity).renameGroup(group, newName) + callback() + dismiss() + } + } + } + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt index ee1dc9b0..3dbddfcb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt @@ -387,13 +387,13 @@ class ContactsHelper(val activity: BaseSimpleActivity) { } fun createNewGroup(title: String): Group? { - try { - val operations = ArrayList() - ContentProviderOperation.newInsert(ContactsContract.Groups.CONTENT_URI).apply { - withValue(ContactsContract.Groups.TITLE, title) - operations.add(build()) - } + val operations = ArrayList() + ContentProviderOperation.newInsert(ContactsContract.Groups.CONTENT_URI).apply { + withValue(ContactsContract.Groups.TITLE, title) + operations.add(build()) + } + try { val results = activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations) val rawId = ContentUris.parseId(results[0].uri) return Group(rawId, title) @@ -403,14 +403,32 @@ class ContactsHelper(val activity: BaseSimpleActivity) { return null } - fun deleteGroup(id: Long) { - try { - val operations = ArrayList() - val uri = ContentUris.withAppendedId(ContactsContract.Groups.CONTENT_URI, id).buildUpon() - .appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true") - .build() + fun renameGroup(group: Group, newTitle: String) { + val operations = ArrayList() + ContentProviderOperation.newUpdate(ContactsContract.Groups.CONTENT_URI).apply { + val selection = "${ContactsContract.Groups._ID} = ?" + val selectionArgs = arrayOf(group.id.toString()) + withSelection(selection, selectionArgs) + withValue(ContactsContract.Groups.TITLE, newTitle) + operations.add(build()) + } - operations.add(ContentProviderOperation.newDelete(uri).build()) + try { + activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations) + } catch (e: Exception) { + activity.showErrorToast(e) + } + } + + fun deleteGroup(id: Long) { + val operations = ArrayList() + val uri = ContentUris.withAppendedId(ContactsContract.Groups.CONTENT_URI, id).buildUpon() + .appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true") + .build() + + operations.add(ContentProviderOperation.newDelete(uri).build()) + + try { activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations) } catch (e: Exception) { activity.showErrorToast(e) diff --git a/app/src/main/res/layout/dialog_rename_group.xml b/app/src/main/res/layout/dialog_rename_group.xml new file mode 100644 index 00000000..fb63ce42 --- /dev/null +++ b/app/src/main/res/layout/dialog_rename_group.xml @@ -0,0 +1,19 @@ + + + + + +