custom

 avatar
unknown
java
3 years ago
1.2 kB
6
Indexable
package com.ossovita.hesapkimdenative.db

import android.content.Context
import android.content.SharedPreferences
import com.google.gson.Gson
import com.ossovita.hesapkimdenative.model.auth.AuthResponse
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
class CustomSharedPreferences {

    private lateinit var pref: SharedPreferences;

    @Singleton
    @Provides
    fun provideSharedPreference(@ApplicationContext context: Context): SharedPreferences {
        pref = context.getSharedPreferences("user_pref", Context.MODE_PRIVATE)
        return pref
    }

    fun saveUserCreds(authResponse: AuthResponse) {
        val gson = Gson()
        val json = gson.toJson(authResponse)
        pref.edit().putString("authResponse", json).commit()
    }

    fun getUserCreds(): AuthResponse {
        val gson = Gson()
        val json = pref.getString("authResponse", "")
        val authResponse = gson.fromJson(json, AuthResponse::class.java)
        return authResponse
    }


}
Editor is loading...