英文:
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<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.
答案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) -> 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 -> { ... }
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论