Skip to content

Commit 79be2c1

Browse files
committed
added all results in onComplete
1 parent f62f95d commit 79be2c1

1 file changed

Lines changed: 105 additions & 4 deletions

File tree

  • geofirequery/src/main/java/com/ckdroid/geofirequery/listener

geofirequery/src/main/java/com/ckdroid/geofirequery/listener/GeoQueryTask.kt

Lines changed: 105 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,20 @@ class GeoQueryTask internal constructor(query: GeoQuery) {
3535
val removedList: MutableList<DocumentSnapshot> = mutableListOf()
3636

3737
task.result?.let { querySnapshot ->
38-
removedList.addAll(querySnapshot.documentChanges.filter { documentChange -> documentChange.type == DocumentChange.Type.REMOVED }.map { documentChange -> documentChange.document }.toMutableList())
38+
removedList.addAll(querySnapshot.documentChanges.filter { documentChange -> documentChange.type == DocumentChange.Type.REMOVED }
39+
.map { documentChange -> documentChange.document }.toMutableList())
3940

4041
val nonRemovedList =
4142
querySnapshot.documentChanges.filter { documentChange -> documentChange.type != DocumentChange.Type.REMOVED }
4243
.map { documentChange -> documentChange.document }.toMutableList()
4344

4445
nonRemovedList.forEach { document ->
4546
if (geoQuery.distance != null && geoQuery.currentLocation != null) {
46-
if (document.isInGivenDistance(geoQuery.currentLocation, geoQuery.distance)) {
47+
if (document.isInGivenDistance(
48+
geoQuery.currentLocation,
49+
geoQuery.distance
50+
)
51+
) {
4752
addedOrModifiedList.add(document)
4853
} else {
4954
removedList.add(document)
@@ -59,6 +64,96 @@ class GeoQueryTask internal constructor(query: GeoQuery) {
5964
}
6065
}
6166

67+
68+
/**
69+
* FUNCTION COMMENT
70+
*
71+
* add task's onComplete listener to get exception or data in a single function
72+
* @param onCompleteListener as lambda function
73+
* @return none
74+
*/
75+
fun addOnCompleteListener(onCompleteListener: (Exception?, MutableList<DocumentSnapshot>, MutableList<DocumentSnapshot>, MutableList<DocumentSnapshot>) -> Unit) {
76+
if (geoQuery.querySnapshotTask == null) {
77+
throw Exception("You must call get() method before calling addOnCompleteListener() method!")
78+
}
79+
val addedOrModifiedList: MutableList<DocumentSnapshot> = mutableListOf()
80+
val addedList: MutableList<DocumentSnapshot> = mutableListOf()
81+
val modifiedList: MutableList<DocumentSnapshot> = mutableListOf()
82+
val removedList: MutableList<DocumentSnapshot> = mutableListOf()
83+
84+
geoQuery.querySnapshotTask?.let { task ->
85+
if (!task.isSuccessful) {
86+
val exception = task.exception
87+
onCompleteListener.invoke(
88+
exception,
89+
mutableListOf(),
90+
mutableListOf(),
91+
mutableListOf()
92+
)
93+
return
94+
}
95+
96+
val it = task.result!!
97+
98+
removedList.addAll(it.documentChanges.filter { documentChange -> documentChange.type == DocumentChange.Type.REMOVED }
99+
.map { documentChange -> documentChange.document }.toMutableList())
100+
101+
val nonRemovedList =
102+
it.documentChanges.filter { documentChange -> documentChange.type != DocumentChange.Type.REMOVED }
103+
.map { documentChange -> documentChange.document }.toMutableList()
104+
105+
val theModifiedList =
106+
it.documentChanges.filter { documentChange ->
107+
documentChange.type != DocumentChange.Type.REMOVED
108+
documentChange.type == DocumentChange.Type.MODIFIED
109+
}
110+
.map { documentChange -> documentChange.document }.toMutableList()
111+
112+
val theAddedList =
113+
it.documentChanges.filter { documentChange ->
114+
documentChange.type != DocumentChange.Type.REMOVED
115+
documentChange.type == DocumentChange.Type.ADDED
116+
}
117+
.map { documentChange -> documentChange.document }.toMutableList()
118+
119+
theModifiedList.forEach { document ->
120+
if (geoQuery.distance != null && geoQuery.currentLocation != null) {
121+
if (document.isInGivenDistance(geoQuery.currentLocation, geoQuery.distance)) {
122+
modifiedList.add(document)
123+
}
124+
} else {
125+
modifiedList.add(document)
126+
}
127+
}
128+
129+
130+
theAddedList.forEach { document ->
131+
if (geoQuery.distance != null && geoQuery.currentLocation != null) {
132+
if (document.isInGivenDistance(geoQuery.currentLocation, geoQuery.distance)) {
133+
addedList.add(document)
134+
}
135+
} else {
136+
addedList.add(document)
137+
}
138+
}
139+
140+
141+
nonRemovedList.forEach { document ->
142+
if (geoQuery.distance != null && geoQuery.currentLocation != null) {
143+
if (document.isInGivenDistance(geoQuery.currentLocation, geoQuery.distance)) {
144+
addedOrModifiedList.add(document)
145+
} else {
146+
removedList.add(document)
147+
}
148+
} else {
149+
addedOrModifiedList.add(document)
150+
}
151+
}
152+
153+
onCompleteListener.invoke(null, addedList, modifiedList, removedList)
154+
}
155+
}
156+
62157
/**
63158
* FUNCTION COMMENT
64159
*
@@ -93,15 +188,20 @@ class GeoQueryTask internal constructor(query: GeoQuery) {
93188
val removedList: MutableList<DocumentSnapshot> = mutableListOf()
94189

95190
task.result?.let { querySnapshot ->
96-
removedList.addAll(querySnapshot.documentChanges.filter { documentChange -> documentChange.type == DocumentChange.Type.REMOVED }.map { documentChange -> documentChange.document }.toMutableList())
191+
removedList.addAll(querySnapshot.documentChanges.filter { documentChange -> documentChange.type == DocumentChange.Type.REMOVED }
192+
.map { documentChange -> documentChange.document }.toMutableList())
97193

98194
val nonRemovedList =
99195
querySnapshot.documentChanges.filter { documentChange -> documentChange.type != DocumentChange.Type.REMOVED }
100196
.map { documentChange -> documentChange.document }.toMutableList()
101197

102198
nonRemovedList.forEach { document ->
103199
if (geoQuery.distance != null && geoQuery.currentLocation != null) {
104-
if (document.isInGivenDistance(geoQuery.currentLocation, geoQuery.distance)) {
200+
if (document.isInGivenDistance(
201+
geoQuery.currentLocation,
202+
geoQuery.distance
203+
)
204+
) {
105205
addedOrModifiedList.add(document)
106206
} else {
107207
removedList.add(document)
@@ -130,6 +230,7 @@ class GeoQueryTask internal constructor(query: GeoQuery) {
130230
}
131231
}
132232

233+
133234
/**
134235
* FUNCTION COMMENT
135236
*

0 commit comments

Comments
 (0)