-
Notifications
You must be signed in to change notification settings - Fork 2
[Summit prep] Add a search feature #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -20,8 +20,28 @@ class CountryListViewModel( | |||||||||
val error: CountryListError? = null, | ||||||||||
val serverStatus: ServerStatus? = null, | ||||||||||
val navigationTarget: Country? = null, | ||||||||||
var searchQuery: String = "", | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] Defining
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||
) | ||||||||||
|
||||||||||
val filteredContinents: List<Continent> | ||||||||||
get() { | ||||||||||
val query = _state.value?.searchQuery.orEmpty() | ||||||||||
return _state.value?.continents | ||||||||||
?.map { continent -> | ||||||||||
val filteredCountries = continent.countries.filter { | ||||||||||
it.countryName.contains(query, ignoreCase = true) || | ||||||||||
it.regionCode.contains(query, ignoreCase = true) | ||||||||||
} | ||||||||||
continent.copy(countries = filteredCountries) | ||||||||||
} | ||||||||||
?.filter { it.countries.isNotEmpty() } | ||||||||||
?: emptyList() | ||||||||||
} | ||||||||||
|
||||||||||
fun updateSearchQuery(query: String) { | ||||||||||
_state.onNext(_state.value!!.copy(searchQuery = query)) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||
} | ||||||||||
|
||||||||||
private val _state: BehaviorSubject<UiState> = BehaviorSubject.createDefault(UiState()) | ||||||||||
val state: Observable<UiState> = _state | ||||||||||
|
||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,4 +80,44 @@ class CountryListViewModelTests: KoinTest { | |
} | ||
} | ||
} | ||
|
||
@Nested | ||
@DisplayName("filteredContinents") | ||
inner class FilteredContinents { | ||
@Test | ||
fun `returns only continents with matching countries`() { | ||
// Arrange: set up continents with countries | ||
val continents = listOf( | ||
com.example.domainmodels.Continent( | ||
name = "Europe", | ||
countries = listOf( | ||
com.example.domainmodels.Country(regionCode = "FR"), | ||
com.example.domainmodels.Country(regionCode = "DE") | ||
) | ||
), | ||
com.example.domainmodels.Continent( | ||
name = "Asia", | ||
countries = listOf( | ||
com.example.domainmodels.Country(regionCode = "JP"), | ||
com.example.domainmodels.Country(regionCode = "CN") | ||
) | ||
) | ||
) | ||
// Set continents in state | ||
val stateField = CountryListViewModel::class.java.getDeclaredField("_state") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] The test uses reflection to access a private field Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
stateField.isAccessible = true | ||
val subject = stateField.get(viewModel) as io.reactivex.rxjava3.subjects.BehaviorSubject<CountryListViewModel.UiState> | ||
subject.onNext(CountryListViewModel.UiState(continents = continents)) | ||
|
||
// Act: update search query | ||
viewModel.updateSearchQuery("fr") | ||
|
||
// Assert: only Europe with France should match | ||
val filtered = viewModel.filteredContinents | ||
filtered.size.shouldBeEqualTo(1) | ||
filtered[0].name.shouldBeEqualTo("Europe") | ||
filtered[0].countries.size.shouldBeEqualTo(1) | ||
filtered[0].countries[0].regionCode.shouldBeEqualTo("FR") | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The label string is hardcoded; for localization support, consider moving it into
strings.xml
and referring to it via a resource ID.Copilot uses AI. Check for mistakes.