Skip to content

Commit

Permalink
Added OsmAnd Shared Library (kotlin multiplatform)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-osm committed Mar 9, 2024
1 parent 77211dc commit 2e2f053
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 1 deletion.
54 changes: 54 additions & 0 deletions OsmAnd-shared/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

plugins {
id("org.jetbrains.kotlin.multiplatform")
id("com.android.library")
}

kotlin {
androidTarget {
compilations.all {
kotlinOptions {
jvmTarget = "11"
}
}
}

listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
).forEach { iosTarget ->
iosTarget.binaries.framework {
baseName = "OsmAndShared"
isStatic = true
}
}

val sqliteVersion = "2.3.1"
val sqlDelightVersion = "1.5.4"

sourceSets {
commonMain.dependencies {
implementation("com.squareup.sqldelight:runtime:$sqlDelightVersion")
}
androidMain.dependencies {
implementation("com.squareup.sqldelight:android-driver:$sqlDelightVersion")
implementation("androidx.sqlite:sqlite-framework:$sqliteVersion")
}
iosMain.dependencies {
implementation("com.squareup.sqldelight:native-driver:$sqlDelightVersion")
}
}
}

android {
namespace = "net.osmand.shared"
compileSdk = 33
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
defaultConfig {
minSdk = 23
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package net.osmand.shared.db

import android.content.Context
import androidx.sqlite.db.SupportSQLiteDatabase
import androidx.sqlite.db.SupportSQLiteOpenHelper.Callback
import androidx.sqlite.db.SupportSQLiteOpenHelper.Configuration
import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory
import com.squareup.sqldelight.android.AndroidSqliteDriver
import com.squareup.sqldelight.db.SqlDriver

actual class DatabaseDriverFactory(
private val context: Context,
private val name: String,
private val version: Int
) {
actual fun createDriver(): SqlDriver {
val callback = object: Callback(version) {
override fun onCreate(db: SupportSQLiteDatabase) {
}

override fun onUpgrade(db: SupportSQLiteDatabase, oldVersion: Int, newVersion: Int) {
}
}
return AndroidSqliteDriver(
FrameworkSQLiteOpenHelperFactory().create(
Configuration.builder(context)
.callback(callback)
.name(name)
.noBackupDirectory(false)
.build()
)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package net.osmand.shared.db

import com.squareup.sqldelight.db.SqlCursor

class Database @Throws(Exception::class) constructor(databaseDriverFactory: DatabaseDriverFactory) {
private val driver = databaseDriverFactory.createDriver()

@Throws(Exception::class)
fun execSQL(sql: String) {
try {
driver.execute(null, sql, 0)
} catch (e: Exception) {
throw e
}
}

@Throws(Exception::class)
fun execSQL(sql: String, parameters: List<Any>) {
try {
driver.execute(null, sql, parameters.size) {
for ((index, param) in parameters.withIndex()) {
val i = index + 1
when (param) {
is Long -> bindLong(i, param)
is String -> bindString(i, param)
is Double -> bindDouble(i, param)
is ByteArray -> bindBytes(i, param)
else -> throw IllegalArgumentException("Non supported parameter: $param")
}
}
}
} catch (e: Exception) {
throw e
}
}

@Throws(Exception::class)
fun execSQLQuery(sql: String): SqlCursor {
try {
return driver.executeQuery(null, sql, 0)
} catch (e: Exception) {
throw e
}
}

@Throws(Exception::class)
fun execSQLQuery(sql: String, parameters: List<Any>): SqlCursor {
try {
return driver.executeQuery(null, sql, parameters.size) {
for ((index, param) in parameters.withIndex()) {
val i = index + 1
when (param) {
is Long -> bindLong(i, param)
is String -> bindString(i, param)
is Double -> bindDouble(i, param)
is ByteArray -> bindBytes(i, param)
else -> throw IllegalArgumentException("Non supported parameter: $param")
}
}
}
} catch (e: Exception) {
throw e
}
}

@Throws(Exception::class)
fun close() {
try {
driver.close()
} catch (e: Exception) {
throw e
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.osmand.shared.db

import com.squareup.sqldelight.db.SqlDriver

expect class DatabaseDriverFactory {
fun createDriver(): SqlDriver
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.osmand.shared.db

import co.touchlab.sqliter.DatabaseConfiguration
import com.squareup.sqldelight.db.SqlDriver
import com.squareup.sqldelight.drivers.native.NativeSqliteDriver

actual class DatabaseDriverFactory(private val dbName: String,
private val dbPath: String,
private val version: Int) {
actual fun createDriver(): SqlDriver {
return NativeSqliteDriver(
DatabaseConfiguration(
name = dbName,
version = version,
create = {},
extendedConfig = DatabaseConfiguration.Extended(basePath = dbPath))
)
}
}
4 changes: 4 additions & 0 deletions OsmAnd/build-common.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ task appStart(type: Exec) {
dependencies {
implementation project(path: ':OsmAnd-java', configuration: 'android')
implementation project(':OsmAnd-api')

implementation project(':OsmAnd-shared')
implementation("androidx.sqlite:sqlite-framework:2.3.1")

implementation 'androidx.multidex:multidex:2.0.1'
implementation 'androidx.gridlayout:gridlayout:1.0.0'
implementation 'androidx.cardview:cardview:1.0.0'
Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.8.22'
ext.kotlin_version = '1.9.22'
repositories {
google()
mavenCentral()
Expand All @@ -12,6 +12,7 @@ buildscript {
classpath 'com.android.tools.build:gradle:7.4.2'
classpath 'com.google.gms:google-services:3.0.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath("com.squareup.sqldelight:gradle-plugin:1.5.5")
if (gradle.startParameter.taskNames.toString().toLowerCase().contains("huawei")) {
classpath 'com.huawei.agconnect:agcp:1.8.0.300'
}
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ include ':OsmAnd'
include ':OsmAnd-java'
include ':OsmAnd-api'
include ':OsmAnd-telegram'
include ':OsmAnd-shared'
include ':plugins:Osmand-Nautical'
include ':plugins:Osmand-SRTMPlugin'
include ':plugins:Osmand-Skimaps'

0 comments on commit 2e2f053

Please sign in to comment.