From 9f70454a07d5ce93ec6b40f8ba12d2b4ffa499b8 Mon Sep 17 00:00:00 2001 From: JaniruTEC <52893617+JaniruTEC@users.noreply.github.com> Date: Tue, 16 Apr 2024 18:31:07 +0200 Subject: [PATCH] Moved logic for handling extra calls to dedicated class "OneOffDelegate" *This commit is related to issue #529 [1]* [1] https://github.com/cryptomator/android/issues/529 --- .../db/sqlmapping/MappingSupportSQLiteDatabase.kt | 13 +++---------- .../data/db/sqlmapping/OneOffDelegate.kt | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 10 deletions(-) create mode 100644 data/src/main/java/org/cryptomator/data/db/sqlmapping/OneOffDelegate.kt diff --git a/data/src/main/java/org/cryptomator/data/db/sqlmapping/MappingSupportSQLiteDatabase.kt b/data/src/main/java/org/cryptomator/data/db/sqlmapping/MappingSupportSQLiteDatabase.kt index 1ba009fd2..5858f7e5d 100644 --- a/data/src/main/java/org/cryptomator/data/db/sqlmapping/MappingSupportSQLiteDatabase.kt +++ b/data/src/main/java/org/cryptomator/data/db/sqlmapping/MappingSupportSQLiteDatabase.kt @@ -164,18 +164,11 @@ internal class MappingSupportSQLiteDatabase( private val delegateQuery: SupportSQLiteQuery ) : SupportSQLiteQuery by delegateQuery { - private val lock = Any() - private var called = false - private val _sql = map(delegateQuery.sql) + private val sqlDelegate = OneOffDelegate { Timber.tag("MappingSupportSQLiteQuery").e("SQL queried twice") } + override val sql: String - get() = synchronized(lock) { - if (called) { - Timber.tag("MappingSupportSQLiteQuery").e("SQL queried twice") - } - called = true - return _sql - } + get() = sqlDelegate.call { _sql } } } diff --git a/data/src/main/java/org/cryptomator/data/db/sqlmapping/OneOffDelegate.kt b/data/src/main/java/org/cryptomator/data/db/sqlmapping/OneOffDelegate.kt new file mode 100644 index 000000000..8d88ad43d --- /dev/null +++ b/data/src/main/java/org/cryptomator/data/db/sqlmapping/OneOffDelegate.kt @@ -0,0 +1,15 @@ +package org.cryptomator.data.db.sqlmapping + +internal class OneOffDelegate(private val beforeExtraCalls: () -> Unit) { + + private val lock = Any() + private var called = false + + fun call(delegateCallable: () -> R): R = synchronized(lock) { + if (called) { + beforeExtraCalls() + } + called = true + return delegateCallable() + } +} \ No newline at end of file