Skip to content

Commit

Permalink
Add first draft of "Show diff"
Browse files Browse the repository at this point in the history
  • Loading branch information
Asalle committed Jan 17, 2020
1 parent eb0cbec commit a81d93b
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 0 deletions.
29 changes: 29 additions & 0 deletions app/src/main/java/com/orgzly/android/git/GitFileSynchronizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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<DiffEntry> diffs = git.diff().setNewTree(newTreeIter).setOldTree(oldTreeIter).call();

for (DiffEntry entry : diffs) {
Log.e(TAG, "Entry: " + entry);
}
}

} catch (Exception e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
34 changes: 34 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 @@ -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
Expand Down Expand Up @@ -197,6 +198,10 @@ class BooksFragment : Fragment(), Fab, DrawerItem, OnViewHolderClickListener<Boo
viewModel.deleteBookRequest(bookId)
}

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

else -> {
}
}
Expand Down Expand Up @@ -282,6 +287,28 @@ class BooksFragment : Fragment(), Fab, DrawerItem, OnViewHolderClickListener<Boo
dialog = builder.show()
}

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

dialogBinding.book.setText("Local book")
dialogBinding.rook.setText("Remote book")


val dialogBuilder = AlertDialog.Builder(context)
.setTitle(getString(R.string.rename_book, MiscUtils.quotedString(book.book.name)))
.setView(dialogBinding.root)

val d = dialogBuilder.create()

d.setOnDismissListener { ActivityUtils.closeSoftKeyboard(activity) }

d.show()

d.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled = false

dialog = d
}

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

Expand Down Expand Up @@ -383,6 +410,13 @@ class BooksFragment : Fragment(), Fab, DrawerItem, OnViewHolderClickListener<Boo
}
})

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


viewModel.bookExportRequestEvent.observeSingle(viewLifecycleOwner, Observer { (book, format) ->
listener?.onBookExportRequest(book, format)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class BooksViewModel(private val dataRepository: DataRepository) : CommonViewMod

val bookRenameRequestEvent: SingleLiveEvent<BookView> = SingleLiveEvent()

val bookDiffRequestEvent: SingleLiveEvent<BookView> = SingleLiveEvent()

val bookExportRequestEvent: SingleLiveEvent<Pair<Book, BookFormat>> = SingleLiveEvent()

enum class ViewState {
Expand Down Expand Up @@ -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 {
Expand Down
28 changes: 28 additions & 0 deletions app/src/main/res/layout/dialog_book_diff.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>

<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/screen_edge"
android:orientation="horizontal">

<TextView
android:id="@+id/book"
android:layout_width="wrap_content"
android:layout_height="100dp"
app:autoSizeTextType="uniform" />

<TextView
android:id="@+id/rook"
android:layout_width="wrap_content"
android:layout_height="100dp"
app:autoSizeTextType="uniform" />

</LinearLayout>

</layout>
6 changes: 6 additions & 0 deletions app/src/main/res/menu/books_context.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="@string/show_diff"
app:showAsAction="never">
</item>

</menu>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<string name="edit">Edit</string>
<string name="edit_note">Edit note</string>
<string name="delete">Delete</string>
<string name="show_diff">Show Diff</string>
<string name="delete_with_quoted_argument">Delete “%s”</string>
<string name="name">Name</string>
<string name="value">Value</string>
Expand Down

0 comments on commit a81d93b

Please sign in to comment.