|
| 1 | +package com.terning.core.firebase.remoteconfig |
| 2 | + |
| 3 | +import com.google.firebase.Firebase |
| 4 | +import com.google.firebase.remoteconfig.FirebaseRemoteConfig |
| 5 | +import com.google.firebase.remoteconfig.remoteConfig |
| 6 | +import com.google.firebase.remoteconfig.remoteConfigSettings |
| 7 | +import com.terning.core.firebase.remoteconfig.RemoteConfigKey.LATEST_APP_VERSION |
| 8 | +import com.terning.core.firebase.remoteconfig.RemoteConfigKey.MAJOR_UPDATE_BODY |
| 9 | +import com.terning.core.firebase.remoteconfig.RemoteConfigKey.MAJOR_UPDATE_TITLE |
| 10 | +import timber.log.Timber |
| 11 | + |
| 12 | +object RemoteConfigUtil { |
| 13 | + private const val TAG = "FirebaseRemoteConfig" |
| 14 | + |
| 15 | + private val remoteConfig: FirebaseRemoteConfig by lazy { |
| 16 | + val configSettings = remoteConfigSettings { |
| 17 | + minimumFetchIntervalInSeconds = 3600 |
| 18 | + } |
| 19 | + Firebase.remoteConfig.apply { |
| 20 | + setConfigSettingsAsync(configSettings) |
| 21 | + setDefaultsAsync( |
| 22 | + mapOf( |
| 23 | + LATEST_APP_VERSION.key to "", |
| 24 | + MAJOR_UPDATE_BODY.key to "", |
| 25 | + MAJOR_UPDATE_TITLE.key to "", |
| 26 | + ) |
| 27 | + ) |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + fun onVersionFetchCompleteListener(callback: (Map<RemoteConfigKey, String>) -> Unit) { |
| 32 | + try { |
| 33 | + val fetchTask = remoteConfig.fetchAndActivate() |
| 34 | + fetchTask.addOnCompleteListener { task -> |
| 35 | + if (task.isSuccessful) { |
| 36 | + val map = mutableMapOf( |
| 37 | + getConfigKeyToStringPair(LATEST_APP_VERSION), |
| 38 | + getConfigKeyToStringPair(MAJOR_UPDATE_TITLE), |
| 39 | + getConfigKeyToStringPair(MAJOR_UPDATE_BODY), |
| 40 | + ) |
| 41 | + callback(map) |
| 42 | + } |
| 43 | + } |
| 44 | + } catch (e: Exception) { |
| 45 | + Timber.tag(TAG).e(e.printStackTrace().toString()) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + private fun getConfigKeyToStringPair(configKey: RemoteConfigKey): Pair<RemoteConfigKey, String> = |
| 51 | + configKey to remoteConfig.getString(configKey.key) |
| 52 | +} |
| 53 | + |
| 54 | + |
0 commit comments