mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-17 19:07:02 +05:30
app: use lifecycleScope for fetching posts
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
parent
e7860e4f55
commit
699a475315
2 changed files with 8 additions and 5 deletions
|
@ -67,6 +67,7 @@ dependencies {
|
|||
implementation "androidx.compose.ui:ui-text:$compose_version"
|
||||
implementation "androidx.compose.ui:ui-text-android:$compose_version"
|
||||
implementation "androidx.compose.ui:ui-unit:$compose_version"
|
||||
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.0-alpha07"
|
||||
implementation "androidx.ui:ui-tooling:$compose_version"
|
||||
implementation 'com.google.android.material:material:1.3.0-alpha02'
|
||||
implementation "com.google.dagger:hilt-android:$hilt_version"
|
||||
|
|
|
@ -10,16 +10,17 @@ import androidx.compose.runtime.Composable
|
|||
import androidx.compose.runtime.Providers
|
||||
import androidx.compose.runtime.ambientOf
|
||||
import androidx.compose.runtime.mutableStateListOf
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.ui.platform.setContent
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import dev.msfjarvis.lobsters.api.LobstersApi
|
||||
import dev.msfjarvis.lobsters.model.LobstersPost
|
||||
import dev.msfjarvis.lobsters.ui.LobstersItem
|
||||
import dev.msfjarvis.lobsters.ui.LobstersTheme
|
||||
import dev.msfjarvis.lobsters.urllauncher.UrlLauncher
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import javax.inject.Inject
|
||||
|
||||
val UrlLauncherAmbient = ambientOf<UrlLauncher> { error("Needs to be provided") }
|
||||
|
@ -34,10 +35,11 @@ class MainActivity : AppCompatActivity() {
|
|||
setContent {
|
||||
Providers(UrlLauncherAmbient provides urlLauncher) {
|
||||
LobstersTheme {
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
val posts = mutableStateListOf<LobstersPost>()
|
||||
coroutineScope.launch {
|
||||
posts.addAll(apiClient.getHottestPosts(1))
|
||||
lifecycleScope.launchWhenCreated {
|
||||
withContext(Dispatchers.IO) {
|
||||
posts.addAll(apiClient.getHottestPosts(1))
|
||||
}
|
||||
}
|
||||
LobstersApp(posts)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue