-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from team-JMT/feat/all_search
All Search 식당 데이터 작업
- Loading branch information
Showing
50 changed files
with
2,067 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.gdsc.data | ||
|
||
import android.content.Context | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.edit | ||
import androidx.datastore.preferences.core.stringPreferencesKey | ||
import androidx.datastore.preferences.preferencesDataStore | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.flow.map | ||
import javax.inject.Inject | ||
|
||
val Context.historyDataStore: DataStore<Preferences> by preferencesDataStore(name = "history") | ||
|
||
class LocalHistoryDataStore @Inject constructor(private val context: Context) { | ||
private val searchedKeywordKey = stringPreferencesKey("accessToken") | ||
|
||
suspend fun updateSearchedKeyword(newKeyword: String): List<String> { | ||
val previous = getSearchedKeywordToken()?.toConvertedList() ?: emptyList() | ||
val newHistories = (previous + newKeyword).distinct().toConvertedString() | ||
updateSearchedKeywordToken(newHistories) | ||
return newHistories.toConvertedList() | ||
} | ||
|
||
suspend fun getSearchedKeyword() = getSearchedKeywordToken()?.toConvertedList() ?: emptyList() | ||
|
||
suspend fun deleteSearchedKeyword(targetKeyword: String): List<String> { | ||
val previous = getSearchedKeywordToken()?.toConvertedList() ?: emptyList() | ||
val newHistories = previous.filter { it != targetKeyword }.toConvertedString() | ||
updateSearchedKeywordToken(newHistories) | ||
return newHistories.toConvertedList() | ||
} | ||
|
||
suspend fun initSearchedKeyword(): List<String> { | ||
updateSearchedKeywordToken(emptyList<String>().toConvertedString()) | ||
return emptyList() | ||
} | ||
|
||
private suspend fun getSearchedKeywordToken() = context.historyDataStore.data | ||
.map { preferences -> | ||
preferences[searchedKeywordKey] | ||
}.first() | ||
|
||
private suspend fun updateSearchedKeywordToken(newKeywords: String) { | ||
context.historyDataStore.edit { preferences -> | ||
preferences[searchedKeywordKey] = newKeywords | ||
} | ||
} | ||
|
||
private fun List<String>.toConvertedString(): String { | ||
return this.toString().replace("[", "").replace("]", "") | ||
} | ||
|
||
private fun String.toConvertedList(): List<String> { | ||
return this.split(",").map { it.trim() } | ||
} | ||
|
||
} |
10 changes: 3 additions & 7 deletions
10
data/src/main/java/org/gdsc/data/database/RestaurantByMapPagingSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
data/src/main/java/org/gdsc/data/database/RestaurantBySearchPagingSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.gdsc.data.database | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import org.gdsc.data.model.RegisteredRestaurantResponse | ||
import org.gdsc.data.network.RestaurantAPI | ||
import org.gdsc.domain.model.request.RestaurantSearchRequest | ||
|
||
class RestaurantBySearchPagingSource( | ||
private val api: RestaurantAPI, | ||
private val restaurantSearchRequest: RestaurantSearchRequest, | ||
): PagingSource<Int, RegisteredRestaurantResponse>() { | ||
override fun getRefreshKey(state: PagingState<Int, RegisteredRestaurantResponse>): Int? { | ||
return state.anchorPosition?.let { anchorPosition -> | ||
val anchorPage = state.closestPageToPosition(anchorPosition) | ||
anchorPage?.prevKey?.plus(1) ?: anchorPage?.nextKey?.minus(1) | ||
} | ||
} | ||
|
||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, RegisteredRestaurantResponse> { | ||
val page = params.key ?: 1 | ||
return try { | ||
val items = api.getRegisteredRestaurantsBySearch( | ||
restaurantSearchRequest = restaurantSearchRequest | ||
) | ||
LoadResult.Page( | ||
data = items.data.restaurants, | ||
prevKey = null, | ||
nextKey = if (items.data.restaurants.isEmpty()) null else page + 1 | ||
) | ||
} catch (e: Exception) { | ||
return LoadResult.Error(e) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.