Skip to content

Commit

Permalink
feat: shardPref 에 object 넣기 Util 화
Browse files Browse the repository at this point in the history
  • Loading branch information
SeongHoonC committed May 1, 2024
1 parent a1f5870 commit 3fc1dd0
Showing 1 changed file with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.festago.festago.data.util

import android.content.SharedPreferences
import com.google.gson.GsonBuilder

inline fun <reified T> SharedPreferences.putObject(key: String, value: T?) {
val jsonString = GsonBuilder().create().toJson(value)
edit().putString(key, jsonString).apply()
}

inline fun <reified T> SharedPreferences.getObject(key: String, default: T?): T? {
val value = getString(key, null) ?: return default
return GsonBuilder().create().fromJson(value, T::class.java)
}

0 comments on commit 3fc1dd0

Please sign in to comment.