diff --git a/sdk/src/main/java/network/xyo/client/XyoPanel.kt b/sdk/src/main/java/network/xyo/client/XyoPanel.kt index 039021c..50e3180 100644 --- a/sdk/src/main/java/network/xyo/client/XyoPanel.kt +++ b/sdk/src/main/java/network/xyo/client/XyoPanel.kt @@ -13,10 +13,10 @@ import network.xyo.client.archivist.api.XyoArchivistApiConfig import network.xyo.client.archivist.wrapper.ArchivistWrapper import network.xyo.client.boundwitness.XyoBoundWitnessBuilder import network.xyo.client.boundwitness.XyoBoundWitnessJson -import network.xyo.client.datastore.AccountPrefsRepository import network.xyo.client.node.client.NodeClient import network.xyo.client.node.client.PostQueryResult import network.xyo.client.payload.XyoPayload +import network.xyo.client.settings.XyoSdk data class XyoPanelReportResult(val bw: XyoBoundWitnessJson, val apiResults: List) data class XyoPanelReportQueryResult(val bw: XyoBoundWitnessJson, val apiResults: List?, val payloads: List?) @@ -78,7 +78,7 @@ class XyoPanel(val context: Context, private val archivists: List().let { this@XyoPanel.nodeUrlsAndAccounts.forEach { pair -> diff --git a/sdk/src/main/java/network/xyo/client/settings/Defaults.kt b/sdk/src/main/java/network/xyo/client/settings/Defaults.kt index 6a1e0c3..9b0560c 100644 --- a/sdk/src/main/java/network/xyo/client/settings/Defaults.kt +++ b/sdk/src/main/java/network/xyo/client/settings/Defaults.kt @@ -1,19 +1,7 @@ package network.xyo.client.settings -import android.content.Context -import android.os.Build -import androidx.annotation.RequiresApi -import network.xyo.client.account.model.AccountInstance -import network.xyo.client.datastore.AccountPrefsRepository - -open class DefaultXyoSdkSettings: SettingsInterface { +open class DefaultXyoSdkSettings(): SettingsInterface { override val accountPreferences: AccountPreferences = DefaultAccountPreferences() - - @RequiresApi(Build.VERSION_CODES.M) - override suspend fun getAccount(context: Context): AccountInstance? { - val repository = AccountPrefsRepository.getInstance(context) - return repository.getAccount() - } } open class DefaultAccountPreferences: AccountPreferences { diff --git a/sdk/src/main/java/network/xyo/client/settings/SettingsInterface.kt b/sdk/src/main/java/network/xyo/client/settings/SettingsInterface.kt index 8430fb1..85b62ee 100644 --- a/sdk/src/main/java/network/xyo/client/settings/SettingsInterface.kt +++ b/sdk/src/main/java/network/xyo/client/settings/SettingsInterface.kt @@ -1,11 +1,7 @@ package network.xyo.client.settings -import android.content.Context -import network.xyo.client.account.model.AccountInstance - interface SettingsInterface { val accountPreferences: AccountPreferences - suspend fun getAccount(context: Context): AccountInstance? } interface AccountPreferences { diff --git a/sdk/src/main/java/network/xyo/client/settings/XyoSdk.kt b/sdk/src/main/java/network/xyo/client/settings/XyoSdk.kt index 6a83907..3407655 100644 --- a/sdk/src/main/java/network/xyo/client/settings/XyoSdk.kt +++ b/sdk/src/main/java/network/xyo/client/settings/XyoSdk.kt @@ -1,9 +1,31 @@ package network.xyo.client.settings import android.content.Context +import android.os.Build +import androidx.annotation.RequiresApi +import network.xyo.client.account.model.AccountInstance import network.xyo.client.datastore.AccountPrefsRepository -class XyoSdk(val settings: SettingsInterface) { +class XyoSdk private constructor(context: Context, val settings: SettingsInterface) { + private val appContext = context.applicationContext + var _account: AccountInstance? = null + + @RequiresApi(Build.VERSION_CODES.M) + suspend fun getAccount(): AccountInstance { + if (INSTANCE !== null) { + val validInstance = INSTANCE!! + if (validInstance._account !== null) { + return validInstance._account!! + } else { + val repository = AccountPrefsRepository.getInstance(validInstance.appContext) + validInstance._account = repository.getAccount() + return _account!! + } + } else { + throw Exception("Tried to access instance but it was null. Did you forget to initialize XyoSdk first?") + } + } + companion object { @Volatile private var INSTANCE: XyoSdk? = null @@ -13,7 +35,7 @@ class XyoSdk(val settings: SettingsInterface) { AccountPrefsRepository.getInstance(context.applicationContext, settings.accountPreferences) val newInstance = INSTANCE ?: synchronized(this) { - INSTANCE ?: XyoSdk(settings).also { INSTANCE = it } + INSTANCE ?: XyoSdk(context.applicationContext, settings).also { INSTANCE = it } } return newInstance } @@ -22,7 +44,7 @@ class XyoSdk(val settings: SettingsInterface) { synchronized(this) { // Initialize the singleton with the users accountPreferences AccountPrefsRepository.getInstance(context.applicationContext, settings.accountPreferences) - INSTANCE = XyoSdk(settings) + INSTANCE = XyoSdk(context.applicationContext, settings) } return INSTANCE!! } diff --git a/sdk/src/main/java/network/xyo/client/witness/location/info/Handler.kt b/sdk/src/main/java/network/xyo/client/witness/location/info/Handler.kt index 8bd71d2..6634d9f 100644 --- a/sdk/src/main/java/network/xyo/client/witness/location/info/Handler.kt +++ b/sdk/src/main/java/network/xyo/client/witness/location/info/Handler.kt @@ -11,13 +11,13 @@ import network.xyo.client.witness.types.WitnessResult import network.xyo.client.XyoPanel import network.xyo.client.account.model.AccountInstance import network.xyo.client.boundwitness.XyoBoundWitnessBodyJson -import network.xyo.client.datastore.AccountPrefsRepository import network.xyo.client.payload.XyoPayload +import network.xyo.client.settings.XyoSdk open class WitnessLocationHandler : WitnessHandlerInterface> { @RequiresApi(Build.VERSION_CODES.M) override suspend fun witness(context: Context, nodeUrlsAndAccounts: ArrayList>): WitnessResult> { - val account = AccountPrefsRepository.getInstance(context).getAccount() + val account = XyoSdk.getInstance(context.applicationContext).getAccount() val panel = XyoPanel(context, nodeUrlsAndAccounts, listOf( XyoLocationWitness(account) ))