Skip to content

Commit 0c12f2b

Browse files
author
Ahmad Idrees
committed
remote config fixes
1 parent a003653 commit 0c12f2b

File tree

5 files changed

+40
-28
lines changed

5 files changed

+40
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var remoteAdSettings = RemoteModel()
4040
```
4141
Make your Custom model the way you want to receive model, call getRemoteConfig() to get data:
4242
```kotlin
43-
remoteConfig.getRemoteConfig {
43+
remoteConfig.getRemoteConfig(context) {
4444
it?.let {
4545
val remoteJson = Gson().toJson(it)
4646
if (!remoteJson.isNullOrEmpty())

app/src/main/java/ai/bom/firebase/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class MainActivity : AppCompatActivity() {
2727
//Send FireBase Analytic Event
2828
sendFbEvent("MainActivity", "Index Screen Open")
2929

30-
remoteConfig.getRemoteConfig {
30+
remoteConfig.getRemoteConfig(this) {
3131
it?.let {
3232
val remoteJson = Gson().toJson(it)
3333
if (!remoteJson.isNullOrEmpty())

firebaseLib/build.gradle

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,6 @@ android {
1313
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1414
consumerProguardFiles "consumer-rules.pro"
1515
}
16-
17-
buildTypes {
18-
debug {
19-
minifyEnabled true
20-
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
21-
}
22-
23-
release {
24-
minifyEnabled true
25-
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
26-
}
27-
}
28-
compileOptions {
29-
sourceCompatibility JavaVersion.VERSION_1_8
30-
targetCompatibility JavaVersion.VERSION_1_8
31-
}
32-
kotlinOptions {
33-
jvmTarget = '1.8'
34-
}
3516
}
3617

3718
dependencies {

firebaseLib/src/main/java/ai/bom/firebase/lib/config/RemoteConfigData.kt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ai.bom.firebase.lib.config
22

33
import ai.bom.firebase.lib.BuildConfig
4+
import android.content.Context
45
import androidx.annotation.Keep
56
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
67
import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings
@@ -26,19 +27,24 @@ class RemoteConfigDate(private val remoteTopic: String) {
2627
return remoteConfig
2728
}
2829

29-
private fun getRemoteConfig(): Any? {
30-
return Gson().fromJson(
31-
getInstance()?.getString(remoteTopic),
32-
Any::class.java
33-
)
30+
private fun getRemoteConfig(context: Context): Any? {
31+
val pref = SharedPref(context)
32+
var json = getInstance()?.getString(remoteTopic)
33+
34+
if (json.isNullOrEmpty() || json == "{}") {
35+
json = pref.getRemoteString()
36+
}
37+
pref.putRemoteString(json)
38+
39+
return Gson().fromJson(json, Any::class.java)
3440
}
3541

36-
fun getRemoteConfig(listener: ((Any?) -> Unit)) {
42+
fun getRemoteConfig(context: Context, listener: ((Any?) -> Unit)) {
3743
getInstance()?.reset()
3844
getInstance()?.fetchAndActivate()
3945
?.addOnCompleteListener { task ->
4046
if (task.isSuccessful) {
41-
val value = getRemoteConfig()
47+
val value = getRemoteConfig(context)
4248
listener.invoke(value)
4349
} else {
4450
listener.invoke(null)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package ai.bom.firebase.lib.config
2+
3+
4+
import android.content.Context
5+
import android.content.SharedPreferences
6+
7+
class SharedPref(private val context: Context) {
8+
9+
private val strRemote = "remote"
10+
private val strPref = "pref"
11+
12+
fun putRemoteString(value: String) {
13+
val sharedPref: SharedPreferences =
14+
context.getSharedPreferences(strPref, Context.MODE_PRIVATE)
15+
val editor = sharedPref.edit()
16+
editor.putString(strRemote, value)
17+
editor.apply()
18+
}
19+
20+
fun getRemoteString(): String {
21+
val sharedPref: SharedPreferences =
22+
context.getSharedPreferences(strPref, Context.MODE_PRIVATE)
23+
return sharedPref.getString(strRemote, "{}") ?: "{}"
24+
}
25+
}

0 commit comments

Comments
 (0)