-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.kt
More file actions
130 lines (107 loc) · 3.88 KB
/
MainActivity.kt
File metadata and controls
130 lines (107 loc) · 3.88 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package io.teller.connect.example
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.add
import androidx.fragment.app.commit
import com.google.android.material.snackbar.Snackbar
import io.teller.connect.sdk.*
import timber.log.Timber
class MainActivity : FragmentActivity(), ConnectListener {
companion object {
val config = Config(
appId = "YOUR-APPLICATION-ID",
environment = Environment.SANDBOX,
selectAccount = SelectAccount.SINGLE,
products = listOf(Product.IDENTITY),
debug = false
)
}
private lateinit var fragmentContainer: View
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
fragmentContainer = findViewById(R.id.fragmentContainer)
findViewById<View>(R.id.connectFragmentButton).setOnClickListener {
startTellerConnectFragment(config)
}
findViewById<View>(R.id.connectActivityButton).setOnClickListener {
startTellerConnectActivity(config)
}
}
private fun showSuccess(registration: Registration?) {
Timber.i("token: ${registration?.accessToken}")
val message =
"\uD83D\uDCB8 Success! \uD83D\uDCB8\naccessToken: ${registration?.accessToken}"
Snackbar.make(fragmentContainer, message, Snackbar.LENGTH_LONG).show()
}
private fun showSuccess(payment: Payment?) {
val message =
"\uD83D\uDCB8 Success! \uD83D\uDCB8\npayment: ${payment?.id}"
Snackbar.make(fragmentContainer, message, Snackbar.LENGTH_LONG).show()
}
private fun showSuccess(payee: Payee?) {
val message =
"\uD83D\uDCB8 Success! \uD83D\uDCB8\npayee: ${payee?.id}"
Snackbar.make(fragmentContainer, message, Snackbar.LENGTH_LONG).show()
}
private fun handleError(error: Error?) {
if (error != null) {
Snackbar.make(fragmentContainer, "Error: ${error.message}", Snackbar.LENGTH_LONG)
.show()
} else {
Snackbar.make(fragmentContainer, "Failure", Snackbar.LENGTH_LONG).show()
}
}
private fun startTellerConnectFragment(config: Config) {
val args = ConnectFragment.buildArgs(config)
supportFragmentManager.commit {
setReorderingAllowed(true)
addToBackStack("TellerConnect")
add<ConnectFragment>(R.id.fragmentContainer, args = args)
}
}
private fun startTellerConnectActivity(config: Config) {
val intent = Intent(this, ConnectActivity::class.java)
intent.putExtra(ConnectActivity.EXTRA_CONFIG, config)
startActivity(intent)
}
override fun onInit() {
Timber.d("Initialized Teller Connect")
}
override fun onExit() {
handleError(null)
removeTellerConnectFragment()
}
override fun onSuccess(registration: Registration) {
showSuccess(registration)
removeTellerConnectFragment()
}
override fun onSuccess(payment: Payment) {
showSuccess(payment)
removeTellerConnectFragment()
}
override fun onSuccess(payee: Payee) {
showSuccess(payee)
removeTellerConnectFragment()
}
override fun onEvent(name: String, data: Map<String, Any>) {
Timber.d("$name: $data")
}
override fun onFailure(error: Error) {
handleError(error)
removeTellerConnectFragment()
}
private fun removeTellerConnectFragment() {
supportFragmentManager.popBackStack()
}
@Deprecated("Deprecated by Android")
override fun onBackPressed() {
if (supportFragmentManager.fragments.isNotEmpty()) {
removeTellerConnectFragment()
} else {
super.onBackPressed()
}
}
}