-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#91 검색 목록 저장/로드/삭제 하는 로직 제작, SearchHistoryDao, SearchHistoryDto, Sear…
…chHistoryRepository, RoomDb, RoomModule등 Hilt로 의존성 주입, Room 라이브러리 의존성 추가, :core:database 모듈 추가
- Loading branch information
Showing
26 changed files
with
418 additions
and
69 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
76 changes: 76 additions & 0 deletions
76
core/common/src/main/java/com/android/mediproject/core/common/adapter/GodBindListAdapter.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,76 @@ | ||
package com.android.mediproject.core.common.adapter | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.annotation.IdRes | ||
import androidx.databinding.DataBindingUtil | ||
import androidx.databinding.ViewDataBinding | ||
import androidx.recyclerview.widget.DiffUtil | ||
import androidx.recyclerview.widget.ListAdapter | ||
import androidx.recyclerview.widget.RecyclerView | ||
import androidx.recyclerview.widget.RecyclerView.ViewHolder | ||
|
||
/** | ||
* 귀찮음 줄여주는 ListAdapter | ||
* | ||
* @param T : ListAdapter에 들어갈 아이템 타입 | ||
* @param V : ViewDataBinding | ||
* @param differ : DiffUtil.ItemCallback<T> | ||
* - ListAdapter의 DiffUtil.ItemCallback | ||
* - 아이템이 같은지 비교하는 메소드를 구현해야함 | ||
* @param initViewHolder : (((CViewHolder<T, V>) -> Unit) | ||
* - ViewHolder 초기화 | ||
* - ex) { holder -> ViewHolder class init 시 필요한 작업 처리 } | ||
* @param onBind : (CViewHolder<T, V>, T, Int) -> Unit | ||
* - ViewHolder 바인딩 | ||
* - ex) { holder, item, position -> 필요한 작업 처리 } | ||
* @param itemViewId : Int | ||
* - ViewHolder의 itemView id | ||
* - ex) R.layout.item_view | ||
* | ||
*/ | ||
abstract class GodBindListAdapter<T, V : ViewDataBinding>( | ||
differ: DiffUtil.ItemCallback<T>, | ||
private val initViewHolder: ((GodBindViewHolder<T, V>) -> Unit)?, | ||
private val onBind: (GodBindViewHolder<T, V>, T, Int) -> Unit, | ||
@IdRes private val itemViewId: Int | ||
) : ListAdapter<T, GodBindViewHolder<T, V>>(differ) { | ||
|
||
private var _inflater: LayoutInflater? = null | ||
private val inflater get() = _inflater!! | ||
|
||
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) { | ||
super.onAttachedToRecyclerView(recyclerView) | ||
_inflater = null | ||
_inflater = LayoutInflater.from(recyclerView.context) | ||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GodBindViewHolder<T, V> = | ||
GodBindViewHolder(DataBindingUtil.inflate<V>(inflater, itemViewId, parent, false), initViewHolder, onBind) | ||
|
||
|
||
override fun onBindViewHolder(holder: GodBindViewHolder<T, V>, position: Int) { | ||
holder.bind(getItem(position)) | ||
} | ||
|
||
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) { | ||
super.onDetachedFromRecyclerView(recyclerView) | ||
_inflater = null | ||
} | ||
} | ||
|
||
class GodBindViewHolder<T, V : ViewDataBinding>( | ||
private val binding: V, | ||
private val initViewHolder: ((GodBindViewHolder<T, V>) -> Unit)?, | ||
private val onBind: (GodBindViewHolder<T, V>, T, Int) -> Unit, | ||
) : ViewHolder(binding.root) { | ||
|
||
init { | ||
initViewHolder?.invoke(this) | ||
} | ||
|
||
fun bind(item: T) { | ||
onBind.invoke(this, item, absoluteAdapterPosition) | ||
binding.executePendingBindings() | ||
} | ||
} |
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
14 changes: 14 additions & 0 deletions
14
core/data/src/main/java/com/android/mediproject/core/data/search/SearchHistoryRepository.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,14 @@ | ||
package com.android.mediproject.core.data.search | ||
|
||
import com.android.mediproject.core.database.searchhistory.SearchHistoryDto | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface SearchHistoryRepository { | ||
suspend fun insertSearchHistory(query: String) | ||
|
||
suspend fun getSearchHistoryList(count: Int = 5): Flow<List<SearchHistoryDto>> | ||
|
||
suspend fun deleteSearchHistory(id: Long) | ||
|
||
suspend fun deleteAllSearchHistory() | ||
} |
44 changes: 44 additions & 0 deletions
44
...ata/src/main/java/com/android/mediproject/core/data/search/SearchHistoryRepositoryImpl.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,44 @@ | ||
package com.android.mediproject.core.data.search | ||
|
||
import com.android.mediproject.core.common.network.Dispatcher | ||
import com.android.mediproject.core.common.network.MediDispatchers | ||
import com.android.mediproject.core.database.searchhistory.SearchHistoryDao | ||
import com.android.mediproject.core.database.searchhistory.SearchHistoryDto | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.withContext | ||
import java.time.LocalDateTime | ||
import java.time.format.DateTimeFormatter | ||
import javax.inject.Inject | ||
|
||
class SearchHistoryRepositoryImpl @Inject constructor( | ||
private val searchHistoryDao: SearchHistoryDao, @Dispatcher(MediDispatchers.IO) private val ioDispatcher: CoroutineDispatcher | ||
) : SearchHistoryRepository { | ||
override suspend fun insertSearchHistory(query: String) { | ||
withContext(ioDispatcher) { | ||
searchHistoryDao.insert( | ||
SearchHistoryDto( | ||
query = query, id = 0L, searchDateTime = LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME) | ||
) | ||
) | ||
} | ||
} | ||
|
||
override suspend fun getSearchHistoryList(count: Int): Flow<List<SearchHistoryDto>> = withContext(ioDispatcher) { | ||
searchHistoryDao.select(count) | ||
} | ||
|
||
|
||
override suspend fun deleteSearchHistory(id: Long) { | ||
withContext(ioDispatcher) { | ||
searchHistoryDao.delete(id) | ||
} | ||
} | ||
|
||
override suspend fun deleteAllSearchHistory() { | ||
withContext(ioDispatcher) { | ||
searchHistoryDao.deleteAll() | ||
} | ||
} | ||
|
||
} |
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,21 @@ | ||
plugins { | ||
id("mediproject.android.library") | ||
id("mediproject.android.hilt") | ||
alias(libs.plugins.ksp) | ||
} | ||
|
||
android { | ||
namespace = "com.android.mediproject.core.database" | ||
|
||
buildFeatures { | ||
buildConfig = true | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation(project(":core:common")) | ||
implementation(project(":core:model")) | ||
|
||
implementation(libs.bundles.rooms) | ||
implementation(libs.kotlinx.coroutines.android) | ||
} |
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/android/mediproject/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.android.mediproject.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.android.mediproject.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/android/mediproject/core/database/RoomDb.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.android.mediproject.core.database | ||
|
||
import androidx.room.Database | ||
import androidx.room.RoomDatabase | ||
import com.android.mediproject.core.database.searchhistory.SearchHistoryDao | ||
import com.android.mediproject.core.database.searchhistory.SearchHistoryDto | ||
|
||
|
||
@Database(entities = [SearchHistoryDto::class], version = 1) | ||
abstract class RoomDb : RoomDatabase() { | ||
abstract fun searchHistoryDao(): SearchHistoryDao | ||
} |
30 changes: 30 additions & 0 deletions
30
core/database/src/main/java/com/android/mediproject/core/database/RoomModule.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.android.mediproject.core.database | ||
|
||
import android.content.Context | ||
import androidx.room.Room | ||
import com.android.mediproject.core.database.searchhistory.SearchHistoryDao | ||
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(includes = [DaoModule::class]) | ||
@InstallIn(SingletonComponent::class) | ||
object RoomModule { | ||
|
||
@Provides | ||
@Singleton | ||
fun provideRoomDb(@ApplicationContext context: Context): RoomDb = Room.databaseBuilder( | ||
context, RoomDb::class.java, "medi_database" | ||
).build() | ||
} | ||
|
||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object DaoModule { | ||
@Provides | ||
fun provideSearchHistoryDao(roomDb: RoomDb): SearchHistoryDao = roomDb.searchHistoryDao() | ||
} |
Oops, something went wrong.