|
| 1 | +/* |
| 2 | + * Copyright 2018 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.firestore.snippets; |
| 18 | + |
| 19 | +import com.google.api.core.SettableApiFuture; |
| 20 | +import com.google.cloud.firestore.DocumentChange; |
| 21 | +import com.google.cloud.firestore.DocumentChange.Type; |
| 22 | +import com.google.cloud.firestore.DocumentReference; |
| 23 | +import com.google.cloud.firestore.DocumentSnapshot; |
| 24 | +import com.google.cloud.firestore.EventListener; |
| 25 | +import com.google.cloud.firestore.Firestore; |
| 26 | +import com.google.cloud.firestore.FirestoreException; |
| 27 | +import com.google.cloud.firestore.ListenerRegistration; |
| 28 | +import com.google.cloud.firestore.Query; |
| 29 | +import com.google.cloud.firestore.QuerySnapshot; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Map; |
| 33 | +import java.util.concurrent.TimeUnit; |
| 34 | +import javax.annotation.Nullable; |
| 35 | + |
| 36 | +/** |
| 37 | + * Snippets to demonstrate Firestore 'listen' operations. |
| 38 | + */ |
| 39 | +@SuppressWarnings("Convert2Lambda") |
| 40 | +public class ListenDataSnippets { |
| 41 | + |
| 42 | + private static final long TIMEOUT_SECONDS = 5; |
| 43 | + |
| 44 | + private final Firestore db; |
| 45 | + |
| 46 | + ListenDataSnippets(Firestore db) { |
| 47 | + this.db = db; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Listen to a single document, returning data after the first snapshot. |
| 52 | + */ |
| 53 | + Map<String, Object> listenToDocument() throws Exception { |
| 54 | + final SettableApiFuture<Map<String, Object>> future = SettableApiFuture.create(); |
| 55 | + |
| 56 | + // [START listen_to_document] |
| 57 | + DocumentReference docRef = db.collection("cities").document("SF"); |
| 58 | + docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() { |
| 59 | + @Override |
| 60 | + public void onEvent(@Nullable DocumentSnapshot snapshot, |
| 61 | + @Nullable FirestoreException e) { |
| 62 | + if (e != null) { |
| 63 | + System.err.println("Listen failed: " + e); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + if (snapshot != null && snapshot.exists()) { |
| 68 | + System.out.println("Current data: " + snapshot.getData()); |
| 69 | + } else { |
| 70 | + System.out.print("Current data: null"); |
| 71 | + } |
| 72 | + // [START_EXCLUDE silent] |
| 73 | + if (!future.isDone()) { |
| 74 | + future.set(snapshot.getData()); |
| 75 | + } |
| 76 | + // [END_EXCLUDE] |
| 77 | + } |
| 78 | + }); |
| 79 | + // [END listen_to_document] |
| 80 | + |
| 81 | + return future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Listen to a query, returning the names of all cities in the first snapshot. |
| 86 | + */ |
| 87 | + List<String> listenForMultiple() throws Exception { |
| 88 | + final SettableApiFuture<List<String>> future = SettableApiFuture.create(); |
| 89 | + |
| 90 | + // [START listen_to_multiple] |
| 91 | + db.collection("cities") |
| 92 | + .whereEqualTo("state", "CA") |
| 93 | + .addSnapshotListener(new EventListener<QuerySnapshot>() { |
| 94 | + @Override |
| 95 | + public void onEvent(@Nullable QuerySnapshot snapshots, |
| 96 | + @Nullable FirestoreException e) { |
| 97 | + if (e != null) { |
| 98 | + System.err.println("Listen failed:" + e); |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + List<String> cities = new ArrayList<>(); |
| 103 | + for (DocumentSnapshot doc : snapshots) { |
| 104 | + if (doc.get("name") != null) { |
| 105 | + cities.add(doc.getString("name")); |
| 106 | + } |
| 107 | + } |
| 108 | + System.out.println("Current cites in CA: " + cities); |
| 109 | + // [START_EXCLUDE silent] |
| 110 | + if (!future.isDone()) { |
| 111 | + future.set(cities); |
| 112 | + } |
| 113 | + // [END_EXCLUDE] |
| 114 | + } |
| 115 | + }); |
| 116 | + // [END listen_to_multiple] |
| 117 | + |
| 118 | + return future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Listen to a query, returning the list of DocumentChange events in the first snapshot. |
| 123 | + */ |
| 124 | + List<DocumentChange> listenForChanges() throws Exception { |
| 125 | + SettableApiFuture<List<DocumentChange>> future = SettableApiFuture.create(); |
| 126 | + |
| 127 | + // [START listen_for_changes] |
| 128 | + db.collection("cities") |
| 129 | + .whereEqualTo("state", "CA") |
| 130 | + .addSnapshotListener(new EventListener<QuerySnapshot>() { |
| 131 | + @Override |
| 132 | + public void onEvent(@Nullable QuerySnapshot snapshots, |
| 133 | + @Nullable FirestoreException e) { |
| 134 | + if (e != null) { |
| 135 | + System.err.println("Listen failed: " + e); |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + for (DocumentChange dc : snapshots.getDocumentChanges()) { |
| 140 | + switch (dc.getType()) { |
| 141 | + case ADDED: |
| 142 | + System.out.println("New city: " + dc.getDocument().getData()); |
| 143 | + break; |
| 144 | + case MODIFIED: |
| 145 | + System.out.println("Modified city: " + dc.getDocument().getData()); |
| 146 | + break; |
| 147 | + case REMOVED: |
| 148 | + System.out.println("Removed city: " + dc.getDocument().getData()); |
| 149 | + break; |
| 150 | + default: |
| 151 | + break; |
| 152 | + } |
| 153 | + } |
| 154 | + // [START_EXCLUDE silent] |
| 155 | + if (!future.isDone()) { |
| 156 | + future.set(snapshots.getDocumentChanges()); |
| 157 | + } |
| 158 | + // [END_EXCLUDE] |
| 159 | + } |
| 160 | + }); |
| 161 | + // [END listen_for_changes] |
| 162 | + |
| 163 | + return future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Demonstrate how to detach an event listener. |
| 168 | + */ |
| 169 | + void detachListener() { |
| 170 | + // [START detach_errors] |
| 171 | + Query query = db.collection("cities"); |
| 172 | + ListenerRegistration registration = query.addSnapshotListener( |
| 173 | + new EventListener<QuerySnapshot>() { |
| 174 | + // [START_EXCLUDE] |
| 175 | + @Override |
| 176 | + public void onEvent(@Nullable QuerySnapshot snapshots, |
| 177 | + @Nullable FirestoreException e) { |
| 178 | + |
| 179 | + } |
| 180 | + // [END_EXCLUDE] |
| 181 | + }); |
| 182 | + |
| 183 | + // ... |
| 184 | + |
| 185 | + // Stop listening to changes |
| 186 | + registration.remove(); |
| 187 | + // [END detach_errors] |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Demonstrate how to handle listening errors. |
| 192 | + */ |
| 193 | + void listenErrors() { |
| 194 | + // [START listen_errors] |
| 195 | + db.collection("cities") |
| 196 | + .addSnapshotListener(new EventListener<QuerySnapshot>() { |
| 197 | + @Override |
| 198 | + public void onEvent(@Nullable QuerySnapshot snapshots, |
| 199 | + @Nullable FirestoreException e) { |
| 200 | + if (e != null) { |
| 201 | + System.err.println("Listen failed: " + e); |
| 202 | + return; |
| 203 | + } |
| 204 | + |
| 205 | + for (DocumentChange dc : snapshots.getDocumentChanges()) { |
| 206 | + if (dc.getType() == Type.ADDED) { |
| 207 | + System.out.println("New city: " + dc.getDocument().getData()); |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | + }); |
| 212 | + // [END listen_errors] |
| 213 | + } |
| 214 | + |
| 215 | +} |
0 commit comments