-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Authenticate and redirect to new page
- Loading branch information
1 parent
0fd9f4f
commit 1840707
Showing
9 changed files
with
85 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 42 additions & 2 deletions
44
app/src/main/java/one/njk/celestidesk/viewmodels/AuthViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,47 @@ | ||
package one.njk.celestidesk.viewmodels | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.asLiveData | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.flatMapLatest | ||
import kotlinx.coroutines.flow.flowOf | ||
import kotlinx.coroutines.flow.flowOn | ||
import kotlinx.coroutines.flow.update | ||
import one.njk.celestidesk.data.auth.AuthRepository | ||
import one.njk.celestidesk.data.auth.model.AuthLoginRequest | ||
import one.njk.celestidesk.data.auth.model.AuthResult | ||
import javax.inject.Inject | ||
|
||
class AuthViewModel: ViewModel() { | ||
@HiltViewModel | ||
class AuthViewModel @Inject constructor( | ||
private val api: AuthRepository | ||
) : ViewModel() { | ||
|
||
} | ||
private val uiState = MutableStateFlow(UiState()) | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
val state: LiveData<UiState> = uiState.flatMapLatest { uiState -> | ||
flowOf(uiState).flowOn(Dispatchers.Default) | ||
}.asLiveData() | ||
|
||
suspend fun logIn(username: String, password: String){ | ||
uiState.update { | ||
it.copy(isLoading = true) | ||
} | ||
val result = api.logIn( | ||
AuthLoginRequest(username, password) | ||
) | ||
uiState.update { | ||
it.copy(isLoading = false, authResult = result) | ||
} | ||
} | ||
} | ||
|
||
data class UiState( | ||
val isLoading: Boolean = false, | ||
val authResult: AuthResult<Unit> = AuthResult.UnknownError(), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters