From 928eecc55e18bd970b93e3924e3fe42db524192c Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Fri, 17 May 2024 21:28:56 +0900 Subject: [PATCH 1/6] Add passengers property. --- .../com/example/flightsearchapp/data/AirportsRepository.kt | 5 +++-- .../example/flightsearchapp/data/database/AirportEntity.kt | 1 + .../example/flightsearchapp/ui/model/SuggestionAirport.kt | 1 + .../ui/suggestion/SuggestionsListItemUiComponent.kt | 1 + .../flightsearchapp/ui/suggestion/SuggestionsListScreen.kt | 2 ++ 5 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/data/AirportsRepository.kt b/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/data/AirportsRepository.kt index 58733474..41bb9a19 100644 --- a/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/data/AirportsRepository.kt +++ b/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/data/AirportsRepository.kt @@ -3,12 +3,12 @@ package com.example.flightsearchapp.data import com.example.flightsearchapp.data.database.AirportEntity import com.example.flightsearchapp.di.DispatcherDefault import com.example.flightsearchapp.ui.model.SuggestionAirport -import javax.inject.Inject -import javax.inject.Singleton import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.map import kotlinx.coroutines.withContext +import javax.inject.Inject +import javax.inject.Singleton interface AirportsRepository { suspend fun getSuggestionsStream(query: String): Flow> @@ -33,6 +33,7 @@ class InDiskAirportsRepository @Inject constructor( id = airport.id, iataCode = airport.iataCode, name = airport.name, + passengers = airport.passengers, ) } } diff --git a/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/data/database/AirportEntity.kt b/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/data/database/AirportEntity.kt index fdbcc6a5..6de0e371 100644 --- a/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/data/database/AirportEntity.kt +++ b/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/data/database/AirportEntity.kt @@ -29,4 +29,5 @@ fun AirportEntity.asSuggestionAirport() = SuggestionAirport( id = id, iataCode = iataCode, name = name, + passengers = passengers, ) diff --git a/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/ui/model/SuggestionAirport.kt b/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/ui/model/SuggestionAirport.kt index 751cf51f..b222150f 100644 --- a/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/ui/model/SuggestionAirport.kt +++ b/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/ui/model/SuggestionAirport.kt @@ -4,4 +4,5 @@ data class SuggestionAirport( val id: Long, val iataCode: String, val name: String, + val passengers: Long, ) diff --git a/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/ui/suggestion/SuggestionsListItemUiComponent.kt b/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/ui/suggestion/SuggestionsListItemUiComponent.kt index eb06680b..7ce1c99d 100644 --- a/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/ui/suggestion/SuggestionsListItemUiComponent.kt +++ b/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/ui/suggestion/SuggestionsListItemUiComponent.kt @@ -33,6 +33,7 @@ fun SuggestListItemUiComponentPreview() { id = 1, iataCode = "ABC", name = "Airport", + passengers = 10, ), onItemClick = {}, ) diff --git a/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/ui/suggestion/SuggestionsListScreen.kt b/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/ui/suggestion/SuggestionsListScreen.kt index b568664f..53b4398c 100644 --- a/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/ui/suggestion/SuggestionsListScreen.kt +++ b/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/ui/suggestion/SuggestionsListScreen.kt @@ -40,11 +40,13 @@ fun SuggestListScreenPreview() { id = 1, iataCode = "ABC", name = "Airport", + passengers = 1, ), SuggestionAirport( id = 2, iataCode = "DEF", name = "Airport2", + passengers = 2, ), ), onSuggestionClick = {}, From 6f8b9240e224ea08f16bd85ee4fe9024545f5d7c Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Fri, 17 May 2024 21:29:13 +0900 Subject: [PATCH 2/6] Change passengers value. --- .../testing/model/database/AirportEntitiesTestData.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Compose-Flight-Search-App/app/src/test/kotlin/com/example/flightsearchapp/testing/model/database/AirportEntitiesTestData.kt b/Compose-Flight-Search-App/app/src/test/kotlin/com/example/flightsearchapp/testing/model/database/AirportEntitiesTestData.kt index 49b2a9a3..17317fde 100644 --- a/Compose-Flight-Search-App/app/src/test/kotlin/com/example/flightsearchapp/testing/model/database/AirportEntitiesTestData.kt +++ b/Compose-Flight-Search-App/app/src/test/kotlin/com/example/flightsearchapp/testing/model/database/AirportEntitiesTestData.kt @@ -19,5 +19,5 @@ fun testAirportEntity(id: Long, code: String) = AirportEntity( id = id, name = "name$id", iataCode = code, - passengers = id, + passengers = 10-id, ) From b9a796f6b1e56a0621448fc8ac3df28aba594da9 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Fri, 17 May 2024 21:29:51 +0900 Subject: [PATCH 3/6] Add sortedByDescending passengers. --- .../com/example/flightsearchapp/ui/SearchScreenViewModel.kt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/ui/SearchScreenViewModel.kt b/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/ui/SearchScreenViewModel.kt index 1d61e973..86433638 100644 --- a/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/ui/SearchScreenViewModel.kt +++ b/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/ui/SearchScreenViewModel.kt @@ -13,7 +13,6 @@ import com.example.flightsearchapp.domain.SetSavedSearchTextUseCase import com.example.flightsearchapp.ui.model.Flight import com.example.flightsearchapp.ui.model.SuggestionAirport import dagger.hilt.android.lifecycle.HiltViewModel -import javax.inject.Inject import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.SharingStarted @@ -26,6 +25,7 @@ import kotlinx.coroutines.flow.mapLatest import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch +import javax.inject.Inject @HiltViewModel class SearchScreenViewModel @Inject constructor( @@ -74,8 +74,10 @@ class SearchScreenViewModel @Inject constructor( } else { getSuggestionsStreamUseCase(query = savedSearchText) .mapLatest { searchedApi -> + val sortedSearchedApi = searchedApi + .sortedByDescending { it.passengers } SearchScreenUiState.ShowSuggests( - results = searchedApi, + results = sortedSearchedApi, ) } } From 6f0d5f11d74e405520dbe52758d852cc9722550a Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Fri, 17 May 2024 21:33:20 +0900 Subject: [PATCH 4/6] Change `value` into `first()`. --- .../example/flightsearchapp/SearchScreenViewModelTest.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Compose-Flight-Search-App/app/src/test/kotlin/com/example/flightsearchapp/SearchScreenViewModelTest.kt b/Compose-Flight-Search-App/app/src/test/kotlin/com/example/flightsearchapp/SearchScreenViewModelTest.kt index a47a40a6..08b23131 100644 --- a/Compose-Flight-Search-App/app/src/test/kotlin/com/example/flightsearchapp/SearchScreenViewModelTest.kt +++ b/Compose-Flight-Search-App/app/src/test/kotlin/com/example/flightsearchapp/SearchScreenViewModelTest.kt @@ -198,7 +198,7 @@ class SearchScreenViewModelTest { ) searchScreenViewModel.setSavedSearchText("") - searchScreenUiState = searchScreenViewModel.searchScreenUiState.value + searchScreenUiState = searchScreenViewModel.searchScreenUiState.first() assertIs(searchScreenUiState) assertEquals(searchScreenUiState.results.count(), 1) @@ -223,7 +223,7 @@ class SearchScreenViewModelTest { ) searchScreenViewModel.setSavedSearchText("") - searchScreenUiState = searchScreenViewModel.searchScreenUiState.value + searchScreenUiState = searchScreenViewModel.searchScreenUiState.first() assertIs(searchScreenUiState) assertEquals(searchScreenUiState.results.count(), 1) @@ -234,7 +234,7 @@ class SearchScreenViewModelTest { ) searchScreenViewModel.setSavedSearchText("") - searchScreenUiState = searchScreenViewModel.searchScreenUiState.value + searchScreenUiState = searchScreenViewModel.searchScreenUiState.first() assertIs(searchScreenUiState) assertEquals(searchScreenUiState.results.count(), 0) From f78418e78271f47a0a2ec303ce742f1a7e4d934b Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Fri, 17 May 2024 22:14:02 +0900 Subject: [PATCH 5/6] Add Test function - searchQuery_find_matchOrder - getAirportFtsEntities - initDb --- .../SearchScreenViewModelTest.kt | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Compose-Flight-Search-App/app/src/test/kotlin/com/example/flightsearchapp/SearchScreenViewModelTest.kt b/Compose-Flight-Search-App/app/src/test/kotlin/com/example/flightsearchapp/SearchScreenViewModelTest.kt index 08b23131..31e154e8 100644 --- a/Compose-Flight-Search-App/app/src/test/kotlin/com/example/flightsearchapp/SearchScreenViewModelTest.kt +++ b/Compose-Flight-Search-App/app/src/test/kotlin/com/example/flightsearchapp/SearchScreenViewModelTest.kt @@ -4,6 +4,7 @@ import com.example.flightsearchapp.data.AllFlightsRepository import com.example.flightsearchapp.data.InDiskAirportsFtsRepository import com.example.flightsearchapp.data.InDiskAirportsRepository import com.example.flightsearchapp.data.InDiskFavoritesRepository +import com.example.flightsearchapp.data.database.asFtsEntity import com.example.flightsearchapp.domain.DeleteFavoriteUseCase import com.example.flightsearchapp.domain.GetAllFavoriteFlightsStreamUseCase import com.example.flightsearchapp.domain.GetAllFlightsStreamUseCase @@ -147,6 +148,30 @@ class SearchScreenViewModelTest { assertEquals("Set", result) } + @OptIn(ExperimentalCoroutinesApi::class) + @Test + fun searchQuery_find_matchOrder() = runTest { + initDb() + backgroundScope.launch(UnconfinedTestDispatcher()) { + searchScreenViewModel.showFlightUiState.collect() + searchScreenViewModel.searchScreenUiState.collect() + } + + var searchScreenUiState = searchScreenViewModel.searchScreenUiState.first() + assertIs(searchScreenUiState) + assertEquals(searchScreenUiState.results.count(), 0) + + searchScreenViewModel.setSearchQuery("C") + searchScreenViewModel.setSavedSearchText("C") + searchScreenUiState = searchScreenViewModel.searchScreenUiState.first() + + assertIs(searchScreenUiState) + val expectOrder = listOf(9, 8) + searchScreenUiState.results.forEachIndexed { index, suggestionAirport -> + assertEquals(expectOrder[index], suggestionAirport.passengers) + } + } + @OptIn(ExperimentalCoroutinesApi::class) @Test fun departureId_notIn_matchUiState() = runTest { @@ -239,4 +264,13 @@ class SearchScreenViewModelTest { assertIs(searchScreenUiState) assertEquals(searchScreenUiState.results.count(), 0) } + + private fun getAirportFtsEntities() = airportEntitiesTestData.map { + it.asFtsEntity() + } + + private suspend fun initDb() { + val ftsEntities = getAirportFtsEntities() + airportsFtsRepository.deleteAndInsertAll(ftsEntities) + } } From e2cac8a0fdbc71f1b46b40ae6e87fb83de06fca2 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Fri, 17 May 2024 22:15:27 +0900 Subject: [PATCH 6/6] Fix spotless. --- .../com/example/flightsearchapp/data/AirportsRepository.kt | 4 ++-- .../testing/model/database/AirportEntitiesTestData.kt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/data/AirportsRepository.kt b/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/data/AirportsRepository.kt index 41bb9a19..b0ff9d7d 100644 --- a/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/data/AirportsRepository.kt +++ b/Compose-Flight-Search-App/app/src/main/kotlin/com/example/flightsearchapp/data/AirportsRepository.kt @@ -3,12 +3,12 @@ package com.example.flightsearchapp.data import com.example.flightsearchapp.data.database.AirportEntity import com.example.flightsearchapp.di.DispatcherDefault import com.example.flightsearchapp.ui.model.SuggestionAirport +import javax.inject.Inject +import javax.inject.Singleton import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.map import kotlinx.coroutines.withContext -import javax.inject.Inject -import javax.inject.Singleton interface AirportsRepository { suspend fun getSuggestionsStream(query: String): Flow> diff --git a/Compose-Flight-Search-App/app/src/test/kotlin/com/example/flightsearchapp/testing/model/database/AirportEntitiesTestData.kt b/Compose-Flight-Search-App/app/src/test/kotlin/com/example/flightsearchapp/testing/model/database/AirportEntitiesTestData.kt index 17317fde..8242b397 100644 --- a/Compose-Flight-Search-App/app/src/test/kotlin/com/example/flightsearchapp/testing/model/database/AirportEntitiesTestData.kt +++ b/Compose-Flight-Search-App/app/src/test/kotlin/com/example/flightsearchapp/testing/model/database/AirportEntitiesTestData.kt @@ -19,5 +19,5 @@ fun testAirportEntity(id: Long, code: String) = AirportEntity( id = id, name = "name$id", iataCode = code, - passengers = 10-id, + passengers = 10 - id, )