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
override fun search(term: String, page: Int): PhotoListPage { | |
val apiResponse = flickrService.search(apiKey, term, PERPAGE, page) | |
return PhotoListPage(term, | |
page, apiResponse.photos.photo.map(adapter::convert), page<apiResponse.photos.pages) | |
} |
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
interface FlickrService { | |
fun search( | |
apiKey: String, term: String, perPage: Int, page: Int | |
): ApiResponse | |
} |
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
fun load(url: String, imageView: ImageView, @DrawableRes loader: Int, height: Int) { | |
imageViews[imageView] = url | |
queuePhoto(url, imageView, height) | |
imageView.setImageResource(loader) | |
} |
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
override fun deserialize(json: String): ApiResponse { | |
val root = JSONObject(json) | |
val stat = root.getString("stat") | |
val photos = root.getJSONObject("photos") | |
val page = photos.getInt("page") | |
val pages = photos.getInt("pages") | |
val perPage = photos.getInt("perpage") |
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
class GsonSerializer(private val gson: Gson): Serializer{ | |
override fun deserialize(json: String): ApiResponse { | |
return gson.fromJson<ApiResponse>(json, ApiResponse::class.java) | |
} | |
} |
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
val inputStream = connection.inputStream ?: throw HttpException( | |
responseCode, | |
"Error executing request" | |
) | |
reader = BufferedReader(InputStreamReader(inputStream)) | |
val readLines = reader.readLines() | |
if (readLines.isEmpty()) { | |
return "" | |
} |
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
override fun getSearch(apiKey: String, term: String): Single<ApiResponse> { | |
return Single.fromCallable { | |
searchRequest(apiKey, term) ?: throw Exception("Could not retrieve") | |
} | |
} |
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
private fun searchRequest(apiKey: String, term: String) : ApiResponse?{ | |
val fullUrl = | |
"$baseUrl/services/rest?method=flickr.photos.search&format=json&nojsoncallback=1&api_key=$apiKey&text=$term" | |
val response = client.doGet(fullUrl) ?: return null | |
return deserialize(response) | |
} |
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
class StringResponseHttpClient : HttpClient { | |
override fun doGet(serviceUrl: String) : String? { | |
var reader: BufferedReader? = null | |
try { | |
val url = URL(serviceUrl) | |
val connection = url.openConnection() as HttpURLConnection | |
connection.requestMethod = "GET" | |
connection.connect() |
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
interface ServiceLocator { | |
companion object { | |
private val LOCK = Any() | |
private var instance: ServiceLocator? = null | |
fun instance(context: Context): ServiceLocator { | |
synchronized(LOCK) { | |
if (instance == null) { | |
instance = DefaultServiceLocator( |