diff --git a/app/src/main/java/com/orgzly/android/git/GitFileSynchronizer.java b/app/src/main/java/com/orgzly/android/git/GitFileSynchronizer.java index de80163ae..90309ce0a 100644 --- a/app/src/main/java/com/orgzly/android/git/GitFileSynchronizer.java +++ b/app/src/main/java/com/orgzly/android/git/GitFileSynchronizer.java @@ -11,6 +11,8 @@ import com.orgzly.android.util.MiscUtils; import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.diff.DiffEntry; +import org.eclipse.jgit.diff.DiffFormatter; import org.eclipse.jgit.api.MergeResult; import org.eclipse.jgit.api.ResetCommand; import org.eclipse.jgit.api.Status; @@ -22,12 +24,16 @@ import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.treewalk.TreeWalk; +import org.eclipse.jgit.lib.ObjectReader; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.treewalk.CanonicalTreeParser; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.TimeZone; +import java.util.List; public class GitFileSynchronizer { private static String TAG = GitFileSynchronizer.class.getSimpleName(); @@ -330,4 +336,27 @@ public boolean fileMatchesInRevisions(String pathString, RevCommit start, RevCom throws IOException { return getFileRevision(pathString, start).equals(getFileRevision(pathString, end)); } + + public void getDiff(ObjectId fileRevision, RevCommit revision) { + try { + ObjectId oldHead = currentHead().toObjectId(); // aka local + ObjectId head = revision.toObjectId(); // aka remote + Repository repository = git.getRepository(); + try (ObjectReader reader = repository.newObjectReader()) { + CanonicalTreeParser oldTreeIter = new CanonicalTreeParser(); + oldTreeIter.reset(reader, oldHead); + CanonicalTreeParser newTreeIter = new CanonicalTreeParser(); + newTreeIter.reset(reader, head); + + List diffs = git.diff().setNewTree(newTreeIter).setOldTree(oldTreeIter).call(); + + for (DiffEntry entry : diffs) { + Log.e(TAG, "Entry: " + entry); + } + } + + } catch (Exception e) { + e.printStackTrace(); + } + } } diff --git a/app/src/main/java/com/orgzly/android/repos/TwoWaySyncRepo.kt b/app/src/main/java/com/orgzly/android/repos/TwoWaySyncRepo.kt index 75ae239a2..fdac1aac4 100644 --- a/app/src/main/java/com/orgzly/android/repos/TwoWaySyncRepo.kt +++ b/app/src/main/java/com/orgzly/android/repos/TwoWaySyncRepo.kt @@ -7,4 +7,5 @@ import java.io.IOException interface TwoWaySyncRepo { @Throws(IOException::class) fun syncBook(uri: Uri, current: VersionedRook, fromDB: File): TwoWaySyncResult +// fun getDiff(uri: Uri, current: VersionedRook, fromDB: File): TwoWaySyncResult } \ No newline at end of file diff --git a/app/src/main/java/com/orgzly/android/ui/books/BooksFragment.kt b/app/src/main/java/com/orgzly/android/ui/books/BooksFragment.kt index c7ed36d34..ce22d5029 100644 --- a/app/src/main/java/com/orgzly/android/ui/books/BooksFragment.kt +++ b/app/src/main/java/com/orgzly/android/ui/books/BooksFragment.kt @@ -36,6 +36,7 @@ import com.orgzly.android.usecase.BookDelete import com.orgzly.android.util.LogUtils import com.orgzly.android.util.MiscUtils import com.orgzly.databinding.DialogBookDeleteBinding +import com.orgzly.databinding.DialogBookDiffBinding import com.orgzly.databinding.DialogBookRenameBinding import com.orgzly.databinding.FragmentBooksBinding import javax.inject.Inject @@ -197,6 +198,10 @@ class BooksFragment : Fragment(), Fab, DrawerItem, OnViewHolderClickListener { + viewModel.diffBookRequest(bookId) + } + else -> { } } @@ -282,6 +287,28 @@ class BooksFragment : Fragment(), Fab, DrawerItem, OnViewHolderClickListener + if (bookView != null) { + showBookDiffDialog(bookView) + } + }) + + viewModel.bookExportRequestEvent.observeSingle(viewLifecycleOwner, Observer { (book, format) -> listener?.onBookExportRequest(book, format) }) diff --git a/app/src/main/java/com/orgzly/android/ui/books/BooksViewModel.kt b/app/src/main/java/com/orgzly/android/ui/books/BooksViewModel.kt index bed5609af..fa3fa78c3 100644 --- a/app/src/main/java/com/orgzly/android/ui/books/BooksViewModel.kt +++ b/app/src/main/java/com/orgzly/android/ui/books/BooksViewModel.kt @@ -26,6 +26,8 @@ class BooksViewModel(private val dataRepository: DataRepository) : CommonViewMod val bookRenameRequestEvent: SingleLiveEvent = SingleLiveEvent() + val bookDiffRequestEvent: SingleLiveEvent = SingleLiveEvent() + val bookExportRequestEvent: SingleLiveEvent> = SingleLiveEvent() enum class ViewState { @@ -77,6 +79,12 @@ class BooksViewModel(private val dataRepository: DataRepository) : CommonViewMod } } + fun diffBookRequest(bookId: Long) { + App.EXECUTORS.diskIO().execute { + bookDiffRequestEvent.postValue(dataRepository.getBookView(bookId)) + } + } + fun renameBook(book: BookView, name: String) { App.EXECUTORS.diskIO().execute { catchAndPostError { diff --git a/app/src/main/res/layout/dialog_book_diff.xml b/app/src/main/res/layout/dialog_book_diff.xml new file mode 100644 index 000000000..430b048df --- /dev/null +++ b/app/src/main/res/layout/dialog_book_diff.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/books_context.xml b/app/src/main/res/menu/books_context.xml index 1b9f5822f..0ca8bb2b8 100644 --- a/app/src/main/res/menu/books_context.xml +++ b/app/src/main/res/menu/books_context.xml @@ -40,4 +40,10 @@ app:showAsAction="never"> + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 39054de77..f9b79d4c1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -22,6 +22,7 @@ Edit Edit note Delete + Show Diff Delete ā€œ%sā€ Name Value