From 81fcf5704a05cf7a89fe08da68b9354cc04035d2 Mon Sep 17 00:00:00 2001 From: michaelbel Date: Sun, 25 Feb 2024 17:26:42 +0300 Subject: [PATCH] Update project --- .../movies/interactor/Interactor.kt | 2 ++ .../movies/interactor/SearchInteractor.kt | 10 +++++++++ .../movies/interactor/di/InteractorModule.kt | 8 +++++++ .../interactor/impl/SearchInteractorImpl.kt | 21 +++++++++++++++++++ .../movies/search/SearchViewModel.kt | 5 ++--- .../movies/search/ui/SearchScreenContent.kt | 14 +++---------- .../movies/search/ui/SearchToolbar.kt | 12 +++-------- 7 files changed, 49 insertions(+), 23 deletions(-) create mode 100644 core/interactor/src/main/kotlin/org/michaelbel/movies/interactor/SearchInteractor.kt create mode 100644 core/interactor/src/main/kotlin/org/michaelbel/movies/interactor/impl/SearchInteractorImpl.kt diff --git a/core/interactor/src/main/kotlin/org/michaelbel/movies/interactor/Interactor.kt b/core/interactor/src/main/kotlin/org/michaelbel/movies/interactor/Interactor.kt index 3b31c6b84..c3f6db014 100644 --- a/core/interactor/src/main/kotlin/org/michaelbel/movies/interactor/Interactor.kt +++ b/core/interactor/src/main/kotlin/org/michaelbel/movies/interactor/Interactor.kt @@ -8,6 +8,7 @@ class Interactor @Inject constructor( imageInteractor: ImageInteractor, movieInteractor: MovieInteractor, notificationInteractor: NotificationInteractor, + searchInteractor: SearchInteractor, settingsInteractor: SettingsInteractor, suggestionInteractor: SuggestionInteractor ): AccountInteractor by accountInteractor, @@ -15,5 +16,6 @@ class Interactor @Inject constructor( ImageInteractor by imageInteractor, MovieInteractor by movieInteractor, NotificationInteractor by notificationInteractor, + SearchInteractor by searchInteractor, SettingsInteractor by settingsInteractor, SuggestionInteractor by suggestionInteractor \ No newline at end of file diff --git a/core/interactor/src/main/kotlin/org/michaelbel/movies/interactor/SearchInteractor.kt b/core/interactor/src/main/kotlin/org/michaelbel/movies/interactor/SearchInteractor.kt new file mode 100644 index 000000000..712f4083e --- /dev/null +++ b/core/interactor/src/main/kotlin/org/michaelbel/movies/interactor/SearchInteractor.kt @@ -0,0 +1,10 @@ +package org.michaelbel.movies.interactor + +import kotlinx.coroutines.flow.StateFlow + +interface SearchInteractor { + + val isSearchActive: StateFlow + + fun setSearchActive(value: Boolean) +} \ No newline at end of file diff --git a/core/interactor/src/main/kotlin/org/michaelbel/movies/interactor/di/InteractorModule.kt b/core/interactor/src/main/kotlin/org/michaelbel/movies/interactor/di/InteractorModule.kt index 9f9695fee..9b4c01d69 100644 --- a/core/interactor/src/main/kotlin/org/michaelbel/movies/interactor/di/InteractorModule.kt +++ b/core/interactor/src/main/kotlin/org/michaelbel/movies/interactor/di/InteractorModule.kt @@ -10,6 +10,7 @@ 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 @@ -17,6 +18,7 @@ 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 @@ -54,6 +56,12 @@ internal interface InteractorModule { interactor: NotificationInteractorImpl ): NotificationInteractor + @Binds + @Singleton + fun provideSearchInteractor( + interactor: SearchInteractorImpl + ): SearchInteractor + @Binds @Singleton fun provideSettingsInteractor( diff --git a/core/interactor/src/main/kotlin/org/michaelbel/movies/interactor/impl/SearchInteractorImpl.kt b/core/interactor/src/main/kotlin/org/michaelbel/movies/interactor/impl/SearchInteractorImpl.kt new file mode 100644 index 000000000..b3a2e677d --- /dev/null +++ b/core/interactor/src/main/kotlin/org/michaelbel/movies/interactor/impl/SearchInteractorImpl.kt @@ -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 = MutableStateFlow(true) + + override val isSearchActive: StateFlow + get() = isActiveMutableFlow.asStateFlow() + + override fun setSearchActive(value: Boolean) { + isActiveMutableFlow.value = value + } +} \ No newline at end of file diff --git a/feature/search-impl/src/main/kotlin/org/michaelbel/movies/search/SearchViewModel.kt b/feature/search-impl/src/main/kotlin/org/michaelbel/movies/search/SearchViewModel.kt index 75f75e0ee..26ecd1354 100644 --- a/feature/search-impl/src/main/kotlin/org/michaelbel/movies/search/SearchViewModel.kt +++ b/feature/search-impl/src/main/kotlin/org/michaelbel/movies/search/SearchViewModel.kt @@ -59,8 +59,7 @@ class SearchViewModel @Inject constructor( private val _query: MutableStateFlow = MutableStateFlow("") private val query: StateFlow = _query.asStateFlow() - private val _active: MutableStateFlow = MutableStateFlow(true) - val active: StateFlow = _active.asStateFlow() + val isSearchActive: StateFlow = interactor.isSearchActive val pagingDataFlow: Flow> = query .flatMapLatest { query -> interactor.moviesPagingData(query) } @@ -75,7 +74,7 @@ class SearchViewModel @Inject constructor( } fun onChangeActiveState(state: Boolean) { - _active.value = state + interactor.setSearchActive(state) } fun onSaveToHistory(movieId: Int) = launch { diff --git a/feature/search-impl/src/main/kotlin/org/michaelbel/movies/search/ui/SearchScreenContent.kt b/feature/search-impl/src/main/kotlin/org/michaelbel/movies/search/ui/SearchScreenContent.kt index 03726590d..8c4aefbcc 100644 --- a/feature/search-impl/src/main/kotlin/org/michaelbel/movies/search/ui/SearchScreenContent.kt +++ b/feature/search-impl/src/main/kotlin/org/michaelbel/movies/search/ui/SearchScreenContent.kt @@ -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, @@ -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 diff --git a/feature/search-impl/src/main/kotlin/org/michaelbel/movies/search/ui/SearchToolbar.kt b/feature/search-impl/src/main/kotlin/org/michaelbel/movies/search/ui/SearchToolbar.kt index 584d40c12..27578e3cf 100644 --- a/feature/search-impl/src/main/kotlin/org/michaelbel/movies/search/ui/SearchToolbar.kt +++ b/feature/search-impl/src/main/kotlin/org/michaelbel/movies/search/ui/SearchToolbar.kt @@ -45,9 +45,7 @@ fun SearchToolbar( onCloseClick: () -> Unit, onInputText: (String) -> Unit, suggestions: List, - onSuggestionClick: (SuggestionDb) -> Unit, searchHistoryMovies: List, - onHistoryMovieClick: (String) -> Unit, onHistoryMovieRemoveClick: (Int) -> Unit, onClearHistoryClick: () -> Unit, modifier: Modifier = Modifier @@ -77,7 +75,7 @@ fun SearchToolbar( } }, trailingIcon = { - if (active) { + if (query.isNotEmpty()) { CloseIcon( onClick = onCloseClick ) @@ -110,7 +108,7 @@ fun SearchToolbar( modifier = Modifier .fillMaxWidth() .height(52.dp) - .clickable { onHistoryMovieClick(movie.title) } + .clickable { onInputText(movie.title) } ) } } @@ -128,7 +126,7 @@ fun SearchToolbar( modifier = Modifier .fillMaxWidth() .height(52.dp) - .clickable { onSuggestionClick(suggestion) } + .clickable { onInputText(suggestion.title) } ) } } @@ -154,9 +152,7 @@ private fun SearchToolbarPreview( onCloseClick = {}, onInputText = {}, suggestions = suggestions, - onSuggestionClick = {}, searchHistoryMovies = emptyList(), - onHistoryMovieClick = {}, onHistoryMovieRemoveClick = {}, onClearHistoryClick = {} ) @@ -181,9 +177,7 @@ private fun SearchToolbarAmoledPreview( onCloseClick = {}, onInputText = {}, suggestions = suggestions, - onSuggestionClick = {}, searchHistoryMovies = emptyList(), - onHistoryMovieClick = {}, onHistoryMovieRemoveClick = {}, onClearHistoryClick = {} )