From f6ebd62c8d934079c615f9fa76003cb0575d6897 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Mon, 27 Nov 2017 22:26:30 +0100 Subject: [PATCH] Code style improvements (thanks to codebeat.co) --- .../abit/repository/AndroidLabelRepository.kt | 10 ++-- .../repository/AndroidMessageRepository.kt | 10 +--- .../abit/repository/AndroidNodeRegistry.kt | 47 ++++++++----------- 3 files changed, 24 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/ch/dissem/apps/abit/repository/AndroidLabelRepository.kt b/app/src/main/java/ch/dissem/apps/abit/repository/AndroidLabelRepository.kt index 27f490a..f00880c 100644 --- a/app/src/main/java/ch/dissem/apps/abit/repository/AndroidLabelRepository.kt +++ b/app/src/main/java/ch/dissem/apps/abit/repository/AndroidLabelRepository.kt @@ -24,6 +24,7 @@ import ch.dissem.apps.abit.util.Labels import ch.dissem.bitmessage.entity.valueobject.Label import ch.dissem.bitmessage.ports.AbstractLabelRepository import ch.dissem.bitmessage.ports.MessageRepository +import org.jetbrains.anko.db.transaction import java.util.* /** @@ -40,7 +41,7 @@ class AndroidLabelRepository(private val sql: SqlHelper, private val context: Co sql.readableDatabase.query( TABLE_NAME, projection, - where, null, null, null, + where, null, null, null, COLUMN_ORDER ).use { c -> while (c.moveToNext()) { @@ -60,9 +61,7 @@ class AndroidLabelRepository(private val sql: SqlHelper, private val context: Co values.put(COLUMN_ORDER, label.ord) db.update(TABLE_NAME, values, "id=?", arrayOf(label.id.toString())) } else { - try { - db.beginTransaction() - + db.transaction { val exists = DatabaseUtils.queryNumEntries(db, TABLE_NAME, "label=?", arrayOf(label.toString())) > 0 if (exists) { @@ -79,9 +78,6 @@ class AndroidLabelRepository(private val sql: SqlHelper, private val context: Co values.put(COLUMN_ORDER, label.ord) db.insertOrThrow(TABLE_NAME, null, values) } - db.setTransactionSuccessful() - } finally { - db.endTransaction() } } } diff --git a/app/src/main/java/ch/dissem/apps/abit/repository/AndroidMessageRepository.kt b/app/src/main/java/ch/dissem/apps/abit/repository/AndroidMessageRepository.kt index b41761f..66167f7 100644 --- a/app/src/main/java/ch/dissem/apps/abit/repository/AndroidMessageRepository.kt +++ b/app/src/main/java/ch/dissem/apps/abit/repository/AndroidMessageRepository.kt @@ -33,6 +33,7 @@ import ch.dissem.bitmessage.ports.AlreadyStoredException import ch.dissem.bitmessage.ports.MessageRepository import ch.dissem.bitmessage.utils.Encode import ch.dissem.bitmessage.utils.Strings.hex +import org.jetbrains.anko.db.transaction import java.io.ByteArrayInputStream import java.util.* @@ -182,9 +183,7 @@ class AndroidMessageRepository(private val sql: SqlHelper) : AbstractMessageRepo saveContactIfNecessary(message.from) saveContactIfNecessary(message.to) val db = sql.writableDatabase - try { - db.beginTransaction() - + db.transaction { // save message if (message.id == null) { insert(db, message) @@ -204,11 +203,6 @@ class AndroidMessageRepository(private val sql: SqlHelper) : AbstractMessageRepo values.put(JT_COLUMN_MESSAGE, message.id as Long?) db.insertOrThrow(JOIN_TABLE_NAME, null, values) } - db.setTransactionSuccessful() - } catch (e: SQLiteConstraintException) { - throw AlreadyStoredException(cause = e) - } finally { - db.endTransaction() } } diff --git a/app/src/main/java/ch/dissem/apps/abit/repository/AndroidNodeRegistry.kt b/app/src/main/java/ch/dissem/apps/abit/repository/AndroidNodeRegistry.kt index 2cc8b5c..5e4a68f 100644 --- a/app/src/main/java/ch/dissem/apps/abit/repository/AndroidNodeRegistry.kt +++ b/app/src/main/java/ch/dissem/apps/abit/repository/AndroidNodeRegistry.kt @@ -16,6 +16,7 @@ import ch.dissem.bitmessage.utils.UnixTime.DAY import ch.dissem.bitmessage.utils.UnixTime.MINUTE import ch.dissem.bitmessage.utils.UnixTime.now import ch.dissem.bitmessage.utils.max +import org.jetbrains.anko.db.transaction import org.slf4j.LoggerFactory import java.util.* import kotlin.concurrent.getOrSet @@ -64,30 +65,25 @@ class AndroidNodeRegistry(private val sql: SqlHelper) : NodeRegistry { } override fun getKnownAddresses(limit: Int, vararg streams: Long): List { - val projection = arrayOf(COLUMN_STREAM, COLUMN_ADDRESS, COLUMN_PORT, COLUMN_SERVICES, COLUMN_TIME) - val result = LinkedList() - try { - sql.readableDatabase.query( - TABLE_NAME, projection, - "stream IN (?)", - arrayOf(SqlStrings.join(*streams)), null, null, - "time DESC", - limit.toString() - ).use { c -> - while (c.moveToNext()) { - result.add(NetworkAddress( - time = c.getLong(c.getColumnIndex(COLUMN_TIME)), - stream = c.getLong(c.getColumnIndex(COLUMN_STREAM)), - services = c.getLong(c.getColumnIndex(COLUMN_SERVICES)), - IPv6 = c.getBlob(c.getColumnIndex(COLUMN_ADDRESS)), - port = c.getInt(c.getColumnIndex(COLUMN_PORT)) - )) - } + + sql.readableDatabase.query( + TABLE_NAME, + arrayOf(COLUMN_STREAM, COLUMN_ADDRESS, COLUMN_PORT, COLUMN_SERVICES, COLUMN_TIME), + "stream IN (?)", + arrayOf(SqlStrings.join(*streams)), null, null, + "time DESC", + limit.toString() + ).use { c -> + while (c.moveToNext()) { + result.add(NetworkAddress( + time = c.getLong(c.getColumnIndex(COLUMN_TIME)), + stream = c.getLong(c.getColumnIndex(COLUMN_STREAM)), + services = c.getLong(c.getColumnIndex(COLUMN_SERVICES)), + IPv6 = c.getBlob(c.getColumnIndex(COLUMN_ADDRESS)), + port = c.getInt(c.getColumnIndex(COLUMN_PORT)) + )) } - } catch (e: Exception) { - LOG.error(e.message, e) - throw ApplicationException(e) } if (result.isEmpty()) { @@ -101,9 +97,7 @@ class AndroidNodeRegistry(private val sql: SqlHelper) : NodeRegistry { } override fun offerAddresses(nodes: List) { - val db = sql.writableDatabase - db.beginTransaction() - try { + sql.writableDatabase.transaction { cleanUp() nodes .filter { @@ -120,9 +114,6 @@ class AndroidNodeRegistry(private val sql: SqlHelper) : NodeRegistry { } } } - db.setTransactionSuccessful() - } finally { - db.endTransaction() } }