Author: Adam Evyčędo <git@apiote.xyz>
fix updating favourites and geonames on older versions of Android `on conflict` is not supported on older versions so we have to check for existing records manually
%!v(PANIC=String method: strings: negative Repeat count)
diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/repo/OfflineRepository.kt b/app/src/main/java/xyz/apiote/bimba/czwek/repo/OfflineRepository.kt index 165e9c932064611d7cf5be4f334a473356886eb9..f8126a10ef5aafae21a5afbbc30bbcca87a1f49e 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/repo/OfflineRepository.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/repo/OfflineRepository.kt @@ -101,20 +101,25 @@ } cursor.close() s } - db.execSQL( - "insert into favourites(sequence, feed_id, feed_name, stop_code, stop_name, lines) values (?, ?,?,?,?,?) on conflict(feed_id, stop_code) do update set stop_name = ?, lines = ?, sequence = ?", - arrayOf( - sequence, - favourite.feedID, - favourite.feedName, - favourite.stopCode, - favourite.stopName, - favourite.lines.joinToString(separator = "||"), - favourite.stopName, - favourite.lines.joinToString(separator = "||"), - favourite.sequence + + // XXX `on conflict` is not supported on older versions of Android + val cursor = db.rawQuery("select * from favourites where feed_id = ? and stop_code = ?", arrayOf(favourite.feedID, favourite.stopCode)) + if (cursor.count > 0) { + db.execSQL("update favourites set stop_name = ?, lines = ?, sequence = ? where feed_id = ? and stop_code = ?", arrayOf(favourite.stopName, favourite.lines.joinToString(separator = "||"), favourite.sequence, favourite.feedID, favourite.stopCode)) + } else { + db.execSQL( + "insert into favourites(sequence, feed_id, feed_name, stop_code, stop_name, lines) values (?, ?,?,?,?,?)", + arrayOf( + sequence, + favourite.feedID, + favourite.feedName, + favourite.stopCode, + favourite.stopName, + favourite.lines.joinToString(separator = "||"), + ) ) - ) + } + cursor.close() } override suspend fun saveFavourites(favourites: Set<Favourite>) { diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/settings/DownloadCitiesWorker.kt b/app/src/main/java/xyz/apiote/bimba/czwek/settings/DownloadCitiesWorker.kt index 6167fe4d3be5d7ae886b91b3ba9f6a8e00eaca33..feb3d2b4e863968a3bd9715c5990bba505205e6f 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/settings/DownloadCitiesWorker.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/settings/DownloadCitiesWorker.kt @@ -3,6 +3,7 @@ import android.Manifest import android.content.Context import android.content.pm.PackageManager +import android.database.sqlite.SQLiteConstraintException import android.database.sqlite.SQLiteDatabase import android.util.Log import androidx.core.app.ActivityCompat @@ -27,6 +28,7 @@ import java.util.UUID import java.util.zip.ZipEntry import java.util.zip.ZipInputStream +// FIXME doesn't work on older versions of Android class DownloadCitiesWorker(appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams) { @@ -183,14 +185,28 @@ val id = UUID.randomUUID() db.execSQL("insert into places2 values(?, ?, ?)", arrayOf(id, row[4], row[5])) names.split(",").toSet().forEach { name -> - db.execSQL( - "insert into place_names2 values(?, ?) on conflict(name) do nothing", - arrayOf(id, name) - ) - db.execSQL( - "insert into place_names2 values(?, ?) on conflict(name) do nothing", - arrayOf(id, "$name, ${row[8]}") - ) + try { + db.execSQL( + "insert into place_names2 values(?, ?)", + arrayOf(id, name) + ) + } catch (e: SQLiteConstraintException) { + // XXX `on conflict` doesn't work on older versions of Android + if (e.message?.contains("UNIQUE constraint failed: place_names2.name") != true) { + throw e + } + } + try { + db.execSQL( + "insert into place_names2 values(?, ?)", + arrayOf(id, "$name, ${row[8]}") + ) + } catch (e: SQLiteConstraintException) { + // XXX `on conflict` doesn't work on older versions of Android + if (e.message?.contains("UNIQUE constraint failed: place_names2.name") != true) { + throw e + } + } } } }