Skip to content

Commit

Permalink
merged and tweaked
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed May 10, 2024
2 parents 9366791 + 4f40150 commit 3bce89b
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#########################################################*/
package net.gsantner.markor.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
Expand Down Expand Up @@ -185,6 +183,15 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
((DocumentActivity) activity).setDocumentTitle(_document.getTitle());
}

// Preview mode set before loadDocument to prevent flicker
final Bundle args = getArguments();
final boolean startInPreview = _appSettings.getDocumentPreviewState(_document.getPath());
if (args != null && savedInstanceState == null) { // Use the launch flag on first launch
setViewModeVisibility(args.getBoolean(START_PREVIEW, startInPreview), false);
} else {
setViewModeVisibility(startInPreview, false);
}

_hlEditor.setSaveInstanceState(false); // We will reload from disk
_document.resetChangeTracking(); // force next reload
loadDocument();
Expand Down Expand Up @@ -216,15 +223,6 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
updateMenuToggleStates(0);
// ---------------------------------------------------------

// Start preview _after_ text load
final Bundle args = getArguments();
final boolean startInPreview = _appSettings.getDocumentPreviewState(_document.getPath());
if (args != null && savedInstanceState == null) { // Use the launch flag on first launch
setViewModeVisibility(args.getBoolean(START_PREVIEW, startInPreview));
} else {
setViewModeVisibility(startInPreview);
}

final Runnable debounced = TextViewUtils.makeDebounced(500, () -> {
checkTextChangeState();
updateUndoRedoIconStates();
Expand Down Expand Up @@ -806,7 +804,11 @@ public void updateViewModeText() {
_format.getConverter().convertMarkupShowInWebView(_document, text, getActivity(), _webView, _nextConvertToPrintMode, _hlEditor.getLineNumbersEnabled());
}

public void setViewModeVisibility(boolean show) {
public void setViewModeVisibility(final boolean show) {
setViewModeVisibility(show, true);
}

public void setViewModeVisibility(boolean show, final boolean animate) {
final Activity activity = getActivity();
show |= _document.isBinaryFileNoTextLoading();

Expand All @@ -816,10 +818,10 @@ public void setViewModeVisibility(boolean show) {
_cu.showSoftKeyboard(activity, false, _hlEditor);
_hlEditor.clearFocus();
_hlEditor.postDelayed(() -> _cu.showSoftKeyboard(activity, false, _hlEditor), 300);
fadeInOut(_webView, _primaryScrollView);
GsContextUtils.fadeInOut(_webView, _primaryScrollView, animate);
} else {
_webViewClient.setRestoreScrollY(_webView.getScrollY());
fadeInOut(_primaryScrollView, _webView);
GsContextUtils.fadeInOut(_primaryScrollView, _webView, animate);
}

_nextConvertToPrintMode = false;
Expand All @@ -839,28 +841,6 @@ public void webViewJavascriptCallback(final String[] jsArgs) {
}
}

private static boolean fadeInOut(final View in, final View out) {
// Do nothing if we are already in the correct state
if (in.getVisibility() == View.VISIBLE && out.getVisibility() == View.GONE) {
return false;
}

in.setAlpha(0);
in.setVisibility(View.VISIBLE);
in.animate().alpha(1).setDuration(200).setListener(null);
out.animate()
.alpha(0)
.setDuration(200)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
out.setVisibility(View.GONE);
}
});

return true;
}

