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

Commit

Permalink
refactor + fixed serious bug
Browse files Browse the repository at this point in the history
Passwords, tags and folders lists won't be cleared after some time anymore (damn you Google).
  • Loading branch information
seldon1000 committed Jul 18, 2021
1 parent 38832f7 commit 31a50f9
Show file tree
Hide file tree
Showing 18 changed files with 99 additions and 103 deletions.
18 changes: 9 additions & 9 deletions app/src/main/java/eu/seldon1000/nextpass/CentralAppControl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import androidx.biometric.BiometricPrompt
import androidx.compose.material.SnackbarHostState
import androidx.compose.runtime.Composable
import androidx.navigation.NavController
import eu.seldon1000.nextpass.api.NextcloudApiProvider
import eu.seldon1000.nextpass.api.NextcloudApi
import eu.seldon1000.nextpass.services.NextPassAutofillService
import eu.seldon1000.nextpass.ui.layout.Routes
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -118,7 +118,7 @@ object CentralAppControl {

sharedPreferences = CentralAppControl.context!!.getSharedPreferences("nextpass", 0)

NextcloudApiProvider.initializeApi(
NextcloudApi.initializeApi(
res = CentralAppControl.context!!.resources,
pref = sharedPreferences!!
)
Expand Down Expand Up @@ -237,7 +237,7 @@ object CentralAppControl {

private fun startAutofillService(): Boolean {
return if (autofillManager!!.hasEnabledAutofillServices() &&
NextcloudApiProvider.isLogged() && setAutofillIntent(
NextcloudApi.isLogged() && setAutofillIntent(
intent = Intent(context, NextPassAutofillService::class.java)
)
) {
Expand Down Expand Up @@ -308,12 +308,12 @@ object CentralAppControl {

fun openApp(shouldRememberScreen: Boolean = false) {
if (unlocked) {
if (NextcloudApiProvider.isLogged()) {
if (NextcloudApi.isLogged()) {
startAutofillService()

if (!shouldRememberScreen) navigate(route = Routes.Passwords.route)

NextcloudApiProvider.refreshServerList()
NextcloudApi.refreshServerList()
}
} else navigate(route = Routes.AccessPin.getRoute(arg = true))
}
Expand Down Expand Up @@ -460,7 +460,7 @@ object CentralAppControl {
navControllerState.value?.popBackStack()

if (navControllerState.value?.currentDestination?.route!! != Routes.NewPassword.route)
NextcloudApiProvider.faviconRequest(data = "")
NextcloudApi.faviconRequest(data = "")

setKeyboardMode()

Expand Down Expand Up @@ -496,8 +496,8 @@ object CentralAppControl {
selectedFolderState.value = folder
} else {
currentFolderState.value =
NextcloudApiProvider.storedFolders.value.indexOfFirst {
it.id == NextcloudApiProvider.storedFolders.value[currentFolderState.value].parent
NextcloudApi.storedFolders.value.indexOfFirst {
it.id == NextcloudApi.storedFolders.value[currentFolderState.value].parent
}
selectedFolderState.value = currentFolderState.value
}
Expand All @@ -508,7 +508,7 @@ object CentralAppControl {
}

fun enableTags(refresh: Boolean = true) {
if (refresh) NextcloudApiProvider.refreshServerList(refreshTags = true)
if (refresh) NextcloudApi.refreshServerList(refreshTags = true)

sharedPreferences!!.edit().putBoolean("tags", true).apply()
tagsState.value = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ import android.net.Uri
import android.webkit.CookieManager
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.Text
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.unit.sp
import eu.seldon1000.nextpass.CentralAppControl
import eu.seldon1000.nextpass.R
Expand All @@ -50,7 +48,7 @@ import kotlinx.serialization.json.jsonPrimitive
import kotlinx.serialization.serializer

@SuppressLint("StaticFieldLeak")
object NextcloudApiProvider {
object NextcloudApi {
private val coroutineScope = CoroutineScope(context = Dispatchers.Unconfined)

private var resources: Resources? = null
Expand Down Expand Up @@ -82,13 +80,13 @@ object NextcloudApiProvider {
"\"updated\":0}"
)

private val storedPasswordsState = MutableStateFlow(value = mutableStateListOf<Password>())
private val storedPasswordsState = MutableStateFlow(value = mutableListOf<Password>())
val storedPasswords = storedPasswordsState

private val storedFoldersState = MutableStateFlow(value = mutableStateListOf(baseFolder))
private val storedFoldersState = MutableStateFlow(value = mutableListOf(baseFolder))
val storedFolders = storedFoldersState

private val storedTagsState = MutableStateFlow(value = mutableStateListOf<Tag>())
private val storedTagsState = MutableStateFlow(value = mutableListOf<Tag>())
val storedTags = storedTagsState

private val currentRequestedFaviconState = MutableStateFlow<Bitmap?>(value = null)
Expand Down Expand Up @@ -269,26 +267,24 @@ object NextcloudApiProvider {
}
}

private suspend inline fun <reified T> listRequest(): SnapshotStateList<T> {
private suspend inline fun <reified T> listRequest(): MutableList<T> {
return try {
json.decodeFromString(
deserializer = SnapshotListSerializer(dataSerializer = serializer()),
string = client.get(
urlString = "$server$endpoint/${
when (T::class) {
Password::class -> "password"
Folder::class -> "folder"
else -> "tag"
}
}/list"
) {
if (T::class == Password::class)
parameter(key = "details", value = "model+tags")
})
json.decodeFromString(string = client.get(
urlString = "$server$endpoint/${
when (T::class) {
Password::class -> "password"
Folder::class -> "folder"
else -> "tag"
}
}/list"
) {
if (T::class == Password::class)
parameter(key = "details", value = "model+tags")
})
} catch (e: Exception) {
showError()

mutableStateListOf()
mutableListOf()
}
}

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/eu/seldon1000/nextpass/api/Password.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ data class Password(

@Contextual
var customFieldsList = try {
NextcloudApiProvider.json.decodeFromString(
NextcloudApi.json.decodeFromString(
deserializer = SnapshotListSerializer(CustomField.serializer()),
string = customFields
)
Expand All @@ -79,7 +79,7 @@ data class Password(

fun resetCustomFields() {
customFieldsList = try {
NextcloudApiProvider.json.decodeFromString(
NextcloudApi.json.decodeFromString(
deserializer = SnapshotListSerializer(CustomField.serializer()),
string = customFields
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import android.view.autofill.AutofillId
import android.view.autofill.AutofillValue
import android.widget.RemoteViews
import eu.seldon1000.nextpass.R
import eu.seldon1000.nextpass.api.NextcloudApiProvider
import eu.seldon1000.nextpass.api.NextcloudApiProvider.toRoundedCorners
import eu.seldon1000.nextpass.api.NextcloudApi
import eu.seldon1000.nextpass.api.NextcloudApi.toRoundedCorners
import eu.seldon1000.nextpass.api.Password
import kotlinx.serialization.decodeFromString
import java.math.BigInteger
Expand All @@ -53,7 +53,7 @@ class NextPassAutofillService : AutofillService() {
override fun onCreate() {
super.onCreate()

NextcloudApiProvider.initializeApi(
NextcloudApi.initializeApi(
res = resources,
pref = getSharedPreferences("nextpass", 0)
)
Expand All @@ -62,10 +62,10 @@ class NextPassAutofillService : AutofillService() {
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
return if (NextcloudApiProvider.storedPasswords.value.isEmpty() &&
NextcloudApiProvider.isLogged()
return if (NextcloudApi.storedPasswords.value.isEmpty() &&
NextcloudApi.isLogged()
) {
NextcloudApiProvider.refreshServerList(refreshFolders = false, refreshTags = false)
NextcloudApi.refreshServerList(refreshFolders = false, refreshTags = false)

START_STICKY
} else {
Expand All @@ -78,9 +78,9 @@ class NextPassAutofillService : AutofillService() {
override fun onConnected() {
super.onConnected()

if (NextcloudApiProvider.storedPasswords.value.isEmpty() &&
NextcloudApiProvider.isLogged()
) NextcloudApiProvider.refreshServerList(refreshFolders = false, refreshTags = false)
if (NextcloudApi.storedPasswords.value.isEmpty() &&
NextcloudApi.isLogged()
) NextcloudApi.refreshServerList(refreshFolders = false, refreshTags = false)
}

override fun onFillRequest(
Expand Down Expand Up @@ -168,7 +168,7 @@ class NextPassAutofillService : AutofillService() {
)

if (viewWebDomain.isEmpty()) {
params["customFields"] = NextcloudApiProvider.json.decodeFromString(
params["customFields"] = NextcloudApi.json.decodeFromString(
string = listOf(
mapOf(
"label" to "Android app",
Expand All @@ -179,7 +179,7 @@ class NextPassAutofillService : AutofillService() {
)
}

NextcloudApiProvider.createPasswordRequest(params = params, tags = emptyList())
NextcloudApi.createPasswordRequest(params = params, tags = emptyList())

callback.onSuccess()
}
Expand All @@ -200,7 +200,7 @@ class NextPassAutofillService : AutofillService() {

if (!mode) {
if (usernameId.isNotEmpty() && passwordId.isNotEmpty() && !ready) {
NextcloudApiProvider.storedPasswords.value.forEach { password ->
NextcloudApi.storedPasswords.value.forEach { password ->
if (checkSuggestions(password = password)) {
val credentialsPresentation =
RemoteViews(packageName, R.layout.autofill_list_item)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import eu.seldon1000.nextpass.R
import eu.seldon1000.nextpass.api.NextcloudApiProvider
import eu.seldon1000.nextpass.api.NextcloudApi
import eu.seldon1000.nextpass.CentralAppControl
import eu.seldon1000.nextpass.ui.layout.Routes

Expand All @@ -41,7 +41,7 @@ import eu.seldon1000.nextpass.ui.layout.Routes
fun DropdownFolderList(enabled: Boolean = true, canAdd: Boolean = true, folder: Int) {
val context = LocalContext.current

val storedFolders by NextcloudApiProvider.storedFolders.collectAsState()
val storedFolders by NextcloudApi.storedFolders.collectAsState()

val selectedFolder by CentralAppControl.selectedFolder.collectAsState()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import eu.seldon1000.nextpass.R
import eu.seldon1000.nextpass.api.Folder
import eu.seldon1000.nextpass.api.NextcloudApiProvider
import eu.seldon1000.nextpass.api.NextcloudApi
import eu.seldon1000.nextpass.CentralAppControl
import eu.seldon1000.nextpass.ui.layout.Routes
import eu.seldon1000.nextpass.ui.theme.NextcloudBlue
Expand All @@ -43,7 +43,7 @@ import eu.seldon1000.nextpass.ui.theme.NextcloudBlue
fun FolderCard(index: Int, folder: Folder, icon: Painter? = null) {
val context = LocalContext.current

val storedFolders by NextcloudApiProvider.storedFolders.collectAsState()
val storedFolders by NextcloudApi.storedFolders.collectAsState()

val currentFolder by CentralAppControl.currentFolder.collectAsState()

Expand Down Expand Up @@ -90,7 +90,7 @@ fun FolderCard(index: Int, folder: Folder, icon: Painter? = null) {
)
if (it) params["favorite"] = "true"

NextcloudApiProvider.updateFolderRequest(params = params)
NextcloudApi.updateFolderRequest(params = params)
}
}
if (icon == null) {
Expand Down Expand Up @@ -163,7 +163,7 @@ fun FolderCard(index: Int, folder: Folder, icon: Painter? = null) {
},
confirm = true
) {
NextcloudApiProvider.deleteFolderRequest(id = folder.id)
NextcloudApi.deleteFolderRequest(id = folder.id)
CentralAppControl.showSnackbar(message = context.getString(R.string.folder_deleted_snack))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import eu.seldon1000.nextpass.ui.layout.Routes
fun PasswordCard(index: Int, password: Password) {
val context = LocalContext.current

val storedFolders by NextcloudApiProvider.storedFolders.collectAsState()
val storedFolders by NextcloudApi.storedFolders.collectAsState()

val currentScreen by CentralAppControl.navController.collectAsState().value!!.currentBackStackEntryAsState()
val folderMode by CentralAppControl.folderMode.collectAsState()
Expand Down Expand Up @@ -125,7 +125,7 @@ fun PasswordCard(index: Int, password: Password) {
"password" to password.password,
"url" to password.url,
"notes" to password.notes,
"customFields" to NextcloudApiProvider.json.encodeToString(
"customFields" to NextcloudApi.json.encodeToString(
serializer = SnapshotListSerializer(
dataSerializer = CustomField.serializer()
), value = password.customFieldsList
Expand All @@ -135,7 +135,7 @@ fun PasswordCard(index: Int, password: Password) {
)
if (it) params["favorite"] = "true"

NextcloudApiProvider.updatePasswordRequest(params = params, tags = password.tags)
NextcloudApi.updatePasswordRequest(params = params, tags = password.tags)
}
}
DropdownMenu(
Expand Down Expand Up @@ -220,7 +220,7 @@ fun PasswordCard(index: Int, password: Password) {
},
confirm = true
) {
NextcloudApiProvider.deletePasswordRequest(id = password.id)
NextcloudApi.deletePasswordRequest(id = password.id)
CentralAppControl.showSnackbar(message = context.getString(R.string.password_deleted))
}

Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/eu/seldon1000/nextpass/ui/items/TagsRow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import eu.seldon1000.nextpass.R
import eu.seldon1000.nextpass.api.NextcloudApiProvider
import eu.seldon1000.nextpass.api.NextcloudApi
import eu.seldon1000.nextpass.api.Tag
import eu.seldon1000.nextpass.CentralAppControl
import eu.seldon1000.nextpass.ui.layout.SimpleFlowRow
Expand All @@ -60,7 +60,7 @@ fun TagsRow(
) {
val context = LocalContext.current

val storedTags by NextcloudApiProvider.storedTags.collectAsState()
val storedTags by NextcloudApi.storedTags.collectAsState()

var newTagLabel by remember { mutableStateOf(value = "") }
var newTagColor by remember { mutableStateOf(value = pickerColors[0]) }
Expand Down Expand Up @@ -157,7 +157,7 @@ fun TagsRow(
}"
)

NextcloudApiProvider.updateTagRequest(params = params)
NextcloudApi.updateTagRequest(params = params)
CentralAppControl.showSnackbar(message = context.getString(R.string.tag_updated_snack))

tagClickAction(null)
Expand Down Expand Up @@ -186,7 +186,7 @@ fun TagsRow(
) {
CentralAppControl.setRefreshing(refreshing = true)

NextcloudApiProvider.deleteTagRequest(id = tag.id)
NextcloudApi.deleteTagRequest(id = tag.id)
CentralAppControl.showSnackbar(message = context.getString(R.string.tag_deleted_snack))

tagClickAction(null)
Expand Down Expand Up @@ -243,7 +243,7 @@ fun TagsRow(
)

CentralAppControl.setRefreshing(refreshing = true)
NextcloudApiProvider.createTagRequest(params = params)
NextcloudApi.createTagRequest(params = params)
CentralAppControl.showSnackbar(message = context.getString(R.string.tag_created_snack))

newTagLabel = ""
Expand Down
Loading

0 comments on commit 31a50f9

Please sign in to comment.