Last active
August 24, 2019 09:03
-
-
Save PetkevichPavel/6896f3d78f7183ee0fdf6b67fb6324fd to your computer and use it in GitHub Desktop.
AN_Cloud Function: Helpful class with certain functions for remote config.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class RemoteConfigFunctions(private val gson: Gson, private val context: Context, | |
| private val app: App?, | |
| private val remoteConfigManager: RemoteConfigManager) { | |
| companion object { | |
| private const val TAG = "RemoteConfigFunctions:" | |
| private const val RECEIVER_TAG = "FCM Receiver:" | |
| } | |
| /** | |
| * This function fetch actual data from FRC file, if something went | |
| * wrong it will be set as a default RC file. | |
| */ | |
| fun fetchRemoteConfig(update: ((isItServiceUpdate: Boolean, | |
| isFeatureOnBoardingVisible: Boolean) -> Unit)? = null) { | |
| app?.firebaseRemoteConfig?.let { fbConf -> | |
| fbConf.fetchData()?.task?.addOnCompleteListener { task -> | |
| task.takeIf { it.isSuccessful }?.apply { | |
| (result as? RCModel)?.let { rcModel -> | |
| //here is invoke for overriding functionality in the open function handleRcUpdate. | |
| update?.invoke(rcModel.isItServiceUpdate, | |
| rcModel.isFeatureOnBoardingVisible) | |
| } | |
| } ?: setDefaultRC() | |
| } ?: setDefaultRC() | |
| } ?: setDefaultRC() | |
| } | |
| private fun FirebaseRemoteConfig.fetchData(): TaskCompletionSource<Any?>? { | |
| val source: TaskCompletionSource<Any?> = TaskCompletionSource() | |
| fetchAndActivate().addOnCompleteListener { task -> | |
| task.isSuccessful.takeIf { it }?.apply { | |
| gson.fromJson(getString(RemoteConfigConstants.RC_JSON_PARAM_NAME), | |
| RCModel::class.java).let { rcModel -> | |
| remoteConfigManager.updateRc(rcModel) | |
| remoteConfigManager.updateFetchState(false, currentDateTimestamp) | |
| source.setResult(rcModel) | |
| } | |
| } ?: source.setResult(null) | |
| } | |
| return source | |
| } | |
| fun setDefaultRC() { | |
| if (remoteConfigManager.lastFetchedTime == null) { | |
| val defaultRC = mutableMapOf<String, Any>() | |
| context.assets.open(RemoteConfigConstants.DEFAULT_LOCAL_RC_JSON).let { st -> | |
| val defaultRCJson = st.convertLocalJsonToString() | |
| defaultRC[RemoteConfigConstants.RC_JSON_PARAM_NAME] = defaultRCJson | |
| st.close() | |
| app?.firebaseRemoteConfig?.setDefaults(defaultRC) | |
| gson.fromJson(defaultRCJson, RCModel::class.java).let { rcModel -> | |
| remoteConfigManager.updateRc(rcModel) | |
| } | |
| remoteConfigManager.updateFetchState(true, currentDateTimestamp) | |
| Timber.tag(TAG).i("Set remote config by default local file.") | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment