|
@@ -17,23 +17,33 @@ internal class FlickrPhotosViewModel : ViewModel() {
|
|
|
|
|
|
fun getRecentPhotos() = viewModelScope.launch {
|
|
|
_photos.postValue(Flickr.getRecentPhotos())
|
|
|
- visiblePhotosState = PhotosState(PhotoType.RECENT, 1)
|
|
|
+ visiblePhotosState = PhotosState(PhotoType.RECENT, 1, null)
|
|
|
}
|
|
|
|
|
|
fun searchPhotos(searchQuery: String?) = viewModelScope.launch {
|
|
|
_photos.postValue(Flickr.search(searchQuery))
|
|
|
- visiblePhotosState = PhotosState(PhotoType.SEARCH, 1)
|
|
|
+ visiblePhotosState = PhotosState(PhotoType.SEARCH, 1, searchQuery)
|
|
|
}
|
|
|
|
|
|
fun loadMorePhotos() = viewModelScope.launch {
|
|
|
val nextPage = visiblePhotosState.currentPage + 1
|
|
|
- if (visiblePhotosState.type == PhotoType.RECENT) {
|
|
|
- _photos.postValue(Flickr.getRecentPhotos(page = nextPage))
|
|
|
+ when (visiblePhotosState.type) {
|
|
|
+ PhotoType.RECENT -> _photos.postValue(Flickr.getRecentPhotos(page = nextPage))
|
|
|
+ PhotoType.SEARCH -> _photos.postValue(
|
|
|
+ Flickr.search(
|
|
|
+ page = nextPage,
|
|
|
+ searchQuery = visiblePhotosState.searchQuery
|
|
|
+ )
|
|
|
+ )
|
|
|
}
|
|
|
visiblePhotosState = visiblePhotosState.copy(currentPage = nextPage)
|
|
|
}
|
|
|
|
|
|
- private data class PhotosState(var type: PhotoType, var currentPage: Int)
|
|
|
+ private data class PhotosState(
|
|
|
+ var type: PhotoType,
|
|
|
+ var currentPage: Int,
|
|
|
+ var searchQuery: String?
|
|
|
+ )
|
|
|
|
|
|
private enum class PhotoType {
|
|
|
RECENT,
|