Skip to content

Commit

Permalink
move getAccount calls to XyoSdk and store appContext as weak ref
Browse files Browse the repository at this point in the history
  • Loading branch information
jonesmac committed Nov 18, 2024
1 parent 4e9aebd commit 507fd2c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 24 deletions.
4 changes: 2 additions & 2 deletions sdk/src/main/java/network/xyo/client/XyoPanel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<PostBoundWitnessesResult>)
data class XyoPanelReportQueryResult(val bw: XyoBoundWitnessJson, val apiResults: List<PostQueryResult>?, val payloads: List<XyoPayload>?)
Expand Down Expand Up @@ -78,7 +78,7 @@ class XyoPanel(val context: Context, private val archivists: List<XyoArchivistAp

suspend fun resolveNodes(resetNodes: Boolean = false) {
if (resetNodes) nodes = null
this.defaultAccount = AccountPrefsRepository.getInstance(context).getAccount()
this.defaultAccount = XyoSdk.getInstance(context).getAccount()
if (nodeUrlsAndAccounts?.isNotEmpty() == true) {
nodes = mutableListOf<NodeClient>().let {
this@XyoPanel.nodeUrlsAndAccounts.forEach { pair ->
Expand Down
14 changes: 1 addition & 13 deletions sdk/src/main/java/network/xyo/client/settings/Defaults.kt
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
28 changes: 25 additions & 3 deletions sdk/src/main/java/network/xyo/client/settings/XyoSdk.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
Expand All @@ -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!!
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<XyoPayload?>> {
@RequiresApi(Build.VERSION_CODES.M)
override suspend fun witness(context: Context, nodeUrlsAndAccounts: ArrayList<Pair<String, AccountInstance?>>): WitnessResult<List<XyoPayload?>> {
val account = AccountPrefsRepository.getInstance(context).getAccount()
val account = XyoSdk.getInstance(context.applicationContext).getAccount()
val panel = XyoPanel(context, nodeUrlsAndAccounts, listOf(
XyoLocationWitness(account)
))
Expand Down

0 comments on commit 507fd2c

Please sign in to comment.