|
1 | 1 | package com.kickstarter.features.search.viewmodel
|
2 | 2 |
|
| 3 | +import android.util.Pair |
3 | 4 | import androidx.lifecycle.ViewModel
|
4 | 5 | import androidx.lifecycle.ViewModelProvider
|
5 | 6 | import androidx.lifecycle.viewModelScope
|
6 | 7 | import com.kickstarter.libs.Environment
|
| 8 | +import com.kickstarter.libs.RefTag |
| 9 | +import com.kickstarter.libs.utils.extensions.isNull |
| 10 | +import com.kickstarter.libs.utils.extensions.isTrue |
7 | 11 | import com.kickstarter.models.Project
|
8 | 12 | import com.kickstarter.services.DiscoveryParams
|
9 | 13 | import kotlinx.coroutines.CoroutineDispatcher
|
| 14 | +import kotlinx.coroutines.FlowPreview |
10 | 15 | import kotlinx.coroutines.flow.MutableStateFlow
|
11 | 16 | import kotlinx.coroutines.flow.SharingStarted
|
12 | 17 | import kotlinx.coroutines.flow.StateFlow
|
13 | 18 | import kotlinx.coroutines.flow.asStateFlow
|
| 19 | +import kotlinx.coroutines.flow.collectLatest |
| 20 | +import kotlinx.coroutines.flow.debounce |
| 21 | +import kotlinx.coroutines.flow.onEach |
14 | 22 | import kotlinx.coroutines.flow.stateIn
|
15 | 23 | import kotlinx.coroutines.launch
|
16 | 24 | import kotlinx.coroutines.plus
|
17 | 25 | import kotlin.coroutines.EmptyCoroutineContext
|
| 26 | +import kotlin.text.isNotBlank |
18 | 27 |
|
19 | 28 | data class SearchUIState(
|
20 | 29 | val isLoading: Boolean = false,
|
21 |
| - val isErrored: Boolean = false, |
22 |
| - val popularProjectsList: List<Project> = emptyList(), // TODO MBL-2135 popular & search lists could be potentially unified |
| 30 | + val popularProjectsList: List<Project> = emptyList(), |
23 | 31 | val searchList: List<Project> = emptyList()
|
24 | 32 | )
|
25 | 33 |
|
| 34 | +@OptIn(FlowPreview::class) |
26 | 35 | class SearchAndFilterViewModel(
|
27 | 36 | private val environment: Environment,
|
28 | 37 | private val testDispatcher: CoroutineDispatcher? = null
|
29 | 38 | ) : ViewModel() {
|
30 | 39 |
|
31 | 40 | private val scope = viewModelScope + (testDispatcher ?: EmptyCoroutineContext)
|
32 | 41 | private val apolloClient = requireNotNull(environment.apolloClientV2())
|
| 42 | + private val analyticEvents = requireNotNull(environment.analytics()) |
33 | 43 |
|
34 | 44 | private val _searchUIState = MutableStateFlow(SearchUIState())
|
35 | 45 | val searchUIState: StateFlow<SearchUIState>
|
36 | 46 | get() = _searchUIState
|
37 | 47 | .asStateFlow()
|
38 | 48 | .stateIn(
|
39 |
| - scope = viewModelScope, |
| 49 | + scope = scope, |
40 | 50 | started = SharingStarted.WhileSubscribed(),
|
41 | 51 | initialValue = SearchUIState()
|
42 | 52 | )
|
43 | 53 |
|
44 |
| - // Popular projects sorting selection |
| 54 | + // - Popular projects sorting selection |
45 | 55 | private val popularDiscoveryParam = DiscoveryParams.builder().sort(DiscoveryParams.Sort.POPULAR).build()
|
46 |
| - // TODO Will be updated with the params used to call search with, private for now |
47 |
| - private val listOfSearchParams = listOf(popularDiscoveryParam) |
| 56 | + |
| 57 | + private val _params = MutableStateFlow(popularDiscoveryParam) |
| 58 | + val params: StateFlow<DiscoveryParams> = _params |
| 59 | + |
| 60 | + private val debouncePeriod = 300L |
| 61 | + private val _searchTerm = MutableStateFlow("") |
| 62 | + private val searchTerm: StateFlow<String> = _searchTerm |
| 63 | + |
| 64 | + private var errorAction: (message: String?) -> Unit = {} |
| 65 | + |
| 66 | + private var projectsList = emptyList<Project>() |
| 67 | + private var popularProjectsList = emptyList<Project>() |
| 68 | + |
| 69 | + init { |
| 70 | + scope.launch { |
| 71 | + searchTerm |
| 72 | + .debounce(debouncePeriod) |
| 73 | + .onEach { debouncedTerm -> |
| 74 | + // - Reset to initial state in case of empty search term |
| 75 | + if (debouncedTerm.isEmpty() || debouncedTerm.isBlank()) { |
| 76 | + _params.emit(popularDiscoveryParam) |
| 77 | + } else |
| 78 | + _params.emit( |
| 79 | + DiscoveryParams.builder() |
| 80 | + .term(debouncedTerm) |
| 81 | + .sort(DiscoveryParams.Sort.POPULAR) // TODO: update once sort option is ready MBL-2131, by default popular in every search |
| 82 | + .build() |
| 83 | + ) |
| 84 | + }.collectLatest { |
| 85 | + updateSearchResultsState() |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + fun provideErrorAction(errorAction: (message: String?) -> Unit) { |
| 91 | + this.errorAction = errorAction |
| 92 | + } |
48 | 93 |
|
49 | 94 | /**
|
50 |
| - * Search screen will present the list of popular projects |
51 |
| - * as default when presenting SearchAndFilterActivity. |
| 95 | + * Update UIState with after executing Search query with latest params |
52 | 96 | */
|
53 |
| - fun getPopularProjects() { |
54 |
| - scope.launch { |
55 |
| - // TODO trigger loading state UI will handle on MBL-2135 |
56 |
| - _searchUIState.emit( |
57 |
| - SearchUIState( |
58 |
| - isLoading = true, |
59 |
| - ) |
60 |
| - ) |
| 97 | + private suspend fun updateSearchResultsState() { |
| 98 | + analyticEvents.trackSearchCTAButtonClicked(params.value) |
| 99 | + |
| 100 | + emitCurrentState(isLoading = true) |
61 | 101 |
|
62 |
| - val searchEnvelopeResult = search(listOfSearchParams.last()) |
| 102 | + // - Result from API |
| 103 | + val searchEnvelopeResult = apolloClient.getSearchProjects(params.value) |
| 104 | + |
| 105 | + if (searchEnvelopeResult.isFailure) { |
| 106 | + // - errorAction.invoke(searchEnvelopeResult.exceptionOrNull()?.message) to return API level message |
| 107 | + errorAction.invoke(null) |
| 108 | + } |
63 | 109 |
|
64 |
| - if (searchEnvelopeResult.isFailure) { |
65 |
| - // TODO trigger error state UI will handle on MBL-2135 |
66 |
| - _searchUIState.emit( |
67 |
| - SearchUIState( |
68 |
| - isErrored = true, |
69 |
| - ) |
| 110 | + if (searchEnvelopeResult.isSuccess) { |
| 111 | + searchEnvelopeResult.getOrNull()?.projectList?.let { |
| 112 | + if (params.value.term().isNull()) popularProjectsList = it |
| 113 | + if (params.value.term()?.isNotBlank().isTrue()) projectsList = it |
| 114 | + |
| 115 | + emitCurrentState(isLoading = false) |
| 116 | + |
| 117 | + analyticEvents.trackSearchResultPageViewed( |
| 118 | + params.value, |
| 119 | + 1, // TODO: this will contain the page when pagination ready MBL-2139 |
| 120 | + params.value.sort() ?: DiscoveryParams.Sort.POPULAR |
70 | 121 | )
|
71 | 122 | }
|
| 123 | + } |
| 124 | + } |
72 | 125 |
|
73 |
| - if (searchEnvelopeResult.isSuccess) { |
74 |
| - searchEnvelopeResult.getOrNull()?.projectList?.let { |
75 |
| - _searchUIState.emit( |
76 |
| - SearchUIState( |
77 |
| - isErrored = false, |
78 |
| - isLoading = false, |
79 |
| - popularProjectsList = it |
80 |
| - ) |
81 |
| - ) |
82 |
| - } |
83 |
| - } |
| 126 | + private suspend fun emitCurrentState(isLoading: Boolean = false) { |
| 127 | + _searchUIState.emit( |
| 128 | + SearchUIState( |
| 129 | + isLoading = isLoading, |
| 130 | + popularProjectsList = popularProjectsList, |
| 131 | + searchList = projectsList |
| 132 | + ) |
| 133 | + ) |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Returns a project and its appropriate ref tag given its location in a list of popular projects or search results. |
| 138 | + * |
| 139 | + * @param searchTerm The search term entered to determine list of search results. |
| 140 | + * @param projects The list of popular or search result projects. |
| 141 | + * @param selectedProject The project selected by the user. |
| 142 | + * @return The project and its appropriate ref tag. |
| 143 | + */ |
| 144 | + private fun projectAndRefTag( |
| 145 | + searchTerm: String, |
| 146 | + projects: List<Project>, |
| 147 | + selectedProject: Project |
| 148 | + ): Pair<Project, RefTag> { |
| 149 | + val isFirstResult = if (projects.isEmpty()) false else selectedProject === projects[0] |
| 150 | + return if (searchTerm.isEmpty()) { |
| 151 | + if (isFirstResult) Pair.create( |
| 152 | + selectedProject, |
| 153 | + RefTag.searchPopularFeatured() |
| 154 | + ) else Pair.create(selectedProject, RefTag.searchPopular()) |
| 155 | + } else { |
| 156 | + if (isFirstResult) Pair.create( |
| 157 | + selectedProject, |
| 158 | + RefTag.searchFeatured() |
| 159 | + ) else Pair.create(selectedProject, RefTag.search()) |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + fun updateSearchTerm(searchTerm: String) { |
| 164 | + scope.launch { |
| 165 | + _searchTerm.emit(searchTerm) |
84 | 166 | }
|
85 | 167 | }
|
86 | 168 |
|
87 |
| - private suspend fun search(params: DiscoveryParams) = apolloClient.getSearchProjects(params) |
| 169 | + fun getProjectAndRefTag(project: Project): Pair<Project, RefTag> { |
| 170 | + val allProjectsList = popularProjectsList.union(projectsList).toList() |
| 171 | + return projectAndRefTag(searchTerm.value, allProjectsList, project) |
| 172 | + } |
88 | 173 |
|
89 | 174 | class Factory(
|
90 | 175 | private val environment: Environment,
|
|
0 commit comments