Skip to content

Commit

Permalink
Start on asset importer, implement themes.
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Apr 12, 2022
1 parent 7e0f14a commit 7e39a40
Show file tree
Hide file tree
Showing 42 changed files with 540 additions and 194 deletions.
15 changes: 9 additions & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@ repositories {
val exposedVersion: String by project
val okioVersion: String by project
val koinVersion: String by project
val ktorVersion: String by project

dependencies {
testImplementation(kotlin("test"))
implementation(compose.desktop.currentOs)
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2")
implementation("io.ktor:ktor-client-core:1.6.8")
implementation("io.ktor:ktor-client-cio:1.6.8")
implementation("io.ktor:ktor-client-apache:1.6.8")
implementation("io.ktor:ktor-client-serialization:1.6.8")
implementation("io.ktor:ktor-client-logging:1.6.8")
implementation("io.ktor:ktor-client-okhttp:1.6.8")
implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
implementation("io.ktor:ktor-client-core:$ktorVersion")
implementation("io.ktor:ktor-client-cio:$ktorVersion")
implementation("io.ktor:ktor-client-apache:$ktorVersion")
implementation("io.ktor:ktor-client-logging:$ktorVersion")
implementation("io.ktor:ktor-client-okhttp:$ktorVersion")
implementation("org.jsoup:jsoup:1.14.3")
implementation("ch.qos.logback:logback-classic:1.2.11")
implementation("org.xerial:sqlite-jdbc:3.36.0.3")
Expand All @@ -40,6 +42,7 @@ dependencies {
implementation("org.jetbrains.exposed:exposed-dao:$exposedVersion")
implementation("org.jetbrains.exposed:exposed-jdbc:$exposedVersion")
implementation("com.squareup.okio:okio:$okioVersion")
implementation("io.github.microutils:kotlin-logging:2.1.21")

}

Expand Down
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ kotlin.code.style=official

exposedVersion=0.37.3
okioVersion=3.0.0
koinVersion=3.2.0-beta-1
koinVersion=3.2.0-beta-1
ktorVersion=2.0.0
26 changes: 14 additions & 12 deletions src/main/kotlin/com/abysl/assetmanager/Main.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.abysl.assetmanager

import androidx.compose.ui.unit.dp
import androidx.compose.material.MaterialTheme
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import androidx.compose.ui.window.rememberWindowState
import com.abysl.assetmanager.ui.components.main.MainComponent
import com.abysl.assetmanager.services.DBService
import com.abysl.assetmanager.services.NavigationService
import com.abysl.assetmanager.services.ImageService
import com.abysl.assetmanager.services.NavigationService
import com.abysl.assetmanager.ui.components.main.MainComponent
import com.abysl.assetmanager.ui.util.Theme
import org.koin.core.context.startKoin
import org.koin.core.module.dsl.singleOf
import org.koin.dsl.module
Expand All @@ -23,22 +24,23 @@ fun main() {

}
startKoin {
// printLogger()
modules(mainModule)
}

val mainComponent = MainComponent()
println(Prefs.itchApiKey)

application {
val state = rememberWindowState()
Window(
title = "AbyslTCG",
onCloseRequest = ::exitApplication,
state = state,
) {
this.window.size = Dimension(1440, 900)
mainComponent.view()
MaterialTheme(colors = Theme.darkMaterial) {
Window(
title = "AbyslTCG",
onCloseRequest = ::exitApplication,
state = state,
) {
this.window.size = Dimension(1440, 900)
mainComponent.view()
}

}
}
}
19 changes: 14 additions & 5 deletions src/main/kotlin/com/abysl/assetmanager/Prefs.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
package com.abysl.assetmanager

import com.abysl.assetmanager.db.tables.PreferencesTable
import kotlinx.coroutines.selects.select
import okio.Path.Companion.toPath
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.transactions.transaction

object Prefs {

val HOME_FOLDER = ".abysl/assetmanager".toPath().toFile().also { it.mkdir() }
val HOME_FOLDER = System.getProperty("user.home").toPath().toFile()
.resolve(".abysl/assetmanager")
.also { it.mkdirs() }
val DB_FILE = HOME_FOLDER.resolve("assetmanager.sqlite").also { it.createNewFile() }
val IMAGE_PATH = HOME_FOLDER.resolve("images").also { it.mkdirs() }
val IMAGE_CACHE = IMAGE_PATH.resolve("cache").also { it.mkdirs() }

val itchApiKey by lazy { PreferencesTable["itchApiKey"] }
var itchApiKey: String
get() = PreferencesTable["itchApiKey"] ?: ""
set(value) {
PreferencesTable["itchApiKey"] = value
}

var darkMode: Boolean
get() = PreferencesTable["darkMode"] != "0"
set(value) {
PreferencesTable["darkMode"] = if(value) "1" else "0"
}
}
12 changes: 4 additions & 8 deletions src/main/kotlin/com/abysl/assetmanager/db/MigrationService.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.abysl.assetmanager.db

import com.abysl.assetmanager.db.migrations.AssetCreator
import com.abysl.assetmanager.db.migrations.AssetStoreSchema
import com.abysl.assetmanager.db.migrations.InitialSchema
import com.abysl.assetmanager.db.migrations.PreferencesMigration
import com.abysl.assetmanager.db.migrations.*
import com.abysl.assetmanager.db.tables.VersionTable
import org.jetbrains.exposed.sql.exists
import org.jetbrains.exposed.sql.insert
Expand Down Expand Up @@ -52,9 +49,8 @@ class MigrationService {
}

private fun registerMigrations() {
migrations[0] = InitialSchema()
migrations[1] = AssetStoreSchema()
migrations[2] = AssetCreator()
migrations[3] = PreferencesMigration()
migrations[0] = InitialMigration()
migrations[1] = AssetTableMigration()
migrations[2] = PreferencesMigration()
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.abysl.assetmanager.db.tables.AssetTable
import com.abysl.assetmanager.db.tables.AssetTable.downloads
import com.abysl.assetmanager.db.tables.AssetTable.iconUrl
import com.abysl.assetmanager.db.tables.AssetTable.name
import com.abysl.assetmanager.db.tables.AssetTable.sourcePlatform
import com.abysl.assetmanager.db.tables.AssetTable.sourceUrl
import com.abysl.assetmanager.db.tables.ImageTable
import com.abysl.assetmanager.model.Asset
Expand All @@ -13,10 +14,9 @@ import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.batchInsert
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.transactions.transaction

class AssetStoreSchema: Migration() {
class AssetTableMigration: Migration() {
override fun up() {
transaction {
SchemaUtils.create (AssetTable)
Expand All @@ -26,13 +26,15 @@ class AssetStoreSchema: Migration() {
val assets = Humble().getProducts().map {
Asset(
name = it.name,
sourcePlatform = "humble",
creator = it.creator,
iconUrl = it.iconUrl,
sourceUrl = it.url,
downloads = it.downloads
) }
AssetTable.batchInsert(assets) { asset ->
this[name] = asset.name
this[sourcePlatform] = asset.sourcePlatform
this[iconUrl] = asset.iconUrl
this[sourceUrl] = asset.sourceUrl
this[downloads] = Json.encodeToString(asset.downloads)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.transactions.transaction

class InitialSchema() : Migration() {
class InitialMigration() : Migration() {
override fun up() {
transaction {
SchemaUtils.create (VersionTable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.jetbrains.exposed.sql.ResultRow

object AssetTable: IntIdTable() {
val name = varchar("name", 256);
val sourcePlatform = varchar("source_platform", 16)
val creator = varchar("creator", 256).default("Unknown")
val iconUrl = varchar("iconUrl", 256)
val sourceUrl = varchar("sourceUrl", 256);
Expand All @@ -16,6 +17,7 @@ object AssetTable: IntIdTable() {
fun fromRow(row: ResultRow): Asset =
Asset(
name = row[name],
sourcePlatform = row[sourcePlatform],
creator = row[creator],
iconUrl = row[iconUrl],
sourceUrl = row[sourceUrl],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.ResultRow

object ImageTable: IntIdTable() {
val url = varchar("name", 256);
val url = varchar("name", 256)

fun fromRow(row: ResultRow): CachedImage =
CachedImage(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
package com.abysl.assetmanager.db.tables

import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction

object PreferencesTable: IntIdTable() {
val key = varchar("key", 256)
val value = varchar("value",4096)
object PreferencesTable : Table() {
val key = varchar("key", 256).uniqueIndex()
val value = varchar("value", 4096)

operator fun get(pref: String): String? {
return transaction { PreferencesTable.select { key eq pref }.firstOrNull() }?.get(value)
}

operator fun set(pref: String, prefvalue: String){
operator fun set(pref: String, prefvalue: String) {
val oldValue = get(pref)
transaction {
PreferencesTable.insert {
it[key] = pref
it[value] = prefvalue
if(oldValue == null) {
PreferencesTable.insert {
it[key] = pref
it[value] = prefvalue
}
}else {
PreferencesTable.update {
it[value] = prefvalue
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/com/abysl/assetmanager/model/Asset.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.abysl.humble.model.DownloadStruct
@kotlinx.serialization.Serializable
data class Asset (
val name: String,
val sourcePlatform: String,
val creator: String,
val iconUrl: String,
val sourceUrl: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.abysl.assetmanager.services

import com.abysl.assetmanager.Prefs.DB_FILE
import com.abysl.assetmanager.db.MigrationService
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.Database


class DBService {
Expand Down
11 changes: 9 additions & 2 deletions src/main/kotlin/com/abysl/assetmanager/services/ImageService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import io.ktor.client.engine.cio.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import kotlinx.coroutines.*
import mu.KotlinLogging
import org.jetbrains.exposed.sql.insertAndGetId
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.skia.Image
import java.io.File

class ImageService(val cacheLocation: File) {
private val logger = KotlinLogging.logger {}
val client = HttpClient(CIO)

val defaultImage by lazy {
Expand All @@ -29,6 +31,7 @@ class ImageService(val cacheLocation: File) {
delay: Long = 1000, maxAttempts: Int = 2
): MutableState<ImageBitmap> {
val image = mutableStateOf(defaultImage)
if (url.isEmpty()) return image
CoroutineScope(Dispatchers.IO).launch {
var attempt = 0
while (attempt++ < maxAttempts) {
Expand All @@ -49,8 +52,12 @@ class ImageService(val cacheLocation: File) {
fun cache(url: String) {
CoroutineScope(Dispatchers.IO).launch {
val id = transaction { ImageTable.insertAndGetId { it[ImageTable.url] = url } }
val response: HttpResponse = client.get(url)
cacheLocation.resolve("$id.png").also { it.createNewFile() }.writeBytes(response.readBytes())
try {
val response: HttpResponse = client.get(url)
cacheLocation.resolve("$id.png").also { it.createNewFile() }.writeBytes(response.readBytes())
} catch (e: Exception) {
logger.warn("Failed to load image $url")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package com.abysl.assetmanager.ui.components

Loading

0 comments on commit 7e39a40

Please sign in to comment.