|
26 | 26 | import com.google.android.gms.tasks.Task;
|
27 | 27 | import com.google.firebase.functions.FirebaseFunctions;
|
28 | 28 | import com.google.firebase.functions.FirebaseFunctionsException;
|
| 29 | +import com.google.firebase.functions.HttpsCallableReference; |
29 | 30 | import com.google.firebase.functions.HttpsCallableResult;
|
| 31 | +import com.google.firebase.functions.StreamResponse; |
30 | 32 |
|
| 33 | +import org.reactivestreams.Subscriber; |
| 34 | +import org.reactivestreams.Subscription; |
| 35 | + |
| 36 | +import java.util.ArrayList; |
31 | 37 | import java.util.HashMap;
|
| 38 | +import java.util.List; |
32 | 39 | import java.util.Map;
|
33 | 40 |
|
34 | 41 | public class MainActivity extends AppCompatActivity {
|
@@ -127,22 +134,99 @@ public void onComplete(@NonNull Task<Integer> task) {
|
127 | 134 | // [END call_add_numbers]
|
128 | 135 | }
|
129 | 136 |
|
130 |
| - private void callAddMessage(String inputMessage) { |
131 |
| - // [START call_add_message] |
132 |
| - addMessage(inputMessage) |
133 |
| - .addOnCompleteListener(new OnCompleteListener<String>() { |
134 |
| - @Override |
135 |
| - public void onComplete(@NonNull Task<String> task) { |
136 |
| - if (!task.isSuccessful()) { |
137 |
| - Exception e = task.getException(); |
138 |
| - if (e instanceof FirebaseFunctionsException) { |
139 |
| - FirebaseFunctionsException ffe = (FirebaseFunctionsException) e; |
140 |
| - FirebaseFunctionsException.Code code = ffe.getCode(); |
141 |
| - Object details = ffe.getDetails(); |
142 |
| - } |
143 |
| - } |
144 |
| - } |
145 |
| - }); |
146 |
| - // [END call_add_message] |
147 |
| - } |
| 137 | + private void callAddMessage(String inputMessage) { |
| 138 | + // [START call_add_message] |
| 139 | + addMessage(inputMessage) |
| 140 | + .addOnCompleteListener(new OnCompleteListener<String>() { |
| 141 | + @Override |
| 142 | + public void onComplete(@NonNull Task<String> task) { |
| 143 | + if (!task.isSuccessful()) { |
| 144 | + Exception e = task.getException(); |
| 145 | + if (e instanceof FirebaseFunctionsException) { |
| 146 | + FirebaseFunctionsException ffe = (FirebaseFunctionsException) e; |
| 147 | + FirebaseFunctionsException.Code code = ffe.getCode(); |
| 148 | + Object details = ffe.getDetails(); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + }); |
| 153 | + // [END call_add_message] |
| 154 | + } |
| 155 | + |
| 156 | + private void callStreamingFunctions() { |
| 157 | + List<devrel.firebase.google.com.functions.kotlin.MainActivity.Location> favoriteLocations = new ArrayList<>(); |
| 158 | + favoriteLocations.add(new devrel.firebase.google.com.functions.kotlin.MainActivity.Location( |
| 159 | + "The Googleplex", |
| 160 | + 37.4220199895279, |
| 161 | + -122.08531347325561)); |
| 162 | + favoriteLocations.add(new devrel.firebase.google.com.functions.kotlin.MainActivity.Location( |
| 163 | + "Yosemite Valley", |
| 164 | + 37.745192257741984, |
| 165 | + -119.5945133017153)); |
| 166 | + favoriteLocations.add(new devrel.firebase.google.com.functions.kotlin.MainActivity.Location( |
| 167 | + "Old Faithful", |
| 168 | + 44.46037818049411, |
| 169 | + -110.82802255265777)); |
| 170 | + // [START stream_data_client] |
| 171 | + // Get the callable by passing an initialized functions SDK. |
| 172 | + HttpsCallableReference getForecast = mFunctions.getHttpsCallable("getForecast"); |
| 173 | + getForecast.stream( |
| 174 | + new HashMap<String, Object>() {{ |
| 175 | + put("locations", favoriteLocations); |
| 176 | + }} |
| 177 | + ).subscribe(new Subscriber<StreamResponse>() { |
| 178 | + @Override |
| 179 | + public void onSubscribe(Subscription subscription) { |
| 180 | + subscription.request(Long.MAX_VALUE); |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public void onNext(StreamResponse streamResponse) { |
| 185 | + if (streamResponse instanceof StreamResponse.Message) { |
| 186 | + // The flow will emit a [StreamResponse.Message] value every time the |
| 187 | + // callable function calls `sendChunk()`. |
| 188 | + StreamResponse.Message response = (StreamResponse.Message) streamResponse; |
| 189 | + Map<String, Object> forecastDataChunk = |
| 190 | + (Map<String, Object>) response.getMessage().getData(); |
| 191 | + // Update the UI every time a new chunk is received |
| 192 | + // from the callable function |
| 193 | + updateUI( |
| 194 | + (double) forecastDataChunk.get("latitude"), |
| 195 | + (double) forecastDataChunk.get("longitude"), |
| 196 | + (double) forecastDataChunk.get("forecast") |
| 197 | + ); |
| 198 | + } else if(streamResponse instanceof StreamResponse.Result) { |
| 199 | + // The flow will emit a [StreamResponse.Result] value when the |
| 200 | + // callable function completes. |
| 201 | + StreamResponse.Result response = (StreamResponse.Result) streamResponse; |
| 202 | + List<Map<String, Object>> allWeatherForecasts = |
| 203 | + (List<Map<String, Object>>) response.getResult().getData(); |
| 204 | + finalizeUI(); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + @Override |
| 209 | + public void onError(Throwable throwable) { |
| 210 | + // an error occurred in the function |
| 211 | + } |
| 212 | + |
| 213 | + @Override |
| 214 | + public void onComplete() { |
| 215 | + |
| 216 | + } |
| 217 | + }); |
| 218 | + // [END stream_data_client] |
| 219 | + } |
| 220 | + |
| 221 | + private void updateUI( |
| 222 | + double latitude, |
| 223 | + double longitude, |
| 224 | + double forecast |
| 225 | + ) { |
| 226 | + |
| 227 | + } |
| 228 | + |
| 229 | + private void finalizeUI() { |
| 230 | + |
| 231 | + } |
148 | 232 | }
|
0 commit comments