package com.laseroptek.raman.utils

import android.content.Context
import android.content.SharedPreferences

object SharedPreferenceHelper {
    // Define a name for your preference file
    private const val PREFS_FILENAME = "com.laseroptek.raman.prefs"

    /**
     * Saves a string value to SharedPreferences.
     *
     * @param context The application context.
     * @param key The name of the preference to modify.
     * @param value The new value for the preference.
     */
    fun saveString(context: Context, key: String, value: String) {
        val sharedPreferences: SharedPreferences = context.getSharedPreferences(PREFS_FILENAME, Context.MODE_PRIVATE)
        val editor: SharedPreferences.Editor = sharedPreferences.edit()
        editor.putString(key, value)
        editor.apply() // Asynchronously saves the changes. Use commit() for synchronous save.
    }

    /**
     * Retrieves a string value from SharedPreferences.
     *
     * @param context The application context.
     * @param key The name of the preference to retrieve.
     * @param defaultValue The value to return if this preference does not exist.
     * @return The preference value if it exists, or defaultValue.
     */
    fun getString(context: Context, key: String, defaultValue: String? = null): String? {
        val sharedPreferences: SharedPreferences = context.getSharedPreferences(PREFS_FILENAME, Context.MODE_PRIVATE)
        return sharedPreferences.getString(key, defaultValue)
    }
}