-
Notifications
You must be signed in to change notification settings - Fork 2
/
MessagesFragment.kt
102 lines (89 loc) · 4.1 KB
/
MessagesFragment.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
package io.iohk.atala.prism.sampleapp.ui.messages
import android.app.AlertDialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import io.iohk.atala.prism.sampleapp.databinding.CredentialDialogBinding
import io.iohk.atala.prism.sampleapp.databinding.FragmentMessagesBinding
import io.iohk.atala.prism.walletsdk.domain.models.Credential
import io.iohk.atala.prism.walletsdk.domain.models.DID
class MessagesFragment : Fragment() {
private var _binding: FragmentMessagesBinding? = null
private val viewModel: MessagesViewModel by viewModels()
private val binding get() = _binding!!
private val adapter = MessagesAdapter()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentMessagesBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.list.adapter = adapter
binding.sendMessage.setOnClickListener {
viewModel.sendMessage(DID("did:peer:2.Ez6LSkjhgJcoGRTSTpjN5XBSKGpNtDSa55qidsahb1s3ucWkJ.Vz6MkgG8bJA2P2HNhCwh4DGHmBtUbKiCafYwBtDMjKnAihaE9.SeyJ0IjoiZG0iLCJzIjp7InVyaSI6ImRpZDpwZWVyOjIuRXo2TFNnaHdTRTQzN3duREUxcHQzWDZoVkRVUXpTanNIemlucFgzWEZ2TWpSQW03eS5WejZNa2hoMWU1Q0VZWXE2SkJVY1RaNkNwMnJhbkNXUnJ2N1lheDNMZTRONTlSNmRkLlNleUowSWpvaVpHMGlMQ0p6SWpwN0luVnlhU0k2SW1oMGRIQnpPaTh2WTNKcGMzUnBZVzR0YldWa2FXRjBiM0l1YW5KcFltOHVhMmwzYVNJc0ltRWlPbHNpWkdsa1kyOXRiUzkyTWlKZGZYMC5TZXlKMElqb2laRzBpTENKeklqcDdJblZ5YVNJNkluZHpjem92TDJOeWFYTjBhV0Z1TFcxbFpHbGhkRzl5TG1weWFXSnZMbXRwZDJrdmQzTWlMQ0poSWpwYkltUnBaR052YlcwdmRqSWlYWDE5IiwiciI6W10sImEiOltdfX0"))
}
binding.sendVerification.setOnClickListener {
InitiateVerificationDialogFragment(viewModel).show(
parentFragmentManager,
"InitiateVerificationDialogFragment"
)
}
setupStreamObservers()
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
// Function to show the dialog
fun showDialogWithOptions(
credentials: List<Credential>,
onCredentialSelected: (Credential) -> Unit
) {
val dialogBinding = CredentialDialogBinding.inflate(layoutInflater)
// Set up the spinner with the options
context?.let {
val adapter = CustomArrayAdapter(it, android.R.layout.simple_spinner_dropdown_item, credentials)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
dialogBinding.spinner.adapter = adapter
// Create and show the dialog
AlertDialog.Builder(context)
.setTitle("Choose an Option")
.setView(dialogBinding.root)
// Add any other dialog buttons or actions here
.setPositiveButton("OK") { _, which ->
val credential = credentials[dialogBinding.spinner.selectedItemPosition]
onCredentialSelected(credential)
}
.setNegativeButton("Cancel") { dialog, _ -> dialog.dismiss() }
.show()
}
}
private fun setupStreamObservers() {
viewModel.messagesStream().observe(this.viewLifecycleOwner) { messages ->
adapter.updateMessages(messages)
}
viewModel.proofRequestToProcess().observe(this.viewLifecycleOwner) { proofRequest ->
val message = proofRequest.first
val credentials = proofRequest.second
showDialogWithOptions(credentials) { credential ->
viewModel.preparePresentationProof(credential, message)
}
}
}
companion object {
@JvmStatic
fun newInstance(): MessagesFragment {
return MessagesFragment()
}
}
interface CredentialSelected {
fun onCredentialSelected(credential: Credential)
}
}