From 0b1b6281d2d467a317a12e06c84392c4e339d910 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 21 Oct 2017 15:55:48 +0200 Subject: [PATCH] insert the birthdays in db --- .../calendar/activities/MainActivity.kt | 21 ++++++++++++++++--- .../calendar/helpers/Constants.kt | 1 + app/src/main/res/layout/activity_event.xml | 11 +++++++++- app/src/main/res/values/colors.xml | 1 + 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt index 0a0585b00..de25b02d4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/MainActivity.kt @@ -300,10 +300,18 @@ class MainActivity : SimpleActivity(), NavigationListener { } private fun addBirthdays() { + val birthdays = getString(R.string.birthdays) + var eventTypeId = dbHelper.getEventTypeIdWithTitle(birthdays) + if (eventTypeId == -1) { + val eventType = EventType(0, birthdays, resources.getColor(R.color.default_birthdays_color)) + eventTypeId = dbHelper.insertEventType(eventType) + } + var birthdaysAdded = 0 val uri = ContactsContract.Data.CONTENT_URI val projection = arrayOf(ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.CommonDataKinds.Event.CONTACT_ID, + ContactsContract.CommonDataKinds.Event.CONTACT_LAST_UPDATED_TIMESTAMP, ContactsContract.CommonDataKinds.Event.START_DATE) val selection = "${ContactsContract.Data.MIMETYPE} = ? AND ${ContactsContract.CommonDataKinds.Event.TYPE} = ?" @@ -313,12 +321,19 @@ class MainActivity : SimpleActivity(), NavigationListener { cursor = contentResolver.query(uri, projection, selection, selectionArgs, null) if (cursor?.moveToFirst() == true) { do { - val contactId = cursor.getIntValue(ContactsContract.CommonDataKinds.Event.CONTACT_ID) + val contactId = cursor.getIntValue(ContactsContract.CommonDataKinds.Event.CONTACT_ID).toString() val name = cursor.getStringValue(ContactsContract.Contacts.DISPLAY_NAME) val birthDay = cursor.getStringValue(ContactsContract.CommonDataKinds.Event.START_DATE) val formatter = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()) - val timestamp = formatter.parse(birthDay).time / 1000 - birthdaysAdded++ + val timestamp = (formatter.parse(birthDay).time / 1000).toInt() + val lastUpdated = cursor.getLongValue(ContactsContract.CommonDataKinds.Event.CONTACT_LAST_UPDATED_TIMESTAMP) + val event = Event(0, timestamp, timestamp, name, importId = contactId, flags = FLAG_ALL_DAY, repeatInterval = YEAR, + eventType = eventTypeId, source = SOURCE_CONTACT_BIRTHDAY, lastUpdated = lastUpdated) + + dbHelper.insert(event, false) { + birthdaysAdded++ + } + } while (cursor.moveToNext()) } } catch (e: Exception) { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt index 78307ec96..bf0a80a2e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Constants.kt @@ -124,6 +124,7 @@ val FONT_SIZE_LARGE = 2 val SOURCE_SIMPLE_CALENDAR = "simple-calendar" val SOURCE_IMPORTED_ICS = "imported-ics" +val SOURCE_CONTACT_BIRTHDAY = "contact-birthday" // deprecated val SOURCE_GOOGLE_CALENDAR = 1 diff --git a/app/src/main/res/layout/activity_event.xml b/app/src/main/res/layout/activity_event.xml index c9676c145..c53a70f69 100644 --- a/app/src/main/res/layout/activity_event.xml +++ b/app/src/main/res/layout/activity_event.xml @@ -421,7 +421,6 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/event_caldav_calendar_divider" - android:layout_marginBottom="@dimen/medium_margin" android:layout_marginTop="@dimen/medium_margin" android:layout_toEndOf="@+id/event_type_image" android:layout_toRightOf="@+id/event_type_image" @@ -451,5 +450,15 @@ android:clickable="false"/> + + diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index ec5086582..249d793b8 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -3,4 +3,5 @@ #ff444444 #66808080 #FFF62F2F + #FF303DF6