<commit_before>/*
 * Copyright 2017 mk
 *
 * 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.
 */

package mk.gdx.firebase.ios.auth;

import com.google.firebaseauth.FIRAuth;
import com.google.firebaseauth.FIRAuthDataResult;
import com.google.firebaseauth.FIRUser;

import org.moe.natj.general.ptr.Ptr;
import org.moe.natj.general.ptr.impl.PtrFactory;

import apple.foundation.NSError;
import mk.gdx.firebase.auth.GdxFirebaseUser;
import mk.gdx.firebase.auth.UserInfo;
import mk.gdx.firebase.callbacks.AuthCallback;
import mk.gdx.firebase.callbacks.SignOutCallback;
import mk.gdx.firebase.distributions.AuthDistribution;

/**
 * iOS Firebase authorization API implementation.
 * <p>
 *
 * @see AuthDistribution
 */
public class Auth implements AuthDistribution {

    /**
     * {@inheritDoc}
     */
    @Override
    public GdxFirebaseUser getCurrentUser() {
        FIRUser firUser = FIRAuth.auth().currentUser();
        if (firUser == null) return null;
        UserInfo.Builder builder = new UserInfo.Builder();
        builder.setDisplayName(firUser.displayName())
                .setPhotoUrl(firUser.photoURL() != null ? firUser.photoURL().path() : null)
                .setProviderId(firUser.providerID())
                .setUid(firUser.uid())
                .setIsEmailVerified(firUser.isEmailVerified())
                .setIsAnonymous(firUser.isAnonymous())
                .setEmail(firUser.email());
        return GdxFirebaseUser.create(builder.build());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void createUserWithEmailAndPassword(String email, char[] password, final AuthCallback callback) {
        FIRAuth.auth().createUserWithEmailPasswordCompletion(email, new String(password), new FIRAuth.Block_createUserWithEmailPasswordCompletion() {
            @Override
            public void call_createUserWithEmailPasswordCompletion(FIRAuthDataResult arg0, NSError arg1) {
                if (handleError(arg1, callback)) return;
                // TODO - arg0 was chacnged from FIRUser to FIRAuthDataResult - need to adopt it
                callback.onSuccess(getCurrentUser());
            }
        });
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void signInWithEmailAndPassword(String email, char[] password, final AuthCallback callback) {
        FIRAuth.auth().signInWithEmailPasswordCompletion(email, new String(password), new FIRAuth.Block_signInWithEmailPasswordCompletion() {
            @Override
            public void call_signInWithEmailPasswordCompletion(FIRAuthDataResult arg0, NSError arg1) {
                if (handleError(arg1, callback)) return;
                // TODO
                callback.onSuccess(getCurrentUser());
            }
        });
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void signInWithToken(String token, final AuthCallback callback) {
        FIRAuth.auth().signInWithCustomTokenCompletion(token, new FIRAuth.Block_signInWithCustomTokenCompletion() {
            @Override
            public void call_signInWithCustomTokenCompletion(FIRAuthDataResult arg0, NSError arg1) {
                if (handleError(arg1, callback)) return;
                // TODO
                callback.onSuccess(getCurrentUser());
            }
        });
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void signInAnonymously(final AuthCallback callback) {
        FIRAuth.auth().signInAnonymouslyWithCompletion(new FIRAuth.Block_signInAnonymouslyWithCompletion() {
            @Override
            public void call_signInAnonymouslyWithCompletion(FIRAuthDataResult arg0, NSError arg1) {
                if (handleError(arg1, callback)) return;
                callback.onSuccess(getCurrentUser());
            }
        });
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void signOut(SignOutCallback callback) {
        try {
            Ptr<NSError> ptr = PtrFactory.newObjectReference(NSError.class);
            FIRAuth.auth().signOut(ptr);
            if (ptr.get() != null)
                throw new Exception(ptr.get().localizedDescription());
        } catch (Exception e) {
            callback.onFail(e);<commit_msg>iOS API: Fix Auth sign-out callback call<commit_after>/*
 * Copyright 2017 mk
 *
 * 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.
 */

package mk.gdx.firebase.ios.auth;

import com.google.firebaseauth.FIRAuth;
import com.google.firebaseauth.FIRAuthDataResult;
import com.google.firebaseauth.FIRUser;

import org.moe.natj.general.ptr.Ptr;
import org.moe.natj.general.ptr.impl.PtrFactory;

import apple.foundation.NSError;
import mk.gdx.firebase.auth.GdxFirebaseUser;
import mk.gdx.firebase.auth.UserInfo;
import mk.gdx.firebase.callbacks.AuthCallback;
import mk.gdx.firebase.callbacks.SignOutCallback;
import mk.gdx.firebase.distributions.AuthDistribution;

/**
 * iOS Firebase authorization API implementation.
 * <p>
 *
 * @see AuthDistribution
 */
public class Auth implements AuthDistribution {

    /**
     * {@inheritDoc}
     */
    @Override
    public GdxFirebaseUser getCurrentUser() {
        FIRUser firUser = FIRAuth.auth().currentUser();
        if (firUser == null) return null;
        UserInfo.Builder builder = new UserInfo.Builder();
        builder.setDisplayName(firUser.displayName())
                .setPhotoUrl(firUser.photoURL() != null ? firUser.photoURL().path() : null)
                .setProviderId(firUser.providerID())
                .setUid(firUser.uid())
                .setIsEmailVerified(firUser.isEmailVerified())
                .setIsAnonymous(firUser.isAnonymous())
                .setEmail(firUser.email());
        return GdxFirebaseUser.create(builder.build());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void createUserWithEmailAndPassword(String email, char[] password, final AuthCallback callback) {
        FIRAuth.auth().createUserWithEmailPasswordCompletion(email, new String(password), new FIRAuth.Block_createUserWithEmailPasswordCompletion() {
            @Override
            public void call_createUserWithEmailPasswordCompletion(FIRAuthDataResult arg0, NSError arg1) {
                if (handleError(arg1, callback)) return;
                // TODO - arg0 was chacnged from FIRUser to FIRAuthDataResult - need to adopt it
                callback.onSuccess(getCurrentUser());
            }
        });
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void signInWithEmailAndPassword(String email, char[] password, final AuthCallback callback) {
        FIRAuth.auth().signInWithEmailPasswordCompletion(email, new String(password), new FIRAuth.Block_signInWithEmailPasswordCompletion() {
            @Override
            public void call_signInWithEmailPasswordCompletion(FIRAuthDataResult arg0, NSError arg1) {
                if (handleError(arg1, callback)) return;
                // TODO
                callback.onSuccess(getCurrentUser());
            }
        });
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void signInWithToken(String token, final AuthCallback callback) {
        FIRAuth.auth().signInWithCustomTokenCompletion(token, new FIRAuth.Block_signInWithCustomTokenCompletion() {
            @Override
            public void call_signInWithCustomTokenCompletion(FIRAuthDataResult arg0, NSError arg1) {
                if (handleError(arg1, callback)) return;
                // TODO
                callback.onSuccess(getCurrentUser());
            }
        });
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void signInAnonymously(final AuthCallback callback) {
        FIRAuth.auth().signInAnonymouslyWithCompletion(new FIRAuth.Block_signInAnonymouslyWithCompletion() {
            @Override
            public void call_signInAnonymouslyWithCompletion(FIRAuthDataResult arg0, NSError arg1) {
                if (handleError(arg1, callback)) return;
                callback.onSuccess(getCurrentUser());
            }
        });
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void signOut(SignOutCallback callback) {
        try {
            Ptr<NSError> ptr = PtrFactory.newObjectReference(NSError.class);
            FIRAuth.auth().signOut(ptr);
            if (ptr.get() != null) {
                callback.onFail(new Exception(ptr.get().localizedDescription()));
            } else {
                callback.onSuccess();
            }
        } catch (Exception e) {
            callback.onFail(e);
{'fraction_non_alphanumeric': 0.09897969916903335, 'fraction_numerical': 0.004417797412432944, 'mean_word_length': 2.635946462715105, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '529051', 'n_tokens_mistral': 2676, 'n_tokens_neox': 2575, 'n_words': 574}