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

Implemented sessionDataSource & userDataSource in UseranmeRegistration

parent e061a5a3
No related branches found
No related tags found
No related merge requests found
Showing
with 221 additions and 0 deletions
File moved
File moved
package io.elixxir.data.networking
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
@Module
@InstallIn(SingletonComponent::class)
interface NetworkModule {
}
\ No newline at end of file
package io.elixxir.data.networking
interface NetworkRepository {
suspend fun initializeNetwork(): Result<Unit>
}
\ No newline at end of file
package io.elixxir.data.networking.data
import io.elixxir.core.logging.log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
private const val MAX_NETWORK_RETRIES = 29
private const val NETWORK_POLL_INTERVAL_MS = 1000L
class NetworkDataSource {
private suspend fun connectToCmix(retries: Int = 0) {
networking.checkRegisterNetworkCallback()
if (retries < MAX_NETWORK_RETRIES) {
if (initializeNetworkFollower()) {
log("Started network follower after #${retries + 1} attempt(s).")
withContext(Dispatchers.Main) {
onUsernameNextClicked()
}
} else {
delay(NETWORK_POLL_INTERVAL_MS)
log("Attempting to start network follower, attempt #${retries + 1}.")
connectToCmix(retries + 1)
}
} else throw Exception("Failed to connect to network after ${retries + 1} attempts. Please try again.")
}
private suspend fun initializeNetworkFollower(): Boolean = suspendCoroutine { continuation ->
networking.tryStartNetworkFollower { successful ->
continuation.resume(successful)
}
}
}
\ No newline at end of file
...@@ -2,6 +2,8 @@ package io.elixxir.data.session.data ...@@ -2,6 +2,8 @@ package io.elixxir.data.session.data
import io.elixxir.data.session.SessionRepository import io.elixxir.data.session.SessionRepository
import io.elixxir.data.session.model.SessionState import io.elixxir.data.session.model.SessionState
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import javax.inject.Inject import javax.inject.Inject
class SessionDataSource @Inject internal constructor() : SessionRepository { class SessionDataSource @Inject internal constructor() : SessionRepository {
...@@ -20,4 +22,18 @@ class SessionDataSource @Inject internal constructor() : SessionRepository { ...@@ -20,4 +22,18 @@ class SessionDataSource @Inject internal constructor() : SessionRepository {
override fun deleteSession() { override fun deleteSession() {
TODO("Not yet implemented") TODO("Not yet implemented")
} }
private fun getOrCreateSession() {
scope.launch(Dispatchers.IO) {
val appFolder = repo.createSessionFolder(context)
try {
repo.newClient(appFolder, sessionPassword)
preferences.lastAppVersion = BuildConfig.VERSION_CODE
connectToCmix()
} catch (err: Exception) {
err.printStackTrace()
displayError(err.toString())
}
}
}
} }
\ No newline at end of file
/build
\ No newline at end of file
plugins {
id("com.android.library")
kotlin("android")
kotlin("kapt")
id("dagger.hilt.android.plugin")
}
android {
compileSdk = Versions.COMPILE_SDK
defaultConfig {
minSdk = Versions.MIN_SDK
targetSdk = Versions.TARGET_SDK
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
kapt {
correctErrorTypes = true
}
dependencies {
implementation(project(":depconstraints"))
implementation(project(":xxclient"))
implementation(project(":core:logging"))
implementation(project(":core:preferences"))
implementation(Libs.Core.ANDROIDX_APPCOMPAT)
implementation(Libs.Core.CORE_KTX)
implementation(Libs.Core.CORE_COROUTINES)
implementation(Libs.DI.HILT)
kapt(Libs.DI.HILT_KAPT)
}
\ No newline at end of file
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
\ No newline at end of file
package io.elixxir.data.userdiscovery
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.Assert.*
/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("io.elixxir.data.userdiscovery.test", appContext.packageName)
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.elixxir.data.userdiscovery">
</manifest>
\ No newline at end of file
package io.elixxir.data.userdiscovery
interface UserModule {
}
\ No newline at end of file
package io.elixxir.data.userdiscovery
interface UserRepository {
suspend fun registerUsername(username: String): Result<Unit>
}
\ No newline at end of file
package io.elixxir.data.userdiscovery.data
import io.elixxir.data.userdiscovery.UserRepository
class UserDataSource : UserRepository {
override suspend fun registerUsername(username: String): Result<Unit> {
// Save name to preferences if successful
TODO("Not yet implemented")
}
private fun registerUsername(username: String, isDemoAcct: Boolean = false) {
if (!sessionExists) {
getOrCreateSession()
return
}
repo.registerUdUsername(username)
.subscribeOn(scheduler.single)
.observeOn(scheduler.main)
.doOnError {
it.message?.let { error ->
if (error.isNetworkNotHealthyError()) handleNetworkHealthError()
else {
displayError(error)
enableUI()
}
}
}.doOnSuccess {
onSuccessfulRegistration(username, isDemoAcct)
}.subscribe()
}
private fun displayError(errorMsg: String) {
error.postValue(bindingsErrorMessage(Exception(errorMsg)))
}
\ 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