Last active
January 9, 2018 01:08
-
-
Save scottroemeschke/08e38f224f1db44391f4fad80a34e88c to your computer and use it in GitHub Desktop.
part of conversation: https://www.reddit.com/r/androiddev/comments/7owabf/reactive_apps_architecture_design_asymnetry_that/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class SearchByTagRequest(val tag: String) | |
data class ImageColor(val hexValue: String) //some validation on the value here | |
data class SearchByColorRequest(val includedColors: List<ImageColor>, val excludedColors: List<ImageColor>) | |
interface ImageSearcher { | |
fun searchForImageByTag(requests: Observable<SearchByTagRequest>): Observable<SearchResults> | |
fun searchForImageByColors(requests: Observable<SearchByColorRequest>): Observable<SearchResults> | |
} | |
class GoogleImageSearcher(val googleImagesApi: GoogleImagesApi, val analyticsClient: AnalyticsThing): ImageSearcher { | |
override fun searchForImageByTag(searchRequests: Observable<SearchByTagRequest>) { | |
return searchRequests.filter { | |
it.length > 3 //google doesn't search for less than three characters for this example | |
}.switchMap { | |
googleImagesApi.searchForImageByTag(it.tag) | |
}.map(this::apiResultToGeneralSearchResults) | |
} | |
override fun searchForImageByColors(searchRequests: Observable<SearchByColorRequest>) { | |
return searchRequests | |
.doOnNext { | |
//some logging perhaps or analytics around most searched for colors, I dunno. | |
//you can see here with this component, you can do whatever RX-y stuff you want, | |
//because you pass in a stream, get back a stream. | |
//and nothing happens until you subscribe | |
analyticsClient.trackIncludedColorsSearchRequest(it.includedColors) | |
} .switchMap { | |
googleImagesApi.searchForByColors(mapColorsToGoogleApiColors(it.includedColors, it.excludedColors) | |
}.map(this::apiResultToGeneralSearchResults) | |
} | |
private fun apiResultToGeneralSearchResults(response: GoogleImagesApiResponse) { | |
//blah blah whatever you need to do | |
} | |
private fun mapColorsToGoogleApiColors(included: List<ImageColor>, excluded: List<ImageColor> { | |
//blah blah transform to the way the google image api expects these to be, like maybe a comma separated string, whatever.. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment