将一个函数从Java转换为Kotlin(Android)。

huangapple go评论70阅读模式
英文:

Convert one function from java to kotlin android

问题

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:

我想要知道这个答案。因为我已经尝试过在Android Studio中进行转换,但编译器没有正确地进行转换。所以我想知道如何在Kotlin中使用这个函数。

提前感谢您。

英文:

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&lt;Object, Void&gt; delegate) {
    auth.signInWithCredential(authCredential).addOnCompleteListener(task -&gt; {
        if (task.isSuccessful()) {
            delegate.apply(auth.getCurrentUser());
        } else {
            delegate.apply(task.getException());
        }
    });
}

Use of this Function in Java :

FireSignInHelper.firebaseAuth(mFireAuth, authCredential, o -&gt; {

            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&lt;Any?, Void?&gt;
) {
    auth.signInWithCredential(authCredential!!)
        .addOnCompleteListener { task: Task&lt;AuthResult?&gt; -&gt;
            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.

答案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 -> { ... }
     }
}
英文:

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) -&gt; 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 -&gt; { ... }
        is Exception -&gt; { ... }
     }
}

huangapple
  • 本文由 发表于 2020年4月6日 02:35:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/61047582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定