package de.tsschulz.tt_tagebuch.shared.state

import android.content.Context
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class AndroidLanguageStorage(
    context: Context,
) : LanguageStorage {
    private val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)

    override suspend fun loadLanguageCode(): String? = withContext(Dispatchers.Default) {
        prefs.getString(KEY_LANGUAGE_CODE, null)
    }

    override suspend fun saveLanguageCode(languageCode: String) = withContext(Dispatchers.Default) {
        prefs.edit().putString(KEY_LANGUAGE_CODE, languageCode).apply()
    }

    private companion object {
        private const val PREFS_NAME = "tttagebuch_language"
        private const val KEY_LANGUAGE_CODE = "languageCode"
    }
}
