Skip to content

Commit

Permalink
Update to new ktlint
Browse files Browse the repository at this point in the history
  • Loading branch information
DeKaN committed Oct 23, 2023
1 parent dd5e123 commit 756b0b1
Show file tree
Hide file tree
Showing 13 changed files with 175 additions and 216 deletions.
6 changes: 4 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ ij_editorconfig_space_before_colon = false
ij_editorconfig_space_before_comma = false
ij_editorconfig_spaces_around_assignment_operators = true

[{*.gradle, *.groovy, *.gant, *.gdsl, *.gy}]
[{*.gradle,*.groovy,*.gant,*.gdsl,*.gy}]
ij_groovy_align_group_field_declarations = false
ij_groovy_align_multiline_array_initializer_expression = false
ij_groovy_align_multiline_assignment = false
Expand Down Expand Up @@ -398,14 +398,16 @@ ij_groovy_while_brace_force = never
ij_groovy_while_on_new_line = false
ij_groovy_wrap_long_lines = false

[*.{kt, kts}]
[*.{kt,kts}]
ktlint_code_style = android_studio
ktlint_standard_import-ordering = disabled
ij_kotlin_align_in_columns_case_branch = false
ij_kotlin_align_multiline_binary_operation = false
ij_kotlin_align_multiline_extends_list = false
ij_kotlin_align_multiline_method_parentheses = false
ij_kotlin_align_multiline_parameters = true
ij_kotlin_align_multiline_parameters_in_calls = false
ij_kotlin_allow_trailing_comma = true
ij_kotlin_allow_trailing_comma_on_call_site = false
ij_kotlin_assignment_wrap = normal
ij_kotlin_blank_lines_after_class_header = 0
Expand Down
8 changes: 6 additions & 2 deletions piano-analytics/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ plugins {
alias(libs.plugins.mavenRelease)
}

@Suppress("PropertyName")
val GROUP: String by project

@Suppress("PropertyName")
val VERSION_NAME: String by project

group = GROUP
version = VERSION_NAME

Expand Down Expand Up @@ -43,8 +47,8 @@ kotlin {
}

ktlint {
version.set("0.50.0")
android.set(true)
android = true
version = "1.0.1"
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,39 +42,35 @@ internal class DatabaseHelper(
}
}

internal fun EventRecord.toContentValues(): ContentValues =
ContentValues().apply {
put(EventRecord.DATA, dataEncoder.encode(data))
put(EventRecord.TIME, timestamp)
put(EventRecord.IS_SENT, isSent)
}
internal fun EventRecord.toContentValues(): ContentValues = ContentValues().apply {
put(EventRecord.DATA, dataEncoder.encode(data))
put(EventRecord.TIME, timestamp)
put(EventRecord.IS_SENT, isSent)
}

fun ContentValues.toEventRecord(): EventRecord =
EventRecord(
dataEncoder.decode(getAsString(EventRecord.DATA)),
getAsLong(EventRecord.TIME),
getAsLong(EventRecord.ID),
getAsBoolean(EventRecord.IS_SENT)
)
fun ContentValues.toEventRecord(): EventRecord = EventRecord(
dataEncoder.decode(getAsString(EventRecord.DATA)),
getAsLong(EventRecord.TIME),
getAsLong(EventRecord.ID),
getAsBoolean(EventRecord.IS_SENT)
)

fun save(eventRecord: EventRecord): Long =
eventRecord.id?.let { id ->
writableDatabase.update(
EventRecord.TABLE_NAME,
eventRecord.toContentValues(),
"${EventRecord.ID} = ?",
arrayOf(id.toString())
).toLong()
} ?: writableDatabase.insert(
fun save(eventRecord: EventRecord): Long = eventRecord.id?.let { id ->
writableDatabase.update(
EventRecord.TABLE_NAME,
null,
eventRecord.toContentValues()
)
eventRecord.toContentValues(),
"${EventRecord.ID} = ?",
arrayOf(id.toString())
).toLong()
} ?: writableDatabase.insert(
EventRecord.TABLE_NAME,
null,
eventRecord.toContentValues()
)

fun delete(eventRecord: EventRecord): Int =
eventRecord.id?.let { id ->
delete("${EventRecord.ID} = ?", id.toString())
} ?: -1
fun delete(eventRecord: EventRecord): Int = eventRecord.id?.let { id ->
delete("${EventRecord.ID} = ?", id.toString())
} ?: -1

fun delete(whereClause: String?, vararg whereArgs: String): Int =
writableDatabase.delete(EventRecord.TABLE_NAME, whereClause, whereArgs)
Expand All @@ -87,42 +83,40 @@ internal class DatabaseHelper(
having: String? = null,
orderBy: String? = "${EventRecord.TIME} ASC",
limit: String? = null,
): List<EventRecord> =
readableDatabase.query(
EventRecord.TABLE_NAME,
columns,
selection,
selectionArgs,
groupBy,
having,
orderBy,
limit
).use { c ->
generateSequence { if (c.moveToNext()) c else null }
.map(Companion::cursorRowToContentValues)
.map {
it.toEventRecord()
}.filter {
it.isValid
}.toList()
}
): List<EventRecord> = readableDatabase.query(
EventRecord.TABLE_NAME,
columns,
selection,
selectionArgs,
groupBy,
having,
orderBy,
limit
).use { c ->
generateSequence { if (c.moveToNext()) c else null }
.map(Companion::cursorRowToContentValues)
.map {
it.toEventRecord()
}.filter {
it.isValid
}.toList()
}

