Skip to content

Commit

Permalink
#91 검색 기록 저장/수정하는 SearchHistoryRepository내 로직 최적화, 불필요한 suspend 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
pknujsp committed May 31, 2023
1 parent 652c2c1 commit 5531eed
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import com.android.mediproject.core.model.medicine.medicinedetailinfo.MedicineDe
import kotlinx.coroutines.flow.Flow

interface MedicineApprovalRepository {
suspend fun getMedicineApprovalList(
fun getMedicineApprovalList(
itemName: String?,
entpName: String?,
medicationType: String?,
): Flow<PagingData<Item>>

suspend fun getMedicineDetailInfo(itemName: String): Flow<Result<MedicineDetailInfoResponse.Body.Item>>
fun getMedicineDetailInfo(itemName: String): Flow<Result<MedicineDetailInfoResponse.Body.Item>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import com.android.mediproject.core.network.datasource.medicineapproval.Medicine
import com.android.mediproject.core.network.datasource.medicineapproval.MedicineApprovalListDataSourceImpl
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.channelFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.map
import javax.inject.Inject

Expand All @@ -37,24 +34,19 @@ class MedicineApprovalRepositoryImpl @Inject constructor(
* @param entpName 업체명
*
*/
override suspend fun getMedicineApprovalList(itemName: String?, entpName: String?, medicationType: String?): Flow<PagingData<Item>> =
if (itemName == null && entpName == null) {
emptyFlow()
} else {
searchHistoryRepository.insertSearchHistory(SearchHistoryDto(itemName ?: entpName!!))
Pager(config = PagingConfig(pageSize = DATA_GO_KR_PAGE_SIZE, prefetchDistance = 5), pagingSourceFactory = {
MedicineApprovalListDataSourceImpl(
medicineApprovalDataSource, itemName, entpName, medicationType
)
}).flow
}
override fun getMedicineApprovalList(itemName: String?, entpName: String?, medicationType: String?): Flow<PagingData<Item>> {
searchHistoryRepository.insertSearchHistory(SearchHistoryDto(itemName ?: entpName!!))
return Pager(config = PagingConfig(pageSize = DATA_GO_KR_PAGE_SIZE, prefetchDistance = 5), pagingSourceFactory = {
MedicineApprovalListDataSourceImpl(
medicineApprovalDataSource, itemName, entpName, medicationType
)
}).flow
}


override suspend fun getMedicineDetailInfo(itemName: String): Flow<Result<MedicineDetailInfoResponse.Body.Item>> = channelFlow {
override fun getMedicineDetailInfo(itemName: String): Flow<Result<MedicineDetailInfoResponse.Body.Item>> =
medicineApprovalDataSource.getMedicineDetailInfo(itemName).map { result ->
result.fold(onSuccess = { Result.success(it.body.items.first()) }, onFailure = { Result.failure(it) })
}.collectLatest {
send(it)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import kotlinx.coroutines.flow.Flow

interface SearchHistoryRepository {
@WorkerThread
suspend fun insertSearchHistory(searchHistoryDto: SearchHistoryDto)
fun insertSearchHistory(searchHistoryDto: SearchHistoryDto)

@WorkerThread

suspend fun getSearchHistoryList(count: Int = 5): Flow<List<SearchHistoryDto>>
fun getSearchHistoryList(count: Int = 5): Flow<List<SearchHistoryDto>>

@WorkerThread

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ import javax.inject.Inject
class SearchHistoryRepositoryImpl @Inject constructor(
private val searchHistoryDao: SearchHistoryDao
) : SearchHistoryRepository {
override suspend fun insertSearchHistory(searchHistoryDto: SearchHistoryDto) = runBlocking {
searchHistoryDao.insert(searchHistoryDto.apply {
override fun insertSearchHistory(searchHistoryDto: SearchHistoryDto) = runBlocking {
searchHistoryDao.insert(searchHistoryDto.apply
{
query = query.trim()
})
}

override fun getSearchHistoryList(count: Int): Flow<List<SearchHistoryDto>> = searchHistoryDao.select(count)

override suspend fun getSearchHistoryList(count: Int): Flow<List<SearchHistoryDto>> = searchHistoryDao.select(count)


override suspend fun deleteSearchHistory(id: Long) = runBlocking {
override suspend fun deleteSearchHistory(id: Long) {
searchHistoryDao.delete(id)
}


override suspend fun deleteAllSearchHistory() = runBlocking {
override suspend fun deleteAllSearchHistory() {
searchHistoryDao.deleteAll()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
package com.android.mediproject.core.domain

import androidx.paging.map
import com.android.mediproject.core.common.network.Dispatcher
import com.android.mediproject.core.common.network.MediDispatchers
import com.android.mediproject.core.data.remote.medicineapproval.MedicineApprovalRepository
import com.android.mediproject.core.model.constants.MedicationType
import com.android.mediproject.core.model.parameters.ApprovalListSearchParameter
import com.android.mediproject.core.model.medicine.medicineapproval.toDto
import com.android.mediproject.core.model.parameters.ApprovalListSearchParameter
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import javax.inject.Inject

class GetMedicineApprovalListUseCase @Inject constructor(
private val medicineApprovalRepository: MedicineApprovalRepository
private val medicineApprovalRepository: MedicineApprovalRepository,
@Dispatcher(MediDispatchers.Default) private val defaultDispatcher: CoroutineDispatcher,
) {

suspend operator fun invoke(
/**
* 약품 목록 조회 UseCase
*/
operator fun invoke(
parameter: ApprovalListSearchParameter
) = medicineApprovalRepository.getMedicineApprovalList(
itemName = parameter.itemName, entpName = parameter.entpName, medicationType = when (parameter.medicationType) {
MedicationType.ALL -> null
else -> parameter.medicationType.description
}
).let { pager ->
pager.map { pagingData ->
pagingData.map { item ->
item.toDto()
}
).map { pagingData ->
pagingData.map { item ->
// 데이터 변환
item.toDto()
}
}
}.flowOn(defaultDispatcher)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.android.mediproject.core.domain
import com.android.mediproject.core.data.search.SearchHistoryRepository
import com.android.mediproject.core.database.searchhistory.SearchHistoryDto
import com.android.mediproject.core.database.searchhistory.toSearchHistoryItemDto
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapLatest
import javax.inject.Inject
import javax.inject.Singleton

Expand All @@ -13,9 +13,9 @@ class SearchHistoryUseCase @Inject constructor(
) {
suspend fun insertSearchHistory(query: String) = searchHistoryRepository.insertSearchHistory(SearchHistoryDto(query))

suspend fun getSearchHistoryList(count: Int) = searchHistoryRepository.getSearchHistoryList(count).map {
it.map {
it.toSearchHistoryItemDto()
fun getSearchHistoryList(count: Int) = searchHistoryRepository.getSearchHistoryList(count).mapLatest {
it.map { dto ->
dto.toSearchHistoryItemDto()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface MedicineApprovalDataSource {
pageNo: Int,
): Result<MedicineApprovalListResponse>

suspend fun getMedicineDetailInfo(
fun getMedicineDetailInfo(
itemName: String,
): Flow<Result<MedicineDetailInfoResponse>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class MedicineApprovalDataSourceImpl @Inject constructor(
Result.failure(it)
})

override suspend fun getMedicineDetailInfo(itemName: String): Flow<Result<MedicineDetailInfoResponse>> = channelFlow {
override fun getMedicineDetailInfo(itemName: String): Flow<Result<MedicineDetailInfoResponse>> = channelFlow {
dataGoKrNetworkApi.getMedicineDetailInfo(itemName = itemName).onResponse().fold(onSuccess = { response ->
response.isSuccess().let {
if (it is DataGoKrResult.isSuccess) Result.success(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.os.Bundle
import android.view.View
import android.view.ViewGroup.LayoutParams
import androidx.core.os.bundleOf
import androidx.core.view.size
import androidx.fragment.app.activityViewModels
import com.android.mediproject.core.model.search.local.SearchHistoryItemDto
import com.android.mediproject.core.ui.base.BaseFragment
Expand Down Expand Up @@ -37,6 +38,9 @@ class RecentSearchListFragment :
viewLifecycleOwner.repeatOnStarted {
fragmentViewModel.searchHistoryList().collectLatest {
// 최근 검색 목록을 가져옵니다.

// 리스트를 비웁니다.
if (binding.searchHistoryList.size > 0) binding.searchHistoryList.removeAllViews()
it.forEach { searchHistoryItemDto ->
addHistoryItemChips(searchHistoryItemDto)
}
Expand All @@ -58,7 +62,6 @@ class RecentSearchListFragment :
}
data = searchHistoryItemDto.query
setChipText(data.toString())

setOnChipClickListener {
onClicked(searchHistoryItemDto.query)
}
Expand All @@ -67,17 +70,12 @@ class RecentSearchListFragment :
}

override fun onDestroyView() {
binding.searchHistoryList.removeAllViews()
super.onDestroyView()
}

private fun onClicked(query: String) {
parentFragmentManager.apply {
setFragmentResult(
ResultKey.RESULT_KEY.name, bundleOf(
ResultKey.WORD.name to query
)
)
setFragmentResult(ResultKey.RESULT_KEY.name, bundleOf(ResultKey.WORD.name to query))
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
package com.android.mediproject.feature.search.recentsearchlist

import androidx.lifecycle.viewModelScope
import com.android.mediproject.core.common.network.Dispatcher
import com.android.mediproject.core.common.network.MediDispatchers
import com.android.mediproject.core.domain.SearchHistoryUseCase
import com.android.mediproject.core.ui.base.BaseViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.flow.stateIn
import javax.inject.Inject

@HiltViewModel
class RecentSearchListViewModel @Inject constructor(
private val searchHistoryUseCase: SearchHistoryUseCase,
@Dispatcher(MediDispatchers.IO) private val ioDispatcher: CoroutineDispatcher,
) : BaseViewModel() {
val searchHistoryList by lazy {
suspend {
searchHistoryUseCase.getSearchHistoryList(6).distinctUntilChanged().shareIn(
viewModelScope, started = SharingStarted.Lazily, replay = 1
)
searchHistoryUseCase.getSearchHistoryList(6).stateIn(viewModelScope)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class PagingLoadStateAdapter(
private val retry: () -> Unit
) : LoadStateAdapter<LoadStateViewHolder>() {


override fun onBindViewHolder(holder: LoadStateViewHolder, loadState: LoadState) = when (loadState) {
is LoadState.Loading -> holder.setLoadingState(LoadingState.Loading)
is LoadState.Error -> holder.setLoadingState(LoadingState.Error(loadState.error.message ?: ""))
Expand Down

0 comments on commit 5531eed

Please sign in to comment.