Aplication Class For Kotlin In Android

Application Class For Kotlin 

import android.annotation.SuppressLintimport android.app.Application
import android.content.Context
import android.content.SharedPreferences

class AppApplication : Application() {

    val KEY = "AppApplication"    override fun onCreate() {
       super.onCreate()

        sharedPreferences = getSharedPreferences(KEY, Context.MODE_PRIVATE)
        sharedPreferencesEditor = sharedPreferences!!.edit()

    }

    companion object {
        val ISUPDATED = "isUpdated"        var sharedPreferences: SharedPreferences? = null        var sharedPreferencesEditor: SharedPreferences.Editor? = null
        fun preferencePutInteger(key: String, value: Int) {
            sharedPreferencesEditor!!.putInt(key, value)
            sharedPreferencesEditor!!.commit()
        }

        fun preferenceGetInteger(key: String, defaultValue: Int): Int {
            return sharedPreferences!!.getInt(key, defaultValue)
        }

        fun preferencePutBoolean(key: String, value: Boolean) {
            sharedPreferencesEditor!!.putBoolean(key, value)
            sharedPreferencesEditor!!.commit()
        }

        fun preferenceGetBoolean(key: String, defaultValue: Boolean): Boolean {
            return sharedPreferences!!.getBoolean(key, defaultValue)
        }

        fun preferencePutString(key: String, value: String) {
            sharedPreferencesEditor!!.putString(key, value)
            sharedPreferencesEditor!!.commit()
        }

        fun preferenceGetString(key: String, defaultValue: String): String? {
            return sharedPreferences!!.getString(key, defaultValue)
        }

        fun preferencePutLong(key: String, value: Long) {
            sharedPreferencesEditor!!.putLong(key, value)
            sharedPreferencesEditor!!.commit()
        }

        fun preferenceGetLong(key: String, defaultValue: Long): Long {
            return sharedPreferences!!.getLong(key, defaultValue)
        }

        fun preferencePutFloat(key: String, value: Float) {
            sharedPreferencesEditor!!.putFloat(key, value)
            sharedPreferencesEditor!!.commit()
        }

        fun preferenceGetFloat(key: String, defaultValue: Float): Float {
            return sharedPreferences!!.getFloat(key, defaultValue)
        }

        fun preferenceRemoveKey(key: String) {
            sharedPreferencesEditor!!.remove(key)
            sharedPreferencesEditor!!.commit()
        }
    }

}

Comments

Popular posts from this blog