Skip to content

Commit

Permalink
Update project
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbel committed Feb 25, 2024
1 parent 02ded41 commit 81fcf57
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ class Interactor @Inject constructor(
imageInteractor: ImageInteractor,
movieInteractor: MovieInteractor,
notificationInteractor: NotificationInteractor,
searchInteractor: SearchInteractor,
settingsInteractor: SettingsInteractor,
suggestionInteractor: SuggestionInteractor
): AccountInteractor by accountInteractor,
AuthenticationInteractor by authenticationInteractor,
ImageInteractor by imageInteractor,
MovieInteractor by movieInteractor,
NotificationInteractor by notificationInteractor,
SearchInteractor by searchInteractor,
SettingsInteractor by settingsInteractor,
SuggestionInteractor by suggestionInteractor
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.michaelbel.movies.interactor

import kotlinx.coroutines.flow.StateFlow

interface SearchInteractor {

val isSearchActive: StateFlow<Boolean>

fun setSearchActive(value: Boolean)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import org.michaelbel.movies.interactor.AuthenticationInteractor
import org.michaelbel.movies.interactor.ImageInteractor
import org.michaelbel.movies.interactor.MovieInteractor
import org.michaelbel.movies.interactor.NotificationInteractor
import org.michaelbel.movies.interactor.SearchInteractor
import org.michaelbel.movies.interactor.SettingsInteractor
import org.michaelbel.movies.interactor.SuggestionInteractor
import org.michaelbel.movies.interactor.impl.AccountInteractorImpl
import org.michaelbel.movies.interactor.impl.AuthenticationInteractorImpl
import org.michaelbel.movies.interactor.impl.ImageInteractorImpl
import org.michaelbel.movies.interactor.impl.MovieInteractorImpl
import org.michaelbel.movies.interactor.impl.NotificationInteractorImpl
import org.michaelbel.movies.interactor.impl.SearchInteractorImpl
import org.michaelbel.movies.interactor.impl.SettingsInteractorImpl
import org.michaelbel.movies.interactor.impl.SuggestionInteractorImpl

Expand Down Expand Up @@ -54,6 +56,12 @@ internal interface InteractorModule {
interactor: NotificationInteractorImpl
): NotificationInteractor

@Binds
@Singleton
fun provideSearchInteractor(
interactor: SearchInteractorImpl
): SearchInteractor

@Binds
@Singleton
fun provideSettingsInteractor(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.michaelbel.movies.interactor.impl

import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import org.michaelbel.movies.interactor.SearchInteractor

@Singleton
internal class SearchInteractorImpl @Inject constructor(): SearchInteractor {

private val isActiveMutableFlow: MutableStateFlow<Boolean> = MutableStateFlow(true)

override val isSearchActive: StateFlow<Boolean>
get() = isActiveMutableFlow.asStateFlow()

override fun setSearchActive(value: Boolean) {
isActiveMutableFlow.value = value
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ class SearchViewModel @Inject constructor(
private val _query: MutableStateFlow<String> = MutableStateFlow("")
private val query: StateFlow<String> = _query.asStateFlow()

private val _active: MutableStateFlow<Boolean> = MutableStateFlow(true)
val active: StateFlow<Boolean> = _active.asStateFlow()
val isSearchActive: StateFlow<Boolean> = interactor.isSearchActive

val pagingDataFlow: Flow<PagingData<MovieDb>> = query
.flatMapLatest { query -> interactor.moviesPagingData(query) }
Expand All @@ -75,7 +74,7 @@ class SearchViewModel @Inject constructor(
}

fun onChangeActiveState(state: Boolean) {
_active.value = state
interactor.setSearchActive(state)
}

fun onSaveToHistory(movieId: Int) = launch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fun SearchRoute(
val networkStatus by viewModel.networkStatus.collectAsStateWithLifecycle()
val suggestions by viewModel.suggestionsFlow.collectAsStateWithLifecycle()
val searchHistoryMovies by viewModel.searchHistoryMoviesFlow.collectAsStateWithLifecycle()
val active by viewModel.active.collectAsStateWithLifecycle()
val active by viewModel.isSearchActive.collectAsStateWithLifecycle()

SearchScreenContent(
pagingItems = pagingItems,
Expand Down Expand Up @@ -153,23 +153,15 @@ private fun SearchScreenContent(
onCloseClick = {
onChangeActiveState(query.isNotEmpty())
query = ""
focusRequester.requestFocus()
},
onInputText = { text ->
query = text
onChangeSearchQuery(text)
onChangeActiveState(query.isEmpty())
},
suggestions = suggestions,
onSuggestionClick = { suggestion ->
query = suggestion.title
onChangeSearchQuery(suggestion.title)
onChangeActiveState(false)
},
searchHistoryMovies = searchHistoryMovies,
onHistoryMovieClick = { title ->
query = title
onChangeSearchQuery(title)
onChangeActiveState(false)
},
onHistoryMovieRemoveClick = onRemoveMovieFromHistory,
onClearHistoryClick = onHistoryClear,
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ fun SearchToolbar(
onCloseClick: () -> Unit,
onInputText: (String) -> Unit,
suggestions: List<SuggestionDb>,
onSuggestionClick: (SuggestionDb) -> Unit,
searchHistoryMovies: List<MovieDb>,
onHistoryMovieClick: (String) -> Unit,
onHistoryMovieRemoveClick: (Int) -> Unit,
onClearHistoryClick: () -> Unit,
modifier: Modifier = Modifier
Expand Down Expand Up @@ -77,7 +75,7 @@ fun SearchToolbar(
}
},
trailingIcon = {
if (active) {
if (query.isNotEmpty()) {
CloseIcon(
onClick = onCloseClick
)
Expand Down Expand Up @@ -110,7 +108,7 @@ fun SearchToolbar(
modifier = Modifier
.fillMaxWidth()
.height(52.dp)
.clickable { onHistoryMovieClick(movie.title) }
.clickable { onInputText(movie.title) }
)
}
}
Expand All @@ -128,7 +126,7 @@ fun SearchToolbar(
modifier = Modifier
.fillMaxWidth()
.height(52.dp)
.clickable { onSuggestionClick(suggestion) }
.clickable { onInputText(suggestion.title) }
)
}
}
Expand All @@ -154,9 +152,7 @@ private fun SearchToolbarPreview(
onCloseClick = {},
onInputText = {},
suggestions = suggestions,
onSuggestionClick = {},
searchHistoryMovies = emptyList(),
onHistoryMovieClick = {},
onHistoryMovieRemoveClick = {},
onClearHistoryClick = {}
)
Expand All @@ -181,9 +177,7 @@ private fun SearchToolbarAmoledPreview(
onCloseClick = {},
onInputText = {},
suggestions = suggestions,
onSuggestionClick = {},
searchHistoryMovies = emptyList(),
onHistoryMovieClick = {},
onHistoryMovieRemoveClick = {},
onClearHistoryClick = {}
)
Expand Down

0 comments on commit 81fcf57

Please sign in to comment.