package com.common.commonlib.utils

import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences

/**
 * sharedPreference管理类
 *
 * @author wangym
 * @since 2021/7/28
 */
object SpManager {
    const val HAS_INIT = "has_init"
    private var sp: SharedPreferences? = null
    private var editor: SharedPreferences.Editor? = null

    @SuppressLint("CommitPrefEdits")
    fun init(context: Context, name: String, mod: Int) {
        sp = context.getSharedPreferences(name, mod)
        editor = sp!!.edit()
    }

    fun putBoolean(key: String, boolean: Boolean) {
        editor!!.putBoolean(key, boolean)
        editor!!.apply()
    }

    fun getBoolean(key: String): Boolean {
        return sp!!.getBoolean(key, false)
    }

    fun putInt(key: String, value: Int) {
        editor!!.putInt(key, value)
        editor!!.apply()
    }

    fun getInt(key: String): Int {
        return sp!!.getInt(key, -1)
    }

    fun putString(key: String, value: String) {
        editor!!.putString(key, value)
        editor!!.apply()
    }

    fun getString(key: String): String? {
        return sp!!.getString(key, "")
    }

    fun putLong(key: String, value: Long) {
        editor!!.putLong(key, value)
        editor!!.apply()
    }

    fun getLong(key: String): Long {
        return sp!!.getLong(key, -1L)
    }
}