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 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
/** | |
* Context extension function which will save the file into the device local storage. | |
* @param data - content of the file. | |
* @param fileName - name of the final file. | |
*/ | |
fun Context.saveFile(data: ByteArray, fileName: String) = | |
openFileOutput(fileName, Context.MODE_PRIVATE)?.apply { | |
write(data) | |
close() | |
} |
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
fetchAndActivate().addOnCompleteListener { task -> | |
task.isSuccessful.takeIf { it }?.apply { | |
context.saveFile(getString(RemoteConfigConstants.RC_JSON_PARAM_NAME).toByteArray(), | |
LOCAL_RC_JSON) | |
} | |
} |
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
fun Context.setDefaultRCFromFile() : Boolean = | |
File(filesDir, LOCAL_RC_JSON).takeIf { it.exists() }?.let {file-> | |
val defaultRC = mutableMapOf<String, Any>() | |
gson.fromJson(file.inputStream().convertLocalJsonToString(), RCModel::class.java)?.let { rcModel -> | |
defaultRC[RemoteConfigConstants.RC_JSON_PARAM_NAME] = file.inputStream().convertLocalJsonToString() | |
remoteConfigManager.updateRc(rcModel) | |
remoteConfigManager.updateFetchState(true, currentDateTimestamp) | |
} | |
app?.firebaseRemoteConfig?.setDefaults(defaultRC) | |
file.inputStream().close() |
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
open class PreferencesDelegate<T>(private val key: String) { | |
private var variable: Any? = null | |
/** | |
* Performing operation on synchronized thread. | |
* @param thisRef - is the reference to the class that contains the property | |
* @param property - is an instance of the [KProperty] class, which contains metadata. | |
* @return T - desired value by a given key or null. | |
*/ | |
open operator fun getValue(thisRef: Any?, property: KProperty<*>): T? { |
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
//SomeManager.kt | |
interface SomeManager { | |
var userId: Long? | |
var userName: String? | |
/** | |
* Returns true if user valid. | |
*/ | |
fun isUserValid(): Boolean |
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
open class PreferencesDelegate<T>(private val key: String) { | |
private var variable: Any? = null | |
private var initialized = false | |
/** | |
* Return desired value by a given key. Performing operation on synchronized thread. | |
* @param thisRef - is the reference to the class that contains the property | |
* @param property - is an instance of the [KProperty] class, which contains metadata. | |
* @return T - T or null. |
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
open class PreferencesDelegate<T>(private val key: String) { | |
private var variable: Any? = null | |
private var initialized = false | |
/** | |
* Saves given value to SharedPreferences on synchronized thread. | |
* @param thisRef - is the reference to the class that contains the property | |
* @param property - is an instance of the [KProperty] class, which contains metadata. | |
*/ | |
open operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { |
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
/** | |
* SharedPreferences.Editor extension function for putting [value] as Any into Shared preferences. | |
* @param key - key for the value. | |
* @param value - value as Any. | |
*/ | |
@Suppress("UNCHECKED_CAST") | |
fun SharedPreferences.Editor.putAny(key: String, value: Any) { | |
when { | |
Generic<String>().checkType(value) -> (value as? String)?.let { putString(key, it) } | |
Generic<Boolean>().checkType(value) -> (value as? Boolean)?.let { putBoolean(key, it) } |