Skip to content

Commit fb0f748

Browse files
committed
Initialize databases before broadcasting user
1 parent 7cb070e commit fb0f748

File tree

5 files changed

+30
-15
lines changed

5 files changed

+30
-15
lines changed

src/app/src/androidTest/java/com/couchbase/learningpath/DatabaseIntegrationTests.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import com.couchbase.learningpath.models.*
1414
import com.couchbase.learningpath.services.MockAuthenticationService
1515
import com.couchbase.lite.CouchbaseLiteException
1616
import kotlinx.coroutines.flow.*
17+
import kotlinx.coroutines.runBlocking
1718
import kotlinx.coroutines.test.runTest
1819
import org.junit.*
1920
import org.junit.Assert.*
@@ -80,8 +81,10 @@ class DatabaseIntegrationTests {
8081
databaseManager.deleteDatabases()
8182
databaseManager.initializeDatabases(user1)
8283

83-
authenticationService = MockAuthenticationService()
84-
val isAuth = authenticationService.authenticatedUser(user1.username, user1.password)
84+
authenticationService = MockAuthenticationService(databaseManager)
85+
val isAuth = runBlocking {
86+
authenticationService.authenticatedUser(user1.username, user1.password)
87+
}
8588

8689
//arrange repositories
8790
auditRepository = AuditRepositoryDb(authenticationService, databaseManager)

src/app/src/main/java/com/couchbase/learningpath/services/AuthenticationService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ import com.couchbase.learningpath.models.User
66
interface AuthenticationService {
77
val currentUser: LiveData<User?>
88
fun getCurrentUser() : User
9-
fun authenticatedUser(username: String, password: String) : Boolean
9+
suspend fun authenticatedUser(username: String, password: String) : Boolean
1010
fun logout()
1111
}

src/app/src/main/java/com/couchbase/learningpath/services/MockAuthenticationService.kt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ package com.couchbase.learningpath.services
22

33
import androidx.lifecycle.LiveData
44
import androidx.lifecycle.MutableLiveData
5+
import com.couchbase.learningpath.data.DatabaseManager
56
import com.couchbase.learningpath.models.User
7+
import kotlinx.coroutines.Dispatchers
8+
import kotlinx.coroutines.withContext
69

7-
class MockAuthenticationService : AuthenticationService {
10+
class MockAuthenticationService(
11+
private val databaseManager: DatabaseManager
12+
) : AuthenticationService {
813

914
private var _user = MutableLiveData<User?>()
1015
private var _mockUsers = HashMap<String, User>()
@@ -15,11 +20,17 @@ class MockAuthenticationService : AuthenticationService {
1520
return _user.value ?: User("", "", "")
1621
}
1722

18-
override fun authenticatedUser(username: String, password: String): Boolean {
23+
override suspend fun authenticatedUser(username: String, password: String): Boolean {
1924
return if (_mockUsers.containsKey(username)){
2025
val user = _mockUsers[username]
2126
if (user?.password == password){
22-
_user.value = user
27+
withContext(Dispatchers.IO) {
28+
//initialize database if needed
29+
databaseManager.initializeDatabases(user)
30+
withContext(Dispatchers.Main) {
31+
_user.value = user
32+
}
33+
}
2334
true
2435
} else {
2536
false

src/app/src/main/java/com/couchbase/learningpath/ui/login/LoginView.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import androidx.compose.foundation.text.KeyboardOptions
88
import androidx.compose.material.*
99
import androidx.compose.runtime.Composable
1010
import androidx.compose.runtime.livedata.observeAsState
11+
import androidx.compose.runtime.rememberCoroutineScope
1112
import androidx.compose.ui.Alignment
1213
import androidx.compose.ui.Modifier
1314
import androidx.compose.ui.graphics.Color
@@ -28,6 +29,7 @@ import com.google.accompanist.drawablepainter.rememberDrawablePainter
2829
import com.couchbase.learningpath.R
2930
import com.couchbase.learningpath.ui.theme.LearningPathTheme
3031
import com.couchbase.learningpath.ui.theme.Red500
32+
import kotlinx.coroutines.launch
3133

3234
@Composable
3335
fun LoginView(onSuccessLogin: () -> Unit,
@@ -36,12 +38,16 @@ fun LoginView(onSuccessLogin: () -> Unit,
3638
val password = viewModel.password.observeAsState("")
3739
val isError = viewModel.isError.observeAsState(false)
3840

41+
val composableScope = rememberCoroutineScope()
42+
3943
//****
4044
//checks authentication if works, route to UserProfile
4145
//****
4246
val onLoginCheck: () -> Unit = {
43-
if (viewModel.login()) {
44-
onSuccessLogin()
47+
composableScope.launch {
48+
if (viewModel.login()) {
49+
onSuccessLogin()
50+
}
4551
}
4652
}
4753

src/app/src/main/java/com/couchbase/learningpath/ui/login/LoginViewModel.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,12 @@ class LoginViewModel(
3434
private val _isError = MutableLiveData(false)
3535
val isError: LiveData<Boolean> = _isError
3636

37-
fun login(): Boolean {
37+
suspend fun login(): Boolean {
3838
_username.value?.let { uname ->
3939
_password.value?.let { pwd ->
4040
if (authenticationService.authenticatedUser(username = uname, password = pwd)) {
4141
_isError.value = false
42-
val currentUser = authenticationService.getCurrentUser()
43-
viewModelScope.launch(Dispatchers.IO) {
44-
//initialize database if needed
45-
databaseManager.initializeDatabases(currentUser)
46-
replicatorService.updateAuthentication(isReset = false)
47-
}
42+
replicatorService.updateAuthentication(isReset = false)
4843
return true
4944
}
5045
}

0 commit comments

Comments
 (0)