Skip to content

Commit

Permalink
Merge pull request google-developer-training#33 from google-developer…
Browse files Browse the repository at this point in the history
…-training/retrofit-callbacks

Uses Retrofit *Call* callbacks vs discrete types/suspend functions
  • Loading branch information
android-dev-lxl committed Aug 26, 2020
2 parents e339470 + 48b4486 commit 64e2463
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
13 changes: 10 additions & 3 deletions DevBytes-starter/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ android {
buildFeatures {
dataBinding true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
}

dependencies {
Expand Down Expand Up @@ -70,12 +77,12 @@ dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines"

// retrofit for networking
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2'
implementation 'com.squareup.retrofit2:converter-moshi:2.5.0'
implementation 'com.squareup.retrofit2:converter-moshi:2.9.0'

// moshi for parsing the JSON format
def moshi_version = "1.6.0"
def moshi_version = "1.9.3"
implementation "com.squareup.moshi:moshi:$moshi_version"
implementation "com.squareup.moshi:moshi-kotlin:$moshi_version"
kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshi_version"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import retrofit2.http.GET
*/
interface DevbyteService {
@GET("devbytes")
fun getPlaylist(): Deferred<NetworkVideoContainer>
suspend fun getPlaylist(): NetworkVideoContainer
}

/**
Expand All @@ -43,11 +43,7 @@ object DevByteNetwork {
private val retrofit = Retrofit.Builder()
.baseUrl("https://android-kotlin-fun-mars-server.appspot.com/")
.addConverterFactory(MoshiConverterFactory.create())
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.build()

val devbytes = retrofit.create(DevbyteService::class.java)

}


Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class DevByteViewModel(application: Application) : AndroidViewModel(application)
private fun refreshDataFromNetwork() = viewModelScope.launch {

try {
val playlist = DevByteNetwork.devbytes.getPlaylist().await()
val playlist = DevByteNetwork.devbytes.getPlaylist()
_playlist.postValue(playlist.asDomainModel())

_eventNetworkError.value = false
Expand Down

0 comments on commit 64e2463

Please sign in to comment.