FirebaseExtended / make-it-so-android

Apache License 2.0
214 stars 58 forks source link

When I add orderBy to tasks collection it doesn't orderd #11

Closed abdelkrimkr closed 2 years ago

abdelkrimkr commented 2 years ago

Fix This problem I want it How To fix When I add orderBy to tasks collection it doesn't orderd Or just add Order Tasks by createdAt Timestamp

thatfiredev commented 2 years ago

@Ronaldbennet Can you please share the full query you wrote to order the tasks?

abdelkrimkr commented 2 years ago

@Ronaldbennet Can you please share the full query you wrote to order the tasks?

val query = Firebase.firestore.collection(BOOKS_COLLECTION) .orderBy("createdAt", Query.Direction.ASCENDING)

    listenerRegistration = query.addSnapshotListener { value, error ->
        if (error != null) {
            onError(error)
            return@addSnapshotListener
        }
        Log.d("listenerRegistration", "addListener: $value")

        value?.documentChanges?.forEach {
            val wasDocumentDeleted = it.type == REMOVED
            val book = it.document.toObject<Book>().copy(id = it.document.id)
            onDocumentEvent(wasDocumentDeleted, book)
        }
    }
abdelkrimkr commented 2 years ago

@thatfiredev also when i add data to collection the data was randomize

I want from you to update project and add sorting data with createdAt: Timestamp

I want it help me

abdelkrimkr commented 2 years ago

The problem is from mutableStateMapof

thatfiredev commented 2 years ago

@abdelkrimkr Can you please check your logcat for errors when you use that orderBy query? You might be having an issue with indexes. You'll need to create an index in your Firebase Console to be able to order by the "createdAt" field. See more detailing about creating indexes here: https://firebase.google.com/docs/firestore/query-data/indexing

abdelkrimkr commented 2 years ago

@thatfiredev

Yes check it the problem is when

 var books = mutableStateMapOf<String, Book>()
        private set

also see this

private fun onDocumentEvent(wasDocumentDeleted: Boolean, book: Book) {
        if (wasDocumentDeleted) books.remove(book.bookId) else books[book.bookId] = book
    }

this data class

data class Book(
    val bookId: String = "",
    val bookName: String = "",
    val bookLink: String = "",
    val createdAt: String = Timestamp.now().seconds.toString()
    )

it will be Converted to List the order was gone

val books = viewModel.books

LazyColumn(
               // ......
            ) {
                items(books.values.toList(),
                    key = { bookId ->
                        bookId.bookId
                    }
                ) { bookItem ->

                    MaterialTaskItem(
                        book = bookItem,

                    ){

                    }
                }

the problem from thisbooks.values.toList()

thatfiredev commented 2 years ago

@abdelkrimkr Thanks for pointing out that the issue is from the Map to List transformation (books.values.toList()).

You can update your code to use a list instead:

The ViewModel:

 var books = mutableStateListOf<Book>() // <-- changed this line
        private set

    private fun onDocumentEvent(wasDocumentDeleted: Boolean, book: Book) {
        if (wasDocumentDeleted) books.remove(book) else updateBookInList(book) // <-- changed
    }

    // Added this function
    private fun updateBookInList(book: Book) {
        val index = books.indexOfFirst { it.id == book.id }
        if (index < 0) books.add(book) else books[index] = book
    }

The UI:

            LazyColumn(
               // ......
            ) {
                items(books, // <-- changed this line
                    key = { bookId ->
                        bookId.bookId
                    }
                ) { bookItem ->

                    MaterialTaskItem(
                        book = bookItem,

                    ){

                    }
                }

Please note that this solution is less performant than using a Map because updating an item in a list has a longer runtime (O(n)) than updating an item in a Map (O(1)).

abdelkrimkr commented 2 years ago

@thatfiredev Thanks Bro