implement new group creation

This commit is contained in:
tibbi 2018-03-18 19:33:54 +01:00
parent 6750512f11
commit c3a148282f
6 changed files with 133 additions and 34 deletions

View File

@ -505,7 +505,7 @@ class EditContactActivity : ContactActivity() {
removeContactEventButton.beGone()
}
private fun removeGroup(id: Int) {
private fun removeGroup(id: Long) {
contact!!.groups = contact!!.groups.filter { it.id != id } as ArrayList<Group>
setupGroups()
}

View File

@ -0,0 +1,41 @@
package com.simplemobiletools.contacts.dialogs
import android.support.v7.app.AlertDialog
import android.view.View
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.commons.extensions.showKeyboard
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.value
import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.helpers.ContactsHelper
import com.simplemobiletools.contacts.models.Group
import kotlinx.android.synthetic.main.dialog_create_new_group.view.*
class CreateNewGroupDialog(val activity: BaseSimpleActivity, val callback: (newGroup: Group) -> Unit) {
init {
val view = activity.layoutInflater.inflate(R.layout.dialog_create_new_group, null)
AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null)
.create().apply {
activity.setupDialogStuff(view, this, R.string.create_new_group) {
showKeyboard(view.group_name)
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(View.OnClickListener {
val name = view.group_name.value
if (name.isEmpty()) {
activity.toast(R.string.empty_name)
return@OnClickListener
}
val newGroup = ContactsHelper(activity).createNewGroup(name)
if (newGroup != null) {
callback(newGroup)
}
dismiss()
})
}
}
}
}

View File

@ -6,6 +6,7 @@ import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.commons.views.MyAppCompatCheckbox
import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.activities.SimpleActivity
import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.helpers.ContactsHelper
import com.simplemobiletools.contacts.models.Group
import kotlinx.android.synthetic.main.dialog_select_groups.view.*
@ -14,38 +15,18 @@ import kotlinx.android.synthetic.main.item_textview.view.*
import java.util.*
class SelectGroupsDialog(val activity: SimpleActivity, val selectedGroups: ArrayList<Group>, val callback: (newGroups: ArrayList<Group>) -> Unit) {
private val NEW_GROUP_ID = -1
private val dialog: AlertDialog?
private val view = activity.layoutInflater.inflate(R.layout.dialog_select_groups, null) as ViewGroup
private val checkboxes = ArrayList<MyAppCompatCheckbox>()
private val groups = ContactsHelper(activity).getStoredGroups()
private val config = activity.config
private val dialog: AlertDialog?
init {
val view = activity.layoutInflater.inflate(R.layout.dialog_select_groups, null) as ViewGroup
groups.forEach {
activity.layoutInflater.inflate(R.layout.item_checkbox, null, false).apply {
checkboxes.add(item_checkbox)
item_checkbox_holder.setOnClickListener {
item_checkbox.toggle()
}
item_checkbox.apply {
isChecked = selectedGroups.contains(it)
text = it.title
tag = it.id
}
view.dialog_groups_holder.addView(this)
}
addGroupCheckbox(it)
}
val newGroup = Group(NEW_GROUP_ID, activity.getString(R.string.create_new_group))
activity.layoutInflater.inflate(R.layout.item_textview, null, false).item_textview.apply {
text = newGroup.title
tag = newGroup.id
view.dialog_groups_holder.addView(this)
setOnClickListener {
}
}
addCreateNewGroupButton()
dialog = AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok, { dialog, which -> dialogConfirmed() })
@ -55,10 +36,45 @@ class SelectGroupsDialog(val activity: SimpleActivity, val selectedGroups: Array
}
}
private fun addGroupCheckbox(group: Group) {
activity.layoutInflater.inflate(R.layout.item_checkbox, null, false).apply {
checkboxes.add(item_checkbox)
item_checkbox_holder.setOnClickListener {
item_checkbox.toggle()
}
item_checkbox.apply {
isChecked = selectedGroups.contains(group)
text = group.title
tag = group.id
setColors(config.textColor, config.primaryColor, config.backgroundColor)
}
view.dialog_groups_holder.addView(this)
}
}
private fun addCreateNewGroupButton() {
val newGroup = Group(0, activity.getString(R.string.create_new_group))
activity.layoutInflater.inflate(R.layout.item_textview, null, false).item_textview.apply {
text = newGroup.title
tag = newGroup.id
setTextColor(config.textColor)
view.dialog_groups_holder.addView(this)
setOnClickListener {
CreateNewGroupDialog(activity) {
selectedGroups.add(it)
groups.add(it)
view.dialog_groups_holder.removeViewAt(view.dialog_groups_holder.childCount - 1)
addGroupCheckbox(it)
addCreateNewGroupButton()
}
}
}
}
private fun dialogConfirmed() {
val selectedGroups = ArrayList<Group>()
checkboxes.filter { it.isChecked }.forEach {
val groupId = it.tag as Int
val groupId = it.tag as Long
groups.firstOrNull { it.id == groupId }?.apply {
selectedGroups.add(this)
}

View File

@ -13,10 +13,7 @@ import android.provider.ContactsContract.CommonDataKinds.Note
import android.provider.MediaStore
import android.util.SparseArray
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.getIntValue
import com.simplemobiletools.commons.extensions.getStringValue
import com.simplemobiletools.commons.extensions.showErrorToast
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.SORT_BY_FIRST_NAME
import com.simplemobiletools.commons.helpers.SORT_BY_MIDDLE_NAME
import com.simplemobiletools.commons.helpers.SORT_BY_SURNAME
@ -335,7 +332,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
if (cursor?.moveToFirst() == true) {
do {
val id = cursor.getIntValue(ContactsContract.Data.CONTACT_ID)
val newRowId = cursor.getIntValue(ContactsContract.Data.DATA1)
val newRowId = cursor.getLongValue(ContactsContract.Data.DATA1)
if (groups[id] == null) {
groups.put(id, ArrayList())
@ -371,7 +368,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) {
do {
val id = cursor.getIntValue(ContactsContract.Groups._ID)
val id = cursor.getLongValue(ContactsContract.Groups._ID)
val title = cursor.getStringValue(ContactsContract.Groups.TITLE)
groups.add(Group(id, title))
} while (cursor.moveToNext())
@ -385,6 +382,23 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
return groups
}
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 results = activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations)
val rawId = ContentUris.parseId(results[0].uri)
return Group(rawId, title)
} catch (e: Exception) {
activity.showErrorToast(e)
}
return null
}
fun getContactWithId(id: Int, isLocalPrivate: Boolean): Contact? {
if (id == 0) {
return null

View File

@ -1,3 +1,3 @@
package com.simplemobiletools.contacts.models
data class Group(var id: Int, var title: String)
data class Group(var id: Long, var title: String)

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:paddingTop="@dimen/activity_margin">
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/group_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"/>
<com.simplemobiletools.commons.views.MyEditText
android:id="@+id/group_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_margin"
android:inputType="textCapSentences"
android:singleLine="true"
android:textCursorDrawable="@null"
android:textSize="@dimen/normal_text_size"/>
</LinearLayout>