package com.pixelized.biblib.repository.apiCache

import android.content.SharedPreferences
import androidx.core.content.edit
import com.google.gson.Gson
import com.pixelized.biblib.network.data.response.BookListResponse
import javax.inject.Inject

class APICacheRepository @Inject constructor(
    private val gson: Gson,
    private val preferences: SharedPreferences,
) : IAPICacheRepository {

    override var new: BookListResponse?
        get() = preferences.new?.let { gson.fromJson(it, BookListResponse::class.java) }
        set(value) = gson.toJson(value).let { preferences.new = it }

    private var SharedPreferences.new: String?
        get() = getString(NEW, null)
        set(value) = edit { putString(NEW, value) }

    companion object {
        const val NEW = "NEW"
    }
}