mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-18 05:37:02 +05:30
all: use retrofit suspend support to hide implementation details of API
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
parent
a3b1e02783
commit
a66186adc3
3 changed files with 3 additions and 20 deletions
|
@ -20,9 +20,6 @@ import dev.msfjarvis.lobsters.ui.LobstersItem
|
||||||
import dev.msfjarvis.lobsters.ui.LobstersTheme
|
import dev.msfjarvis.lobsters.ui.LobstersTheme
|
||||||
import dev.msfjarvis.lobsters.urllauncher.UrlLauncher
|
import dev.msfjarvis.lobsters.urllauncher.UrlLauncher
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import retrofit2.Call
|
|
||||||
import retrofit2.Callback
|
|
||||||
import retrofit2.Response
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
val UrlLauncherAmbient = ambientOf<UrlLauncher> { error("Needs to be provided") }
|
val UrlLauncherAmbient = ambientOf<UrlLauncher> { error("Needs to be provided") }
|
||||||
|
@ -40,20 +37,7 @@ class MainActivity : AppCompatActivity() {
|
||||||
val coroutineScope = rememberCoroutineScope()
|
val coroutineScope = rememberCoroutineScope()
|
||||||
val posts = mutableStateListOf<LobstersPost>()
|
val posts = mutableStateListOf<LobstersPost>()
|
||||||
coroutineScope.launch {
|
coroutineScope.launch {
|
||||||
apiClient.getHottestPosts().enqueue(object : Callback<List<LobstersPost>> {
|
posts.addAll(apiClient.getHottestPosts())
|
||||||
override fun onResponse(
|
|
||||||
call: Call<List<LobstersPost>>,
|
|
||||||
response: Response<List<LobstersPost>>
|
|
||||||
) {
|
|
||||||
if (response.isSuccessful) {
|
|
||||||
response.body()?.let { posts.addAll(it) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onFailure(call: Call<List<LobstersPost>>, t: Throwable) {
|
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
LobstersApp(posts)
|
LobstersApp(posts)
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ dependencies {
|
||||||
def moshi_version = "1.9.3"
|
def moshi_version = "1.9.3"
|
||||||
def retrofit_version = "2.9.0"
|
def retrofit_version = "2.9.0"
|
||||||
implementation project(":model")
|
implementation project(":model")
|
||||||
api "com.squareup.retrofit2:retrofit:$retrofit_version"
|
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
|
||||||
implementation "com.squareup.retrofit2:converter-moshi:$retrofit_version"
|
implementation "com.squareup.retrofit2:converter-moshi:$retrofit_version"
|
||||||
kaptTest "com.squareup.moshi:moshi-kotlin-codegen:$moshi_version"
|
kaptTest "com.squareup.moshi:moshi-kotlin-codegen:$moshi_version"
|
||||||
testImplementation 'junit:junit:4.13'
|
testImplementation 'junit:junit:4.13'
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
package dev.msfjarvis.lobsters.api
|
package dev.msfjarvis.lobsters.api
|
||||||
|
|
||||||
import dev.msfjarvis.lobsters.model.LobstersPost
|
import dev.msfjarvis.lobsters.model.LobstersPost
|
||||||
import retrofit2.Call
|
|
||||||
import retrofit2.http.GET
|
import retrofit2.http.GET
|
||||||
|
|
||||||
interface LobstersApi {
|
interface LobstersApi {
|
||||||
@GET("hottest.json")
|
@GET("hottest.json")
|
||||||
fun getHottestPosts(): Call<List<dev.msfjarvis.lobsters.model.LobstersPost>>
|
suspend fun getHottestPosts(): List<LobstersPost>
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue