Skip to content
Snippets Groups Projects
Commit f01046b4 authored by Kamal Bramwell's avatar Kamal Bramwell
Browse files

Moved cmix & e2e functions to Networking

parent c56621a0
Branches
No related tags found
No related merge requests found
package io.elixxir.dapp.networking.data
import bindings.AuthCallbacks
import bindings.Bindings
import bindings.Cmix
import io.elixxir.dapp.session.model.*
import kotlinx.coroutines.withContext
class Networking(private val cmix: Cmix) {
fun connectToCmix() {
private fun login(
cmixId: Long,
authCallbacks: AuthCallbacks,
identity: ReceptionIdentity,
e2eParamsJson: E2eParams
): E2eMediator {
return E2eMediator(
Bindings.login(cmixId, authCallbacks, identity.value, e2eParamsJson.value)
)
}
private fun newCmix(
ndfJson: String,
sessionFolderPath: String,
sessionPassword: SessionPassword,
registrationCode: String
) {
Bindings.newCmix(
ndfJson,
sessionFolderPath,
sessionPassword.value,
registrationCode
)
}
private suspend fun loadCmix(
sessionFolderPath: String,
sessionPassword: SessionPassword,
cmixParamsJson: E2eParams
): CmixMediator = withContext(dispatcher) {
CmixMediator(
Bindings.loadCmix(sessionFolderPath, sessionPassword.value, cmixParamsJson.value)
)
}
private fun createIdentity(): ReceptionIdentity {
return ReceptionIdentity(
Cmix().makeReceptionIdentity()
)
}
private fun initNetworkFollower() {
......
package io.elixxir.dapp.session.data
import android.content.Context
import bindings.AuthCallbacks
import java.io.File
import bindings.Bindings
import bindings.Cmix
import io.elixxir.dapp.DappSdk.Companion.context
import io.elixxir.dapp.DappSdk.Companion.defaultDispatcher
import io.elixxir.dapp.DappSdk.Companion.logger
import io.elixxir.dapp.logger.data.Logger
import io.elixxir.dapp.session.model.*
import io.elixxir.dapp.model.CommonProperties
import kotlinx.coroutines.*
/**
* Establishes & persists an identity to perform Cmix operations.
* Responsible for creation & deletion of folder used to persist Cmix session.
* Only needs to be created once per app installation.
*/
internal interface SessionManager {
val sessionFolder: File
suspend fun createSession(): Result<File>
suspend fun deleteSession(): Result<Unit>
}
internal class DappSessionManager private constructor(
logger: Logger,
private val context: () -> Context,
private val dispatcher: CoroutineDispatcher
) : SessionManager, Logger by logger {
properties: CommonProperties
) : SessionManager, CommonProperties by properties {
override val sessionFolder: File get() {
return try {
......@@ -35,7 +27,15 @@ internal class DappSessionManager private constructor(
}
}
fun createSessionFolder(): File {
override suspend fun createSession(): Result<File> = withContext(dispatcher) {
try {
Result.success(createSessionFolder())
} catch (e: Exception) {
Result.failure(e)
}
}
private fun createSessionFolder(): File {
return sessionFolder.apply {
if (exists()) {
log("Session folder from previous installation was found.")
......@@ -48,54 +48,21 @@ internal class DappSessionManager private constructor(
}
}
fun newCmix(
ndfJson: String,
sessionFolderPath: String,
sessionPassword: SessionPassword,
registrationCode: String
) {
Bindings.newCmix(
ndfJson,
sessionFolderPath,
sessionPassword.value,
registrationCode
)
override suspend fun deleteSession(): Result<Unit> = withContext(dispatcher) {
try {
if (sessionFolder.deleteRecursively()) {
Result.success(Unit)
} else {
Result.failure(Exception("Failed to delete all files."))
}
suspend fun loadCmix(
sessionFolderPath: String,
sessionPassword: SessionPassword,
cmixParamsJson: E2eParams
): CmixMediator = withContext(dispatcher) {
CmixMediator(
Bindings.loadCmix(sessionFolderPath, sessionPassword.value, cmixParamsJson.value)
)
}
fun login(
cmixId: Long,
authCallbacks: AuthCallbacks,
identity: ReceptionIdentity,
e2eParamsJson: E2eParams
): E2eMediator {
return E2eMediator(
Bindings.login(cmixId, authCallbacks, identity.value, e2eParamsJson.value)
)
} catch (e: Exception) {
Result.failure(e)
}
fun createIdentity(): ReceptionIdentity {
return ReceptionIdentity(
Cmix().makeReceptionIdentity()
)
}
companion object {
internal fun newInstance(): SessionManager {
return DappSessionManager(
logger = logger,
context = context,
dispatcher = defaultDispatcher
)
internal fun newInstance(properties: CommonProperties): SessionManager {
return DappSessionManager(properties)
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment