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

Commit

Permalink
Move VaultCache to common module
Browse files Browse the repository at this point in the history
  • Loading branch information
M3DZIK committed Jun 8, 2024
1 parent 0e0fb69 commit 2fd826c
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import androidx.fragment.app.FragmentActivity
import androidx.navigation.NavController
import dagger.hilt.android.AndroidEntryPoint
import dev.medzik.android.utils.openEmailApplication
import dev.medzik.librepass.android.common.VaultCache
import dev.medzik.librepass.android.common.popUpToDestination
import dev.medzik.librepass.android.database.Repository
import dev.medzik.librepass.android.ui.LibrePassNavigation
import dev.medzik.librepass.android.ui.screens.auth.Unlock
import dev.medzik.librepass.android.ui.theme.LibrePassTheme
import dev.medzik.librepass.android.utils.Vault
import org.apache.commons.lang3.exception.ExceptionUtils
import javax.inject.Inject

Expand All @@ -24,7 +24,7 @@ class MainActivity : FragmentActivity() {
lateinit var repository: Repository

@Inject
lateinit var vault: Vault
lateinit var vault: VaultCache

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -46,8 +46,8 @@ class MainActivity : FragmentActivity() {

MigrationsManager.run(this, repository)

// retrieves aes key to decrypt vault if key is valid
vault.getVaultSecrets(this)
// retrieves aes key for vault decryption if key is valid
vault.getSecretsIfNotExpired(this)

setContent {
LibrePassTheme(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package dev.medzik.librepass.android.ui

import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import dev.medzik.librepass.android.common.VaultCache
import dev.medzik.librepass.android.database.CredentialsDao
import dev.medzik.librepass.android.database.LocalCipherDao
import dev.medzik.librepass.android.utils.Vault
import javax.inject.Inject

@HiltViewModel
class LibrePassViewModel @Inject constructor(
val cipherRepository: LocalCipherDao,
val credentialRepository: CredentialsDao,
val vault: Vault
val vault: VaultCache
) : ViewModel()
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.serialization.Serializable
import java.util.Date
import java.util.UUID

@Serializable
data class CipherEdit(val cipherId: String)
Expand All @@ -51,7 +52,7 @@ fun CipherEditScreen(
) {
val context = LocalContext.current

val oldCipher = remember { viewModel.vault.find(args.cipherId) } ?: return
val oldCipher = remember { viewModel.vault.find(UUID.fromString(args.cipherId)) } ?: return
var cipher by rememberMutable(oldCipher)

var loading by rememberMutableBoolean()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import kotlinx.coroutines.delay
import kotlinx.serialization.Serializable
import java.text.SimpleDateFormat
import java.util.Locale
import java.util.UUID
import java.util.concurrent.TimeUnit

@Serializable
Expand All @@ -81,7 +82,7 @@ fun CipherViewScreen(
args: CipherView,
viewModel: LibrePassViewModel = hiltViewModel()
) {
val cipher = remember { viewModel.vault.find(args.cipherId) } ?: return
val cipher = remember { viewModel.vault.find(UUID.fromString(args.cipherId)) } ?: return

var totpCode by rememberMutable("")
var totpElapsed by rememberMutable(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
Expand All @@ -36,6 +37,7 @@ import dev.medzik.otp.OTPParameters
import dev.medzik.otp.OTPType
import dev.medzik.otp.TOTPGenerator
import kotlinx.serialization.Serializable
import java.util.UUID

@Serializable
data class OtpConfigure(val cipherId: String)
Expand All @@ -46,7 +48,7 @@ fun OtpConfigureScreen(
args: OtpConfigure,
viewModel: LibrePassViewModel = hiltViewModel()
) {
val cipher = viewModel.vault.find(args.cipherId)
val cipher = remember { viewModel.vault.find(UUID.fromString(args.cipherId)) } ?: return

Scaffold(
topBar = {
Expand Down Expand Up @@ -140,7 +142,7 @@ fun OtpConfigureScreen(
}
} else {
var beginParams: OTPParameters? = null
if (cipher?.loginData?.twoFactor != null) {
if (cipher.loginData?.twoFactor != null) {
beginParams = OTPParameters.parseUrl(cipher.loginData?.twoFactor)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.*
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.Scaffold
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
Expand All @@ -32,7 +38,7 @@ fun SearchScreen(
navController: NavController,
viewModel: LibrePassViewModel = hiltViewModel()
) {
val ciphers = viewModel.vault.sortAlphabetically()
val ciphers = remember { viewModel.vault.getSortedCiphers() }

var searchText by rememberMutableString()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,22 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.*
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.material3.pulltorefresh.PullToRefreshContainer
import androidx.compose.material3.pulltorefresh.rememberPullToRefreshState
import androidx.compose.runtime.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
Expand Down Expand Up @@ -42,7 +54,7 @@ import dev.medzik.librepass.client.api.CipherClient
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.serialization.Serializable
import java.util.*
import java.util.Date
import java.util.concurrent.TimeUnit

@Serializable
Expand All @@ -55,14 +67,12 @@ fun VaultScreen(
viewModel: LibrePassViewModel = hiltViewModel()
) {
val context = LocalContext.current

val scope = rememberCoroutineScope()

val pullToRefreshState = rememberPullToRefreshState()

var ciphers by remember { mutableStateOf(viewModel.vault.sortAlphabetically()) }

val credentials = viewModel.credentialRepository.get() ?: return
var ciphers by remember { mutableStateOf(viewModel.vault.getSortedCiphers()) }
val credentials = remember { viewModel.credentialRepository.get() } ?: return

val cipherClient = CipherClient(
apiKey = credentials.apiKey,
Expand Down Expand Up @@ -101,7 +111,7 @@ fun VaultScreen(
}

// sort ciphers and update UI
ciphers = viewModel.vault.sortAlphabetically()
ciphers = viewModel.vault.getSortedCiphers()
}

fun reSetupBiometrics() {
Expand Down Expand Up @@ -163,11 +173,11 @@ fun VaultScreen(

// decrypt database if needed
if (viewModel.vault.ciphers.isEmpty()) {
viewModel.vault.decryptDatabase(dbCiphers)
viewModel.vault.decrypt(dbCiphers)
}

// sort ciphers and update UI
ciphers = viewModel.vault.sortAlphabetically()
ciphers = viewModel.vault.getSortedCiphers()

// sync remote ciphers
if (context.haveNetworkConnection()) {
Expand Down
12 changes: 10 additions & 2 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
alias(libs.plugins.android.library)
alias(libs.plugins.compose.compiler)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlin.ksp)
alias(libs.plugins.kotlin.parcelize)
}

Expand All @@ -23,7 +23,6 @@ android {
}

buildFeatures {
compose = true
buildConfig = false
}
}
Expand All @@ -33,5 +32,14 @@ dependencies {
implementation(libs.compose.material3)
implementation(libs.compose.navigation)
implementation(libs.compose.ui)
implementation(libs.medzik.android.crypto)
implementation(libs.medzik.android.utils)

implementation(libs.librepass.client)

implementation(libs.dagger.hilt)
implementation(libs.hilt.navigation.compose)
ksp(libs.dagger.hilt.compiler)

implementation(projects.databaseLogic)
}
Loading

0 comments on commit 2fd826c

Please sign in to comment.