Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add first draft of "Show diff"(Help me pls!) #657

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>