ios CommonMain中的Kotlin多平台移动的上下文

2hh7jdfx  于 2023-06-07  发布在  iOS
关注(0)|答案(1)|浏览(136)

CommonMain中的Kotlin多平台移动的上下文

嗨,我目前正在探索KMM开发,我被困在上下文部分。
我想创建一个类,它将处理安全的数据存储。我已经为iOS和Android创建了一个expect class Manager和两个actual class Manager。对于Android部分,框架和所有其他可用工具都需要Context
我想使用此Manager来存储和读取CommonMain中的值。但是,在项目的这个共享部分中,没有提供Context
我如何做到这一点?
这就是代码:

expect class TokenStore(context: Any) {
    fun getToken(): String?
    fun saveToken(token: String)
}
import com.liftric.kvault.KVault


actual class TokenStore actual constructor(context: Any) {
    private val store = KVault("com.example.keystoreexample")
    actual fun getToken(): String? {
        val token = store.string("ACCOUNT_TOKEN")
        return if (token != null) {
            println("✅ The token was found.\nThe value is: $token")
            token
        } else {
            println("❌ There is no token in the Token Store.")
            null
        }
    }

    actual fun saveToken(token: String) {
        if (token == "") println("⚠️ No token was provided.\nWatch out for this one.")
        if (store.existsObject("ACCOUNT_TOKEN")) {
            val existingObject = store.string("ACCOUNT_TOKEN")
            print("ℹ️ Object is already in the Token Store.\nThe token value is: $existingObject")
            if (store.clear()) {
                println("✅ All values in Token Store were removed.")
            } else {
                println("❌ There was an issue removing objects from the Token Store.")
            }
        } else {
            println("ℹ️ Object under this key is not stored in a database yet.")
        }
        if (store.set("ACCOUNT_TOKEN", token)) {
            println("✅ The token was added to the Token Store.")
        } else {
            println("❌ There was an issue inserting token to the Token Store.")
        }
    }
}
import android.content.Context
import com.liftric.kvault.KVault

actual class TokenStore actual constructor(private val context: Any) {
    private val store = KVault(context as Context)
    actual fun getToken(): String? {
        val token = store.string("ACCOUNT_TOKEN")
        return if (token != null) {
            println("✅ The token was found.\nThe value is: $token")
            token
        } else {
            println("❌ There is no token in the Token Store.")
            null
        }
    }

    actual fun saveToken(token: String) {
        if (token == "") println("⚠️ No token was provided.\nWatch out for this one.")
        if (store.existsObject("ACCOUNT_TOKEN")) {
            val existingObject = store.string("ACCOUNT_TOKEN")
            print("ℹ️ Object is already in the Token Store.\nThe token value is: $existingObject")
            if (store.clear()) {
                println("✅ All values in Token Store were removed.")
            } else {
                println("❌ There was an issue removing objects from the Token Store.")
            }
        } else {
            println("ℹ️ Object under this key is not stored in a database yet.")
        }
        if (store.set("ACCOUNT_TOKEN", token)) {
            println("✅ The token was added to the Token Store.")
        } else {
            println("❌ There was an issue inserting token to the Token Store.")
        }
    }
}

我想在这里使用它,例如:

package com.example.keystoresample

// CommonMain
import TokenStore

class Greeting {
    private val platform: Platform = getPlatform()
    private val token = TokenStore(context).getToken() // Where can I get it?

    fun greet(): String {
        return "Hello, ${platform.name}!\nThe token value is:\n$token"
    }
}

我想我已经试过很多了。

1bqhqjot

1bqhqjot1#

是需要Context的东西,必须移动到平台相关的实现。您可能只需要KVault

相关问题