@Override
protected void onToolbarClicked(View v) {
if (_format != null) {
Expand Down
24 changes: 16 additions & 8 deletions app/src/main/java/net/gsantner/markor/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ public void onRestoreInstanceState(final Bundle savedInstanceState) {
_quicknote = (DocumentEditAndViewFragment) manager.getFragment(savedInstanceState, Integer.toString(R.id.nav_quicknote));
_todo = (DocumentEditAndViewFragment) manager.getFragment(savedInstanceState, Integer.toString(R.id.nav_todo));
_more = (MoreFragment) manager.getFragment(savedInstanceState, Integer.toString(R.id.nav_more));

final NewFileDialog nf = (NewFileDialog) manager.findFragmentByTag(NewFileDialog.FRAGMENT_TAG);
if (nf != null) {
nf.setCallback(this::newItemCallback);
}

} catch (NullPointerException | IllegalStateException ignored) {
Log.d(MainActivity.class.getName(), "Child fragment not found in onRestoreInstanceState()");
}
Expand Down Expand Up @@ -296,14 +302,16 @@ public void onClickFab(final View view) {
return;
}

final NewFileDialog dialog = NewFileDialog.newInstance(_notebook.getCurrentFolder(), true, f -> {
if (f.isFile()) {
DocumentActivity.launch(MainActivity.this, f, false, null, null);
} else if (f.isDirectory()) {
_notebook.getAdapter().showFile(f);
}
});
dialog.show(getSupportFragmentManager(), NewFileDialog.FRAGMENT_TAG);
NewFileDialog.newInstance(_notebook.getCurrentFolder(), true, this::newItemCallback)
.show(getSupportFragmentManager(), NewFileDialog.FRAGMENT_TAG);
}
}

