I have a list of offers/deals saved in my firestore database. I want to fetch the first 3 deals sorted by discount in desc order i.e the deals with highest discount comes first and then next highest and so on. I am fetching them in segments of 3.
I am trying to achieve the same using android code. So, that the first 3 elements with discount 55,44,28 should come first, later 27,27,21 and finally 19,4.
class ContentGenerationActivityV1 : AppCompatActivity() {
var lastDocument : DocumentSnapshot?= null
lateinit var filterQuery: Query
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_content_generation)
val doc = FirebaseFirestore.getInstance().collection("steal-deals").document("deals")
filterQuery = doc.collection(getTodayTimeStamp().toString())
.orderBy("discount", Query.Direction.DESCENDING)
filterFetchButton.setOnClickListener {
if(lastDocument == null){
fetchFirstFew()
} else {
fetchMore()
}
}
}
//Get first 3 elements
fun fetchFirstFew(){
Log.d("N/W CALL ::", "FIRST")
filterQuery.limit(3) .get()
.addOnSuccessListener { result ->
lastDocument = result.documents.last()
for (document in result) {
Log.d("DEAL :: ", "${document.id} => ${document.data}")
}
}
}
//Get the next 3 elements
fun fetchMore(){
Log.d("N/W CALL ::", "SECOND")
filterQuery.startAfter(lastDocument)
.limit(3).get()
.addOnSuccessListener { result ->
lastDocument = result.documents.last()
for (document in result) {
Log.d("DEAL :: ", "${document.id} => ${document.data}")
}
}
}
But it is always returning the first three elements (55,44,28) no matter what.
Please guide me so in achieving the same.