Skip to content

Commit

Permalink
fix: fixes demo app fetching job
Browse files Browse the repository at this point in the history
  • Loading branch information
amagyar-iohk committed Nov 20, 2023
1 parent 1e32bb7 commit 8f3f0d7
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,6 @@ class PrismAgent {
store = BasicMediatorHandler.PlutoMediatorRepositoryImpl(pluto)
)
setupMediatorHandler(tmpMediatorHandler)
start()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ class SampleApp : Application() {

override fun onCreate() {
super.onCreate()
Sdk.getInstance(this)
Sdk.getInstance()
}
}
166 changes: 61 additions & 105 deletions sampleapp/src/main/java/io/iohk/atala/prism/sampleapp/Sdk.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package io.iohk.atala.prism.sampleapp

import android.content.Context
import android.app.Application
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import io.iohk.atala.prism.walletsdk.apollo.ApolloImpl
import io.iohk.atala.prism.walletsdk.castor.CastorImpl
import io.iohk.atala.prism.walletsdk.domain.buildingblocks.Apollo
import io.iohk.atala.prism.walletsdk.domain.buildingblocks.Castor
import io.iohk.atala.prism.walletsdk.domain.buildingblocks.Mercury
import io.iohk.atala.prism.walletsdk.domain.buildingblocks.Pluto
import io.iohk.atala.prism.walletsdk.domain.buildingblocks.Pollux
import io.iohk.atala.prism.walletsdk.domain.buildingblocks.*
import io.iohk.atala.prism.walletsdk.domain.models.ApiImpl
import io.iohk.atala.prism.walletsdk.domain.models.DID
import io.iohk.atala.prism.walletsdk.domain.models.Seed
Expand All @@ -23,112 +21,75 @@ import io.iohk.atala.prism.walletsdk.prismagent.mediation.BasicMediatorHandler
import io.iohk.atala.prism.walletsdk.prismagent.mediation.MediationHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.subscribe
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import java.net.UnknownHostException

