Comments (8)
@Ronaldbennet Can you please share the full query you wrote to order the tasks?
from make-it-so-android.
@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.
@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.
@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.
@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.
The problem is from mutableStateMapof
from make-it-so-android.
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.
@thatfiredev Thanks Bro
from make-it-so-android.
Related Issues (11)
- Multiple snackbar messages shown HOT 4
- viewModelScope.launch(showErrorExceptionHandler) what is this?
- Compile Note: Hilt_MakeItSoHiltApp.java uses or overrides a deprecated API. HOT 1
- Create Account doesn't work. HOT 4
- Where is TASK_DEFAULT_ID? HOT 2
- @Preview HOT 1
- The EditTask ViewModel is not being initialized correctly. HOT 2
- Error linking account HOT 4
- The given sign-in provider is disabled for this Firebase project HOT 4
- Use Firestore's auto-generated id instead of a random UUID HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from make-it-so-android.