Coder Social home page Coder Social logo

Comments (8)

thatfiredev avatar thatfiredev commented on June 12, 2024 1

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

from make-it-so-android.

thatfiredev avatar thatfiredev commented on June 12, 2024 1

@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

from make-it-so-android.

thatfiredev avatar thatfiredev commented on June 12, 2024 1

@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)).

from make-it-so-android.

abdelkrimkr avatar abdelkrimkr commented on June 12, 2024

@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)
        }
    }

from make-it-so-android.

abdelkrimkr avatar abdelkrimkr commented on June 12, 2024

@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

from make-it-so-android.

abdelkrimkr avatar abdelkrimkr commented on June 12, 2024

The problem is from mutableStateMapof

from make-it-so-android.

abdelkrimkr avatar abdelkrimkr commented on June 12, 2024

@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 this books.values.toList()

from make-it-so-android.

abdelkrimkr avatar abdelkrimkr commented on June 12, 2024

@thatfiredev Thanks Bro

from make-it-so-android.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.