Explorar o código

Added FlickrService interface, implementation and dependencies for it

Ana Sekuloski %!s(int64=3) %!d(string=hai) anos
pai
achega
2e952e0f87

+ 3 - 0
flickersearchlibrary/build.gradle

@@ -32,6 +32,9 @@ dependencies {
     implementation "com.squareup.retrofit2:converter-moshi:2.9.0"
     implementation "com.squareup.moshi:moshi-kotlin:1.13.0"
 
+    // Koin for dependencies
+    implementation "io.insert-koin:koin-android:3.1.5"
+
     androidTestImplementation 'androidx.test.ext:junit:1.1.3'
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
 }

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

@@ -10,7 +10,7 @@ import retrofit2.http.Query
  */
 internal interface FlickrApi {
 
-    @GET("services/rest")
+    @GET
     suspend fun search(
         @Query(QUERY_TEXT) searchQuery: String,
         @Query(QUERY_METHOD) method: String = SEARCH_METHOD,

+ 3 - 0
flickersearchlibrary/src/main/java/com/livelike/flickersearchlibrary/api/utils/Constants.kt

@@ -1,5 +1,8 @@
 package com.livelike.flickersearchlibrary.api.utils
 
+// Base URL for the Flickr API
+internal const val BASE_URL = "https://www.flickr.com/services/rest/"
+
 // Constant values for the FlickrApi interface
 internal const val DEFAULT_API_KEY = "3976fde3792b699fbcda31f52e1cb306"
 internal const val JSON_FORMAT = "json"

+ 32 - 0
flickersearchlibrary/src/main/java/com/livelike/flickersearchlibrary/di/Dependencies.kt

@@ -0,0 +1,32 @@
+package com.livelike.flickersearchlibrary.di
+
+import com.livelike.flickersearchlibrary.api.FlickrApi
+import com.livelike.flickersearchlibrary.api.utils.BASE_URL
+import com.livelike.flickersearchlibrary.service.logic.FlickrServiceLogic
+import org.koin.dsl.module
+import retrofit2.Retrofit
+import retrofit2.converter.moshi.MoshiConverterFactory
+
+/**
+ * Koin module that declares dependencies required by Flickr API.
+ */
+internal val flickrApiModule = module {
+
+    single {
+        val retrofit = Retrofit.Builder()
+            .baseUrl(BASE_URL)
+            .addConverterFactory(MoshiConverterFactory.create())
+            .build()
+        retrofit.create(FlickrApi::class.java)
+    }
+
+}
+
+/**
+ * Koin module that declares dependencies required by Flickr service features.
+ */
+val flickrModule = module {
+
+    single { FlickrServiceLogic(get()) }
+
+}

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

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

+ 15 - 0
flickersearchlibrary/src/main/java/com/livelike/flickersearchlibrary/service/logic/FlickrServiceLogic.kt

@@ -0,0 +1,15 @@
+package com.livelike.flickersearchlibrary.service.logic
+
+import com.livelike.flickersearchlibrary.api.FlickrApi
+import com.livelike.flickersearchlibrary.api.model.PhotosPage
+import com.livelike.flickersearchlibrary.service.FlickrService
+
+/**
+ * Implementation of [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
+    }
+}