Search in sources :

Example 1 with TaskCompletionSource

use of com.google.android.gms.tasks.TaskCompletionSource in project FirebaseUI-Android by firebase.

the class GoogleApiClientTaskHelper method getConnectedGoogleApiClient.

public Task<GoogleApiClient> getConnectedGoogleApiClient() {
    final TaskCompletionSource<GoogleApiClient> source = new TaskCompletionSource<>();
    if (!mConnectTaskRef.compareAndSet(null, source)) {
        // mConnectTaskRef Task was not null, return Task
        return mConnectTaskRef.get().getTask();
    }
    final GoogleApiClient client = mBuilder.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {

        @Override
        public void onConnected(@Nullable Bundle bundle) {
            source.setResult(mClientRef.get());
            if (mClientRef.get() != null) {
                mClientRef.get().unregisterConnectionCallbacks(this);
            }
        }

        @Override
        public void onConnectionSuspended(int i) {
        }
    }).addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {

        @Override
        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
            source.setException(new IOException("Failed to connect GoogleApiClient: " + connectionResult.getErrorMessage()));
            if (mClientRef.get() != null) {
                mClientRef.get().unregisterConnectionFailedListener(this);
            }
        }
    }).build();
    mClientRef.set(client);
    client.connect();
    return source.getTask();
}
Also used : TaskCompletionSource(com.google.android.gms.tasks.TaskCompletionSource) GoogleApiClient(com.google.android.gms.common.api.GoogleApiClient) Bundle(android.os.Bundle) NonNull(android.support.annotation.NonNull) ConnectionResult(com.google.android.gms.common.ConnectionResult) IOException(java.io.IOException)

Example 2 with TaskCompletionSource

use of com.google.android.gms.tasks.TaskCompletionSource in project Robot-Scouter by SUPERCILEX.

the class Scouts method build.

public Task<Map<TeamHelper, List<Scout>>> build() {
    List<Task<Pair<TeamHelper, List<String>>>> scoutIndicesTasks = new ArrayList<>();
    for (TeamHelper helper : mTeamHelpers) {
        TaskCompletionSource<Pair<TeamHelper, List<String>>> scoutIndicesTask = new TaskCompletionSource<>();
        scoutIndicesTasks.add(scoutIndicesTask.getTask());
        getScoutIndicesRef(helper.getTeam().getKey()).addListenerForSingleValueEvent(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot snapshot) {
                AsyncTaskExecutor.Companion.execute(() -> {
                    List<String> scoutKeys = new ArrayList<>();
                    for (DataSnapshot scoutKeyTemplate : snapshot.getChildren()) {
                        scoutKeys.add(scoutKeyTemplate.getKey());
                    }
                    return scoutKeys;
                }).addOnSuccessListener(scoutKeys -> scoutIndicesTask.setResult(Pair.create(helper, scoutKeys)));
            }

            @Override
            public void onCancelled(DatabaseError error) {
                scoutIndicesTask.setException(error.toException());
                FirebaseCrash.report(error.toException());
            }
        });
    }
    for (Task<Pair<TeamHelper, List<String>>> scoutKeysTask : scoutIndicesTasks) {
        scoutKeysTask.addOnSuccessListener(this).addOnFailureListener(this);
    }
    Tasks.whenAll(scoutIndicesTasks).addOnSuccessListener(aVoid -> Tasks.whenAll(mScoutMetricsTasks).addOnSuccessListener(aVoid1 -> mScoutsTask.setResult(mScouts)).addOnFailureListener(this)).addOnFailureListener(this);
    return mScoutsTask.getTask();
}
Also used : Context(android.content.Context) Query(com.google.firebase.database.Query) Metric(com.supercilex.robotscouter.data.model.Metric) DataSnapshot(com.google.firebase.database.DataSnapshot) Pair(android.util.Pair) DatabaseReference(com.google.firebase.database.DatabaseReference) Timer(java.util.Timer) FIREBASE_NAME(com.supercilex.robotscouter.util.ConstantsKt.FIREBASE_NAME) NonNull(android.support.annotation.NonNull) Task(com.google.android.gms.tasks.Task) ChildEventListener(com.google.firebase.database.ChildEventListener) ArrayList(java.util.ArrayList) ValueEventListener(com.google.firebase.database.ValueEventListener) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) Map(java.util.Map) TimerTask(java.util.TimerTask) ConnectivityUtilsKt.isOffline(com.supercilex.robotscouter.util.ConnectivityUtilsKt.isOffline) AsyncTaskExecutor(com.supercilex.robotscouter.util.AsyncTaskExecutor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) METRIC_PARSER(com.supercilex.robotscouter.data.util.ScoutUtilsKt.METRIC_PARSER) FIREBASE_METRICS(com.supercilex.robotscouter.util.ConstantsKt.FIREBASE_METRICS) FirebaseCrash(com.google.firebase.crash.FirebaseCrash) FIREBASE_SCOUTS(com.supercilex.robotscouter.util.ConstantsKt.FIREBASE_SCOUTS) Size(android.support.annotation.Size) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Tasks(com.google.android.gms.tasks.Tasks) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) TaskCompletionSource(com.google.android.gms.tasks.TaskCompletionSource) Scout(com.supercilex.robotscouter.data.model.Scout) ScoutUtilsKt.getScoutIndicesRef(com.supercilex.robotscouter.data.util.ScoutUtilsKt.getScoutIndicesRef) DatabaseError(com.google.firebase.database.DatabaseError) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Task(com.google.android.gms.tasks.Task) TimerTask(java.util.TimerTask) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) DataSnapshot(com.google.firebase.database.DataSnapshot) TaskCompletionSource(com.google.android.gms.tasks.TaskCompletionSource) DatabaseError(com.google.firebase.database.DatabaseError) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ValueEventListener(com.google.firebase.database.ValueEventListener) Pair(android.util.Pair)

Aggregations

NonNull (android.support.annotation.NonNull)2 TaskCompletionSource (com.google.android.gms.tasks.TaskCompletionSource)2 Context (android.content.Context)1 Bundle (android.os.Bundle)1 Size (android.support.annotation.Size)1 Pair (android.util.Pair)1 ConnectionResult (com.google.android.gms.common.ConnectionResult)1 GoogleApiClient (com.google.android.gms.common.api.GoogleApiClient)1 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)1 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)1 Task (com.google.android.gms.tasks.Task)1 Tasks (com.google.android.gms.tasks.Tasks)1 FirebaseCrash (com.google.firebase.crash.FirebaseCrash)1 ChildEventListener (com.google.firebase.database.ChildEventListener)1 DataSnapshot (com.google.firebase.database.DataSnapshot)1 DatabaseError (com.google.firebase.database.DatabaseError)1 DatabaseReference (com.google.firebase.database.DatabaseReference)1 Query (com.google.firebase.database.Query)1 ValueEventListener (com.google.firebase.database.ValueEventListener)1 Metric (com.supercilex.robotscouter.data.model.Metric)1