Added implementations for iOS

This commit is contained in:
dankito 2024-09-17 15:51:50 +02:00
parent f5a93bdddd
commit 4fa7adeeb1
4 changed files with 69 additions and 2 deletions

View File

@ -98,7 +98,7 @@ kotlin {
implementation(libs.favre.bcrypt)
}
nativeMain.dependencies {
iosMain.dependencies {
implementation(libs.sqldelight.native.driver)
}

View File

@ -7,7 +7,7 @@ import net.codinux.banking.dataaccess.BankmeisterDb
import net.codinux.banking.ui.config.DI
fun MainViewController() = ComposeUIViewController {
DI.setRepository(NativeSqliteDriver(Database.Schema.synchronous(), "Bankmeister.db"))
DI.setRepository(NativeSqliteDriver(BankmeisterDb.Schema.synchronous(), "Bankmeister.db"))
App()
}

View File

@ -1,6 +1,36 @@
package net.codinux.banking.ui
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.useContents
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import platform.CoreGraphics.CGRect
import platform.UIKit.UIDevice
import platform.UIKit.UIScreen
actual val Dispatchers.IOorDefault: CoroutineDispatcher
get() = Dispatchers.IO
@OptIn(ExperimentalForeignApi::class)
@Composable
actual fun rememberScreenSizeInfo(): ScreenSizeInfo {
val density = LocalDensity.current
val screenBounds: CGRect = UIScreen.mainScreen.bounds.useContents { this }
val screenWidth = screenBounds.size.width
val screenHeight = screenBounds.size.height
return remember(density, screenWidth, screenHeight) {
ScreenSizeInfo(
heightDp = with(density) { screenHeight.dp },
widthDp = with(density) { screenWidth.dp }
)
}
}
class IOSPlatform: Platform {
override val name: String = UIDevice.currentDevice.systemName() + " " + UIDevice.currentDevice.systemVersion

View File

@ -0,0 +1,37 @@
package net.codinux.banking.ui.service
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.toComposeImageBitmap
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.readBytes
import org.jetbrains.skia.Image
import kotlinx.coroutines.suspendCancellableCoroutine
import platform.Foundation.*
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
actual fun createImageBitmap(imageBytes: ByteArray): ImageBitmap =
Image.makeFromEncoded(imageBytes).toComposeImageBitmap()
@OptIn(ExperimentalForeignApi::class)
actual suspend fun fetchBytesFromUrl(url: String): ByteArray {
val nsUrl = NSURL(string = url) ?: throw IllegalArgumentException("Invalid URL")
return suspendCancellableCoroutine { continuation ->
val request = NSURLRequest.requestWithURL(nsUrl)
val session = NSURLSession.sharedSession
val task = session.dataTaskWithRequest(request) { data, response, error ->
when {
error != null -> continuation.resumeWithException(Exception(error.localizedDescription))
data != null -> continuation.resume(data.bytes!!.readBytes(data.length.toInt()))
else -> continuation.resumeWithException(Exception("Unknown error"))
}
}
continuation.invokeOnCancellation {
task.cancel() // Cancel the task if the coroutine is cancelled
}
task.resume()
}
}