Skip to content

Commit

Permalink
add a pop up window for the notebook menu
Browse files Browse the repository at this point in the history
  • Loading branch information
Asalle committed Oct 21, 2022
1 parent 55197ae commit 624c27f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
39 changes: 39 additions & 0 deletions app/src/main/java/com/orgzly/android/ui/books/BooksFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ class BooksFragment : Fragment(), DrawerItem, OnViewHolderClickListener<BookView
R.id.books_context_menu_delete -> {
viewModel.deleteBookRequest(bookId)
}

R.id.books_context_menu_show_diff -> {
viewModel.showDiffBookRequest(bookId)
}
}

viewModel.appBar.toMode(APP_BAR_DEFAULT_MODE)
Expand Down Expand Up @@ -305,6 +309,35 @@ class BooksFragment : Fragment(), DrawerItem, OnViewHolderClickListener<BookView
pickFileForBookExport.launch(defaultFileName)
}

private fun showDiffDialog(book: BookView) {
val dialogBinding = DialogBookDeleteBinding.inflate(LayoutInflater.from(context))

dialogBinding.deleteLinkedCheckbox.setOnCheckedChangeListener { _, isChecked ->
dialogBinding.deleteLinkedUrl.isEnabled = isChecked
}

val dialogClickListener = DialogInterface.OnClickListener { _, which ->
when (which) {
DialogInterface.BUTTON_POSITIVE -> {
val deleteLinked = dialogBinding.deleteLinkedCheckbox.isChecked
viewModel.deleteBook(book.book.id, deleteLinked)
}
}
}

val builder = MaterialAlertDialogBuilder(requireContext())
.setTitle(getString(R.string.delete_with_quoted_argument, book.book.name))
.setPositiveButton(R.string.delete, dialogClickListener)
.setNegativeButton(R.string.cancel, dialogClickListener)

if (book.syncedTo != null) {
dialogBinding.deleteLinkedUrl.text = book.syncedTo.uri.toString()
builder.setView(dialogBinding.root)
}

dialog = builder.show()
}

private fun deleteBookDialog(book: BookView) {
val dialogBinding = DialogBookDeleteBinding.inflate(LayoutInflater.from(context))

Expand Down Expand Up @@ -427,6 +460,12 @@ class BooksFragment : Fragment(), DrawerItem, OnViewHolderClickListener<BookView
}
})

viewModel.showDiffEvent.observeSingle(viewLifecycleOwner, Observer { bookView ->
if (bookView != null) {
showDiffDialog(bookView)
}
})

viewModel.bookToRenameEvent.observeSingle(viewLifecycleOwner, Observer { bookView ->
if (bookView != null) {
renameBookDialog(bookView)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class BooksViewModel(private val dataRepository: DataRepository) : CommonViewMod
val bookToExportEvent: SingleLiveEvent<Pair<Book, BookFormat>> = SingleLiveEvent()
val bookExportedEvent: SingleLiveEvent<String> = SingleLiveEvent()
val setBookLinkRequestEvent: SingleLiveEvent<BookLinkOptions> = SingleLiveEvent()

val showDiffEvent: SingleLiveEvent<BookView> = SingleLiveEvent()

enum class ViewState {
LOADING,
Expand Down Expand Up @@ -70,6 +70,12 @@ class BooksViewModel(private val dataRepository: DataRepository) : CommonViewMod
}
}

fun showDiffBookRequest(bookId: Long) {
App.EXECUTORS.diskIO().execute {
showDiffEvent.postValue(dataRepository.getBookView(bookId))
}
}

fun deleteBook(bookId: Long, deleteLinked: Boolean) {
App.EXECUTORS.diskIO().execute {
catchAndPostError {
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/menu/books_cab.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,10 @@
app:showAsAction="never">
</item>

<item
android:id="@+id/books_context_menu_show_diff"
android:title="Show diff"
app:showAsAction="never">
</item>

</menu>

0 comments on commit 624c27f

Please sign in to comment.