Skip to content

Commit

Permalink
As link checkbox to top bar
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed May 26, 2024
1 parent dc23d15 commit bc2d84c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,27 @@
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Pair;
import android.util.Patterns;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.widget.CompoundButtonCompat;
import androidx.fragment.app.FragmentTransaction;
import androidx.preference.CheckBoxPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceGroup;
import androidx.appcompat.widget.Toolbar;

import net.gsantner.markor.ApplicationObject;
import net.gsantner.markor.R;
Expand All @@ -36,7 +42,6 @@
import net.gsantner.markor.frontend.NewFileDialog;
import net.gsantner.markor.frontend.filebrowser.MarkorFileBrowserFactory;
import net.gsantner.markor.frontend.textview.HighlightingEditor;
import net.gsantner.markor.frontend.textview.TextViewUtils;
import net.gsantner.markor.model.AppSettings;
import net.gsantner.markor.model.Document;
import net.gsantner.markor.util.MarkorContextUtils;
Expand Down Expand Up @@ -88,7 +93,7 @@ protected int getLayoutResId() {
@Override
public void onViewCreated(final @NonNull View view, final Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
HighlightingEditor _hlEditor = view.findViewById(R.id.document__fragment__share_into__highlighting_editor);
final HighlightingEditor _hlEditor = view.findViewById(R.id.document__fragment__share_into__highlighting_editor);

final String sharedText = (getArguments() != null ? getArguments().getString(EXTRA_SHARED_TEXT, "") : "").trim();
final ShareIntoImportOptionsFragment _shareIntoImportOptionsFragment;
Expand All @@ -102,7 +107,8 @@ public void onViewCreated(final @NonNull View view, final Bundle savedInstanceSt
}

if (_shareIntoImportOptionsFragment != null) {
_shareIntoImportOptionsFragment.setEditor(_hlEditor);
_shareIntoImportOptionsFragment._editor = _hlEditor;
_shareIntoImportOptionsFragment._linkCheckBox = addCheckBoxToToolbar();
}

_hlEditor.setTextSize(TypedValue.COMPLEX_UNIT_SP, _appSettings.getFontSize());
Expand All @@ -116,22 +122,58 @@ public void onViewCreated(final @NonNull View view, final Bundle savedInstanceSt
}
}

@Override
public String getFragmentTag() {
return FRAGMENT_TAG;
private CheckBox addCheckBoxToToolbar() {
final String CHECKBOX_TAG = "insert_link_checkbox";

final Activity activity = getActivity();
if (activity == null) {
return null;
}

final Toolbar toolbar = activity.findViewById(R.id.toolbar);
if (toolbar == null) {
return null;
}

CheckBox checkBox = toolbar.findViewWithTag(CHECKBOX_TAG);
if (checkBox == null) {

checkBox = new CheckBox(activity);
checkBox.setText(R.string.format_as_link);
checkBox.setTag(CHECKBOX_TAG);
CompoundButtonCompat.setButtonTintList(checkBox, ColorStateList.valueOf(Color.WHITE));
checkBox.setTextColor(Color.WHITE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
checkBox.setLayoutDirection(CheckBox.LAYOUT_DIRECTION_RTL);
}

final Toolbar.LayoutParams layoutParams = new Toolbar.LayoutParams(
Toolbar.LayoutParams.WRAP_CONTENT,
Toolbar.LayoutParams.WRAP_CONTENT,
Gravity.CENTER_VERTICAL | Gravity.END
);

final int margin = _cu.convertDpToPx(activity, 10);
layoutParams.setMargins(0, 0, margin, 0);

toolbar.addView(checkBox, layoutParams);
}

return checkBox;
}

@Override
public boolean onBackPressed() {
return false;
public String getFragmentTag() {
return FRAGMENT_TAG;
}


public static class ShareIntoImportOptionsFragment extends GsPreferenceFragmentBase<AppSettings> {
public static final String TAG = "ShareIntoImportOptionsFragment";
private File workingDir;

private EditText _editor = null;
private CheckBox _linkCheckBox = null;

@Override
public boolean isDividerVisible() {
Expand All @@ -142,10 +184,6 @@ public void setWorkingDir(File dir) {
workingDir = dir;
}

public void setEditor(final EditText editor) {
_editor = editor;
}

@Override
public int getPreferenceResourceForInflation() {
return R.xml.prefactions_share_into;
Expand All @@ -166,20 +204,16 @@ protected void afterOnCreate(Bundle savedInstances, Context context) {
super.afterOnCreate(savedInstances, context);
doUpdatePreferences();

if (_editor != null) {
final CheckBoxPreference asLinkPref = (CheckBoxPreference) findPreference(R.string.pref_key__attach_as_link);
if (asLinkPref != null) {
asLinkPref.setVisible(hasLinks(_editor.getText()));
asLinkPref.setChecked(true);
_editor.addTextChangedListener(GsTextWatcherAdapter.on((ctext, arg2, arg3, arg4) ->
asLinkPref.setVisible(hasLinks(ctext))));
}
if (_editor != null && _linkCheckBox != null) {
_linkCheckBox.setVisibility(hasLinks(_editor.getText()) ? View.VISIBLE : View.GONE);
_linkCheckBox.setChecked(true);
_editor.addTextChangedListener(GsTextWatcherAdapter.on((ctext, arg2, arg3, arg4) ->
_linkCheckBox.setVisibility(hasLinks(_editor.getText()) ? View.VISIBLE : View.GONE)));
}
}

private boolean shareAsLink() {
final CheckBoxPreference asLinkPref = (CheckBoxPreference) findPreference(R.string.pref_key__attach_as_link);
return asLinkPref != null && asLinkPref.isVisible() && asLinkPref.isEnabled() && asLinkPref.isChecked();
return _linkCheckBox != null && _linkCheckBox.getVisibility() == View.VISIBLE && _linkCheckBox.isChecked();
}

private void appendToExistingDocumentAndClose(final File file, final boolean showEditor) {
Expand Down Expand Up @@ -244,50 +278,6 @@ private static boolean hasLinks(final CharSequence text) {
return hasLinks[0];
}

/**
* Iterates over each line of the text and checks if it is a path or link
* The whole line has to be a path or the line must end with a link to match.
*
* @param text Text to parse
* @return List of pairs.
* The first element of the pair is the start and end index of the link/path.
* Second element is true if it is a path, false if it is a link.
*/
public static List<Pair<int[], Boolean>> findLinksAndPaths(final CharSequence text) {
final List<Pair<int[], Boolean>> links = new ArrayList<>();

GsTextUtils.forEachline(text, (line, start, end) -> {

start = TextViewUtils.getNextNonWhitespace(text, start);
end = TextViewUtils.getLastNonWhitespace(text, end - 1) + 1;

if (start != -1 && end != -1 && start <= end) {

final String trimmed = text.subSequence(start, end).toString();
final boolean hasSpaces = trimmed.contains(" ") || trimmed.contains("\\t");

// Test for web links
if (!hasSpaces && Patterns.WEB_URL.matcher(trimmed).matches()) {
links.add(Pair.create(new int[]{start, end}, false));
return true;
}

// Test for file links
try {
if (new File(trimmed.replace("%20", " ")).exists()) {
links.add(Pair.create(new int[]{start, end}, true));
return true;
}
} catch (NullPointerException ignored) {
}
}

return true;
});

return links;
}

public static String getLinkTitle(final String link) {
final Matcher m = Patterns.WEB_URL.matcher(link);
if (m.matches()) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/document__activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<TextView
android:id="@+id/note__activity__text_note_title"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:fontFamily="sans-serif-medium"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ work. If not, see <https://creativecommons.org/publicdomain/zero/1.0/>.

<string name="name">Name</string>
<string name="url_or_path">URL / Path</string>
<string name="insert_link">Insert Link</string>
<string name="format_as_link">Format link</string>
<string name="insert_image">Insert Image</string>

<string name="contributors">Contributors</string>
Expand Down
6 changes: 0 additions & 6 deletions app/src/main/res/xml/prefactions_share_into.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@
android:key="@string/pref_key__share_into__calendar_event"
android:title="@string/calendar" />

<CheckBoxPreference
android:checked="true"
android:icon="@drawable/ic_link_black_24dp"
android:key="@string/pref_key__attach_as_link"
android:title="@string/insert_link" />

<PreferenceCategory android:title="@string/file_browser" />

<Preference
Expand Down

0 comments on commit bc2d84c

Please sign in to comment.