@@ -2,11 +2,14 @@ package daily.dayo.data.datasource.local
22
33import android.content.Context
44import android.content.SharedPreferences
5+ import com.google.gson.Gson
56import daily.dayo.data.util.PreferenceHelper
67import daily.dayo.data.util.PreferenceHelper.get
78import daily.dayo.data.util.PreferenceHelper.set
89import com.google.gson.JsonArray
910import dagger.hilt.android.qualifiers.ApplicationContext
11+ import daily.dayo.domain.model.SearchHistory
12+ import daily.dayo.domain.model.SearchHistoryDetail
1013import daily.dayo.domain.model.User
1114import daily.dayo.domain.model.UserTokens
1215import org.json.JSONArray
@@ -16,6 +19,7 @@ import javax.inject.Singleton
1619@Singleton
1720class SharedManager @Inject constructor(@ApplicationContext context : Context ) {
1821 private val prefs: SharedPreferences = PreferenceHelper .defaultPrefs(context)
22+ private val KEY_SEARCH_HISTORY = " search_history"
1923
2024 fun saveCurrentUser (userInfo : Any? ) = when (userInfo) {
2125 is UserTokens -> {
@@ -58,31 +62,47 @@ class SharedManager @Inject constructor(@ApplicationContext context: Context) {
5862 prefs[" notiNoticePermit" ] = value
5963 }
6064
61- fun setSearchKeywordRecent (searchKeywordRecent : ArrayList <String >) {
62- val jsonArr = JsonArray ()
63- for (i in searchKeywordRecent) {
64- jsonArr.add(i)
65- }
65+ fun saveSearchHistory (searchHistory : SearchHistory ) {
66+ val gson = Gson ()
67+ val json = gson.toJson(searchHistory)
68+ val editor = prefs.edit()
69+ editor.putString(KEY_SEARCH_HISTORY , json)
70+ editor.apply ()
71+ }
6672
67- var result = jsonArr.toString()
68- prefs[" recentSearchKeyword" ] = result
73+ fun getSearchKeywordRecent (): SearchHistory {
74+ val json = prefs.getString(KEY_SEARCH_HISTORY , null )
75+ val gson = Gson ()
76+ return gson.fromJson(json, SearchHistory ::class .java) ? : SearchHistory (0 , mutableListOf ())
6977 }
7078
71- fun getSearchKeywordRecent (): ArrayList <String > {
72- val result = prefs[" recentSearchKeyword" , " " ]
73- val resultArr = ArrayList <String >()
74- val jsonArr: JSONArray = if (result.isEmpty()) {
75- JSONArray ()
76- } else {
77- JSONArray (result)
79+ fun updateSearchHistory (newItem : SearchHistoryDetail ) {
80+ var searchHistory = getSearchKeywordRecent()
81+ // History에 Type 구분을 하지 않음에 따라 OR문으로 처리
82+ val existingItem = searchHistory.data.find {
83+ it.history == newItem.history || it.searchHistoryType == newItem.searchHistoryType
7884 }
7985
80- if (jsonArr.length() != 0 ) {
81- for (i in 0 until jsonArr.length()) {
82- resultArr.add(jsonArr.optString(i))
83- }
86+ if (existingItem != null ) {
87+ searchHistory = searchHistory.copy(
88+ count = searchHistory.count - 1 ,
89+ data = searchHistory.data.filter {
90+ it.history != existingItem.history || it.searchHistoryType != existingItem.searchHistoryType
91+ }.toMutableList()
92+ )
8493 }
85- return resultArr
94+
95+ searchHistory = searchHistory.copy(
96+ count = searchHistory.count + 1 ,
97+ data = listOf (newItem) + searchHistory.data
98+ )
99+ saveSearchHistory(searchHistory)
100+ }
101+
102+ fun clearSearchHistory () {
103+ val editor = prefs.edit()
104+ editor.remove(KEY_SEARCH_HISTORY )
105+ editor.apply ()
86106 }
87107
88108 fun clearPreferences () {
0 commit comments