/
MainActivity.kt
110 lines (98 loc) · 3.94 KB
/
MainActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package devrel.firebase.google.com.functions.kotlin
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.tasks.Task
import com.google.firebase.functions.FirebaseFunctions
import com.google.firebase.functions.FirebaseFunctionsException
import com.google.firebase.functions.functions
import com.google.firebase.Firebase
class MainActivity : AppCompatActivity() {
// [START define_functions_instance]
private lateinit var functions: FirebaseFunctions
// [END define_functions_instance]
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// [START initialize_functions_instance]
functions = Firebase.functions
// [END initialize_functions_instance]
}
fun emulatorSettings() {
// [START functions_emulator_connect]
// 10.0.2.2 is the special IP address to connect to the 'localhost' of
// the host computer from an Android emulator.
val functions = Firebase.functions
functions.useEmulator("10.0.2.2", 5001)
// [END functions_emulator_connect]
}
// [START function_add_numbers]
private fun addNumbers(a: Int, b: Int): Task<Int> {
// Create the arguments to the callable function, which are two integers
val data = hashMapOf(
"firstNumber" to a,
"secondNumber" to b,
)
// Call the function and extract the operation from the result
return functions
.getHttpsCallable("addNumbers")
.call(data)
.continueWith { task ->
// This continuation runs on either success or failure, but if the task
// has failed then task.result will throw an Exception which will be
// propagated down.
val result = task.result?.data as Map<String, Any>
result["operationResult"] as Int
}
}
// [END function_add_numbers]
// [START function_add_message]
private fun addMessage(text: String): Task<String> {
// Create the arguments to the callable function.
val data = hashMapOf(
"text" to text,
"push" to true,
)
return functions
.getHttpsCallable("addMessage")
.call(data)
.continueWith { task ->
// This continuation runs on either success or failure, but if the task
// has failed then result will throw an Exception which will be
// propagated down.
val result = task.result?.data as String
result
}
}
// [END function_add_message]
private fun callAddNumbers(firstNumber: Int, secondNumber: Int) {
// [START call_add_numbers]
addNumbers(firstNumber, secondNumber)
.addOnCompleteListener { task ->
if (!task.isSuccessful) {
val e = task.exception
if (e is FirebaseFunctionsException) {
// Function error code, will be INTERNAL if the failure
// was not handled properly in the function call.
val code = e.code
// Arbitrary error details passed back from the function,
// usually a Map<String, Any>.
val details = e.details
}
}
}
// [END call_add_numbers]
}
private fun callAddMessage(inputMessage: String) {
// [START call_add_message]
addMessage(inputMessage)
.addOnCompleteListener { task ->
if (!task.isSuccessful) {
val e = task.exception
if (e is FirebaseFunctionsException) {
val code = e.code
val details = e.details
}
}
}
// [END call_add_message]
}
}