-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feat/#178 검색화면 data 레이어 추가
- Loading branch information
Showing
39 changed files
with
933 additions
and
44 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
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
8 changes: 8 additions & 0 deletions
8
core/data/src/main/kotlin/com/withpeace/withpeace/core/data/mapper/SearchKeywordMapper.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,8 @@ | ||
package com.withpeace.withpeace.core.data.mapper | ||
|
||
import com.withpeace.withpeace.core.database.SearchKeywordEntity | ||
import com.withpeace.withpeace.core.domain.model.search.SearchKeyword | ||
|
||
fun SearchKeywordEntity.toDomain(): SearchKeyword { | ||
return SearchKeyword(keyword) | ||
} |
60 changes: 60 additions & 0 deletions
60
...data/src/main/kotlin/com/withpeace/withpeace/core/data/paging/PolicySearchPagingSource.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,60 @@ | ||
package com.withpeace.withpeace.core.data.paging | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import com.skydoves.sandwich.ApiResponse | ||
import com.withpeace.withpeace.core.data.mapper.youthpolicy.toDomain | ||
import com.withpeace.withpeace.core.domain.model.error.CheonghaError | ||
import com.withpeace.withpeace.core.domain.model.error.NoSearchResultException | ||
import com.withpeace.withpeace.core.domain.model.policy.YouthPolicy | ||
import com.withpeace.withpeace.core.domain.repository.UserRepository | ||
import com.withpeace.withpeace.core.network.di.service.YouthPolicyService | ||
|
||
class PolicySearchPagingSource( | ||
private val pageSize: Int, | ||
private val youthPolicyService: YouthPolicyService, | ||
private val keyword: String, | ||
private val onError: suspend (CheonghaError) -> Unit, | ||
private val userRepository: UserRepository, | ||
private val onReceiveTotalCount: suspend (Int) -> Unit, | ||
) : PagingSource<Int, Pair<Int, YouthPolicy>>() { | ||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Pair<Int, YouthPolicy>> { | ||
val pageIndex = params.key ?: 1 | ||
val response = youthPolicyService.search( | ||
keyword = keyword, | ||
pageSize = params.loadSize, | ||
pageIndex = pageIndex, | ||
) | ||
|
||
if (response is ApiResponse.Success) { | ||
val successResponse = (response).data | ||
onReceiveTotalCount(successResponse.data.totalCount) | ||
if (response.data.data.totalCount == 0) { | ||
return LoadResult.Error(NoSearchResultException()) | ||
} | ||
return LoadResult.Page( | ||
data = successResponse.data.policies.map { | ||
Pair( | ||
successResponse.data.totalCount, | ||
it.toDomain(), | ||
) | ||
}, | ||
prevKey = if (pageIndex == STARTING_PAGE_INDEX) null else pageIndex - 1, | ||
nextKey = if (successResponse.data.policies.isEmpty()) null else pageIndex + (params.loadSize / pageSize), | ||
) | ||
} else { | ||
return LoadResult.Error(IllegalStateException("api state error")) | ||
} | ||
} | ||
|
||
override fun getRefreshKey(state: PagingState<Int, Pair<Int, YouthPolicy>>): Int? { // 현재 포지션에서 Refresh pageKey 설정 | ||
return state.anchorPosition?.let { anchorPosition -> | ||
val anchorPage = state.closestPageToPosition(anchorPosition) | ||
anchorPage?.prevKey?.plus(1) ?: anchorPage?.nextKey?.minus(1) | ||
} | ||
} | ||
|
||
companion object { | ||
private const val STARTING_PAGE_INDEX = 1 | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...tlin/com/withpeace/withpeace/core/data/repository/DefaultRecentSearchKeywordRepository.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,32 @@ | ||
package com.withpeace.withpeace.core.data.repository | ||
|
||
import com.withpeace.withpeace.core.data.mapper.toDomain | ||
import com.withpeace.withpeace.core.database.SearchKeywordDao | ||
import com.withpeace.withpeace.core.database.SearchKeywordEntity | ||
import com.withpeace.withpeace.core.domain.model.search.SearchKeyword | ||
import com.withpeace.withpeace.core.domain.repository.RecentSearchKeywordRepository | ||
import javax.inject.Inject | ||
|
||
class DefaultRecentSearchKeywordRepository @Inject constructor( | ||
private val searchKeywordDao: SearchKeywordDao, | ||
) : RecentSearchKeywordRepository { | ||
override suspend fun insertKeyword(keyword: SearchKeyword) { | ||
searchKeywordDao.insertKeyword(SearchKeywordEntity(keyword = keyword.value)) | ||
} | ||
|
||
override suspend fun getAllKeywords(): List<SearchKeyword> { | ||
return searchKeywordDao.getAllKeywords().map { it.toDomain() } | ||
} | ||
|
||
override suspend fun deleteKeyword(keyword: SearchKeyword) { | ||
searchKeywordDao.deleteKeyword( | ||
SearchKeywordEntity( | ||
keyword = keyword.value | ||
), | ||
) | ||
} | ||
|
||
override suspend fun clearAllKeywords() { | ||
searchKeywordDao.clearAllKeywords() | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
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,15 @@ | ||
plugins { | ||
id("com.android.library") | ||
id("convention.android.base") | ||
id("convention.android.hilt") | ||
} | ||
|
||
android { | ||
namespace = "com.withpeace.withpeace.core.database" | ||
} | ||
|
||
dependencies { | ||
implementation(libs.room.ktx) | ||
implementation(libs.room.runtime) | ||
kapt (libs.room.compiler) | ||
} |
Empty file.
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,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
24 changes: 24 additions & 0 deletions
24
...ase/src/androidTest/java/com/withpeace/withpeace/core/database/ExampleInstrumentedTest.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,24 @@ | ||
package com.withpeace.withpeace.core.database | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
|
||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
import org.junit.Assert.* | ||
|
||
/** | ||
* Instrumented test, which will execute on an Android device. | ||
* | ||
* See [testing documentation](http://d.android.com/tools/testing). | ||
*/ | ||
@RunWith(AndroidJUnit4::class) | ||
class ExampleInstrumentedTest { | ||
@Test | ||
fun useAppContext() { | ||
// Context of the app under test. | ||
val appContext = InstrumentationRegistry.getInstrumentation().targetContext | ||
assertEquals("com.withpeace.withpeace.core.database.test", appContext.packageName) | ||
} | ||
} |
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,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
</manifest> |
12 changes: 12 additions & 0 deletions
12
core/database/src/main/java/com/withpeace/withpeace/core/database/CheonghaDatabase.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,12 @@ | ||
package com.withpeace.withpeace.core.database | ||
|
||
import androidx.room.Database | ||
import androidx.room.RoomDatabase | ||
|
||
@Database(entities = [SearchKeywordEntity::class], version = 1, exportSchema = false) | ||
abstract class AppDatabase : RoomDatabase() { | ||
abstract fun searchKeywordDao(): SearchKeywordDao | ||
} | ||
|
||
|
||
|
30 changes: 30 additions & 0 deletions
30
core/database/src/main/java/com/withpeace/withpeace/core/database/DatabaseModule.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,30 @@ | ||
package com.withpeace.withpeace.core.database | ||
|
||
import android.content.Context | ||
import androidx.room.Room | ||
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) | ||
object DatabaseModule { | ||
|
||
@Provides | ||
@Singleton | ||
fun provideDatabase(@ApplicationContext context: Context): AppDatabase { | ||
return Room.databaseBuilder( | ||
context, | ||
AppDatabase::class.java, | ||
"app_database", | ||
).build() | ||
} | ||
|
||
@Provides | ||
fun provideSearchKeywordDao(database: AppDatabase): SearchKeywordDao { | ||
return database.searchKeywordDao() | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
core/database/src/main/java/com/withpeace/withpeace/core/database/SearchKeywordDao.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,26 @@ | ||
package com.withpeace.withpeace.core.database | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Delete | ||
import androidx.room.Insert | ||
import androidx.room.OnConflictStrategy | ||
import androidx.room.Query | ||
|
||
@Dao | ||
interface SearchKeywordDao { | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
suspend fun insertKeyword(keyword: SearchKeywordEntity) | ||
|
||
@Query("SELECT * FROM recent_search_keywords ORDER BY timestamp DESC LIMIT 8") | ||
suspend fun getAllKeywords(): List<SearchKeywordEntity> | ||
|
||
@Delete | ||
suspend fun deleteKeyword(keyword: SearchKeywordEntity) | ||
|
||
@Query("DELETE FROM recent_search_keywords WHERE keyword = :keyword") | ||
suspend fun deleteKeywordByValue(keyword: String) | ||
|
||
@Query("DELETE FROM recent_search_keywords") | ||
suspend fun clearAllKeywords() | ||
} |
10 changes: 10 additions & 0 deletions
10
core/database/src/main/java/com/withpeace/withpeace/core/database/SearchKeywordEntity.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,10 @@ | ||
package com.withpeace.withpeace.core.database | ||
|
||
import androidx.room.Entity | ||
import androidx.room.PrimaryKey | ||
|
||
@Entity(tableName = "recent_search_keywords") | ||
data class SearchKeywordEntity( | ||
@PrimaryKey(autoGenerate = false) val keyword: String, | ||
val timestamp: Long = System.currentTimeMillis(), // 저장 시각 | ||
) |
Oops, something went wrong.