Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - Add support for relationships #138

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ buildscript {
ext.kotlin_coroutines_version = '1.6.4'
repositories {
google()
jcenter()
mavenCentral()
}

Expand All @@ -19,7 +18,6 @@ buildscript {
rootProject.allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}
Expand Down Expand Up @@ -51,4 +49,4 @@ dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_coroutines_version"
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.0")
}
}
4 changes: 4 additions & 0 deletions android/src/main/kotlin/co/quis/flutter_contacts/Contact.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import co.quis.flutter_contacts.properties.Name
import co.quis.flutter_contacts.properties.Note
import co.quis.flutter_contacts.properties.Organization
import co.quis.flutter_contacts.properties.Phone
import co.quis.flutter_contacts.properties.Relation
import co.quis.flutter_contacts.properties.SocialMedia
import co.quis.flutter_contacts.properties.Website

Expand All @@ -26,6 +27,7 @@ data class Contact(
var websites: List<Website> = listOf(),
var socialMedias: List<SocialMedia> = listOf(),
var events: List<Event> = listOf(),
var relations: List<Relation> = listOf(),
var notes: List<Note> = listOf(),
var accounts: List<Account> = listOf(),
var groups: List<Group> = listOf()
Expand All @@ -46,6 +48,7 @@ data class Contact(
(m["websites"] as List<Map<String, Any>>).map { Website.fromMap(it) },
(m["socialMedias"] as List<Map<String, Any>>).map { SocialMedia.fromMap(it) },
(m["events"] as List<Map<String, Any?>>).map { Event.fromMap(it) },
(m["relations"] as List<Map<String, Any>>).map { Relation.fromMap(it) },
(m["notes"] as List<Map<String, Any>>).map { Note.fromMap(it) },
(m["accounts"] as List<Map<String, Any>>).map { Account.fromMap(it) },
(m["groups"] as List<Map<String, Any>>).map { Group.fromMap(it) }
Expand All @@ -67,6 +70,7 @@ data class Contact(
"websites" to websites.map { it.toMap() },
"socialMedias" to socialMedias.map { it.toMap() },
"events" to events.map { it.toMap() },
"relations" to relations.map { it.toMap() },
"notes" to notes.map { it.toMap() },
"accounts" to accounts.map { it.toMap() },
"groups" to groups.map { it.toMap() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import android.provider.ContactsContract.CommonDataKinds.Note
import android.provider.ContactsContract.CommonDataKinds.Organization
import android.provider.ContactsContract.CommonDataKinds.Phone
import android.provider.ContactsContract.CommonDataKinds.Photo
import android.provider.ContactsContract.CommonDataKinds.Relation
import android.provider.ContactsContract.CommonDataKinds.StructuredName
import android.provider.ContactsContract.CommonDataKinds.StructuredPostal
import android.provider.ContactsContract.CommonDataKinds.Website
Expand All @@ -39,6 +40,7 @@ import co.quis.flutter_contacts.properties.Name as PName
import co.quis.flutter_contacts.properties.Note as PNote
import co.quis.flutter_contacts.properties.Organization as POrganization
import co.quis.flutter_contacts.properties.Phone as PPhone
import co.quis.flutter_contacts.properties.Relation as PRelation
import co.quis.flutter_contacts.properties.SocialMedia as PSocialMedia
import co.quis.flutter_contacts.properties.Website as PWebsite

Expand Down Expand Up @@ -127,6 +129,8 @@ class FlutterContacts {
Event.START_DATE,
Event.TYPE,
Event.LABEL,
Relation.TYPE,
Relation.LABEL,
Note.NOTE
)
)
Expand Down Expand Up @@ -382,6 +386,17 @@ class FlutterContacts {
contact.events += event
}
}
Relation.CONTENT_ITEM_TYPE -> {
val label: String = getRelationLabel(cursor)
val customLabel: String =
if (label == "custom") getRelationCustomLabel(cursor) else ""
val relation = PRelation(
getString(Relation.NAME),
label,
customLabel
)
contact.relations += relation
}
Note.CONTENT_ITEM_TYPE -> {
val note: String = getString(Note.NOTE)
// It seems that every contact has an empty note by default;
Expand Down Expand Up @@ -504,7 +519,7 @@ class FlutterContacts {
ops.add(
ContentProviderOperation.newDelete(Data.CONTENT_URI)
.withSelection(
"${RawContacts.CONTACT_ID}=? and ${Data.MIMETYPE} in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
"${RawContacts.CONTACT_ID}=? and ${Data.MIMETYPE} in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
arrayOf(
contactId,
StructuredName.CONTENT_ITEM_TYPE,
Expand All @@ -516,6 +531,7 @@ class FlutterContacts {
Website.CONTENT_ITEM_TYPE,
Im.CONTENT_ITEM_TYPE,
Event.CONTENT_ITEM_TYPE,
Relation.CONTENT_ITEM_TYPE,
Note.CONTENT_ITEM_TYPE
)
)
Expand Down Expand Up @@ -996,6 +1012,54 @@ class FlutterContacts {
}
}

private fun getRelationLabel(cursor: Cursor): String {
val type = cursor.getInt(cursor.getColumnIndex(Relation.TYPE))
return when (type) {
Relation.TYPE_CUSTOM -> "custom"
Relation.TYPE_ASSISTANT -> "assistant"
Relation.TYPE_BROTHER -> "brother"
Relation.TYPE_CHILD -> "child"
Relation.TYPE_DOMESTIC_PARTNER -> "domestic-partner"
Relation.TYPE_FATHER -> "father"
Relation.TYPE_FRIEND -> "friend"
Relation.TYPE_MANAGER -> "manager"
Relation.TYPE_MOTHER -> "mother"
Relation.TYPE_PARENT -> "parent"
Relation.TYPE_PARTNER -> "partner"
Relation.TYPE_REFERRED_BY -> "referred-by"
Relation.TYPE_RELATIVE -> "relative"
Relation.TYPE_SISTER -> "sister"
Relation.TYPE_SPOUSE -> "spouse"
else -> "relation"
}
}

private fun getRelationCustomLabel(cursor: Cursor): String {
return cursor.getString(cursor.getColumnIndex(Relation.LABEL)) ?: ""
}

private data class RelationLabelPair(val label: Int, val customLabel: String)
private fun getRelationLabelInv(label: String, customLabel: String): RelationLabelPair {
return when (label) {
"custom" -> RelationLabelPair(Relation.TYPE_CUSTOM, customLabel)
"assistant" -> RelationLabelPair(Relation.TYPE_ASSISTANT, "")
"brother" -> RelationLabelPair(Relation.TYPE_BROTHER, "")
"child" -> RelationLabelPair(Relation.TYPE_CHILD, "")
"domestic-partner" -> RelationLabelPair(Relation.TYPE_DOMESTIC_PARTNER, "")
"father" -> RelationLabelPair(Relation.TYPE_FATHER, "")
"friend" -> RelationLabelPair(Relation.TYPE_FRIEND, "")
"manager" -> RelationLabelPair(Relation.TYPE_MANAGER, "")
"mother" -> RelationLabelPair(Relation.TYPE_MOTHER, "")
"parent" -> RelationLabelPair(Relation.TYPE_PARENT, "")
"partner" -> RelationLabelPair(Relation.TYPE_PARTNER, "")
"referred-by" -> RelationLabelPair(Relation.TYPE_REFERRED_BY, "")
"relative" -> RelationLabelPair(Relation.TYPE_RELATIVE, "")
"sister" -> RelationLabelPair(Relation.TYPE_SISTER, "")
"spouse" -> RelationLabelPair(Relation.TYPE_SPOUSE, "")
else -> RelationLabelPair(Email.TYPE_CUSTOM, label)
}
}

private fun buildOpsForContact(
contact: Contact,
ops: MutableList<ContentProviderOperation>,
Expand Down Expand Up @@ -1131,6 +1195,17 @@ class FlutterContacts {
.build()
)
}
for ((i, relation) in contact.relations.withIndex()) {
val labelPair: RelationLabelPair = getRelationLabelInv(relation.label, relation.customLabel)
ops.add(
newInsert()
.withValue(Data.MIMETYPE, Relation.CONTENT_ITEM_TYPE)
.withValue(Relation.NAME, emptyToNull(relation.name))
.withValue(Relation.TYPE, labelPair.label)
.withValue(Relation.LABEL, emptyToNull(labelPair.customLabel))
.build()
)
}
for (note in contact.notes) {
if (!note.note.isEmpty()) {
ops.add(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package co.quis.flutter_contacts.properties

data class Relation(
var name: String,
// one of: assistant, brother, child, daughter, domestic-partner, father, friend, manager,
// mother, other, parent, partner, referred-by, relative, sister, son, spouse, custom
var label: String = "relative",
var customLabel: String = ""
) {
companion object {
fun fromMap(m: Map<String, Any>): Relation = Relation(
m["name"] as String,
m["label"] as String,
m["customLabel"] as String
)
}

fun toMap(): Map<String, Any> = mapOf(
"name" to name,
"label" to label,
"customLabel" to customLabel
)
}
2 changes: 1 addition & 1 deletion example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ android {
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "co.quis.flutter_contacts_example"
minSdkVersion 16
minSdkVersion flutter.minSdkVersion
targetSdkVersion 31
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
Expand Down
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
6 changes: 3 additions & 3 deletions example_full/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 31
compileSdkVersion 33

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
Expand All @@ -39,8 +39,8 @@ android {
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "co.quis.flutter_contacts_example"
minSdkVersion 16
targetSdkVersion 31
minSdkVersion flutter.minSdkVersion
targetSdkVersion 33
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
6 changes: 3 additions & 3 deletions example_full/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ buildscript {
ext.kotlin_version = '1.7.21'
repositories {
google()
jcenter()
mavenCentral()
}

dependencies {
Expand All @@ -14,7 +14,7 @@ buildscript {
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}

Expand All @@ -26,6 +26,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
12 changes: 6 additions & 6 deletions example_full/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ PODS:
- Flutter (1.0.0)
- flutter_contacts (0.0.1):
- Flutter
- image_picker (0.0.1):
- image_picker_ios (0.0.1):
- Flutter

DEPENDENCIES:
- Flutter (from `Flutter`)
- flutter_contacts (from `.symlinks/plugins/flutter_contacts/ios`)
- image_picker (from `.symlinks/plugins/image_picker/ios`)
- image_picker_ios (from `.symlinks/plugins/image_picker_ios/ios`)

EXTERNAL SOURCES:
Flutter:
:path: Flutter
flutter_contacts:
:path: ".symlinks/plugins/flutter_contacts/ios"
image_picker:
:path: ".symlinks/plugins/image_picker/ios"
image_picker_ios:
:path: ".symlinks/plugins/image_picker_ios/ios"

SPEC CHECKSUMS:
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
flutter_contacts: edb1c5ce76aa433e20e6cb14c615f4c0b66e0983
image_picker: 9c3312491f862b28d21ecd8fdf0ee14e601b3f09
image_picker_ios: 4a8aadfbb6dc30ad5141a2ce3832af9214a705b5

PODFILE CHECKSUM: ef19549a9bc3046e7bb7d2fab4d021637c0c58a3

COCOAPODS: 1.11.3
COCOAPODS: 1.13.0
7 changes: 6 additions & 1 deletion example_full/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import 'package:flutter/material.dart';
import 'package:flutter_contacts/config.dart';
import 'package:flutter_contacts/flutter_contacts.dart';

import 'pages/contact_list_page.dart';
import 'pages/contact_page.dart';
import 'pages/edit_contact_page.dart';
import 'pages/groups_page.dart';

void main() => runApp(FlutterContactsExample());
void main() {
FlutterContacts.config.vCardVersion = VCardVersion.v4;
runApp(FlutterContactsExample());
}

class FlutterContactsExample extends StatelessWidget {
@override
Expand Down
8 changes: 4 additions & 4 deletions example_full/lib/pages/contact_list_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ContactListPage extends StatefulWidget {

class _ContactListPageState extends State<ContactListPage>
with AfterLayoutMixin<ContactListPage> {
List<Contact> _contacts;
List<Contact> _contacts = [];
bool _permissionDenied = false;

@override
Expand All @@ -33,7 +33,7 @@ class _ContactListPageState extends State<ContactListPage>
Future _fetchContacts() async {
if (!await FlutterContacts.requestPermission()) {
setState(() {
_contacts = null;
_contacts.clear();
_permissionDenied = true;
});
return;
Expand All @@ -50,7 +50,7 @@ class _ContactListPageState extends State<ContactListPage>

Future _refetchContacts() async {
// First load all contacts without photo
await _loadContacts(false);
// await _loadContacts(false);

// Next with photo
await _loadContacts(true);
Expand Down Expand Up @@ -91,7 +91,7 @@ class _ContactListPageState extends State<ContactListPage>
if (_permissionDenied) {
return Center(child: Text('Permission denied'));
}
if (_contacts == null) {
if (_contacts.isEmpty) {
return Center(child: CircularProgressIndicator());
}
return ListView.builder(
Expand Down
Loading