Skip to content

Commit

Permalink
XyoSdk class
Browse files Browse the repository at this point in the history
  • Loading branch information
jonesmac committed Nov 13, 2024
1 parent 05e90c4 commit f535aba
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
53 changes: 53 additions & 0 deletions sdk/src/androidTest/java/network/xyo/client/XyoSdkTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package network.xyo.client

import android.content.Context
import androidx.test.platform.app.InstrumentationRegistry
import kotlinx.coroutines.runBlocking
import network.xyo.client.settings.AccountPreferences
import network.xyo.client.settings.DefaultXyoSdkSettings
import network.xyo.client.settings.XyoSdk
import org.junit.Before
import org.junit.Test
import org.junit.jupiter.api.Assertions.assertEquals

class XyoSdkTest {

private lateinit var appContext: Context

@Before
fun resetSingleton() {
XyoSdk.resetInstance()
}

@Before
fun useAppContext() {
// Context of the app under test.
this.appContext = InstrumentationRegistry.getInstrumentation().targetContext
}

@Test
fun testDefaultSettings() {
runBlocking {
val instance = XyoSdk.getInstance(appContext)
assertEquals(instance.settings.accountPreferences.fileName, DefaultXyoSdkSettings().accountPreferences.fileName)
assertEquals(instance.settings.accountPreferences.storagePath, DefaultXyoSdkSettings().accountPreferences.storagePath)
}
}

@Test
fun testCustomSettings() {
runBlocking {
class UpdatedAccountPreferences : AccountPreferences {
override val fileName = "network-xyo-sdk-prefs-1"
override val storagePath = "__xyo-client-sdk-1__"
}
class UpdatedSettings: DefaultXyoSdkSettings() {
override val accountPreferences = UpdatedAccountPreferences()
}
val updatedSettings = UpdatedSettings()
val instance = XyoSdk.getInstance(appContext, updatedSettings)
assertEquals(instance.settings.accountPreferences.fileName, updatedSettings.accountPreferences.fileName)
assertEquals(instance.settings.accountPreferences.storagePath, updatedSettings.accountPreferences.storagePath)
}
}
}
34 changes: 34 additions & 0 deletions sdk/src/main/java/network/xyo/client/settings/XyoSdk.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package network.xyo.client.settings

import android.content.Context
import network.xyo.client.datastore.XyoAccountPrefsRepository

class XyoSdk(val settings: SettingsInterface) {
companion object {
@Volatile
private var INSTANCE: XyoSdk? = null

fun getInstance(context: Context, settings: SettingsInterface = DefaultXyoSdkSettings()): XyoSdk {
// Initialize the singleton with the users accountPreferences
XyoAccountPrefsRepository.getInstance(context, settings.accountPreferences)

val newInstance = INSTANCE ?: synchronized(this) {
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
XyoAccountPrefsRepository.getInstance(context, settings.accountPreferences)
INSTANCE = XyoSdk(settings)
}
return INSTANCE!!
}

fun resetInstance() {
INSTANCE = null
}
}
}

0 comments on commit f535aba

Please sign in to comment.