Home > database >  How to call suspend function in Activity?
How to call suspend function in Activity?

Time:10-04

I am working on a project and I want to fetch the data from Api and display it in UI. I am using Jetpack Compose by the way. How can I call the suspend function in Main Activity? I want to fetch the list and then call the function bookListScreen(bookList : BookList) in LotrAppTheme{}. How can I do this?

Main Activity:

package com.example.lotrapp
 
    @AndroidEntryPoint
    class MainActivity : ComponentActivity() {
    
        private val bookViewModel: BookViewModel by viewModels()
        private lateinit var bookList : BookList
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            setContent {
                LOTRAppTheme {
    
                }
            }
    
        }
    }
    
    @Preview(showBackground = true)
    @Composable
    fun DefaultPreview() {
        LOTRAppTheme {
    
        }
    }

View Model:

package com.example.lotrapp

import androidx.lifecycle.ViewModel
import com.example.lotrapp.models.BookList
import com.example.lotrapp.repository.BookRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class BookViewModel @Inject constructor(
    private val repository: BookRepository
) : ViewModel() {

    suspend fun getBookList() : BookList? {
        return repository.getBookList()
    }
}

BookListScreen

@Composable
fun bookListScreen(bookList : BookList) {

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.Gray)
    ) {

        LazyColumn(
            contentPadding = PaddingValues(all = 12.dp),
            verticalArrangement = Arrangement.spacedBy(12.dp)
        ) {
            itemsIndexed(
                items = bookList.docs
            ) {
                index, book -> BookItem(book)
            }
        }


    }
}

CodePudding user response:

You can get book list as soon as your view model is created using init, and store the result value in a mutable state holder: by using mutable state you're making sure your view will update when new value comes.

Check out more about state in compose documentation, including this youtube video which explains the basic principles.

@HiltViewModel
class BookViewModel @Inject constructor(
    private val repository: BookRepository
) : ViewModel() {

    var bookList by mutableStateOf<BookList?>(null)

    init {
        viewModelScope.launch {
            bookList = getBookList()
        }
    }

    private suspend fun getBookList() : BookList? {
        return repository.getBookList()
    }
}

Then you can display needed view depending on the state:

val bookList = viewModel.bookList
if (bookList != null) {
    BookListScreen(bookList)
} else {
    // loading
}
  • Related