Skip to content

Commit

Permalink
Move auth status to AuthService
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffdgr8 committed Feb 14, 2024
1 parent 51db99d commit 7df744a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@ class UserRepository(
.map(::decodeDocument)
.stateIn(dbProvider.writeScope, SharingStarted.Eagerly, UserDoc())

val authStatus: StateFlow<AuthStatus> =
user.map {
when {
it == null -> AuthStatus.LoggedOut
it.userId.isBlank() -> AuthStatus.Unknown
else -> AuthStatus.LoggedIn
}
}
.stateIn(dbProvider.writeScope, SharingStarted.Eagerly, AuthStatus.Unknown)

val userId: String?
get() = user.value?.userId?.ifBlank { null }

Expand All @@ -52,7 +42,3 @@ class UserRepository(
private const val USER_DOC_ID = "user"
}
}

enum class AuthStatus {
LoggedIn, LoggedOut, Unknown
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
package domain.replication

import data.db.DatabaseProvider
import data.source.user.UserRepository
import io.ktor.client.HttpClient
import io.ktor.client.request.basicAuth
import io.ktor.client.request.get
import io.ktor.http.HttpStatusCode
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn

class AuthService(
private val syncGateway: SyncGateway,
dbProvider: DatabaseProvider,
private val userRepository: UserRepository
) {

val authStatus: StateFlow<AuthStatus> =
userRepository.user.map {
when {
it == null -> AuthStatus.LoggedOut
it.userId.isBlank() -> AuthStatus.Unknown
else -> AuthStatus.LoggedIn
}
}
.stateIn(dbProvider.writeScope, SharingStarted.Eagerly, AuthStatus.Unknown)

suspend fun authenticateUser(username: String, password: String): Boolean {
val result = HttpClient()
.get(syncGateway.httpEndpoint) {
Expand All @@ -29,3 +45,7 @@ class AuthService(
userRepository.deleteUser()
}
}

enum class AuthStatus {
LoggedIn, LoggedOut, Unknown
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package presentation

import data.source.user.AuthStatus
import data.source.user.UserRepository
import domain.replication.AuthService
import domain.replication.AuthStatus
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

class AppViewModel(
scope: CoroutineScope,
private val userRepository: UserRepository
private val authService: AuthService
) {

private val _screen = MutableStateFlow<Screen>(Screen.Splash)
val screen: StateFlow<Screen> get() = _screen

init {
scope.launch {
userRepository.authStatus.collect {
authService.authStatus.collect {
_screen.value = when (it) {
AuthStatus.LoggedIn -> Screen.Main
AuthStatus.LoggedOut -> Screen.Login
Expand Down

0 comments on commit 7df744a

Please sign in to comment.