Commit e73639c8 authored by Stefan Niedermann's avatar Stefan Niedermann
Browse files

Fix #1046 Remember scroll position in preview mode

parent 71542a36
......@@ -135,26 +135,22 @@ public abstract class BaseNoteFragment extends BrandedFragment implements Catego
@Nullable
protected abstract ScrollView getScrollView();
protected abstract void scrollToY(int scrollY);
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
this.originalScrollY = note.getScrollY();
scrollToY(originalScrollY);
final ScrollView scrollView = getScrollView();
if (scrollView != null) {
scrollView.getViewTreeObserver().addOnScrollChangedListener(() -> {
if (scrollView.getScrollY() > 0) {
note.setScrollY(scrollView.getScrollY());
}
});
}
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final ScrollView scrollView = getScrollView();
if (scrollView != null) {
this.originalScrollY = note.getScrollY();
scrollView.post(() -> scrollView.scrollTo(0, originalScrollY));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
scrollView.setOnScrollChangeListener((View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) -> {
if (scrollY > 0) {
note.setScrollY(scrollY);
}
});
}
}
}
......
......@@ -78,6 +78,13 @@ public class NoteEditFragment extends SearchableBaseNoteFragment {
return binding.scrollView;
}
@Override
protected void scrollToY(int y) {
if (binding != null) {
binding.scrollView.post(() -> binding.scrollView.setScrollY(y));
}
}
@Override
protected Layout getLayout() {
binding.editContent.onPreDraw();
......
......@@ -42,6 +42,9 @@ public class NotePreviewFragment extends SearchableBaseNoteFragment implements O
protected FragmentNotePreviewBinding binding;
@Nullable
private Runnable setScrollY;
@Override
public void onPrepareOptionsMenu(@NonNull Menu menu) {
super.onPrepareOptionsMenu(menu);
......@@ -54,6 +57,17 @@ public class NotePreviewFragment extends SearchableBaseNoteFragment implements O
return binding.scrollView;
}
@Override
protected synchronized void scrollToY(int y) {
this.setScrollY = () -> {
if (binding != null) {
Log.v("SCROLL set (preview) to", y + "");
binding.scrollView.post(() -> binding.scrollView.setScrollY(y));
}
setScrollY = null;
};
}
@Override
protected FloatingActionButton getSearchNextButton() {
return binding.searchNext;
......@@ -83,7 +97,7 @@ public class NotePreviewFragment extends SearchableBaseNoteFragment implements O
super.onActivityCreated(savedInstanceState);
registerInternalNoteLinkHandler();
binding.singleNoteContent.setMarkdownString(note.getContent());
binding.singleNoteContent.setMarkdownString(note.getContent(), setScrollY);
binding.singleNoteContent.setMovementMethod(LinkMovementMethod.getInstance());
changedText = note.getContent();
......
......@@ -25,12 +25,17 @@ public interface MarkdownEditor {
*/
void setMarkdownString(CharSequence text);
/**
* The given {@link String} will be parsed and rendered and the {@param afterRender} will be called after the rendering finished
*/
void setMarkdownString(CharSequence text, @Nullable Runnable afterRender);
/**
* Will replace all `@mention`s of Nextcloud users with the avatar and given display name.
*
* @param mentions {@link Map} of mentions, where the key is the user id and the value is the display name
*/
default void setMarkdownString(CharSequence text, @NonNull Map<String, String> mentions) {
default void setMarkdownStringAndHighlightMentions(CharSequence text, @NonNull Map<String, String> mentions) {
setMarkdownString(text);
}
......
......@@ -14,8 +14,6 @@ import androidx.appcompat.widget.AppCompatEditText;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import java.util.Collection;
import java.util.LinkedList;
import java.util.function.Consumer;
import io.noties.markwon.Markwon;
......@@ -117,6 +115,11 @@ public class MarkwonMarkdownEditor extends AppCompatEditText implements Markdown
setMarkdownStringModel(text);
}
@Override
public void setMarkdownString(CharSequence text, Runnable afterRender) {
throw new UnsupportedOperationException("This is not available in " + MarkwonMarkdownEditor.class.getSimpleName() + " because the text is getting rendered all the time.");
}
/**
* Updates the current model which matches the rendered state of the editor *without* triggering
* anything of the native {@link EditText}
......
......@@ -127,6 +127,11 @@ public class MarkwonMarkdownViewer extends AppCompatTextView implements Markdown
@Override
public void setMarkdownString(CharSequence text) {
setMarkdownString(text, null);
}
@Override
public void setMarkdownString(CharSequence text, Runnable afterRender) {
final CharSequence previousText = this.unrenderedText$.getValue();
this.unrenderedText$.setValue(text);
if (listener != null) {
......@@ -136,7 +141,12 @@ public class MarkwonMarkdownViewer extends AppCompatTextView implements Markdown
setText(text);
} else {
if (!text.equals(previousText)) {
this.renderService.execute(() -> post(() -> this.markwon.setMarkdown(this, text.toString())));
this.renderService.execute(() -> post(() -> {
this.markwon.setMarkdown(this, text.toString());
if (afterRender != null) {
afterRender.run();
}
}));
}
}
}
......@@ -162,7 +172,7 @@ public class MarkwonMarkdownViewer extends AppCompatTextView implements Markdown
}
@Override
public void setMarkdownString(CharSequence text, @NonNull Map<String, String> mentions) {
public void setMarkdownStringAndHighlightMentions(CharSequence text, @NonNull Map<String, String> mentions) {
this.markwon = createMarkwonBuilder(getContext(), mentions).build();
setMarkdownString(text);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment