-
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 #19 from inu-appcenter/feat/search
검색 구현 (카테고리 및 상태 제외)
- Loading branch information
Showing
16 changed files
with
342 additions
and
21 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
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
43 changes: 43 additions & 0 deletions
43
app/src/main/java/org/inu/events/data/source/EventSearchPagingSource.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,43 @@ | ||
package org.inu.events.data.source | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import org.inu.events.data.httpservice.EventHttpService | ||
import org.inu.events.data.model.entity.Event | ||
|
||
private const val STARTING_KEY = 0 | ||
|
||
class EventSearchPagingSource( | ||
private val httpService: EventHttpService, | ||
private val categoryId: Int, | ||
private val eventStatus: Boolean, | ||
private val content: String, | ||
) : PagingSource<Int, Event>() { | ||
|
||
private suspend fun getData(pageNum: Int, pageSize: Int): List<Event> { | ||
return httpService.searchEvents( | ||
categoryId = categoryId, | ||
eventStatus = eventStatus, | ||
content = content, | ||
pageNum = pageNum, | ||
pageSize = pageSize, | ||
) | ||
} | ||
|
||
override fun getRefreshKey(state: PagingState<Int, Event>): Int? = null | ||
|
||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Event> { | ||
val start = params.key ?: STARTING_KEY | ||
val page = start / params.loadSize | ||
val data = getData( | ||
pageNum = page, | ||
pageSize = params.loadSize | ||
) | ||
|
||
return LoadResult.Page( | ||
data = data, | ||
prevKey = null, | ||
nextKey = if (data.size == params.loadSize) start + params.loadSize else null | ||
) | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
.../org/inu/events/ui/adapter/LikeAdapter.kt → ...inu/events/ui/adapter/like/LikeAdapter.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
45 changes: 45 additions & 0 deletions
45
app/src/main/java/org/inu/events/ui/adapter/like/LikePagingAdapter.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,45 @@ | ||
package org.inu.events.ui.adapter.like | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.paging.PagingDataAdapter | ||
import androidx.recyclerview.widget.DiffUtil | ||
import androidx.recyclerview.widget.RecyclerView | ||
import org.inu.events.data.model.entity.Event | ||
import org.inu.events.databinding.ItemLikeEventBinding | ||
|
||
class LikePagingAdapter : PagingDataAdapter<Event, LikePagingAdapter.ViewHolder>(LikeDiffUtil) { | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder.from(parent) | ||
|
||
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | ||
getItem(position)?.let { event -> | ||
holder.bind(event) | ||
} | ||
} | ||
|
||
class ViewHolder private constructor( | ||
val binding: ItemLikeEventBinding | ||
) : RecyclerView.ViewHolder(binding.root) { | ||
|
||
fun bind(item: Event) { | ||
binding.item = item | ||
binding.executePendingBindings() | ||
} | ||
|
||
companion object { | ||
fun from(parent: ViewGroup): ViewHolder { | ||
val layoutInflater = LayoutInflater.from(parent.context) | ||
val binding = ItemLikeEventBinding.inflate(layoutInflater, parent, false) | ||
|
||
return ViewHolder(binding) | ||
} | ||
} | ||
} | ||
|
||
companion object LikeDiffUtil : DiffUtil.ItemCallback<Event>() { | ||
override fun areItemsTheSame(oldItem: Event, newItem: Event) = oldItem.id == newItem.id | ||
|
||
override fun areContentsTheSame(oldItem: Event, newItem: Event) = oldItem == newItem | ||
} | ||
} |
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
47 changes: 47 additions & 0 deletions
47
app/src/main/java/org/inu/events/ui/home/SearchActivity.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,47 @@ | ||
package org.inu.events.ui.home | ||
|
||
import android.os.Bundle | ||
import androidx.activity.viewModels | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.lifecycleScope | ||
import androidx.lifecycle.repeatOnLifecycle | ||
import kotlinx.coroutines.flow.collectLatest | ||
import kotlinx.coroutines.launch | ||
import org.inu.events.databinding.ActivitySearchBinding | ||
import org.inu.events.ui.adapter.like.LikePagingAdapter | ||
|
||
class SearchActivity : AppCompatActivity() { | ||
|
||
private val vm: SearchViewModel by viewModels() | ||
private lateinit var binding: ActivitySearchBinding | ||
private val adapter = LikePagingAdapter() | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
binding = ActivitySearchBinding.inflate(layoutInflater).apply { | ||
lifecycleOwner = this@SearchActivity | ||
vm = this@SearchActivity.vm | ||
} | ||
|
||
initRecyclerView() | ||
observeSearch() | ||
|
||
setContentView(binding.root) | ||
} | ||
|
||
|
||
private fun initRecyclerView() { | ||
binding.rvEvent.adapter = adapter | ||
} | ||
|
||
private fun observeSearch() { | ||
lifecycleScope.launch { | ||
repeatOnLifecycle(Lifecycle.State.STARTED) { | ||
vm.searchResult.collectLatest { | ||
adapter.submitData(it) | ||
} | ||
} | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
app/src/main/java/org/inu/events/ui/home/SearchViewModel.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,59 @@ | ||
package org.inu.events.ui.home | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import androidx.paging.PagingData | ||
import kotlinx.coroutines.FlowPreview | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.flow.* | ||
import kotlinx.coroutines.launch | ||
import org.inu.events.data.model.entity.Event | ||
import org.inu.events.data.repository.EventRepository | ||
import org.koin.core.component.KoinComponent | ||
import org.koin.core.component.inject | ||
|
||
class SearchViewModel : ViewModel(), KoinComponent { | ||
|
||
private val eventRepository: EventRepository by inject() | ||
|
||
val searchText = MutableStateFlow("") | ||
private val category = MutableStateFlow(0) | ||
private val eventStatus = MutableStateFlow(false) | ||
|
||
var searchResult: MutableStateFlow<PagingData<Event>> = MutableStateFlow(PagingData.empty()) | ||
private var job: Job? = null | ||
|
||
init { | ||
|
||
viewModelScope.launch { | ||
initSearch() | ||
} | ||
|
||
} | ||
|
||
@OptIn(FlowPreview::class) | ||
private suspend fun initSearch() { | ||
searchText.debounce(500).collect { | ||
job?.cancel() | ||
if (it.isEmpty()) { | ||
searchResult.value = PagingData.empty() | ||
return@collect | ||
} | ||
search().run { | ||
job = this | ||
} | ||
} | ||
} | ||
|
||
private fun search(): Job { | ||
return viewModelScope.launch { | ||
eventRepository.searchEvents( | ||
categoryId = category.value, | ||
eventStatus = eventStatus.value, | ||
content = searchText.value | ||
).collectLatest { pagingData -> | ||
searchResult.value = pagingData | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.