Skip to content

Commit

Permalink
Merge pull request #6 from Jaehwa-Noh/compose-flight-search-app
Browse files Browse the repository at this point in the history
Sort by passengers
  • Loading branch information
Jaehwa-Noh authored May 17, 2024
2 parents 123e05e + 8ae19b8 commit 7fc8f06
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class InDiskAirportsRepository @Inject constructor(
id = airport.id,
iataCode = airport.iataCode,
name = airport.name,
passengers = airport.passengers,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ fun AirportEntity.asSuggestionAirport() = SuggestionAirport(
id = id,
iataCode = iataCode,
name = name,
passengers = passengers,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ data class SuggestionAirport(
val id: Long,
val iataCode: String,
val name: String,
val passengers: Long,
)
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ fun SuggestListItemUiComponentPreview() {
id = 1,
iataCode = "ABC",
name = "Airport",
passengers = 10,
),
onItemClick = {},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ fun SuggestListScreenPreview() {
id = 1,
iataCode = "ABC",
name = "Airport",
passengers = 1,
),
SuggestionAirport(
id = 2,
iataCode = "DEF",
name = "Airport2",
passengers = 2,
),
),
onSuggestionClick = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.ShowFavorite>(searchScreenUiState)
assertEquals(searchScreenUiState.results.count(), 0)

searchScreenViewModel.setSearchQuery("C")
searchScreenViewModel.setSavedSearchText("C")
searchScreenUiState = searchScreenViewModel.searchScreenUiState.first()

assertIs<SearchScreenUiState.ShowSuggests>(searchScreenUiState)
val expectOrder = listOf<Long>(9, 8)
searchScreenUiState.results.forEachIndexed { index, suggestionAirport ->
assertEquals(expectOrder[index], suggestionAirport.passengers)
}
}

@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun departureId_notIn_matchUiState() = runTest {
Expand Down Expand Up @@ -198,7 +223,7 @@ class SearchScreenViewModelTest {
)

searchScreenViewModel.setSavedSearchText("")
searchScreenUiState = searchScreenViewModel.searchScreenUiState.value
searchScreenUiState = searchScreenViewModel.searchScreenUiState.first()

assertIs<SearchScreenUiState.ShowFavorite>(searchScreenUiState)
assertEquals(searchScreenUiState.results.count(), 1)
Expand All @@ -223,7 +248,7 @@ class SearchScreenViewModelTest {
)

searchScreenViewModel.setSavedSearchText("")
searchScreenUiState = searchScreenViewModel.searchScreenUiState.value
searchScreenUiState = searchScreenViewModel.searchScreenUiState.first()

assertIs<SearchScreenUiState.ShowFavorite>(searchScreenUiState)
assertEquals(searchScreenUiState.results.count(), 1)
Expand All @@ -234,9 +259,18 @@ class SearchScreenViewModelTest {
)

searchScreenViewModel.setSavedSearchText("")
searchScreenUiState = searchScreenViewModel.searchScreenUiState.value
searchScreenUiState = searchScreenViewModel.searchScreenUiState.first()

assertIs<SearchScreenUiState.ShowFavorite>(searchScreenUiState)
assertEquals(searchScreenUiState.results.count(), 0)
}

private fun getAirportFtsEntities() = airportEntitiesTestData.map {
it.asFtsEntity()
}

private suspend fun initDb() {
val ftsEntities = getAirportFtsEntities()
airportsFtsRepository.deleteAndInsertAll(ftsEntities)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ fun testAirportEntity(id: Long, code: String) = AirportEntity(
id = id,
name = "name$id",
iataCode = code,
passengers = id,
passengers = 10 - id,
)

0 comments on commit 7fc8f06

Please sign in to comment.