Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
Add dagger injection into LoginViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
M3DZIK committed Jul 15, 2024
1 parent 24f2306 commit 041bb68
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package dev.medzik.librepass.android.ui.auth

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
Expand All @@ -16,7 +14,6 @@ import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
Expand All @@ -29,17 +26,20 @@ import androidx.navigation.compose.rememberNavController
import dev.medzik.android.compose.icons.TopAppBarBackIcon
import dev.medzik.android.compose.ui.LoadingButton
import dev.medzik.android.compose.ui.textfield.TextFieldValue
import dev.medzik.librepass.android.business.injection.VaultCacheModule
import dev.medzik.librepass.android.database.Repository
import dev.medzik.librepass.android.ui.R
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.serialization.Serializable

@Serializable
object Login

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun LoginScreen(navController: NavController) {
fun LoginScreen(
navController: NavController,
viewModel: LoginViewModel = hiltViewModel()
) {
Scaffold(
topBar = {
TopAppBar(
Expand All @@ -52,80 +52,69 @@ fun LoginScreen(navController: NavController) {
)
}
) { innerPadding ->
LoginScreenContent(navController, innerPadding)
}
}

@Composable
fun LoginScreenContent(
navController: NavController,
innerPadding: PaddingValues,
viewModel: LoginViewModel = hiltViewModel()
) {
val context = LocalContext.current

LazyColumn(
modifier = Modifier
.padding(innerPadding)
.fillMaxSize(),
verticalArrangement = Arrangement.SpaceBetween
) {
item {
Column(
modifier = Modifier.padding(horizontal = 12.dp),
verticalArrangement = Arrangement.spacedBy(12.dp)
) {
EmailTextField(
TextFieldValue.fromMutableState(
state = viewModel.email
LazyColumn(
modifier = Modifier
.padding(innerPadding)
.fillMaxSize(),
verticalArrangement = Arrangement.SpaceBetween
) {
item {
Column(
modifier = Modifier.padding(horizontal = 12.dp),
verticalArrangement = Arrangement.spacedBy(12.dp)
) {
EmailTextField(
TextFieldValue.fromMutableState(
state = viewModel.email
)
)
)
PasswordTextField(
TextFieldValue.fromMutableState(
state = viewModel.password
PasswordTextField(
TextFieldValue.fromMutableState(
state = viewModel.password
)
)
)

ChoiceServer(viewModel.server)
ChoiceServer(viewModel.server)
}
}
}

item {
Column(
modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally
) {
LoadingButton(
modifier = Modifier
.padding(vertical = 12.dp)
.height(50.dp)
.fillMaxWidth(0.85f),
onClick = { viewModel.login(context) },
enabled = viewModel.email.value.isNotEmpty() && viewModel.email.value.isNotEmpty()
item {
Column(
modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = stringResource(R.string.Login),
style = MaterialTheme.typography.titleMedium
)
}
LoadingButton(
modifier = Modifier
.padding(vertical = 12.dp)
.height(50.dp)
.fillMaxWidth(0.85f),
onClick = { viewModel.login() },
enabled = viewModel.email.value.isNotEmpty() && viewModel.email.value.isNotEmpty()
) {
Text(
text = stringResource(R.string.Login),
style = MaterialTheme.typography.titleMedium
)
}

OutlinedButton(
modifier = Modifier
.padding(vertical = 12.dp)
.height(45.dp)
.fillMaxWidth(0.75f),
onClick = {
navController.navigate(
ForgotPassword(
server = viewModel.server.value
OutlinedButton(
modifier = Modifier
.padding(vertical = 12.dp)
.height(45.dp)
.fillMaxWidth(0.75f),
onClick = {
navController.navigate(
ForgotPassword(
server = viewModel.server.value
)
)
}
) {
Text(
text = stringResource(R.string.ForgotPassword),
style = MaterialTheme.typography.titleSmall
)
}
) {
Text(
text = stringResource(R.string.ForgotPassword),
style = MaterialTheme.typography.titleSmall
)
}
}
}
Expand All @@ -135,7 +124,17 @@ fun LoginScreenContent(
@Preview
@Composable
fun LoginScreenPreview() {
val context = LocalContext.current
val repository = Repository(LocalContext.current)

MaterialTheme {
LoginScreen(rememberNavController())
LoginScreen(
navController = rememberNavController(),
viewModel = LoginViewModel(
context = context,
repository = repository,
vaultCache = VaultCacheModule.provideVaultCache(repository)
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,32 @@ package dev.medzik.librepass.android.ui.auth
import android.content.Context
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import dev.medzik.android.utils.showToast
import dev.medzik.librepass.android.business.injection.VaultCacheModule
import dev.medzik.librepass.android.business.VaultCache
import dev.medzik.librepass.android.common.haveNetworkConnection
import dev.medzik.librepass.android.database.Credentials
import dev.medzik.librepass.android.database.injection.DatabaseProvider
import dev.medzik.librepass.android.database.Repository
import dev.medzik.librepass.android.ui.R
import dev.medzik.librepass.client.Server
import dev.medzik.librepass.client.api.AuthClient
import dev.medzik.librepass.utils.fromHex

class LoginViewModel : ViewModel() {
import javax.inject.Inject

@HiltViewModel
class LoginViewModel @Inject constructor(
@ApplicationContext private val context: Context,
private val repository: Repository,
private val vaultCache: VaultCache
) : ViewModel() {
var email = mutableStateOf("")
var password = mutableStateOf("")
var server = mutableStateOf(Server.PRODUCTION)

private val authClient = AuthClient(apiUrl = server.value)

suspend fun login(context: Context) {
suspend fun login() {
if (!context.haveNetworkConnection()) {
context.showToast(R.string.NoInternetConnection)
return
Expand All @@ -33,9 +41,6 @@ class LoginViewModel : ViewModel() {
password = password.value
)

val repository = DatabaseProvider.provideRepository(context)
val vaultCache = VaultCacheModule.provideVaultCache(repository)

val credentialsDbEntry = Credentials(
userId = credentials.userId,
email = email.value,
Expand Down

0 comments on commit 041bb68

Please sign in to comment.