class Sdk() {
class Sdk {
private val apollo: Apollo = createApollo()
private val castor: Castor = createCastor()
private var pollux: Pollux = createPollux()
private val seed: Seed = createSeed()
private val agentStatusStream: MutableLiveData<PrismAgent.State> = MutableLiveData()

private var apollo: Apollo? = null
get() {
return field ?: throw IllegalStateException("You haven't started the agent")
}

private var castor: Castor? = null
get() {
return field ?: throw IllegalStateException("You haven't started the agent")
}

var pluto: Pluto? = null
get() {
return field ?: throw IllegalStateException("You haven't started the agent")
}
val pluto: Pluto = createPluto()
val mercury: Mercury = createMercury()

var mercury: Mercury? = null
get() {
return field ?: throw IllegalStateException("You haven't started the agent")
}

var handler: MediationHandler? = null
private set
get() {
return field ?: throw IllegalStateException("You haven't started the agent")
}

private var seed: Seed? = null
private set
get() {
return field ?: throw IllegalStateException("You haven't started the agent")
}
lateinit var handler: MediationHandler
lateinit var agent: PrismAgent

private var pollux: Pollux? = null
private set
get() {
return field ?: throw IllegalStateException("You haven't started the agent")
}
@Throws(PrismAgentError.MediationRequestFailedError::class, UnknownHostException::class)
suspend fun startAgent(mediatorDID: String, context: Application) {
handler = createHandler(mediatorDID)
agent = createAgent(handler)

var agent: PrismAgent? = null
private set
get() {
return field
CoroutineScope(Dispatchers.Default).launch {
agent.flowState.collect {
agentStatusStream.postValue(it)
}
}

@Throws(PrismAgentError.MediationRequestFailedError::class, UnknownHostException::class)
suspend fun startAgent() {
agent?.let {
it.start()
it.startFetchingMessages()
}
}
(pluto as PlutoImpl).start(context)
agent.start()
agent.startFetchingMessages()

suspend fun updateMediatorDID(mediatorDID: String) {
agent?.setupMediatorDID(DID(mediatorDID))
agentStatusStream.postValue(PrismAgent.State.RUNNING)
}

fun stopAgent() {
agent?.let {
agent.let {
it.stopFetchingMessages()
it.stop()
}
}

private suspend fun initializeComponents(context: Context) {
initializeApollo()
initializePluto(context)
initializeCastor()
initializePollux()
initializeMercury()
initializeSeed()
initializeHandler()
initializeAgent()
fun agentStatusStream(): LiveData<PrismAgent.State> {
return agentStatusStream
}

private suspend fun initializePluto(context: Context) {
pluto = PlutoImpl(DbConnection())
(pluto as PlutoImpl).start(context)
private fun createPluto(): Pluto {
return PlutoImpl(DbConnection())
}

private fun initializeApollo() {
apollo = ApolloImpl()
private fun createApollo(): Apollo {
return ApolloImpl()
}

private fun initializeCastor() {
castor = CastorImpl(apollo!!)
private fun createCastor(): Castor {
return CastorImpl(apollo)
}

private fun initializeMercury() {
private fun createMercury(): MercuryImpl {
// This is just to make the code compile, it should be changed accordingly
mercury = MercuryImpl(
castor!!,
DIDCommWrapper(castor!!, pluto!!, apollo!!),
ApiImpl(httpClient())
)
return MercuryImpl(castor, DIDCommWrapper(castor, pluto, apollo), ApiImpl(httpClient()))
}

private fun initializePollux() {
pollux = PolluxImpl(castor!!)
private fun createPollux(): PolluxImpl {
return PolluxImpl(castor)
}

private fun initializeSeed() {
private fun createSeed(): Seed {
val words = arrayOf(
"trumpet",
"mass",
Expand All @@ -155,43 +116,38 @@ class Sdk() {
"admit",
"peanut"
)
seed = apollo!!.createSeed(words, "")
return apollo.createSeed(words, "")
}

private fun initializeHandler() {
handler = BasicMediatorHandler(
// Roots ID
// mediatorDID = DID("did:peer:2.Ez6LSms555YhFthn1WV8ciDBpZm86hK9tp83WojJUmxPGk1hZ.Vz6MkmdBjMyB4TS5UbbQw54szm8yvMMf1ftGV2sQVYAxaeWhE.SeyJpZCI6Im5ldy1pZCIsInQiOiJkbSIsInMiOiJodHRwczovL21lZGlhdG9yLnJvb3RzaWQuY2xvdWQiLCJhIjpbImRpZGNvbW0vdjIiXX0"),
// Prism Mediator
mediatorDID = DID("did:peer:2.Ez6LSghwSE437wnDE1pt3X6hVDUQzSjsHzinpX3XFvMjRAm7y.Vz6Mkhh1e5CEYYq6JBUcTZ6Cp2ranCWRrv7Yax3Le4N59R6dd.SeyJ0IjoiZG0iLCJzIjoiaHR0cHM6Ly9zaXQtcHJpc20tbWVkaWF0b3IuYXRhbGFwcmlzbS5pbyIsInIiOltdLCJhIjpbImRpZGNvbW0vdjIiXX0"),
mercury = mercury!!,
store = BasicMediatorHandler.PlutoMediatorRepositoryImpl(pluto!!)
private fun createHandler(mediatorDID: String): MediationHandler {
return BasicMediatorHandler(
mediatorDID = DID(mediatorDID),
mercury = mercury,
store = BasicMediatorHandler.PlutoMediatorRepositoryImpl(pluto)
)
}

private fun initializeAgent() {
agent = PrismAgent(
apollo = apollo!!,
castor = castor!!,
pluto = pluto!!,
mercury = mercury!!,
pollux = pollux!!,
private fun createAgent(handler: MediationHandler): PrismAgent {
return PrismAgent(
apollo = apollo,
castor = castor,
pluto = pluto,
mercury = mercury,
pollux = pollux,
seed = seed,
mediatorHandler = handler!!
mediatorHandler = handler
)
}

companion object {
private var instance: Sdk? = null
private lateinit var instance: Sdk

fun getInstance(context: Context): Sdk {
if (instance == null) {
fun getInstance(): Sdk {
if (!this::instance.isInitialized) {
instance = Sdk()
CoroutineScope(Dispatchers.Default).launch {
instance!!.initializeComponents(context)
}
}
return instance!!
return instance
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package io.iohk.atala.prism.sampleapp.ui.agent
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import io.iohk.atala.prism.sampleapp.SampleApp
import io.iohk.atala.prism.sampleapp.Sdk
import kotlinx.coroutines.launch

class AgentViewModel(application: Application) : AndroidViewModel(application) {

fun startAgent(mediatorDID: String) {
val sdk = Sdk.getInstance(getApplication<Application>())
val sdk = Sdk.getInstance()
viewModelScope.launch {
sdk.startAgent()
sdk.updateMediatorDID(mediatorDID)
sdk.startAgent(mediatorDID, getApplication<Application>())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class ContactsViewModel(application: Application) : AndroidViewModel(application

init {
viewModelScope.launch {
val pluto = Sdk.getInstance(getApplication<Application>()).pluto
pluto?.getAllDidPairs()?.collect {
val pluto = Sdk.getInstance().pluto
pluto.getAllDidPairs().collect {
contactsStream.postValue(it)
}
}
Expand All @@ -34,8 +34,7 @@ class ContactsViewModel(application: Application) : AndroidViewModel(application

@Throws(PrismAgentError.UnknownInvitationTypeError::class, Exception::class)
fun parseAndAcceptOOB(oobUrl: String) {
val agent = Sdk.getInstance(getApplication<Application>()).agent
agent?.let {
Sdk.getInstance().agent.let { agent ->
viewModelScope.launch {
when (val invitation = agent.parseInvitation(oobUrl)) {
is OutOfBandInvitation -> {
Expand All @@ -51,7 +50,7 @@ class ContactsViewModel(application: Application) : AndroidViewModel(application
}
}
}
} ?: throw Exception("Agent is null")
}
}

fun isValidURL(url: String): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class CredentialsAdapter(private var data: MutableList<Credential> = mutableList
AnonCredential::class -> {
val anon = cred as AnonCredential
type.text = String.format(typeString, "Anoncred")
issuanceDate.text = "Issuer: ${anon.credentialDefinitionID}"
issuanceDate.text = String.format("Issuer: ${anon.credentialDefinitionID}")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CredentialsViewModel(application: Application) : AndroidViewModel(applicat

init {
viewModelScope.launch {
Sdk.getInstance(application).agent?.let {
Sdk.getInstance().agent.let {
it.getAllCredentials().collect { list ->
credentials.postValue(list)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class DIDsViewModel(application: Application) : AndroidViewModel(application) {

init {
viewModelScope.launch {
val pluto = Sdk.getInstance(getApplication<Application>()).pluto
pluto?.getAllPeerDIDs()?.collect { peerDIDs ->
val dids = peerDIDs.map { it.did }
didsStream.postValue(dids)
val pluto = Sdk.getInstance().pluto
pluto.getAllPeerDIDs().collect { peerDIDs ->
val didList = peerDIDs.map { it.did }
didsStream.postValue(didList)
}
}
}
Expand All @@ -32,19 +32,17 @@ class DIDsViewModel(application: Application) : AndroidViewModel(application) {

fun createPeerDID() {
viewModelScope.launch {
val sdk = Sdk.getInstance(getApplication())
sdk.agent?.let {
it.createNewPeerDID(
arrayOf(
DIDDocument.Service(
DIDCOMM1,
arrayOf(DIDCOMM_MESSAGING),
DIDDocument.ServiceEndpoint(sdk.handler?.mediatorDID.toString())
)
),
true
)
}
val sdk = Sdk.getInstance()
sdk.agent.createNewPeerDID(
arrayOf(
DIDDocument.Service(
DIDCOMM1,
arrayOf(DIDCOMM_MESSAGING),
DIDDocument.ServiceEndpoint(sdk.handler.mediatorDID.toString())
)
),
true
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import io.iohk.atala.prism.walletsdk.prismagent.PrismAgent
class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding
private val viewModel: MainViewModel by viewModels()
private lateinit var viewPager: ViewPager
private lateinit var tabs: TabLayout
private lateinit var sectionsPagerAdapter: SectionsPagerAdapter
Expand All @@ -28,9 +27,8 @@ class MainActivity : AppCompatActivity() {
sectionsPagerAdapter = SectionsPagerAdapter(this, supportFragmentManager)
viewPager = binding.viewPager
tabs = binding.tabs
viewModel.agentStatusStream().observe(this) {
if (it == PrismAgent.State.RUNNING.name) {
// binding.agentStatus.text = String.format(getString(R.string.agent_status), it)
Sdk.getInstance().agentStatusStream().observe(this) {
if (it == PrismAgent.State.RUNNING) {
Snackbar.make(binding.root, "Agent state: $it", Snackbar.LENGTH_LONG).show()
agentStartedShowViews()
}
Expand All @@ -46,6 +44,6 @@ class MainActivity : AppCompatActivity() {

override fun onDestroy() {
super.onDestroy()
Sdk.getInstance(this).stopAgent()
Sdk.getInstance().stopAgent()
}
}
Loading

0 comments on commit 8f3f0d7

Please sign in to comment.