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
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()) | |
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 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) |
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 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) | |
} |
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 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) |
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
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() { |
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 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) |
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( |
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
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
override fun getSearch(apiKey: String, term: String): Single<ApiResponse> { | |
return Single.fromCallable { | |
searchRequest(apiKey, term) ?: throw Exception("Could not retrieve") | |
} | |
} |
OlderNewer