浏览代码

Return list of photos instead of PhotosPage data, implemented local cache of results in FlickrServiceLogic

Ana Sekuloski 3 年之前
父节点
当前提交
d2db92ce49

+ 4 - 4
app/src/main/java/com/livelike/livelikeandroidchallenge/FlickrServiceViewModel.kt

@@ -5,16 +5,16 @@ import androidx.lifecycle.MutableLiveData
 import androidx.lifecycle.ViewModel
 import androidx.lifecycle.viewModelScope
 import com.livelike.flickersearchlibrary.Flickr
-import com.livelike.flickersearchlibrary.api.model.PhotosPage
+import com.livelike.flickersearchlibrary.api.model.Photo
 import kotlinx.coroutines.launch
 
 class FlickrServiceViewModel : ViewModel() {
 
-    private val _photosPage = MutableLiveData<PhotosPage>()
-    val photosPage: LiveData<PhotosPage> = _photosPage
+    private val _photos = MutableLiveData<List<Photo>>()
+    val photos: LiveData<List<Photo>> = _photos
 
     fun searchFlickrPhotos(searchQuery: String) = viewModelScope.launch {
-        _photosPage.postValue(Flickr.search(searchQuery))
+        _photos.postValue(Flickr.search(searchQuery))
     }
 
 }

+ 1 - 1
app/src/main/java/com/livelike/livelikeandroidchallenge/MainActivity.kt

@@ -14,7 +14,7 @@ class MainActivity : AppCompatActivity() {
         super.onCreate(savedInstanceState)
         setContentView(R.layout.activity_main)
 
-        viewModel.photosPage.observe(this) {
+        viewModel.photos.observe(this) {
             Log.d("LiveLikeTest: ", "Received result: $it")
         }
 

+ 2 - 1
flickersearchlibrary/src/main/java/com/livelike/flickersearchlibrary/api/FlickrApi.kt

@@ -2,6 +2,7 @@ package com.livelike.flickersearchlibrary.api
 
 import com.livelike.flickersearchlibrary.api.model.response.SearchResponse
 import com.livelike.flickersearchlibrary.api.utils.*
+import retrofit2.Response
 import retrofit2.http.GET
 import retrofit2.http.Query
 
@@ -17,5 +18,5 @@ internal interface FlickrApi {
         @Query(QUERY_API_KEY) apiKey: String = DEFAULT_API_KEY,
         @Query(QUERY_FORMAT) format: String = JSON_FORMAT,
         @Query(QUERY_NO_JSON_CALLBACK) noJsonCallback: Boolean = NO_JSON_CALLBACK,
-    ): SearchResponse
+    ): Response<SearchResponse>
 }

+ 1 - 1
flickersearchlibrary/src/main/java/com/livelike/flickersearchlibrary/api/model/PhotosPage.kt

@@ -2,7 +2,7 @@ package com.livelike.flickersearchlibrary.api.model
 
 import com.squareup.moshi.Json
 
-data class PhotosPage(
+internal data class PhotosPage(
     @Json(name = "page")
     val page: Int,
     @Json(name = "pages")

+ 1 - 1
flickersearchlibrary/src/main/java/com/livelike/flickersearchlibrary/api/model/response/SearchResponse.kt

@@ -5,5 +5,5 @@ import com.squareup.moshi.Json
 
 internal data class SearchResponse(
     @Json(name = "photos")
-    val photos: PhotosPage
+    val photosPage: PhotosPage
 )

+ 4 - 4
flickersearchlibrary/src/main/java/com/livelike/flickersearchlibrary/service/FlickrService.kt

@@ -1,6 +1,6 @@
 package com.livelike.flickersearchlibrary.service
 
-import com.livelike.flickersearchlibrary.api.model.PhotosPage
+import com.livelike.flickersearchlibrary.api.model.Photo
 
 /**
  * Interface for the service providing Flickr features.
@@ -8,10 +8,10 @@ import com.livelike.flickersearchlibrary.api.model.PhotosPage
 interface FlickrService {
 
     /**
-     * Searches for photos and retrieves [PhotosPage] data.
+     * Searches for photos and retrieves [List] of [Photo]s.
      *
      * @param searchQuery [String] text query for searching photos.
-     * @return [PhotosPage] data with photos matching [searchQuery]
+     * @return [List] of [Photo]s matching [searchQuery]
      */
-    suspend fun search(searchQuery: String): PhotosPage
+    suspend fun search(searchQuery: String): List<Photo>
 }

+ 19 - 3
flickersearchlibrary/src/main/java/com/livelike/flickersearchlibrary/service/logic/FlickrServiceLogic.kt

@@ -1,7 +1,8 @@
 package com.livelike.flickersearchlibrary.service.logic
 
+import android.util.Log
 import com.livelike.flickersearchlibrary.api.FlickrApi
-import com.livelike.flickersearchlibrary.api.model.PhotosPage
+import com.livelike.flickersearchlibrary.api.model.Photo
 import com.livelike.flickersearchlibrary.service.FlickrService
 
 /**
@@ -9,7 +10,22 @@ import com.livelike.flickersearchlibrary.service.FlickrService
  */
 internal class FlickrServiceLogic(private val api: FlickrApi) : FlickrService {
 
-    override suspend fun search(searchQuery: String): PhotosPage {
-        return api.search(searchQuery).photos // TODO: handle and return errors
+    private val cachedPhotos = mutableListOf<Photo>()
+
+    override suspend fun search(searchQuery: String): List<Photo> {
+        val response = api.search(searchQuery)
+        if (response.isSuccessful) {
+            response.body()?.let {
+                cachedPhotos.clear()
+                cachedPhotos.addAll(it.photosPage.photos)
+            }
+        } else {
+            Log.d(
+                "FlickrService",
+                "Flickr API responded with error: " + response.errorBody().toString()
+            )
+        }
+
+        return cachedPhotos
     }
 }