Skip to content

Commit

Permalink
Adding the "Delete item" functionality for processed items on the Sca…
Browse files Browse the repository at this point in the history
…nBarcodeScreen.

Upping versionCode to 70
  • Loading branch information
Dima-Android committed Jun 3, 2024
1 parent e588b78 commit 1325144
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,12 @@ internal class ScanBarcodeViewModel @Inject constructor(
title = _title ?: ""
}
val itemData =
LookupRowItem(type = translationData.response.rawType, title = title)
LookupRowItem(
identifier = lookup.identifier,
key = translationData.response.key,
type = translationData.response.rawType,
title = title
)

rowsList.add(LookupRow.item(itemData))

Expand Down Expand Up @@ -339,6 +344,40 @@ internal class ScanBarcodeViewModel @Inject constructor(
super.onCleared()
}

fun onItemDelete(lookupRow: LookupRow.item) {
val state = viewState.lookupState as State.lookup
val item = state.data.find {
val translatedItem = it.state as IdentifierLookupController.LookupData.State.translated
translatedItem.translatedLookupData.response.key == lookupRow.item.key
}!!
val translatedItem = item.state as IdentifierLookupController.LookupData.State.translated

identifierLookupController.trashItem(
identifier = lookupRow.item.identifier,
itemKey = translatedItem.translatedLookupData.response.key,
libraryId = translatedItem.translatedLookupData.libraryId
)

val itemIndex = viewState.lookupRows.indexOf(lookupRow)
val lookupRowsMutable = viewState.lookupRows.toMutableList()
lookupRowsMutable.remove(lookupRow)
if (lookupRowsMutable.isNotEmpty() && itemIndex < lookupRowsMutable.size && lookupRowsMutable[itemIndex] is LookupRow.attachment) {
lookupRowsMutable.removeAt(itemIndex)
}

val mutableStateData = state.data.toMutableList()
mutableStateData.remove(item)
val updatedLookupState = State.lookup(mutableStateData)

updateState {
copy(
lookupRows = lookupRowsMutable,
lookupState = updatedLookupState
)
}

}


sealed interface State {
data class failed(val error: Exception) : State
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ internal fun ScanBarcodeScreen(
}
}
} else {
scanBarcodeTable(rows = viewState.lookupRows)
if (viewState.lookupState == State.loadingIdentifiers ) {
scanBarcodeTable(
rows = viewState.lookupRows,
onDelete = { viewModel.onItemDelete(it) })
if (viewState.lookupState == State.loadingIdentifiers) {
scanBarcodeLoadingIndicator()
}
scanBarcodeError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,18 @@ import org.zotero.android.uicomponents.attachmentprogress.Style
import org.zotero.android.uicomponents.misc.NewDivider
import org.zotero.android.uicomponents.theme.CustomPalette
import org.zotero.android.uicomponents.theme.CustomTheme
import org.zotero.android.uicomponents.topbar.HeadingTextButton

internal fun LazyListScope.scanBarcodeTable(rows: List<LookupRow>) {
internal fun LazyListScope.scanBarcodeTable(rows: List<LookupRow>, onDelete: (row: LookupRow.item) -> Unit) {
rows.forEach { row ->
item {
when (row) {
is LookupRow.item -> {
val data = row.item
LookupItemRow(
title = data.title,
type = data.type
type = data.type,
onDelete = { onDelete(row) }
)
}

Expand Down Expand Up @@ -71,37 +73,50 @@ internal fun LazyListScope.scanBarcodeTable(rows: List<LookupRow>) {

@Composable
internal fun LookupItemRow(
title: String, type: String
title: String, type: String,
onDelete: () -> Unit,
) {
val iconSize = 28.dp

val modifier = Modifier.size(iconSize)
Row(
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 1.dp),
verticalAlignment = Alignment.CenterVertically
) {
Image(
modifier = modifier,
painter = painterResource(id = ItemTypes.iconName(type, null)),
contentDescription = null,
)
Column(
Box {
Row(
modifier = Modifier
.weight(1f)
.padding(start = 16.dp, top = 8.dp)
.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = HtmlCompat.fromHtml(
title,
HtmlCompat.FROM_HTML_MODE_LEGACY
).toString(),
color = CustomTheme.colors.primaryContent,
style = CustomTheme.typography.newBody,
Image(
modifier = modifier,
painter = painterResource(id = ItemTypes.iconName(type, null)),
contentDescription = null,
)
Column(
modifier = Modifier
.weight(1f)
.padding(start = 16.dp)
.padding(vertical = 4.dp)
) {
Text(
text = HtmlCompat.fromHtml(
title,
HtmlCompat.FROM_HTML_MODE_LEGACY
).toString(),
color = CustomTheme.colors.primaryContent,
style = CustomTheme.typography.newBody,
)
}

HeadingTextButton(
text = stringResource(id = Strings.delete),
contentColor = CustomPalette.ErrorRed,
onClick = onDelete
)
NewDivider(modifier = Modifier.padding(top = 8.dp))
}

NewDivider(
modifier = Modifier
.align(Alignment.BottomCenter)
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,12 @@ internal class AddByIdentifierViewModel @Inject constructor(
title = _title ?: ""
}
val itemData =
LookupRowItem(type = translationData.response.rawType, title = title)
LookupRowItem(
identifier = lookup.identifier,
key = translationData.response.key,
type = translationData.response.rawType,
title = title
)

rowsList.add(LookupRow.item(itemData))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,16 @@ class IdentifierLookupController @Inject constructor(
cleanupLookup(force = force, completion = completion)
}

fun trashItem(identifier: String, itemKey: String, libraryId: LibraryIdentifier) {
lookupData.remove(identifier)
val request = MarkItemsAsTrashedDbRequest(
keys = listOf(itemKey),
libraryId = libraryId,
trashed = true
)
dbWrapper.realmDbStorage.perform(request)
}

suspend fun lookUp(
libraryId: LibraryIdentifier,
collectionKeys: Set<String>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import org.zotero.android.database.objects.Attachment
import org.zotero.android.sync.LibraryIdentifier

data class LookupRowItem(
val identifier: String,
val key: String,
val type: String,
val title: String
)
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/BuildConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ object BuildConfig {
const val compileSdkVersion = 34
const val targetSdk = 33

val versionCode = 69 // Must be updated on every build
val versionCode = 70 // Must be updated on every build
val version = Version(
major = 1,
minor = 0,
Expand Down

0 comments on commit 1325144

Please sign in to comment.