Skip to content

Instantly share code, notes, and snippets.

View PetkevichPavel's full-sized avatar
💭
🚀 Never Stop 🚀

Pavel Petkevich PetkevichPavel

💭
🚀 Never Stop 🚀
View GitHub Profile
@PetkevichPavel
PetkevichPavel / RemoteConfigFunctions.kt
Last active August 24, 2019 09:03
AN_Cloud Function: Helpful class with certain functions for remote config.
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:"
}
/**
@PetkevichPavel
PetkevichPavel / FileExtension.kt
Created August 24, 2019 08:37
AN_Cloud Function: Class for File extension functions.
/**
* 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()
}
@PetkevichPavel
PetkevichPavel / CodeExample.kt
Last active August 24, 2019 08:38
AN_Cloud Function: Code example.
fetchAndActivate().addOnCompleteListener { task ->
task.isSuccessful.takeIf { it }?.apply {
context.saveFile(getString(RemoteConfigConstants.RC_JSON_PARAM_NAME).toByteArray(),
LOCAL_RC_JSON)
}
}
@PetkevichPavel
PetkevichPavel / CodeExample.kt
Last active August 28, 2019 15:45
AN_Cloud Function: Code example.
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()
@PetkevichPavel
PetkevichPavel / SharedPreferencesProvider.kt
Created May 3, 2020 18:59
SharedPreferences under Delegated Properties
interface SharedPreferencesProvider {
val preferences: SharedPreferences
}
@PetkevichPavel
PetkevichPavel / PreferencesDelegate.kt
Last active May 3, 2020 20:39
SharedPreferences under Delegated properties - PreferencesDelegate without implementation.
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? {
@PetkevichPavel
PetkevichPavel / SomeManagers.kt
Last active May 4, 2020 18:58
SharedPreferences under Delegated properties
//SomeManager.kt
interface SomeManager {
var userId: Long?
var userName: String?
/**
* Returns true if user valid.
*/
fun isUserValid(): Boolean
@PetkevichPavel
PetkevichPavel / PreferencesDelegate.kt
Last active May 3, 2020 20:42
SharedPreferences under Delegated properties - PreferencesDelegate getValue().
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.
@PetkevichPavel
PetkevichPavel / PreferencesDelegate.kt
Created May 3, 2020 21:35
SharedPreferences under Delegated properties - PreferencesDelegate setValue.
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) {
@PetkevichPavel
PetkevichPavel / GeneralExtensions.kt
Last active May 3, 2020 22:02
SharedPreferences under Delegated properties - PreferencesDelegate putAny.
/**
* 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) }