private void newItemCallback(final File file) {
if (file.isFile()) {
DocumentActivity.launch(MainActivity.this, file, false, null, null);
} else if (file.isDirectory()) {
_notebook.getAdapter().showFile(file);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#########################################################*/
package net.gsantner.markor.format;

import static android.util.Patterns.WEB_URL;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
Expand Down Expand Up @@ -40,6 +42,7 @@

import net.gsantner.markor.ApplicationObject;
import net.gsantner.markor.R;
import net.gsantner.markor.activity.DocumentActivity;
import net.gsantner.markor.frontend.AttachLinkOrFileDialog;
import net.gsantner.markor.frontend.DatetimeFormatDialog;
import net.gsantner.markor.frontend.MarkorDialogFactory;
Expand All @@ -54,6 +57,7 @@
import net.gsantner.opoc.util.GsFileUtils;
import net.gsantner.opoc.wrapper.GsCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -696,7 +700,25 @@ protected final boolean runCommonAction(final @StringRes int action) {
final int sel = TextViewUtils.getSelection(_hlEditor)[0];
final String line = TextViewUtils.getSelectedLines(_hlEditor, sel);
final int cursor = sel - TextViewUtils.getLineStart(_hlEditor.getText(), sel);
String url = GsTextUtils.tryExtractUrlAroundPos(line, cursor);

// First try to pull a resource
String url = null;
final String resource = GsTextUtils.tryExtractResourceAroundPos(line, cursor);
if (resource != null) {
if (WEB_URL.matcher(resource).matches()) {
url = resource;
} else {
final File f = GsFileUtils.makeAbsolute(resource, _document.getFile().getParentFile());
if (f.canRead()) {
DocumentActivity.handleFileClick(getActivity(), f, null);
return true;
}
}

}

// Then try to pull a tag
url = url == null ? GsTextUtils.tryExtractUrlAroundPos(line, cursor) : url;
if (url != null) {
if (url.endsWith(")")) {
url = url.substring(0, url.length() - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ private void callback(final File file) {
}
}

public void setCallback(final GsCallback.a1<File> callback) {
this.callback = callback;
}

private Pair<String, Integer> getTemplateContent(final String template, final String name) {
String text = TextViewUtils.interpolateSnippet(template, name, "");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ public static void setSelectionAndShow(final EditText edit, final int... sel) {

if (inRange(0, edit.length(), start, end)) {
edit.post(() -> {
if (!edit.hasFocus()) {
if (!edit.hasFocus() && edit.getVisibility() != View.GONE) {
edit.requestFocus();
}

Expand Down
33 changes: 33 additions & 0 deletions app/src/main/java/net/gsantner/opoc/format/GsTextUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SuppressWarnings({"unused", "SpellCheckingInspection"})
public class GsTextUtils {
public static String UTF8 = "UTF-8";

// Regex patterns used for finding resources in tags
public final static Pattern SELF_CLOSING_TAG = Pattern.compile("<(\\w+)([^>]*?)src='([^']+)'([^>]*?)/>");
public final static Pattern REGULAR_TAG = Pattern.compile("<(\\w+)([^>]*?)src='([^']+)'([^>]*?)>(.*?)</\\1>");


/**
* This is a simple method that tries to extract an URL around a given index.
* It doesn't do any validation. Separation by whitespace or end. Detects http and https.
Expand All @@ -55,6 +62,32 @@ public static String tryExtractUrlAroundPos(final String text, int pos) {
return null;
}


/**
* This is a simple method that tries to extract the value of the 'src' attribute
* for any tag in the text which surrounds pos.
* It doesn't do any validation. Separation by whitespace or end.
*
* @param text Text to extract from
* @param pos Position to start searching from (backwards)
* @return Extracted resource path or {@code null} if none found
*/
public static String tryExtractResourceAroundPos(final String text, int pos) {

for (final Pattern pattern : Arrays.asList(SELF_CLOSING_TAG, REGULAR_TAG)) {
final Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
int start = matcher.start();
int end = matcher.end();
if (pos >= start && pos <= end) {
return matcher.group(3);
}
}
}

return null; // Return null if no enclosing tag with src attribute is found
}

/**
* find '\n' to the right and left of text[pos] .. text[posEnd].
* If left does not exist 0 (begin of text) is used.
Expand Down
31 changes: 29 additions & 2 deletions app/src/main/java/net/gsantner/opoc/util/GsContextUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import static android.graphics.Bitmap.CompressFormat;

import android.Manifest;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
Expand Down Expand Up @@ -2880,8 +2882,33 @@ public boolean isDarkModeEnabled(final Context context) {

public static void blinkView(final View view) {
if (view != null) {
final float init = view.getAlpha();
ObjectAnimator.ofFloat(view, View.ALPHA, init, 0.1f, 1.0f, 0.1f, 1.0f, init).setDuration(1000).start();
ObjectAnimator.ofFloat(view, View.ALPHA, 1.0f, 0.1f, 1.0f, 0.1f, 1.0f).setDuration(800).start();
}
}

public static boolean fadeInOut(final View in, final View out, final boolean animate) {
// Do nothing if we are already in the correct state
if (in.getVisibility() == View.VISIBLE && out.getVisibility() == View.GONE) {
return false;
}

in.setVisibility(View.VISIBLE);
if (animate) {
in.setAlpha(0);
in.animate().alpha(1).setDuration(200).setListener(null);
out.animate()
.alpha(0)
.setDuration(200)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
out.setVisibility(View.GONE);
}
});
} else {
out.setVisibility(View.GONE);
}

return true;
}
}
9 changes: 4 additions & 5 deletions app/src/main/res/layout/new_file_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
android:textSize="15sp"
tools:ignore="HardcodedText,TextFields"/>


<EditText
android:id="@+id/new_file_dialog__ext"
android:layout_width="0dp"
Expand Down Expand Up @@ -129,7 +128,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="8dp"
android:layout_marginTop="4dp"
android:ellipsize="end"
android:lines="1"
android:text="@string/type"
Expand All @@ -145,7 +144,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="8dp"
android:layout_marginTop="4dp"
android:ellipsize="end"
android:lines="1"
android:text="@string/template"
Expand All @@ -168,14 +167,14 @@
android:id="@+id/new_file_dialog__utf8_bom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginTop="4dp"
android:text="@string/utf8_with_bom" />

<TextView
style="@style/TextAppearance.AppCompat.Caption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_marginTop="20dp"
android:text="@string/folder_is_created_without_extension" />

<TextView
Expand Down

0 comments on commit 3bce89b

Please sign in to comment.