From b3d99e99e0bc0724a723c8ee283e15987e46c445 Mon Sep 17 00:00:00 2001 From: Dirkster99 Date: Mon, 3 Sep 2018 16:32:53 +0200 Subject: [PATCH 1/3] Adding group import and export capability https://github.com/SimpleMobileTools/Simple-Contacts/issues/146 --- .../contacts/helpers/VcfExporter.kt | 12 +++++++ .../contacts/helpers/VcfImporter.kt | 32 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt index b4651fb5..399ab49b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt @@ -123,6 +123,18 @@ class VcfExporter { card.addPhoto(photo) } + // Export a list of groups where this contact is a member of + if (contact.groups.size > 0){ + + // Copy groups into a categorized list and add this category into VCard + val groupList = Categories(); + contact.groups.forEach{ + groupList.addParameter("GROUP", it.title); + } + + card.addCategories(groupList); + } + cards.add(card) contactsExported++ } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfImporter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfImporter.kt index ae9dc510..ca81848c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfImporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfImporter.kt @@ -5,7 +5,9 @@ import android.graphics.BitmapFactory import android.provider.ContactsContract.CommonDataKinds import android.widget.Toast import com.simplemobiletools.commons.extensions.showErrorToast +import com.simplemobiletools.contacts.R import com.simplemobiletools.contacts.activities.SimpleActivity +import com.simplemobiletools.contacts.extensions.dbHelper import com.simplemobiletools.contacts.extensions.getCachePhoto import com.simplemobiletools.contacts.extensions.getCachePhotoUri import com.simplemobiletools.contacts.helpers.VcfImporter.ImportResult.* @@ -102,6 +104,36 @@ class VcfImporter(val activity: SimpleActivity) { val contactId = 0 val notes = ezContact.notes.firstOrNull()?.value ?: "" val groups = ArrayList() + + if (ezContact.categories != null){ // Iterate through categories of this contact (if any) + + // Iterate through group names for this contact + // Check if group already exist and create it if not + // Associate group with this user + val groupNames = ezContact.categories.getParameters("GROUP"); + + if (groupNames != null){ // Resolve group references for this contact + + val storedGroups = ContactsHelper(activity).getStoredGroups(); + groupNames.forEach { + + val groupName = it; + val storedGroup = storedGroups.firstOrNull { it.title == groupName } + + if (storedGroup != null){ + groups.add(storedGroup); // Group is already present on this device + } + else { + // Group is not present on this device, yet so we create a new one + val newcontactGroup = activity.dbHelper.insertGroup(Group(0, groupName)); + + if (newcontactGroup != null) // and associate it with this contact + groups.add(newcontactGroup); + } + } + } + } + val company = ezContact.organization?.values?.firstOrNull() ?: "" val jobPosition = ezContact.titles?.firstOrNull()?.value ?: "" val organization = Organization(company, jobPosition) From a153427646552d7b99bc4c24d0b31694071f98ea Mon Sep 17 00:00:00 2001 From: Dirkster99 Date: Tue, 4 Sep 2018 17:33:43 +0200 Subject: [PATCH 2/3] Minor change Adjusted coding style and removed GROUP attribute as per comment from here: https://github.com/SimpleMobileTools/Simple-Contacts/issues/146 previous post, it should look like CATEGORIES:Testgroup,Testgroup1. Regarding the code, check the spacing, newlines, remove comments. --- .../contacts/helpers/VcfExporter.kt | 11 ++++------ .../contacts/helpers/VcfImporter.kt | 20 +++++++------------ 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt index 399ab49b..e948258d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt @@ -123,16 +123,13 @@ class VcfExporter { card.addPhoto(photo) } - // Export a list of groups where this contact is a member of - if (contact.groups.size > 0){ - - // Copy groups into a categorized list and add this category into VCard + if (contact.groups.size > 0) { val groupList = Categories(); - contact.groups.forEach{ - groupList.addParameter("GROUP", it.title); + contact.groups.forEach { + groupList.getValues().add(it.title); } - card.addCategories(groupList); + card.setCategories(groupList); } cards.add(card) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfImporter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfImporter.kt index ca81848c..d5ccc61b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfImporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfImporter.kt @@ -105,29 +105,23 @@ class VcfImporter(val activity: SimpleActivity) { val notes = ezContact.notes.firstOrNull()?.value ?: "" val groups = ArrayList() - if (ezContact.categories != null){ // Iterate through categories of this contact (if any) - - // Iterate through group names for this contact - // Check if group already exist and create it if not - // Associate group with this user - val groupNames = ezContact.categories.getParameters("GROUP"); - - if (groupNames != null){ // Resolve group references for this contact + if (ezContact.categories != null) { + val groupNames = ezContact.categories.getValues(); + if (groupNames != null) { val storedGroups = ContactsHelper(activity).getStoredGroups(); - groupNames.forEach { + groupNames.forEach { val groupName = it; val storedGroup = storedGroups.firstOrNull { it.title == groupName } - if (storedGroup != null){ - groups.add(storedGroup); // Group is already present on this device + if (storedGroup != null) { + groups.add(storedGroup); } else { - // Group is not present on this device, yet so we create a new one val newcontactGroup = activity.dbHelper.insertGroup(Group(0, groupName)); - if (newcontactGroup != null) // and associate it with this contact + if (newcontactGroup != null) groups.add(newcontactGroup); } } From 6c1b1e2d70457d553b7b86d7bd054596b55f4e93 Mon Sep 17 00:00:00 2001 From: Dirkster99 Date: Fri, 7 Sep 2018 17:49:20 +0200 Subject: [PATCH 3/3] Removed ; and adjsuted to property syntax --- .../contacts/helpers/VcfExporter.kt | 6 +++--- .../contacts/helpers/VcfImporter.kt | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt index e948258d..f539e0d3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt @@ -124,12 +124,12 @@ class VcfExporter { } if (contact.groups.size > 0) { - val groupList = Categories(); + val groupList = Categories() contact.groups.forEach { - groupList.getValues().add(it.title); + groupList.values.add(it.title) } - card.setCategories(groupList); + card.categories = groupList } cards.add(card) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfImporter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfImporter.kt index d5ccc61b..6d0820a0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfImporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfImporter.kt @@ -106,23 +106,23 @@ class VcfImporter(val activity: SimpleActivity) { val groups = ArrayList() if (ezContact.categories != null) { - val groupNames = ezContact.categories.getValues(); + val groupNames = ezContact.categories.values if (groupNames != null) { - val storedGroups = ContactsHelper(activity).getStoredGroups(); + val storedGroups = ContactsHelper(activity).getStoredGroups() groupNames.forEach { - val groupName = it; + val groupName = it val storedGroup = storedGroups.firstOrNull { it.title == groupName } if (storedGroup != null) { - groups.add(storedGroup); + groups.add(storedGroup) } else { - val newcontactGroup = activity.dbHelper.insertGroup(Group(0, groupName)); + val newcontactGroup = activity.dbHelper.insertGroup(Group(0, groupName)) if (newcontactGroup != null) - groups.add(newcontactGroup); + groups.add(newcontactGroup) } } }