allow renaming groups after long pressing them

This commit is contained in:
tibbi 2018-03-20 20:05:48 +01:00
parent 244955e333
commit c93cc3bd85
4 changed files with 98 additions and 14 deletions

View File

@ -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<Group>, val
}
private fun editGroup() {
RenameGroupDialog(activity, groups[selectedPositions.first()]) {
finishActMode()
refreshListener?.refreshContacts(GROUPS_TAB_MASK)
}
}
private fun askConfirmDelete() {

View File

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

View File

@ -387,13 +387,13 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
}
fun createNewGroup(title: String): Group? {
try {
val operations = ArrayList<ContentProviderOperation>()
ContentProviderOperation.newInsert(ContactsContract.Groups.CONTENT_URI).apply {
withValue(ContactsContract.Groups.TITLE, title)
operations.add(build())
}
val operations = ArrayList<ContentProviderOperation>()
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<ContentProviderOperation>()
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>()
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<ContentProviderOperation>()
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)

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rename_group_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/activity_margin">
<com.simplemobiletools.commons.views.MyEditText
android:id="@+id/rename_group_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"
android:singleLine="true"
android:textCursorDrawable="@null"
android:textSize="@dimen/normal_text_size"/>
</LinearLayout>