SunnyApp / flutter_contact

A flutter plugin for retrieving, creating, saving, and watching contacts on native devices
BSD 3-Clause "New" or "Revised" License
81 stars 62 forks source link

ContactDate not working in Android ("value" field v.s. "date" field) #31

Closed suztomo closed 3 years ago

suztomo commented 4 years ago

Thank you for a great library!

I use my Android phone (BlackBerry Priv; Android 6.0.1) to test my app that use flutter_contact 0.6.4. The app cannot get the birthday of people in the "BlackBerry Hub+ Contacts" (version 2.2034.1.8456, the default app pre-installed in the device). It has null for Contact.dates.

I dug the flutter_contact's source code and found ContactDate.fromMap is expecting "date" key (_kdate) in a map but the map actually holds "value" key (screenshot below).

Screen Shot 2020-10-30 at 8 24 06 PM

Can this library support such contact? I'm happy to submit a pull request to support this.

Trying to build the example project for Android

I had to add android.useAndroidX=true to example/android/gradle.properties.

> A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
   > Android resource linking failed
     /Users/suztomo/Documents/flutter_contact_sunnyapp/example/android/app/src/main/AndroidManifest.xml:17:5-36:19: AAPT: error: resource mipmap/ic_launcher (aka co.sunnyapp.flutter_contact_example:mipmap/ic_launcher) not found.

     /Users/suztomo/Documents/flutter_contact_sunnyapp/example/android/app/src/main/AndroidManifest.xml:20:9-34:20: AAPT: error: resource style/LaunchTheme (aka co.sunnyapp.flutter_contact_example:style/LaunchTheme) not found.

     /Users/suztomo/Documents/flutter_contact_sunnyapp/example/android/app/src/main/AndroidManifest.xml:27:13-29:66: AAPT: error: resource drawable/launch_background (aka co.sunnyapp.flutter_contact_example:drawable/launch_background) not found.
suztomo commented 4 years ago

I had my friends to test with their Oppo reno a (Android 9) and Garaxy note 8. Both of them reported they cannot fetch birthdays in their contacts.

I started digging into the code that passes "value" field.

https://github.com/SunnyApp/flutter_contact/commit/d38ad00977898460f7a45854a1f0c912508351e1#diff-b2230f7bf83b85de2b2b2ac103d8980d96c9c86f311b6bab36e66b7864a5095bR115

The library already has ContactDate.kt that

suztomo commented 4 years ago

Can I use ContactDate?

ContactDate.toMap outputs a map for "date" field.

fun DateComponents.toMap(): Map<String, Int> {
    val result = mutableMapOf<String, Int>()
    if (year != null) result["year"] = year
    if (month != null) result["month"] = month
    if (day != null) result["day"] = day
    return result
}

fun ContactDate.toMap(): Map<String, *> {
    return mutableMapOf(
            "label" to label,
            "date" to date.toMap())
}

sunny_dart's DateComponent can handle incoming map types in ContactDate.fromMap(m) (contact.dart).

ericmartineau commented 3 years ago

This is fixed in the latest version 0.6.4+1.

One other issue with android is that it doesn't store dates, but rather arbitrary string fields. So, it's possible that the dates that have been stored will be unparseable. In that case, I now pass back a date map (year, month, day), as well as the original string that was stored. So, if the date map is null, you can consult the String value field

suztomo commented 3 years ago

Thank you.