Modelling augmented filtered list Kotlin Compose Multiplatform

Background

This is a Kotlin Compose Multiplatform Desktop toy I’m playing around with. I’m new to Kotlin (and Compose).

Scenario

I have a library filled with books. I have a view where users can specify a filter criteria and I list a subset of the entire library for them based on the filter. I would like for users to be able to click on those books and alter a view-specific aspect of them (like the book is Open or Closed). Users may have multiple lists with different filters, and Openning/Closing a book in one list does not change that value in another view. However, if the user changes the name of a book, I want it reflected in all lists.

Implementation

Here’s where I’m stuck.

So, I have a Library class that has a mutableStateMapOf<ID,Book>() which has the entire library.

I have MyLibraryView which takes a private val library: Library and has a mutableStateOf a filter predicate. With that in hand, I’m using derivedStateOf to have myBooks

class MyLibraryView(private val library: Library) {

    private val filterPredicate = mutableStateOf<(Book) -> Boolean>({ true /* updated when user updates filter */ })

    val myBooks: State<List<Book>> = derivedStateOf {
        library.allBooks.values.filter(predicate = filterPredicate.value)
    }

I have a @Composable with a LazyColumn to list my books:

@Composable
fun MyLibraryList(
    myBooks: State<List<Book>>) {
    val listState = rememberLazyListState()

    LazyColumn(
        state = listState,
    ) {

        items(items = myBooks.value,
            key = { book -> book.id }) 
              { book -> BookRow(book) }
    }
}

This works as expected (I’ve understood State<List<Book>> is Stable, but List<Book> would not be?).

I’m not sure how to best add the display details (the open/closed, but also maybe if it’s selected, etc).

Broken Solution #1

I could alert the data class Book to include DisplayDetails. This doesn’t work since the Open/Closed becomes part of Book and then is shared across views.

Broken Solution #2

Make a BookWithDisplayDetails data class and have the derivedStateOf lambda for myBooks return that instead of Book. This doesn’t work because (afaict), you can’t reference myBooks from within the lambda (I get an error about it needing to be initialized first), so each time myBooks is recalculated, the existing DisplayDetails is lost (so if a Book changes title, the DisplayDetails would be wiped out).

Broken Solution #3

Have a separate Map<ID,DisplayDetails> in MyLibraryView, and a fun toggleOpenClosed(book: Book) which does as you’d expect.

This doesn’t work directly, so it’d have to be a mutableStateMapOf. But also, if the filterPredicate changes, then the derivedStateOf myBooks will need to change, and it should remove any Entry from that map that is filtered away. This is a mess and causes a full recomposition of the entire list.

I’m missing something obvious and simple, it could be because I’m new to this.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật