use co pose annotation edit view in pdf reader

This commit is contained in:
Satindar Dhillon
2022-12-07 11:04:12 -08:00
parent 486a682ad3
commit fde9528996
3 changed files with 44 additions and 55 deletions

View File

@ -1,5 +1,6 @@
package app.omnivore.omnivore.ui.reader
import android.content.DialogInterface
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
@ -20,10 +21,24 @@ import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.compose.ui.unit.dp
import androidx.fragment.app.Fragment
import androidx.fragment.app.DialogFragment
import app.omnivore.omnivore.ui.theme.OmnivoreTheme
class AnnotationEditFragment : Fragment() {
class AnnotationEditFragment : DialogFragment() {
private var onSave: (String) -> Unit = {}
private var onCancel: () -> Unit = {}
private var initialAnnotation: String = ""
fun configure(
initialAnnotation: String,
onSave: (String) -> Unit,
onCancel: () -> Unit,
) {
this.initialAnnotation = initialAnnotation
this.onSave = onSave
this.onCancel = onCancel
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
@ -36,23 +51,29 @@ class AnnotationEditFragment : Fragment() {
setContent {
OmnivoreTheme {
AnnotationEditView(
initialAnnotation = "Initial Annotation",
onSave = {},
onCancel = {}
initialAnnotation,
onSave,
onCancel,
dismissAction = { dismiss() }
)
}
}
}
}
override fun onDismiss(dialog: DialogInterface) {
onCancel()
super.onDismiss(dialog)
}
}
// TODO: better layout and styling for this view
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AnnotationEditView(
initialAnnotation: String,
onSave: (String) -> Unit,
onCancel: () -> Unit,
dismissAction: () -> Unit = {}
) {
val annotation = remember { mutableStateOf(initialAnnotation) }
val focusRequester = FocusRequester()
@ -71,6 +92,7 @@ fun AnnotationEditView(
TextButton(
onClick = {
onCancel()
dismissAction()
}
) {
Text("Cancel")
@ -85,6 +107,7 @@ fun AnnotationEditView(
TextButton(
onClick = {
onSave(annotation.value)
dismissAction()
}
) {
Text("Save")

View File

@ -86,6 +86,7 @@ class PDFReaderActivity: AppCompatActivity(), DocumentListener, TextSelectionMan
}
override fun onDestroy() {
actionMode?.finish()
resetHighlightTap()
// TODO: remove listeners?
super.onDestroy()
@ -442,30 +443,19 @@ class PDFReaderActivity: AppCompatActivity(), DocumentListener, TextSelectionMan
}
private fun showAnnotationView(initialText: String) {
val annotationDialog = Dialog(this)
annotationDialog.setContentView(R.layout.annotation_edit)
val textField = annotationDialog.findViewById(R.id.highlightNoteTextField) as EditText
textField.setText(initialText)
val confirmButton = annotationDialog.findViewById(R.id.confirmAnnotation) as Button
confirmButton.setOnClickListener {
val newNoteText = textField.text
clickedHighlight?.let { highlight ->
viewModel.updateHighlightNote(highlight, newNoteText.toString())
}
resetHighlightTap()
annotationDialog.dismiss()
}
val cancelBtn = annotationDialog.findViewById(R.id.cancel) as Button
cancelBtn.setOnClickListener {
resetHighlightTap()
annotationDialog.dismiss()
}
annotationDialog.show()
val annotationEditFragment = AnnotationEditFragment()
annotationEditFragment.configure(
onSave = { newNote ->
clickedHighlight?.let { highlight ->
viewModel.updateHighlightNote(highlight, newNote)
}
resetHighlightTap()
},
onCancel = {
resetHighlightTap()
},
initialAnnotation = initialText
)
annotationEditFragment.show(fragment.childFragmentManager, null)
}
}

View File

@ -1,24 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="#ffffff">
<EditText
android:layout_width="fill_parent"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:hint="Add note."
android:id="@+id/highlightNoteTextField"
android:textColor="#000000"
android:lines="3" />
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/confirmAnnotation" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:text="Confirm" android:layout_weight="1" />
<Button android:id="@+id/cancel" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_weight="1"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>