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

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

Convert one function from java to kotlin android

问题

Converted Function In Kotlin :

  1. fun firebaseAuth(auth: FirebaseAuth, authCredential: AuthCredential?,
  2. delegate: Function<Any?, Void?>
  3. ) {
  4. auth.signInWithCredential(authCredential!!)
  5. .addOnCompleteListener { task: Task<AuthResult?> ->
  6. if (task.isSuccessful) {
  7. delegate.apply(auth.currentUser)
  8. } else {
  9. delegate.apply(task.exception)
  10. }
  11. }
  12. }

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 :

  1. public static void firebaseAuth(FirebaseAuth auth, AuthCredential authCredential, Function&lt;Object, Void&gt; delegate) {
  2. auth.signInWithCredential(authCredential).addOnCompleteListener(task -&gt; {
  3. if (task.isSuccessful()) {
  4. delegate.apply(auth.getCurrentUser());
  5. } else {
  6. delegate.apply(task.getException());
  7. }
  8. });
  9. }

Use of this Function in Java :

  1. FireSignInHelper.firebaseAuth(mFireAuth, authCredential, o -&gt; {
  2. if (o instanceof Exception) {
  3. signIn_UnSuccessful((Exception) o);
  4. } else if (o instanceof FirebaseUser) {
  5. FirePacket firePacket = new FirePacket();
  6. firePacket.setProvider(provider);
  7. firePacket.setToken(((FirebaseUser) o).getUid());
  8. firePacket.setFirebaseUser((FirebaseUser) o);
  9. signIn_Successful(firePacket);
  10. }
  11. return null;
  12. });

Converted Function In Kotlin :

  1. fun firebaseAuth(auth: FirebaseAuth,
  2. authCredential: AuthCredential?,
  3. delegate: Function&lt;Any?, Void?&gt;
  4. ) {
  5. auth.signInWithCredential(authCredential!!)
  6. .addOnCompleteListener { task: Task&lt;AuthResult?&gt; -&gt;
  7. if (task.isSuccessful) {
  8. delegate.apply(auth.currentUser)
  9. } else {
  10. delegate.apply(task.exception)
  11. }
  12. }
  13. }

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

  1. fun firebaseAuth(
  2. auth: FirebaseAuth,
  3. authCredential: AuthCredential?,
  4. listener: (Any) -> Unit
  5. ) {
  6. auth.signInWithCredential(authCredential!!)
  7. .addOnCompleteListener {
  8. listener.invoke(
  9. if (it.isSuccessful) auth.currentUser
  10. else it.exception
  11. )
  12. }
  13. }

And then

  1. FireSignInHelper.firebaseAuth(mFireAuth, authCredential) {
  2. when(it){
  3. is FirebaseUser -> { ... }
  4. is Exception -> { ... }
  5. }
  6. }
英文:

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

  1. fun firebaseAuth(
  2. auth: FirebaseAuth,
  3. authCredential: AuthCredential?,
  4. listener: (Any) -&gt; Unit
  5. ) {
  6. auth.signInWithCredential(authCredential!!)
  7. .addOnCompleteListener {
  8. listener.invoke(
  9. if (it.isSuccessful) auth.currentUser
  10. else it.exception
  11. )
  12. }
  13. }

And then

  1. FireSignInHelper.firebaseAuth(mFireAuth, authCredential) {
  2. when(it){
  3. is FirebaseUser -&gt; { ... }
  4. is Exception -&gt; { ... }
  5. }
  6. }

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:

确定