This file contains hidden or 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 AnswerApiDto.toDomain() = Answer( | |
answer = answer, | |
forced = forced, | |
imageUrl = image | |
) |
This file contains hidden or 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 AnswerRepositoryImpl( | |
private val answerApiService: AnswerApiService | |
//In this example there isn't DAO for answer | |
) : AnswerRepository{ | |
override fun getAnswer(): Observable<Answer> { | |
return answerApiService.getAnswer().map { it.toDomain()} | |
} | |
} |
This file contains hidden or 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
....... | |
activity?.let { | |
//we pass the activity as lifecycle owner | |
answerViewModel.answer.observe(it, Observer { result -> | |
when (result) { | |
is Result.Success -> { | |
loadGifFromURL(result.value.image, requireContext(), answerGif) | |
} | |
is Result.Failure -> { |
This file contains hidden or 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
sealed class Result<T> { | |
data class Success<T>(val value: T) : Result<T>() | |
data class Failure<T>(val throwable: Throwable) : Result<T>() | |
} |
This file contains hidden or 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 AnswerViewModel ( | |
private val answerRepository: AnswerRepository | |
): ViewModel() { | |
private var disposable: Disposable? = null | |
val answer by lazy { | |
MutableLiveData<Result<Answer>>() | |
} |
This file contains hidden or 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
object RetrofitClient { | |
private val retrofit by lazy { | |
val okHttpClient = OkHttpClient.Builder() | |
.build() | |
Retrofit.Builder() | |
.baseUrl("https://yesno.wtf/") | |
.client(okHttpClient) |
This file contains hidden or 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 AnswerApiService { | |
@GET("/api") | |
fun getAnswer(): Observable<AnswerApiDto> | |
} |
This file contains hidden or 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
data class AnswerApiDto ( | |
@SerializedName("answer") | |
val answer: String, | |
@SerializedName("forced") | |
val forced: Boolean, | |
@SerializedName("image") | |
val image: String | |
) |
This file contains hidden or 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
apply plugin: 'com.android.application' | |
apply plugin: 'kotlin-android' | |
apply plugin: 'kotlin-android-extensions' | |
android { | |
compileSdkVersion 29 | |
buildToolsVersion "29.0.3" | |
defaultConfig { | |
applicationId "com.alfgarsan.android.retrofitrxyesno" |
This file contains hidden or 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 kotlinx.coroutines.* | |
fun main() { | |
GlobalScope.launch { | |
println("[${Thread.currentThread().name}] Before asyncs") | |
val one = async (Dispatchers.IO) { doSomething1() } | |
val two = async (Dispatchers.IO) { doSomething2() } | |
println("[${Thread.currentThread().name}] After asyncs") | |
println("[${Thread.currentThread().name}] The answer is ${one.await() + two.await()}") |