Skip to content

Commit

Permalink
remote config fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmad Idrees committed Aug 17, 2022
1 parent a003653 commit 0c12f2b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var remoteAdSettings = RemoteModel()
```
Make your Custom model the way you want to receive model, call getRemoteConfig() to get data:
```kotlin
remoteConfig.getRemoteConfig {
remoteConfig.getRemoteConfig(context) {
it?.let {
val remoteJson = Gson().toJson(it)
if (!remoteJson.isNullOrEmpty())
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/ai/bom/firebase/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class MainActivity : AppCompatActivity() {
//Send FireBase Analytic Event
sendFbEvent("MainActivity", "Index Screen Open")

remoteConfig.getRemoteConfig {
remoteConfig.getRemoteConfig(this) {
it?.let {
val remoteJson = Gson().toJson(it)
if (!remoteJson.isNullOrEmpty())
Expand Down
19 changes: 0 additions & 19 deletions firebaseLib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,6 @@ android {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}

buildTypes {
debug {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}

release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ai.bom.firebase.lib.config

import ai.bom.firebase.lib.BuildConfig
import android.content.Context
import androidx.annotation.Keep
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings
Expand All @@ -26,19 +27,24 @@ class RemoteConfigDate(private val remoteTopic: String) {
return remoteConfig
}

private fun getRemoteConfig(): Any? {
return Gson().fromJson(
getInstance()?.getString(remoteTopic),
Any::class.java
)
private fun getRemoteConfig(context: Context): Any? {
val pref = SharedPref(context)
var json = getInstance()?.getString(remoteTopic)

if (json.isNullOrEmpty() || json == "{}") {
json = pref.getRemoteString()
}
pref.putRemoteString(json)

return Gson().fromJson(json, Any::class.java)
}

fun getRemoteConfig(listener: ((Any?) -> Unit)) {
fun getRemoteConfig(context: Context, listener: ((Any?) -> Unit)) {
getInstance()?.reset()
getInstance()?.fetchAndActivate()
?.addOnCompleteListener { task ->
if (task.isSuccessful) {
val value = getRemoteConfig()
val value = getRemoteConfig(context)
listener.invoke(value)
} else {
listener.invoke(null)
Expand Down
25 changes: 25 additions & 0 deletions firebaseLib/src/main/java/ai/bom/firebase/lib/config/SharedPref.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ai.bom.firebase.lib.config


import android.content.Context
import android.content.SharedPreferences

class SharedPref(private val context: Context) {

private val strRemote = "remote"
private val strPref = "pref"

fun putRemoteString(value: String) {
val sharedPref: SharedPreferences =
context.getSharedPreferences(strPref, Context.MODE_PRIVATE)
val editor = sharedPref.edit()
editor.putString(strRemote, value)
editor.apply()
}

fun getRemoteString(): String {
val sharedPref: SharedPreferences =
context.getSharedPreferences(strPref, Context.MODE_PRIVATE)
return sharedPref.getString(strRemote, "{}") ?: "{}"
}
}

0 comments on commit 0c12f2b

Please sign in to comment.