Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Commit

Permalink
feat(status/edit-history): display diff for text
Browse files Browse the repository at this point in the history
Closes #789
  • Loading branch information
FineFindus committed Nov 5, 2023
1 parent 5e92562 commit b74bd66
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
import org.joinmastodon.android.ui.utils.UiUtils;

import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import me.grishka.appkit.api.SimpleCallback;
import name.fraser.neil.plaintext.diff_match_patch;

public class StatusEditHistoryFragment extends StatusListFragment{
private String id, url;
Expand Down Expand Up @@ -58,7 +61,7 @@ public void onSuccess(List<Status> result){

@Override
protected List<StatusDisplayItem> buildDisplayItems(Status s){
List<StatusDisplayItem> items=StatusDisplayItem.buildItems(this, s, accountID, s, knownAccounts, null, StatusDisplayItem.FLAG_NO_FOOTER | StatusDisplayItem.FLAG_INSET | StatusDisplayItem.FLAG_NO_EMOJI_REACTIONS);
List<StatusDisplayItem> items=new ArrayList<>();
int idx=data.indexOf(s);
if(idx>=0){
String date=UiUtils.DATE_TIME_FORMATTER.format(s.createdAt.atZone(ZoneId.systemDefault()));
Expand All @@ -85,6 +88,8 @@ enum StatusEditChangeType{

if(!Objects.equals(s.content, prev.content)){
changes.add(StatusEditChangeType.TEXT_CHANGED);
//update status content to display a diffs
s.content=createDiffText(prev.content, s.content);
}
if(!Objects.equals(s.spoilerText, prev.spoilerText)){
if(s.spoilerText==null){
Expand Down Expand Up @@ -147,6 +152,7 @@ enum StatusEditChangeType{
items.add(0, new ReblogOrReplyLineStatusDisplayItem(s.id, this, action+" "+sep+" "+date, Collections.emptyList(), 0, null, null, s));
items.add(1, new DummyStatusDisplayItem(s.id, this));
}
items.addAll(StatusDisplayItem.buildItems(this, s, accountID, s, knownAccounts, null, StatusDisplayItem.FLAG_NO_FOOTER|StatusDisplayItem.FLAG_INSET|StatusDisplayItem.FLAG_NO_EMOJI_REACTIONS));
return items;
}

Expand All @@ -170,4 +176,28 @@ protected FilterContext getFilterContext() {
public Uri getWebUri(Uri.Builder base) {
return Uri.parse(url);
}

private String createDiffText(String original, String modified) {
diff_match_patch dmp = new diff_match_patch();
LinkedList<diff_match_patch.Diff> diffs = dmp.diff_main(original, modified);
dmp.diff_cleanupSemantic(diffs);

StringBuilder stringBuilder = new StringBuilder();
for(diff_match_patch.Diff diff: diffs){
switch(diff.operation){
case DELETE -> {
stringBuilder.append("<edit_diff_removed>");
stringBuilder.append(diff.text);
stringBuilder.append("</edit_diff_removed>");
}
case INSERT -> {
stringBuilder.append("<edit_diff_added>");
stringBuilder.append(diff.text);
stringBuilder.append("</edit_diff_added>");
}
default -> stringBuilder.append(diff.text);
}
}
return stringBuilder.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.joinmastodon.android.ui.text;

import android.text.TextPaint;
import android.text.style.CharacterStyle;

import org.joinmastodon.android.ui.utils.UiUtils;

public class DiffRemovedSpan extends CharacterStyle {

private final String text;

public DiffRemovedSpan(String text){
this.text=text;
}


@Override
public void updateDrawState(TextPaint tp) {
tp.setStrikeThruText(true);
tp.setColor(0xFFCA5B63);
}

public String getText() {
return text;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.joinmastodon.android.ui.text;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
Expand Down Expand Up @@ -171,6 +172,9 @@ public void head(@NonNull Node node, int depth){
}
case "code", "pre" -> openSpans.add(new SpanInfo(new TypefaceSpan("monospace"), ssb.length(), el));
case "blockquote" -> openSpans.add(new SpanInfo(new LeadingMarginSpan.Standard(V.dp(10)), ssb.length(), el));
//fake elements for the edit history diff view
case "edit_diff_added" -> openSpans.add(new SpanInfo(new ForegroundColorSpan(UiUtils.isDarkTheme() ? 0xFF89bb9c : 0xFF5b8e63), ssb.length(), el));
case "edit_diff_removed" -> openSpans.add(new SpanInfo(new DiffRemovedSpan(el.text()), ssb.length(), el));
}
}
}
Expand Down

0 comments on commit b74bd66

Please sign in to comment.