Skip to content

Commit

Permalink
CachingAndroid#consumeContext() method was added
Browse files Browse the repository at this point in the history
  • Loading branch information
Bohdan-Kim committed May 15, 2024
1 parent 85399b5 commit 1597f65
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 40 deletions.
3 changes: 1 addition & 2 deletions GrowthBook/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ kotlin {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13.2")
implementation("org.mockito:mockito-core:4.8.0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test")
implementation("io.ktor:ktor-client-mock:$ktorVersion")
implementation("com.soywiz.korlibs.krypto:krypto-android:$kryptoVersion")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import com.sdk.growthbook.sandbox.CachingAndroid
* Android App Context Provider
* KotlinX Implementation to simplify SDK Initialization
*/
class AppContextProvider : Initializer<Context> {
internal class AppContextProvider : Initializer<Context> {
override fun create(context: Context) : Context {
CachingAndroid.context = context
CachingAndroid.consumeContext(context)
return context
}
override fun dependencies(): List<Class<out Initializer<*>>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import java.io.FileInputStream
/**
* Actual Implementation for Caching in Android - As expected in KMM
*/
actual internal object CachingImpl {
internal actual object CachingImpl {
actual fun getLayer(): CachingLayer {
return CachingAndroid()
}
Expand All @@ -18,16 +18,12 @@ actual internal object CachingImpl {
/**
* Android Caching Layer
*/
internal class CachingAndroid : CachingLayer {

companion object {
var context: Context? = null
}
class CachingAndroid: CachingLayer {

/**
* JSON Parser SetUp
*/
val json = Json { prettyPrint = true; isLenient = true; ignoreUnknownKeys = true }
private val json = Json { prettyPrint = true; isLenient = true; ignoreUnknownKeys = true }

/**
* Save Content in Android App Specific Internal Memory
Expand Down Expand Up @@ -73,16 +69,28 @@ internal class CachingAndroid : CachingLayer {
* Get Target File - with complete path in internal memory
*/
fun getTargetFile(fileName: String): File? {
if (context != null) {
val path = context!!.getFilesDir()
// Create Directory in Internal Memory
val letDirectory = File(path, "GrowthBook-KMM")
letDirectory.mkdirs()
var targetFileName = fileName
if (fileName.endsWith(".txt", true)) {
targetFileName = fileName.removeSuffix(".txt")
}
return File(letDirectory, targetFileName + ".txt")
} else return null
if (filesDir == null) {
return null
}

// Create Directory in Internal Memory
val letDirectory = File(filesDir, "GrowthBook-KMM")
letDirectory.mkdirs()
var targetFileName = fileName
if (fileName.endsWith(".txt", true)) {
targetFileName = fileName.removeSuffix(".txt")
}
return File(letDirectory, "$targetFileName.txt")
}

companion object {
internal var filesDir: File? = null

/**
* Retrieves filesDir from context
*/
fun consumeContext(context: Context) {
filesDir = context.filesDir
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package com.sdk.growthbook

import android.content.Context
import com.sdk.growthbook.sandbox.CachingAndroid
import com.sdk.growthbook.sandbox.CachingImpl
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.jsonPrimitive
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import kotlin.test.assertTrue
import org.mockito.Mock
import org.mockito.Mockito.`when`
import org.mockito.MockitoAnnotations
import kotlin.test.BeforeTest
import kotlin.test.Test

Expand All @@ -19,26 +15,15 @@ class AndroidCachingTest {
@Rule @JvmField
var mTempFolder = TemporaryFolder()

@Mock
private val mMockContext: Context? = null

@BeforeTest
fun setUp() {
MockitoAnnotations.openMocks(this)

`when`(mMockContext!!.filesDir).thenReturn(mTempFolder.newFolder())

CachingAndroid.context = mMockContext
CachingAndroid.filesDir = mTempFolder.newFolder()
}


@Test
fun testActualLayer() {

val cachingMgr = CachingImpl

assertTrue(cachingMgr.getLayer() is CachingAndroid)

}

@Test
Expand All @@ -65,5 +50,4 @@ class AndroidCachingTest {
assertTrue(fileContents != null)
assertTrue(fileContents.jsonPrimitive.content == "GrowthBook")
}

}
}

0 comments on commit 1597f65

Please sign in to comment.