From c2e7c3823e51dccb0b0c6b1ba827f045c8129c27 Mon Sep 17 00:00:00 2001 From: machiav3lli Date: Wed, 6 Apr 2022 04:02:29 +0200 Subject: [PATCH] Clean up --- src/main/kotlin/com/looker/droidify/Common.kt | 1 - .../droidify/database/ObservableCursor.kt | 62 ------------ .../looker/droidify/database/QueryLoader.kt | 94 ------------------- .../com/looker/droidify/entity/Product.kt | 92 ------------------ .../droidify/utility/extension/Android.kt | 4 - .../droidify/utility/extension/Resources.kt | 7 -- 6 files changed, 260 deletions(-) delete mode 100644 src/main/kotlin/com/looker/droidify/database/ObservableCursor.kt delete mode 100644 src/main/kotlin/com/looker/droidify/database/QueryLoader.kt delete mode 100644 src/main/kotlin/com/looker/droidify/entity/Product.kt diff --git a/src/main/kotlin/com/looker/droidify/Common.kt b/src/main/kotlin/com/looker/droidify/Common.kt index cc51e778..24833891 100644 --- a/src/main/kotlin/com/looker/droidify/Common.kt +++ b/src/main/kotlin/com/looker/droidify/Common.kt @@ -28,7 +28,6 @@ const val ROW_ANTIFEATURES = "categories" const val ROW_LICENSES = "categories" const val ROW_DONATES = "categories" const val ROW_SCREENSHOTS = "categories" -const val ROW_DATA = "data" const val ROW_VERSION = "version" const val ROW_SIGNATURE = "signature" const val ROW_ID = "_id" diff --git a/src/main/kotlin/com/looker/droidify/database/ObservableCursor.kt b/src/main/kotlin/com/looker/droidify/database/ObservableCursor.kt deleted file mode 100644 index 3ada2be4..00000000 --- a/src/main/kotlin/com/looker/droidify/database/ObservableCursor.kt +++ /dev/null @@ -1,62 +0,0 @@ -package com.looker.droidify.database - -import android.database.ContentObservable -import android.database.ContentObserver -import android.database.Cursor -import android.database.CursorWrapper - -class ObservableCursor( - cursor: Cursor, - private val observable: ( - register: Boolean, - observer: () -> Unit, - ) -> Unit, -) : CursorWrapper(cursor) { - private var registered = false - private val contentObservable = ContentObservable() - - private val onChange: () -> Unit = { - contentObservable.dispatchChange(false, null) - } - - init { - observable(true, onChange) - registered = true - } - - override fun registerContentObserver(observer: ContentObserver) { - super.registerContentObserver(observer) - contentObservable.registerObserver(observer) - } - - override fun unregisterContentObserver(observer: ContentObserver) { - super.unregisterContentObserver(observer) - contentObservable.unregisterObserver(observer) - } - - @Suppress("DEPRECATION") - override fun requery(): Boolean { - if (!registered) { - observable(true, onChange) - registered = true - } - return super.requery() - } - - @Suppress("DEPRECATION") - override fun deactivate() { - super.deactivate() - deactivateOrClose() - } - - override fun close() { - super.close() - contentObservable.unregisterAll() - deactivateOrClose() - } - - private fun deactivateOrClose() { - observable(false, onChange) - registered = false - } -} diff --git a/src/main/kotlin/com/looker/droidify/database/QueryLoader.kt b/src/main/kotlin/com/looker/droidify/database/QueryLoader.kt deleted file mode 100644 index 9b151b49..00000000 --- a/src/main/kotlin/com/looker/droidify/database/QueryLoader.kt +++ /dev/null @@ -1,94 +0,0 @@ -package com.looker.droidify.database - -import android.content.Context -import android.database.Cursor -import android.os.CancellationSignal -import android.os.OperationCanceledException -import androidx.loader.content.AsyncTaskLoader - -class QueryLoader(context: Context, private val query: (CancellationSignal) -> Cursor?) : - AsyncTaskLoader(context) { - private val observer = ForceLoadContentObserver() - private var cancellationSignal: CancellationSignal? = null - private var cursor: Cursor? = null - - override fun loadInBackground(): Cursor? { - val cancellationSignal = synchronized(this) { - if (isLoadInBackgroundCanceled) { - throw OperationCanceledException() - } - val cancellationSignal = CancellationSignal() - this.cancellationSignal = cancellationSignal - cancellationSignal - } - try { - val cursor = query(cancellationSignal) - if (cursor != null) { - try { - cursor.count // Ensure the cursor window is filled - cursor.registerContentObserver(observer) - } catch (e: Exception) { - cursor.close() - throw e - } - } - return cursor - } finally { - synchronized(this) { - this.cancellationSignal = null - } - } - } - - override fun cancelLoadInBackground() { - super.cancelLoadInBackground() - - synchronized(this) { - cancellationSignal?.cancel() - } - } - - override fun deliverResult(data: Cursor?) { - if (isReset) { - data?.close() - } else { - val oldCursor = cursor - cursor = data - if (isStarted) { - super.deliverResult(data) - } - if (oldCursor != data) { - oldCursor.closeIfNeeded() - } - } - } - - override fun onStartLoading() { - cursor?.let(this::deliverResult) - if (takeContentChanged() || cursor == null) { - forceLoad() - } - } - - override fun onStopLoading() { - cancelLoad() - } - - override fun onCanceled(data: Cursor?) { - data.closeIfNeeded() - } - - override fun onReset() { - super.onReset() - - stopLoading() - cursor.closeIfNeeded() - cursor = null - } - - private fun Cursor?.closeIfNeeded() { - if (this != null && !isClosed) { - close() - } - } -} diff --git a/src/main/kotlin/com/looker/droidify/entity/Product.kt b/src/main/kotlin/com/looker/droidify/entity/Product.kt deleted file mode 100644 index f2042abc..00000000 --- a/src/main/kotlin/com/looker/droidify/entity/Product.kt +++ /dev/null @@ -1,92 +0,0 @@ -package com.looker.droidify.entity - -import com.looker.droidify.database.entity.Installed -import com.looker.droidify.database.entity.Release -import com.looker.droidify.utility.extension.text.nullIfEmpty -import kotlinx.serialization.Serializable -import kotlinx.serialization.decodeFromString -import kotlinx.serialization.encodeToString -import kotlinx.serialization.json.Json - -@Serializable -data class Product( - var repositoryId: Long, - val packageName: String, - val name: String, - val summary: String, - var description: String, - val whatsNew: String, - val icon: String, - val metadataIcon: String, - val author: Author, - val source: String, - val changelog: String, - val web: String, - val tracker: String, - val added: Long, - val updated: Long, - val suggestedVersionCode: Long, - val categories: List, - val antiFeatures: List, - val licenses: List, - val donates: List, - val screenshots: List, - val releases: List, -) { - // Same releases with different signatures - val selectedReleases: List - get() = releases.filter { it.selected } - - val displayRelease: Release? - get() = selectedReleases.firstOrNull() ?: releases.firstOrNull() - - val version: String - get() = displayRelease?.version.orEmpty() - - val versionCode: Long - get() = selectedReleases.firstOrNull()?.versionCode ?: 0L - - val compatible: Boolean - get() = selectedReleases.firstOrNull()?.incompatibilities?.isEmpty() == true - - val signatures: List - get() = selectedReleases.mapNotNull { it.signature.nullIfEmpty() }.distinct().toList() - - fun item(): ProductItem { - return ProductItem( - repositoryId, - packageName, - name, - summary, - icon, - metadataIcon, - version, - "", - compatible, - false, - 0 - ) - } - - fun canUpdate(installed: Installed?): Boolean { - return installed != null && compatible && versionCode > installed.version_code && - installed.signature in signatures - } - - fun toJSON() = Json.encodeToString(this) - - companion object { - fun fromJson(json: String) = Json.decodeFromString(json) - - fun findSuggested( - products: List, - installed: Installed?, - extract: (T) -> Product, - ): T? { - return products.maxWithOrNull(compareBy({ - extract(it).compatible && - (installed == null || installed.signature in extract(it).signatures) - }, { extract(it).versionCode })) - } - } -} diff --git a/src/main/kotlin/com/looker/droidify/utility/extension/Android.kt b/src/main/kotlin/com/looker/droidify/utility/extension/Android.kt index 19e80cdf..92f46a91 100644 --- a/src/main/kotlin/com/looker/droidify/utility/extension/Android.kt +++ b/src/main/kotlin/com/looker/droidify/utility/extension/Android.kt @@ -14,10 +14,6 @@ fun Cursor.asSequence(): Sequence { return generateSequence { if (moveToNext()) this else null } } -fun Cursor.firstOrNull(): Cursor? { - return if (moveToFirst()) this else null -} - fun SQLiteDatabase.execWithResult(sql: String) { rawQuery(sql, null).use { it.count } } diff --git a/src/main/kotlin/com/looker/droidify/utility/extension/Resources.kt b/src/main/kotlin/com/looker/droidify/utility/extension/Resources.kt index aef9343d..b195002c 100644 --- a/src/main/kotlin/com/looker/droidify/utility/extension/Resources.kt +++ b/src/main/kotlin/com/looker/droidify/utility/extension/Resources.kt @@ -23,13 +23,6 @@ object TypefaceExtra { val light = Typeface.create("sans-serif-light", Typeface.NORMAL)!! } -val Number.toPx - get() = TypedValue.applyDimension( - TypedValue.COMPLEX_UNIT_DIP, - this.toFloat(), - Resources.getSystem().displayMetrics - ) - fun Context.getColorFromAttr(@AttrRes attrResId: Int): ColorStateList { val typedArray = obtainStyledAttributes(intArrayOf(attrResId)) val (colorStateList, resId) = try {