Skip to content

Commit

Permalink
#91 수동 검색 로직 수정 중
Browse files Browse the repository at this point in the history
  • Loading branch information
pknujsp committed May 29, 2023
1 parent d0cfa8e commit a83ad40
Show file tree
Hide file tree
Showing 24 changed files with 151 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ object RepositoryModule {
@Provides
@Singleton
fun provideMedicineApprovalRepository(
medicineApprovalDataSource: MedicineApprovalDataSource, searchHistoryRepository: SearchHistoryRepository
medicineApprovalDataSource: MedicineApprovalDataSource,
searchHistoryRepository: SearchHistoryRepository,
@Dispatcher(MediDispatchers.IO) ioDispatcher: CoroutineDispatcher
): MedicineApprovalRepository = MedicineApprovalRepositoryImpl(
medicineApprovalDataSource, searchHistoryRepository
medicineApprovalDataSource, searchHistoryRepository, ioDispatcher
)

@Provides
Expand Down Expand Up @@ -93,6 +95,6 @@ object RepositoryModule {
@Provides
@Singleton
fun providesSearchHistoryRepository(
searchHistoryDao: SearchHistoryDao, @Dispatcher(MediDispatchers.IO) ioDispatcher: CoroutineDispatcher
): SearchHistoryRepository = SearchHistoryRepositoryImpl(searchHistoryDao, ioDispatcher)
searchHistoryDao: SearchHistoryDao
): SearchHistoryRepository = SearchHistoryRepositoryImpl(searchHistoryDao)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.android.mediproject.core.common.DATA_GO_KR_PAGE_SIZE
import com.android.mediproject.core.common.network.Dispatcher
import com.android.mediproject.core.common.network.MediDispatchers
import com.android.mediproject.core.data.search.SearchHistoryRepository
import com.android.mediproject.core.database.searchhistory.SearchHistoryDto
import com.android.mediproject.core.model.medicine.medicineapproval.Item
import com.android.mediproject.core.model.medicine.medicinedetailinfo.MedicineDetailInfoResponse
import com.android.mediproject.core.network.datasource.medicineapproval.MedicineApprovalDataSource
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
Expand All @@ -17,7 +21,9 @@ import kotlinx.coroutines.flow.map
import javax.inject.Inject

class MedicineApprovalRepositoryImpl @Inject constructor(
private val medicineApprovalDataSource: MedicineApprovalDataSource, private val searchHistoryRepository: SearchHistoryRepository
private val medicineApprovalDataSource: MedicineApprovalDataSource,
private val searchHistoryRepository: SearchHistoryRepository,
@Dispatcher(MediDispatchers.IO) private val ioDispatcher: CoroutineDispatcher
) : MedicineApprovalRepository {

/**
Expand All @@ -35,7 +41,7 @@ class MedicineApprovalRepositoryImpl @Inject constructor(
if (itemName == null && entpName == null) {
emptyFlow()
} else {
searchHistoryRepository.insertSearchHistory(itemName ?: entpName!!)
searchHistoryRepository.insertSearchHistory(SearchHistoryDto(itemName ?: entpName!!))
Pager(config = PagingConfig(pageSize = DATA_GO_KR_PAGE_SIZE, prefetchDistance = 5), pagingSourceFactory = {
MedicineApprovalListDataSourceImpl(
medicineApprovalDataSource, itemName, entpName, medicationType
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package com.android.mediproject.core.data.search

import androidx.annotation.WorkerThread
import com.android.mediproject.core.database.searchhistory.SearchHistoryDto
import kotlinx.coroutines.flow.Flow

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

@WorkerThread

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

@WorkerThread

suspend fun deleteSearchHistory(id: Long)

@WorkerThread

suspend fun deleteAllSearchHistory()
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,33 @@
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 kotlinx.coroutines.runBlocking
import javax.inject.Inject

class SearchHistoryRepositoryImpl @Inject constructor(
private val searchHistoryDao: SearchHistoryDao, @Dispatcher(MediDispatchers.IO) private val ioDispatcher: CoroutineDispatcher
private val searchHistoryDao: SearchHistoryDao,
) : 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 insertSearchHistory(searchHistoryDto: SearchHistoryDto) = runBlocking {
searchHistoryDto.apply {
query = query.trim()
}
if (!searchHistoryDao.isExist(searchHistoryDto.query)) searchHistoryDao.insert(searchHistoryDto)
}

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

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

override suspend fun deleteSearchHistory(id: Long) {
withContext(ioDispatcher) {
searchHistoryDao.delete(id)
}

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

override suspend fun deleteAllSearchHistory() {
withContext(ioDispatcher) {
searchHistoryDao.deleteAll()
}

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


}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.android.mediproject.core.database.searchhistory

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Upsert
import kotlinx.coroutines.flow.Flow

@Dao
Expand All @@ -12,15 +13,15 @@ interface SearchHistoryDao {
* 검색 기록을 추가한다.
* @param searchHistoryDto
*/
@Upsert
fun insert(searchHistoryDto: SearchHistoryDto)
@Insert(entity = SearchHistoryDto::class, onConflict = OnConflictStrategy.IGNORE)
suspend fun insert(SearchHistoryDto: SearchHistoryDto)

/**
* id에 해당하는 검색 기록을 삭제한다.
* @param id
*/
@Query("DELETE FROM search_history_table WHERE id = :id")
fun delete(id: Long)
suspend fun delete(id: Long)

/**
* 개수 만큼 검색 목록을 가져온다.
Expand All @@ -33,5 +34,8 @@ interface SearchHistoryDao {
* 모든 검색 기록을 삭제한다.
*/
@Query("DELETE FROM search_history_table")
fun deleteAll()
suspend fun deleteAll()

@Query("SELECT EXISTS (SELECT * FROM search_history_table WHERE `query` = :query)")
suspend fun isExist(query: String): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,12 @@ import androidx.room.PrimaryKey
data class SearchHistoryDto(
@PrimaryKey(autoGenerate = true) val id: Long,

@ColumnInfo(name = "query") val query: String, @ColumnInfo(name = "search_date_time") val searchDateTime: String
)
@ColumnInfo(name = "query") var query: String,

@ColumnInfo(
name = "search_date_time", defaultValue = "CURRENT_TIMESTAMP"
) val searchDateTime: String

) {
constructor(query: String) : this(0, query, "")
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.android.mediproject.core.database.searchhistory

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

fun SearchHistoryDto.toSearchHistoryItemDto(): com.android.mediproject.core.model.search.local.SearchHistoryItemDto {
return com.android.mediproject.core.model.search.local.SearchHistoryItemDto(
id = id, query = query, searchedAt = LocalDateTime.parse(this.searchDateTime)
id = id, query = query, searchedAt = LocalDateTime.parse(this.searchDateTime, dateTimeFormatter)
)
}
}

private val dateTimeFormatter by lazy { DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") }
Original file line number Diff line number Diff line change
@@ -1,6 +1,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 javax.inject.Inject
Expand All @@ -10,7 +11,7 @@ import javax.inject.Singleton
class SearchHistoryUseCase @Inject constructor(
private val searchHistoryRepository: SearchHistoryRepository
) {
suspend fun insertSearchHistory(query: String) = searchHistoryRepository.insertSearchHistory(query)
suspend fun insertSearchHistory(query: String) = searchHistoryRepository.insertSearchHistory(SearchHistoryDto(query))

suspend fun getSearchHistoryList(count: Int) = searchHistoryRepository.getSearchHistoryList(count).map {
it.map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,6 @@ class ButtonChip<T> : Chip {
listener.onClick(data)
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import com.android.mediproject.core.ui.R
*/
class SimpleListItemView<T> : ConstraintLayout {

constructor(context: Context) : super(context, null) {
init(context, null)
constructor(context: Context, midRatio: Float) : super(context, null) {
init(context, null, midRatio)
}

constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs) {
Expand All @@ -42,33 +42,21 @@ class SimpleListItemView<T> : ConstraintLayout {
get() = _data

@SuppressLint("ResourceType") private val chip = ButtonChip<T>(context).apply {
layoutParams = ConstraintLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT).apply {
topToTop = LayoutParams.PARENT_ID
bottomToBottom = LayoutParams.PARENT_ID
leftToLeft = LayoutParams.PARENT_ID
}
textSize = 13f
isClickable = false
id = 10
}

@SuppressLint("ResourceType") private val textView = TextView(context).apply {
layoutParams = ConstraintLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT).apply {
topToTop = LayoutParams.PARENT_ID
bottomToBottom = LayoutParams.PARENT_ID
leftToRight = chip.id
rightToRight = LayoutParams.PARENT_ID
leftMargin = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f, resources.displayMetrics).toInt()
}
ellipsize = TextUtils.TruncateAt.MARQUEE
ellipsize = TextUtils.TruncateAt.END
isClickable = false
maxLines = 1
textAlignment = TEXT_ALIGNMENT_VIEW_START
textSize = 12f
id = 20
}

private fun init(context: Context, attrs: AttributeSet?) {
private fun init(context: Context, attrs: AttributeSet?, midRatio: Float = 0.6f) {
context.theme.obtainStyledAttributes(
attrs, R.styleable.SimpleCommentItemView, 0, 0
).apply {
Expand All @@ -81,8 +69,18 @@ class SimpleListItemView<T> : ConstraintLayout {
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3f, resources.displayMetrics).toInt()
.apply { setPadding(0, this, 0, this) }

addView(chip)
addView(textView)
addView(chip, LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT).apply {
topToTop = LayoutParams.PARENT_ID
bottomToBottom = LayoutParams.PARENT_ID
leftToLeft = LayoutParams.PARENT_ID
})
addView(textView, LayoutParams(0, LayoutParams.WRAP_CONTENT).apply {
topToTop = LayoutParams.PARENT_ID
bottomToBottom = LayoutParams.PARENT_ID
leftToRight = chip.id
rightToRight = LayoutParams.PARENT_ID
leftMargin = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f, resources.displayMetrics).toInt()
})

isClickable = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class RecentCommentsAdapter : ListAdapter<CommentDto, RecentCommentsAdapter.Rece
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecentCommentListViewHolder =
RecentCommentListViewHolder(SimpleListItemView(parent.context))
RecentCommentListViewHolder(SimpleListItemView(parent.context, 0.6f))

override fun onBindViewHolder(holder: RecentCommentListViewHolder, position: Int) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.android.mediproject.core.common.util.navigateByDeepLink
import com.android.mediproject.core.model.searchmedicines.local.SearchQueryArgs
import com.android.mediproject.core.ui.base.BaseFragment
import com.android.mediproject.feature.comments.recentcommentlist.RecentCommentListFragment
import com.android.mediproject.feature.home.databinding.FragmentHomeBinding
Expand Down Expand Up @@ -72,12 +70,11 @@ class HomeFragment : BaseFragment<FragmentHomeBinding, HomeViewModel>(FragmentHo
}
}
setFragmentResultListener(RecentSearchListFragment.ResultKey.RESULT_KEY.name, viewLifecycleOwner) { _, bundle ->
bundle.apply {
findNavController().navigateByDeepLink(
"medilens://main/search/recentSearchListFragment",
SearchQueryArgs(getString(RecentSearchListFragment.ResultKey.WORD.name) ?: "")
)
}
findNavController().navigate(
HomeFragmentDirections.actionHomeFragmentToSearchMedicinesFragment(
bundle.getString(RecentSearchListFragment.ResultKey.WORD.name) ?: ""
)
)
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions feature/home/src/main/res/navigation/home_nav.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@
</fragment>

<deepLink app:uri="medilens://main/home_nav" />

<fragment
android:id="@+id/searchMedicinesFragment"
android:name="com.android.mediproject.feature.search.SearchMedicinesFragment"
tools:layout="@layout/fragment_search_medicines"
android:label="SearchMedicinesFragment" />
android:label="SearchMedicinesFragment"
tools:layout="@layout/fragment_search_medicines">

<argument
android:name="query"
android:defaultValue=""
app:argType="string" />

</fragment>
</navigation>
Loading

0 comments on commit a83ad40

Please sign in to comment.