//
// Copyright (C) 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
namespace Google {
using System.Collections;
using System.Threading.Tasks;
using UnityEngine;
///
/// Interface for implementations of the Future API.
///
internal interface FutureAPIImpl {
bool Pending { get; }
GoogleSignInStatusCode Status { get; }
T Result { get; }
}
///
/// Future return value.
///
/// This class provides a promise of a result from a method call.
/// The typical usage is to check the Pending property until it is false.
/// At this time either the Status or Result will be available for use.
/// Result is only set if the operation was successful.
/// As a convience, a coroutine to complete a Task is provided.
///
public class Future {
private FutureAPIImpl apiImpl;
internal Future(FutureAPIImpl impl) {
apiImpl = impl;
}
///
/// Gets a value indicating whether this
/// is pending.
///
/// true if pending; otherwise, false.
public bool Pending { get { return apiImpl.Pending; } }
///
/// Gets the status.
///
/// The status is set when Pending == false.
GoogleSignInStatusCode Status { get { return apiImpl.Status; } }
///
/// Gets the result.
///
/// The result is set when Pending == false and there is no error.
///
T Result { get { return apiImpl.Result; } }
///
/// Waits for result then completes the TaskCompleationSource.
///
/// The for result.
/// Tcs.
internal IEnumerator WaitForResult(TaskCompletionSource tcs) {
yield return new WaitUntil(() => !Pending);
if (Status == GoogleSignInStatusCode.Canceled) {
tcs.SetCanceled();
} else if (Status == GoogleSignInStatusCode.Success ||
Status == GoogleSignInStatusCode.SuccessCached) {
tcs.SetResult(Result);
} else {
tcs.SetException(new GoogleSignIn.SignInException(Status));
}
}
}
}