Skip to content

Instantly share code, notes, and snippets.

View kelmer44's full-sized avatar
🤔

Gabriel Sanmartín kelmer44

🤔
View GitHub Profile
import android.os.Handler
import android.os.Looper
import java.util.concurrent.Executor
import java.util.concurrent.Executors
class TaskRunner<T> {
private val executor: Executor = Executors.newSingleThreadExecutor()
private val handler: Handler = Handler(Looper.getMainLooper())
class PhotoRepositoryImpl(
private val flickrService: FlickrService,
private val apiKey: String
) : PhotoRepository {
private val adapter = PhotoAdapter()
override fun search(term: String): Task<List<Photo>> {
return object : Task<List<Photo>> {
override fun call(): List<Photo> {
val apiResponse = flickrService.getSearch(apiKey, term)
class TaskRunner<T>(private val task: Task<T>) {
val TAG = TaskRunnerImpl::class.java.simpleName + ".TAG"
private val executor: ExecutorService = Executors.newSingleThreadExecutor()
private val handler: Handler = Handler(Looper.getMainLooper())
override fun execute(callback: Callback<T>): Future<*> {
return execute(task, callback)
}
class MainViewModel(private val photoRepository: PhotoRepository) : ViewModel() {
val photoLiveData: MutableLiveData<Resource<PhotoListPage>> = MutableLiveData()
private var lastTask: Future<*>? = null
var lastPage: PhotoListPage? = null
fun search(term: String, page: Int = 1) {
// Cancel if there was a previous request
lastTask?.cancel(true)
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import net.kelmer.android.common.Resource
import net.kelmer.android.data.repository.PhotoRepository
import net.kelmer.android.domain.Photo
import net.kelmer.android.network.Callback
import net.kelmer.android.network.TaskRunner
class MainViewModel(private val photoRepository: PhotoRepository) : ViewModel() {
private val executorService = Executors.newFixedThreadPool(5)
private val handler: Handler = Handler(Looper.getMainLooper())
private fun queuePhoto(url: String, imageView: ImageView, height: Int) {
executorService.submit(ImageLoader(ViewAndUrl(url, imageView, height)))
}
inner class ImageLoader(private val image: ViewAndUrl) : Runnable {
override fun run() {
val bmp = getBitmap(image.url, image.height)
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(
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()
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)
}
override fun getSearch(apiKey: String, term: String): Single<ApiResponse> {
return Single.fromCallable {
searchRequest(apiKey, term) ?: throw Exception("Could not retrieve")
}
}