avoid adding the same contact birthday multiple times

This commit is contained in:
tibbi 2017-10-21 16:06:38 +02:00
parent 0b1b6281d2
commit f2a89e8b77
2 changed files with 16 additions and 5 deletions

View File

@ -307,6 +307,7 @@ class MainActivity : SimpleActivity(), NavigationListener {
eventTypeId = dbHelper.insertEventType(eventType)
}
val birthdayImportIDs = dbHelper.getBirthdays().map { it.importId }
var birthdaysAdded = 0
val uri = ContactsContract.Data.CONTENT_URI
val projection = arrayOf(ContactsContract.Contacts.DISPLAY_NAME,
@ -330,10 +331,11 @@ class MainActivity : SimpleActivity(), NavigationListener {
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++
if (!birthdayImportIDs.contains(contactId)) {
dbHelper.insert(event, false) {
birthdaysAdded++
}
}
} while (cursor.moveToNext())
}
} catch (e: Exception) {
@ -344,6 +346,7 @@ class MainActivity : SimpleActivity(), NavigationListener {
runOnUiThread {
toast(if (birthdaysAdded > 0) R.string.birthdays_added else R.string.no_birthdays)
updateViewPager()
}
}

View File

@ -394,6 +394,13 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
return null
}
fun getBirthdays(): List<Event> {
val selection = "$MAIN_TABLE_NAME.$COL_EVENT_SOURCE = ?"
val selectionArgs = arrayOf(SOURCE_CONTACT_BIRTHDAY)
val cursor = getEventsCursor(selection, selectionArgs)
return fillEvents(cursor)
}
fun deleteEvents(ids: Array<String>, deleteFromCalDAV: Boolean) {
val args = TextUtils.join(", ", ids)
val selection = "$MAIN_TABLE_NAME.$COL_ID IN ($args)"
@ -547,10 +554,11 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
val selectionArgs = arrayOf(id.toString())
val cursor = getEventsCursor(selection, selectionArgs)
val events = fillEvents(cursor)
return if (!events.isEmpty())
return if (events.isNotEmpty()) {
events[0]
else
} else {
null
}
}
fun getEvents(fromTS: Int, toTS: Int, eventId: Int = -1, callback: (events: MutableList<Event>) -> Unit) {