0

I have developed one app in JAVA. Now I have made one function for firebase related calls. and Working perfectly. But now I want to convert that function to kotlin also. But I was confused about how to use that function call in kotlin.

Function of Java :

public static void firebaseAuth(FirebaseAuth auth, AuthCredential authCredential, Function<Object, Void> delegate) {
    auth.signInWithCredential(authCredential).addOnCompleteListener(task -> {
        if (task.isSuccessful()) {
            delegate.apply(auth.getCurrentUser());
        } else {
            delegate.apply(task.getException());
        }
    });
}

Use of this Function in Java :

FireSignInHelper.firebaseAuth(mFireAuth, authCredential, o -> {

            if (o instanceof Exception) {
                signIn_UnSuccessful((Exception) o);
            } else if (o instanceof FirebaseUser) {
                FirePacket firePacket = new FirePacket();
                firePacket.setProvider(provider);
                firePacket.setToken(((FirebaseUser) o).getUid());
                firePacket.setFirebaseUser((FirebaseUser) o);
                signIn_Successful(firePacket);
            }
            return null;
        });

Converted Function In Kotlin :

fun firebaseAuth(auth: FirebaseAuth,
    authCredential: AuthCredential?,
    delegate: Function<Any?, Void?>
) {
    auth.signInWithCredential(authCredential!!)
        .addOnCompleteListener { task: Task<AuthResult?> ->
            if (task.isSuccessful) {
                delegate.apply(auth.currentUser)
            } else {
                delegate.apply(task.exception)
            }
        }
}

Use Converted function in kotlin:

I want to know this answer. Because I have try through Android studio but the compiler didn't convert properly. So I want to know how to use this function in kotlin.

Thanks In advance.

2
  • Kindly refer to this post to get an answer. stackoverflow.com/questions/34588117/… Commented Apr 5, 2020 at 18:42
  • I have already tried that but not converted properly. That's why I have posted it here. Commented Apr 5, 2020 at 18:44

1 Answer 1

2

The way IntelliJ convert your code is correct. However the Function interface is of no use in kotlin since functions are first-class citizens. See higher-Order Functions examples from the doc.

I think you simply want to do something like this

fun firebaseAuth(
    auth: FirebaseAuth,
    authCredential: AuthCredential?,
    listener: (Any) -> Unit
) {
    auth.signInWithCredential(authCredential!!)
        .addOnCompleteListener {
            listener.invoke(
                if (it.isSuccessful) auth.currentUser 
                else it.exception
            )                
        }
}

And then

FireSignInHelper.firebaseAuth(mFireAuth, authCredential) { 
     when(it){
        is FirebaseUser -> { ... }
        is Exception -> { ... }
     }
}
Sign up to request clarification or add additional context in comments.

4 Comments

How can I use the this delegate: Function<Any?, Void?> third argument of function?
Why would you? This Function<Foo, Bar> is the Java way of implementing callbacks or function pointers. My code show you the way of doing it in Kotlin.
Okay I will try to do with your answer and keep you posted.
Thank You Very Much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.