22// Use of this source code is governed by a BSD-style license that can be
33// found in the LICENSE file.
44
5- package io .flutter .plugins .firebase .cloud_firestore ;
5+ package io .flutter .plugins .firebase .cloudfirestore ;
66
7+ import android .support .annotation .NonNull ;
78import android .util .SparseArray ;
9+ import com .google .android .gms .tasks .OnFailureListener ;
10+ import com .google .android .gms .tasks .OnSuccessListener ;
11+ import com .google .android .gms .tasks .Task ;
812import com .google .firebase .firestore .CollectionReference ;
913import com .google .firebase .firestore .DocumentChange ;
1014import com .google .firebase .firestore .DocumentReference ;
1519import com .google .firebase .firestore .ListenerRegistration ;
1620import com .google .firebase .firestore .Query ;
1721import com .google .firebase .firestore .QuerySnapshot ;
22+ import com .google .firebase .firestore .SetOptions ;
1823import io .flutter .plugin .common .MethodCall ;
1924import io .flutter .plugin .common .MethodChannel ;
2025import io .flutter .plugin .common .MethodChannel .MethodCallHandler ;
2732
2833public class CloudFirestorePlugin implements MethodCallHandler {
2934
30- public static final String TAG = "FirestorePlugin" ;
3135 private final MethodChannel channel ;
3236
3337 // Handles are ints used as indexes into the sparse array of active observers
@@ -61,6 +65,7 @@ private Query getQuery(Map<String, Object> arguments) {
6165 @ SuppressWarnings ("unchecked" )
6266 Map <String , Object > parameters = (Map <String , Object >) arguments .get ("parameters" );
6367 if (parameters == null ) return query ;
68+ @ SuppressWarnings ("unchecked" )
6469 List <List <Object >> whereConditions = (List <List <Object >>) parameters .get ("where" );
6570 for (List <Object > condition : whereConditions ) {
6671 String fieldName = (String ) condition .get (0 );
@@ -80,6 +85,7 @@ private Query getQuery(Map<String, Object> arguments) {
8085 // Invalid operator.
8186 }
8287 }
88+ @ SuppressWarnings ("unchecked" )
8389 List <Object > orderBy = (List <Object >) parameters .get ("orderBy" );
8490 if (orderBy == null ) return query ;
8591 String orderByFieldName = (String ) orderBy .get (0 );
@@ -163,6 +169,23 @@ public void onEvent(QuerySnapshot querySnapshot, FirebaseFirestoreException e) {
163169 }
164170 }
165171
172+ private void addDefaultListeners (final String description , Task <Void > task , final Result result ) {
173+ task .addOnSuccessListener (
174+ new OnSuccessListener <Void >() {
175+ @ Override
176+ public void onSuccess (Void ignored ) {
177+ result .success (null );
178+ }
179+ });
180+ task .addOnFailureListener (
181+ new OnFailureListener () {
182+ @ Override
183+ public void onFailure (@ NonNull Exception e ) {
184+ result .error ("Error performing " + description , e .getMessage (), null );
185+ }
186+ });
187+ }
188+
166189 @ Override
167190 public void onMethodCall (MethodCall call , final Result result ) {
168191 switch (call .method ) {
@@ -201,16 +224,33 @@ public void onMethodCall(MethodCall call, final Result result) {
201224 {
202225 Map <String , Object > arguments = call .arguments ();
203226 DocumentReference documentReference = getDocumentReference (arguments );
204- documentReference .set (arguments .get ("data" ));
205- result .success (null );
227+ @ SuppressWarnings ("unchecked" )
228+ Map <String , Object > options = (Map <String , Object >) arguments .get ("options" );
229+ Task <Void > task ;
230+ if (options != null && (Boolean ) options .get ("merge" )) {
231+ task = documentReference .set (arguments .get ("data" ), SetOptions .merge ());
232+ } else {
233+ task = documentReference .set (arguments .get ("data" ));
234+ }
235+ addDefaultListeners ("setData" , task , result );
236+ break ;
237+ }
238+ case "DocumentReference#updateData" :
239+ {
240+ Map <String , Object > arguments = call .arguments ();
241+ DocumentReference documentReference = getDocumentReference (arguments );
242+ @ SuppressWarnings ("unchecked" )
243+ Map <String , Object > data = (Map <String , Object >) arguments .get ("data" );
244+ Task <Void > task = documentReference .update (data );
245+ addDefaultListeners ("updateData" , task , result );
206246 break ;
207247 }
208248 case "DocumentReference#delete" :
209249 {
210250 Map <String , Object > arguments = call .arguments ();
211251 DocumentReference documentReference = getDocumentReference (arguments );
212- documentReference .delete ();
213- result . success ( null );
252+ Task < Void > task = documentReference .delete ();
253+ addDefaultListeners ( "delete" , task , result );
214254 break ;
215255 }
216256 default :
0 commit comments