Advertisement
Martinocom

AppPreferences Android Class

Feb 4th, 2020
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.88 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////////////////
  2. // Kotlin SingletonHolder, you can find it here
  3. // https://medium.com/@BladeCoder/kotlin-singletons-with-argument-194ef06edd9e
  4. //
  5. // If not avaiable this is the code
  6. // I'm using it inside AppPreferences class for creating a singleton that can have arguments
  7. ////////////////////////////////////////////////////////////////////////////////////////////
  8.  
  9. // No imports needed
  10.  
  11. // Class
  12. open class SingletonHolder<out T: Any, in A>(creator: (A) -> T) {
  13.     private var creator: ((A) -> T)? = creator
  14.     @Volatile private var instance: T? = null
  15.  
  16.     fun getInstance(arg: A): T {
  17.         val i = instance
  18.         if (i != null) {
  19.             return i
  20.         }
  21.  
  22.         return synchronized(this) {
  23.             val i2 = instance
  24.             if (i2 != null) {
  25.                 i2
  26.             } else {
  27.                 val created = creator!!(arg)
  28.                 instance = created
  29.                 creator = null
  30.                 created
  31.             }
  32.         }
  33.     }
  34. }
  35.  
  36. ////////////////////////////////////////////////////////////////////////////////////////////
  37. // AppPreferences Code
  38. ////////////////////////////////////////////////////////////////////////////////////////////
  39.  
  40. // Imports
  41. import android.app.Activity
  42. import android.content.Context
  43. import android.content.SharedPreferences
  44.  
  45. // Class
  46. class AppPreferences private constructor(context: Context) {
  47.  
  48.     private val sharedPreferences : SharedPreferences
  49.  
  50.     init {
  51.         sharedPreferences = context.getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE)
  52.     }
  53.  
  54.     companion object : SingletonHolder<AppPreferences, Context>(::AppPreferences) {
  55.         private const val EMPTY_STRING = ""
  56.  
  57.         private const val FIRST_TIME_KEY = "firstTime"
  58.         private const val PASSWORD_SETTINGS_KEY = "passwordSettings"
  59.         private const val BACK_BUTTON_KEY = "backButtonEnabling"
  60.         private const val BRIGHTNESS_CONTROL_KEY = "brightnessControlValue"
  61.  
  62.         private const val APP_SHARED_PREFS = "MyPrefs"
  63.  
  64.         private const val DEFAULT_BRIGHTNESS = 100
  65.     }
  66.  
  67.     /**
  68.      * Get or set a value if the application is starting for the first time ever
  69.      */
  70.     var firstTimeKey: Boolean
  71.         get() = sharedPreferences.getBoolean(FIRST_TIME_KEY, true)
  72.         set(firstTime) = sharedPreferences.edit().putBoolean(FIRST_TIME_KEY, firstTime).apply()
  73.  
  74.     /**
  75.      * Get or set the encrypted password for hidden settings
  76.      */
  77.     var passwordSettingsKey: String?
  78.         get() = sharedPreferences.getString(PASSWORD_SETTINGS_KEY, EMPTY_STRING)
  79.         set(password) = sharedPreferences.edit().putString(PASSWORD_SETTINGS_KEY, password).apply()
  80.  
  81.     /**
  82.      * If true, you can go back on evaluation screens, il false, you cannot modify the scores
  83.      */
  84.     var isBackButtonKeyEnabled: Boolean
  85.         get() = sharedPreferences.getBoolean(BACK_BUTTON_KEY, false)
  86.         set(mode) = sharedPreferences.edit().putBoolean(BACK_BUTTON_KEY, mode).apply()
  87.  
  88.     /**
  89.      * Get or set the brightness value
  90.      */
  91.     var brightnessKey: Int
  92.         get() = sharedPreferences.getInt(BRIGHTNESS_CONTROL_KEY, DEFAULT_BRIGHTNESS)
  93.         set(value) = sharedPreferences.edit().putInt(BRIGHTNESS_CONTROL_KEY, value).apply()
  94.  
  95.     /**
  96.      * Get or set the last Port value
  97.      */
  98.     var isWiFiDialogEnabled: Boolean
  99.         get() = sharedPreferences.getBoolean(SHOW_WIFI_DIALOG, true)
  100.         set(value) = sharedPreferences.edit().putBoolean(SHOW_WIFI_DIALOG, value).apply()
  101.  
  102. }
  103.  
  104. ////////////////////////////////////////////////////////////////////////////////////////////
  105. // Usage
  106. ////////////////////////////////////////////////////////////////////////////////////////////
  107.  
  108. // AppPreferences.getInstance(<context>).<attribute> = <value>
  109.  
  110. // In activity
  111. AppPreferences.getInstance(this).firstTimeKey = false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement