https://kotlinlang.org logo
#compose
Title
# compose
o

oday

02/22/2024, 11:44 AM
can I not do this?
Copy code
fun NavHostController.navigateBookDetail(
    bookId: Long,
    bookTitle: String
) {
    setState("bookId", bookId)
    setState("bookTitle", bookTitle)
    navigate(Routes.Slave.BookDetail.path)
}
Copy code
composable(Routes.BookDetail.path) {
        val bookId = navController.getBookIdStateFromState()
        val bookTitle = navController.getBookTitleFromState()


            BookDetailScreen(
                bookId = bookId,
                bookTitle = bookTitle,
            )
    }
Copy code
@HiltViewModel
class BookDetailViewModel @Inject constructor(
    private val snippetRepository: SnippetRepository,
    savedStateHandle: SavedStateHandle
) : ViewModel() {

    init {
        val bookid = savedStateHandle.get<Long>("bookId")
        val bookTitle = savedStateHandle.get<String>("bookTitle")
    }
}
I'm getting back null in the viewmodel, the only way this would work if I have
BookDetail/{bookId}/{bookTitle}
AND I set the arguments as navArgs in the composable but can I not do the above and still get back the values in the statehandle? I don't want to send them in the "normal" way for example
🚫 1
i

Ian Lake

02/22/2024, 3:06 PM
All arguments need to be in the route, that's kind of the whole point
o

oday

02/22/2024, 3:11 PM
yea.. i know..just insisting on a broken solution, sunken cost, ok thanks Ian
i

Ian Lake

02/22/2024, 3:25 PM
Feels weird to be passing a book title at all - I'd assume your actual data source for book data (a repository layer, whether that is hooked into a local database or just a cache for data) would look up the book title and other details given the ID
o

oday

02/22/2024, 3:41 PM
true but I want the title to sit in the top bar, I assume it would be faster to get it there through nav args than to have to wait for a query but yea sure makes sense, I will actually go for that instead
2 Views