Skip to content

Commit

Permalink
Removed context references
Browse files Browse the repository at this point in the history
  • Loading branch information
arietrouw committed Dec 4, 2024
1 parent 09da683 commit bbda03b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class XyoSdkTest {
fun testGetAccount() {
runBlocking {
val instance = XyoSdk.getInstance(appContext)
val account = instance.getAccount()
val account = instance.getAccount(appContext)
assertInstanceOf<AccountInstance>(account)
}
}
Expand All @@ -68,7 +68,7 @@ class XyoSdkTest {
runBlocking {
val instance = XyoSdk.getInstance(appContext)

val testAccount = instance.getAccount()
val testAccount = instance.getAccount(appContext)
val panel = XyoPanel(appContext, testAccount, fun(_:Context): List<XyoEventPayload> {
return listOf(XyoEventPayload("test_event"))
})
Expand Down
13 changes: 6 additions & 7 deletions sdk/src/main/java/network/xyo/client/settings/XyoSdk.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ import network.xyo.client.account.model.AccountInstance
import network.xyo.client.datastore.accounts.AccountPrefsRepository
import network.xyo.client.datastore.previous_hash_store.PreviousHashStorePrefsRepository

class XyoSdk private constructor(context: Context, val settings: SettingsInterface) {
private val appContext = context.applicationContext
class XyoSdk private constructor(val settings: SettingsInterface) {
private var _account: AccountInstance? = null

@RequiresApi(Build.VERSION_CODES.M)
suspend fun getAccount(): AccountInstance {
suspend fun getAccount(context: Context): AccountInstance {
if (INSTANCE !== null) {
val validInstance = INSTANCE!!
if (validInstance._account !== null) {
return validInstance._account!!
} else {
val repository = AccountPrefsRepository.getInstance(validInstance.appContext)
val repository = AccountPrefsRepository.getInstance(context.applicationContext)
validInstance._account = repository.getAccount()
return _account!!
}
Expand All @@ -39,16 +38,16 @@ class XyoSdk private constructor(context: Context, val settings: SettingsInterfa
Account.previousHashStore = PreviousHashStorePrefsRepository.getInstance(context.applicationContext)

val newInstance = INSTANCE ?: synchronized(this) {
INSTANCE ?: XyoSdk(context.applicationContext, settings).also { INSTANCE = it }
INSTANCE ?: XyoSdk(settings).also { INSTANCE = it }
}
return newInstance
}

fun refresh(context: Context, settings: SettingsInterface = DefaultXyoSdkSettings()): XyoSdk {
synchronized(this) {
// Initialize the singleton with the users accountPreferences
AccountPrefsRepository.getInstance(context.applicationContext, settings)
INSTANCE = XyoSdk(context.applicationContext, settings)
AccountPrefsRepository.getInstance(context, settings)
INSTANCE = XyoSdk(settings)
}
return INSTANCE!!
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import network.xyo.client.settings.XyoSdk
open class WitnessLocationHandler : WitnessHandlerInterface<Triple<XyoBoundWitnessJson?, XyoPayload?, XyoPayload?>> {
@RequiresApi(Build.VERSION_CODES.M)
override suspend fun witness(context: Context, nodeUrlsAndAccounts: ArrayList<Pair<String, AccountInstance?>>): WitnessResult<Triple<XyoBoundWitnessJson?, XyoPayload?, XyoPayload?>> {
val account = XyoSdk.getInstance(context.applicationContext).getAccount()
val account = XyoSdk.getInstance(context.applicationContext).getAccount(context)
val panel = XyoPanel(context, account, nodeUrlsAndAccounts, listOf(
XyoLocationWitness(account)
))
Expand Down

0 comments on commit bbda03b

Please sign in to comment.