using System; using System.Collections.Generic; using System.Threading.Tasks; using TapTap.Common; namespace TapTap.Login.Internal { internal class LoginHelper { private static readonly string _accessToken = "taptapsdk_accesstoken"; private static readonly string _profile = "taptapsdk_profile"; public static Task Login() { return Login(new[] { TapLogin.TAP_LOGIN_SCOPE_PUBLIC_PROFILE }); } public static Task Login(string[] permissions) { var tcs = new TaskCompletionSource(); LoginManager.Instance.LogInWithReadPermissions(permissions, result => { if (result == null) { tcs.TrySetException(new TapException((int) TapErrorCode.ERROR_CODE_UNDEFINED, "UnKnow Error")); return; } if (result.IsCanceled) { tcs.TrySetException( new TapException((int) TapErrorCode.ERROR_CODE_LOGIN_CANCEL, "Login Cancel")); return; } if (result.IsFaulted) { var str = result.Exception.Message; if (str == "unknown") { tcs.TrySetException(new TapException((int) TapErrorCode.ERROR_CODE_UNDEFINED, "UnKnow Error")); } else { var dict = Json.Deserialize(str) as Dictionary; var data = SafeDictionary.GetValue(dict, "data") as Dictionary; tcs.TrySetException(new TapException(SafeDictionary.GetValue(data, "code"), SafeDictionary.GetValue(data, "error_description"))); } return; } tcs.TrySetResult(result.Token); }); return tcs.Task; } public static Task GetProfile() { var tcs = new TaskCompletionSource(); try { var profileStr = DataStorage.LoadString(_profile); if (!string.IsNullOrEmpty(profileStr)) { var profile = new Profile(profileStr); tcs.TrySetResult(profile); } } catch (Exception e) { tcs.TrySetException(e); } return tcs.Task; } public static Task GetAccessToken() { var tcs = new TaskCompletionSource(); try { var accessTokenStr = DataStorage.LoadString(_accessToken); if (accessTokenStr != null) { var accessToken = new AccessToken(accessTokenStr); tcs.TrySetResult(accessToken); } } catch (Exception e) { tcs.TrySetException(e); } return tcs.Task; } public static void Logout() { DataStorage.SaveString(_profile, null); DataStorage.SaveString(_accessToken, null); } } }