Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

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

Fix #1046 Remember scroll position in preview mode

parent 71542a36
Loading
Loading
Loading
Loading
+11 −15
Original line number Diff line number Diff line
@@ -135,27 +135,23 @@ 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());
            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);
                    }
                });
            }
        }

    @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));
        }
    }

    @Override
+7 −0
Original line number Diff line number Diff line
@@ -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();
+15 −1
Original line number Diff line number Diff line
@@ -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();

+6 −1
Original line number Diff line number Diff line
@@ -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);
    }

+5 −2
Original line number Diff line number Diff line
@@ -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}
Loading