companion object {
const val DATABASE_VERSION = 1
private const val DATABASE_NAME = "events.db"

@JvmStatic
private fun cursorRowToContentValues(c: Cursor): ContentValues =
ContentValues().apply {
for (i in 0 until c.columnCount) {
val name = c.columnNames[i]
when (c.getType(i)) {
Cursor.FIELD_TYPE_BLOB -> put(name, c.getBlob(i))
Cursor.FIELD_TYPE_FLOAT -> put(name, c.getFloat(i))
Cursor.FIELD_TYPE_INTEGER -> put(name, c.getLong(i))
else -> put(name, c.getString(i))
}
private fun cursorRowToContentValues(c: Cursor): ContentValues = ContentValues().apply {
for (i in 0 until c.columnCount) {
val name = c.columnNames[i]
when (c.getType(i)) {
Cursor.FIELD_TYPE_BLOB -> put(name, c.getBlob(i))
Cursor.FIELD_TYPE_FLOAT -> put(name, c.getFloat(i))
Cursor.FIELD_TYPE_INTEGER -> put(name, c.getLong(i))
else -> put(name, c.getString(i))
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ internal inline fun <T> delegatedPropertyWithDefaultValue(
}
}

internal fun <T> resettableProperty(
resetValue: T,
initializer: () -> T,
) = ResettableProperty(resetValue, initializer)
internal fun <T> resettableProperty(resetValue: T, initializer: () -> T) = ResettableProperty(resetValue, initializer)

internal class ResettableProperty<T>(
private val resetValue: T,
Expand All @@ -48,29 +45,25 @@ internal class SharedPreferenceDelegates(private val prefs: SharedPreferences) {
default: Boolean = false,
key: String? = null,
canBeSaved: (key: String) -> Boolean = DEFAULT_SAVE_FILTER,
): ReadWriteProperty<Any, Boolean> =
create(default, key, canBeSaved, prefs::getBoolean, prefs.edit()::putBoolean)
): ReadWriteProperty<Any, Boolean> = create(default, key, canBeSaved, prefs::getBoolean, prefs.edit()::putBoolean)

fun int(
default: Int = 0,
key: String? = null,
canBeSaved: (key: String) -> Boolean = DEFAULT_SAVE_FILTER,
): ReadWriteProperty<Any, Int> =
create(default, key, canBeSaved, prefs::getInt, prefs.edit()::putInt)
): ReadWriteProperty<Any, Int> = create(default, key, canBeSaved, prefs::getInt, prefs.edit()::putInt)

fun float(
default: Float = 0f,
key: String? = null,
canBeSaved: (key: String) -> Boolean = DEFAULT_SAVE_FILTER,
): ReadWriteProperty<Any, Float> =
create(default, key, canBeSaved, prefs::getFloat, prefs.edit()::putFloat)
): ReadWriteProperty<Any, Float> = create(default, key, canBeSaved, prefs::getFloat, prefs.edit()::putFloat)

fun long(
default: Long = 0L,
key: String? = null,
canBeSaved: (key: String) -> Boolean = DEFAULT_SAVE_FILTER,
): ReadWriteProperty<Any, Long> =
create(default, key, canBeSaved, prefs::getLong, prefs.edit()::putLong)
): ReadWriteProperty<Any, Long> = create(default, key, canBeSaved, prefs::getLong, prefs.edit()::putLong)

fun string(
default: String = "",
Expand All @@ -86,19 +79,6 @@ internal class SharedPreferenceDelegates(private val prefs: SharedPreferences) {
): ReadWriteProperty<Any, String?> =
create(default, key, canBeSaved, { k, d -> prefs.getString(k, d) }, prefs.edit()::putString)

fun stringSet(
default: Set<String> = emptySet(),
key: String? = null,
canBeSaved: (key: String) -> Boolean = DEFAULT_SAVE_FILTER,
): ReadWriteProperty<Any, Set<String>> =
create(
default,
key,
canBeSaved,
{ k, d -> prefs.getStringSet(k, d) as Set<String> },
prefs.edit()::putStringSet
)

private fun <T> create(
default: T,
key: String? = null,
Expand All @@ -108,8 +88,7 @@ internal class SharedPreferenceDelegates(private val prefs: SharedPreferences) {
) = object : ReadWriteProperty<Any, T> {
private fun key(property: KProperty<*>) = key ?: property.name

override fun getValue(thisRef: Any, property: KProperty<*>): T =
getter(key(property), default)
override fun getValue(thisRef: Any, property: KProperty<*>): T = getter(key(property), default)

override fun setValue(thisRef: Any, property: KProperty<*>, value: T) {
val propertyKey = key(property)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ internal class EventRepository(
)
}

fun getNotSentEvents(): List<EventRecord> =
databaseHelper.query(
selection = "${EventRecord.IS_SENT} = 0",
orderBy = "${EventRecord.TIME} ASC"
)
fun getNotSentEvents(): List<EventRecord> = databaseHelper.query(
selection = "${EventRecord.IS_SENT} = 0",
orderBy = "${EventRecord.TIME} ASC"
)

fun markEventsAsSent(events: Collection<EventRecord>) {
events.forEach { e ->
Expand Down
Loading

0 comments on commit 756b0b1

Please sign in to comment.