Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: #248 Refresh from server when in view mode #450

Merged
merged 10 commits into from Dec 31, 2019
Expand Up @@ -5,34 +5,43 @@
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.yydcdut.markdown.MarkdownProcessor;
import com.yydcdut.markdown.syntax.text.TextFactory;
import com.yydcdut.rxmarkdown.RxMDTextView;
import com.yydcdut.rxmarkdown.RxMarkdown;

import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.model.LoginStatus;
import it.niedermann.owncloud.notes.persistence.NoteSQLiteOpenHelper;
import it.niedermann.owncloud.notes.util.ICallback;
import it.niedermann.owncloud.notes.util.MarkDownUtil;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class NotePreviewFragment extends BaseNoteFragment {

private static final String TAG = NotePreviewFragment.class.getSimpleName();

private NoteSQLiteOpenHelper db = null;

private String changedText;

MarkdownProcessor markdownProcessor;

@BindView(R.id.swiperefreshlayout)
SwipeRefreshLayout swipeRefreshLayout;

@BindView(R.id.single_note_content)
RxMDTextView noteContent;

Expand Down Expand Up @@ -62,49 +71,60 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ButterKnife.bind(this, Objects.requireNonNull(getView()));

markdownProcessor = new MarkdownProcessor(getActivity());
markdownProcessor.factory(TextFactory.create());
markdownProcessor.config(
MarkDownUtil.getMarkDownConfiguration(noteContent.getContext())
// .setOnTodoClickCallback((view, line, lineNumber) -> {
// String[] lines = TextUtils.split(note.getContent(), "\\r?\\n");
// /*
// * Workaround for a bug when checkbox is the last line:
// * When (un)checking a checkbox which is in the last line, every time it gets toggled, the last character of the line gets lost.
// */
// if ((lines.length - 1) == lineNumber) {
// if(lines[lineNumber].contains("- [ ]")) {
// lines[lineNumber] = lines[lineNumber].replace("- [ ]", "- [x]");
// } else {
// lines[lineNumber] = lines[lineNumber].replace("- [x]", "- [ ]");
// }
//
// } else if (lines.length >= lineNumber) {
// lines[lineNumber] = line;
// }
// changedText = TextUtils.join("\n", lines);
// noteContent.setText(markdownProcessor.parse(changedText));
// saveNote(null);
// return line;
// }
// )
.build());
setActiveTextView(noteContent);
noteContent.setText(markdownProcessor.parse(note.getContent()));
changedText = note.getContent();
noteContent.setMovementMethod(LinkMovementMethod.getInstance());

String content = note.getContent();

RxMarkdown.with(content, getActivity())
.config(
MarkDownUtil.getMarkDownConfiguration(noteContent.getContext())
/*.setOnTodoClickCallback(new OnTodoClickCallback() {
@Override
public CharSequence onTodoClicked(View view, String line, int lineNumber) {
String[] lines = TextUtils.split(note.getContent(), "\\r?\\n");
if(lines.length >= lineNumber) {
lines[lineNumber] = line;
}
noteContent.setText(TextUtils.join("\n", lines), TextView.BufferType.SPANNABLE);
saveNote(null);
return line;
}
}
)*/.build()
)
.factory(TextFactory.create())
.intoObservable()
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<CharSequence>() {
@Override
public void onCompleted() {
}

db = NoteSQLiteOpenHelper.getInstance(getActivity().getApplicationContext());
// Pull to Refresh
swipeRefreshLayout.setOnRefreshListener(() -> {
if (db.getNoteServerSyncHelper().isSyncPossible()) {
swipeRefreshLayout.setRefreshing(true);
db.getNoteServerSyncHelper().addCallbackPull(new ICallback() {
@Override
public void onError(Throwable e) {
Log.v(TAG, "RxMarkdown error", e);
public void onFinish() {
noteContent.setText(markdownProcessor.parse(db.getNote(note.getAccountId(), note.getId()).getContent()));
swipeRefreshLayout.setRefreshing(false);
}

@Override
public void onNext(CharSequence charSequence) {
noteContent.setText(charSequence, TextView.BufferType.SPANNABLE);
public void onScheduled() {
}
});
noteContent.setText(content);
noteContent.setMovementMethod(LinkMovementMethod.getInstance());
db.getNoteServerSyncHelper().scheduleSync(false);
} else {
swipeRefreshLayout.setRefreshing(false);
Toast.makeText(getActivity().getApplicationContext(), getString(R.string.error_sync, getString(LoginStatus.NO_NETWORK.str)), Toast.LENGTH_LONG).show();
}
});

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
noteContent.setTextSize(TypedValue.COMPLEX_UNIT_PX, getFontSizeFromPreferences(sp));
Expand All @@ -115,6 +135,6 @@ public void onNext(CharSequence charSequence) {

@Override
protected String getContent() {
return note.getContent();
return changedText;
}
}
34 changes: 22 additions & 12 deletions app/src/main/res/layout/activity_single_note.xml
@@ -1,19 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swiperefreshlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="it.niedermann.owncloud.notes.android.activity.EditNoteActivity">
android:background="@color/bg_normal"
tools:context="it.niedermann.owncloud.notes.android.activity.NotesListViewActivity"
tools:ignore="MergeRootFrame">

<com.yydcdut.rxmarkdown.RxMDTextView
android:id="@+id/single_note_content"
<ScrollView
android:id="@+id/editContentContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/fg_default"
android:background="@color/bg_normal"
android:textIsSelectable="true" />
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="it.niedermann.owncloud.notes.android.activity.EditNoteActivity">

</ScrollView>
<com.yydcdut.rxmarkdown.RxMDTextView
android:id="@+id/single_note_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/bg_normal"
android:padding="16dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/fg_default"
android:textIsSelectable="true" />
</ScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>