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

add option to remove common indent from note body #500

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
import org.junit.Test;

import static androidx.test.espresso.Espresso.onData;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.Espresso.pressBack;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.longClick;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static com.orgzly.android.espresso.EspressoUtils.clickSetting;
import static com.orgzly.android.espresso.EspressoUtils.onActionItemClick;
Expand Down Expand Up @@ -45,7 +48,9 @@ public void setUp() throws Exception {
"SCHEDULED: <2018-01-01>\n" +
"Content for [a-1]\n" +
"* Note [a-2]\n" +
"SCHEDULED: <2014-01-01>\n"
"SCHEDULED: <2014-01-01>\n" +
"* Indented content\n" +
" indented note content"
);

activityRule.launchActivity(null);
Expand Down Expand Up @@ -101,6 +106,32 @@ public void testDisplayedContentInBook() {
onNoteInBook(1, R.id.item_head_content).check(matches(not(isDisplayed())));
}

@Test
public void testRemoveIndent() {
onBook(0).perform(click());
onNoteInBook(3, R.id.item_head_content)
.check(matches(withText("indented note content")));
onNoteInBook(3).perform(longClick());
onView(withId(R.id.body_view)).check(matches(withText("indented note content")));
onView(withId(R.id.body_edit)).check(matches(withText(" indented note content")));
pressBack();
pressBack();

// Disable remove indent setting
onActionItemClick(R.id.activity_action_settings, R.string.settings);
clickSetting("prefs_screen_look_and_feel", R.string.look_and_feel);
clickSetting("pref_key_remove_indent_from_body", R.string.remove_indent_from_body);
pressBack();
pressBack();

onBook(0).perform(click());
onNoteInBook(3, R.id.item_head_content)
.check(matches(withText(" indented note content")));
onNoteInBook(3).perform(longClick());
onView(withId(R.id.body_view)).check(matches(withText(" indented note content")));
onView(withId(R.id.body_edit)).check(matches(withText(" indented note content")));
}

private void setDefaultPriority(String priority) {
onActionItemClick(R.id.activity_action_settings, R.string.settings);
clickSetting("prefs_screen_notebooks", R.string.pref_title_notebooks);
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/orgzly/android/prefs/AppPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,17 @@ public static boolean isFontMonospaced(Context context) {
context.getResources().getBoolean(R.bool.pref_default_is_font_monospaced));
}

public static boolean removeIndentFromBody(Context context) {
return getDefaultSharedPreferences(context).getBoolean(
context.getResources().getString(R.string.pref_key_remove_indent_from_body),
context.getResources().getBoolean(R.bool.pref_default_remove_indent_from_body));
}

public static void removeIndentFromBody(Context context, boolean value) {
String key = context.getResources().getString(R.string.pref_key_remove_indent_from_body);
getDefaultSharedPreferences(context).edit().putBoolean(key, value).apply();
}

public static boolean styleText(Context context) {
return getDefaultSharedPreferences(context).getBoolean(
context.getResources().getString(R.string.pref_key_style_text),
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/java/com/orgzly/android/ui/util/ViewUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,28 @@ public static List<View> getAllChildren(View parent, Class klass) {

return result;
}

public static String removeIndent(CharSequence text) {
int indentCount = Integer.MAX_VALUE;
String[] lines = text.toString().split("\n");
for (String line: lines) {
int indentCountCurrentLine = 0;
for (int i = 0; i < line.length(); i++) {
if (line.charAt(i) == ' ') {
indentCountCurrentLine++;
} else {
break;
}
}

indentCount = Math.min(indentCount, indentCountCurrentLine);
}

StringBuilder stringBuilder = new StringBuilder();
for (String line: lines) {
stringBuilder.append(line.substring(indentCount));
}

return stringBuilder.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import android.util.AttributeSet
import android.util.Log
import android.widget.TextView
import com.orgzly.BuildConfig
import com.orgzly.android.prefs.AppPreferences
import com.orgzly.android.ui.main.MainActivity
import com.orgzly.android.ui.util.ViewUtils
import com.orgzly.android.ui.views.style.CheckboxSpan
import com.orgzly.android.ui.views.style.DrawerEndSpan
import com.orgzly.android.ui.views.style.DrawerSpan
Expand All @@ -32,7 +34,10 @@ class TextViewWithMarkup : TextViewFixed {
fun setRawText(text: CharSequence) {
rawText = text

setText(OrgFormatter.parse(text, context), BufferType.SPANNABLE)
val formattedText = OrgFormatter.parse(text, context)
val textToDisplay = if (AppPreferences.removeIndentFromBody(context))
ViewUtils.removeIndent(formattedText) else formattedText
setText(textToDisplay, BufferType.SPANNABLE)
}

fun getRawText() : CharSequence? {
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/prefs_keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
<string name="pref_key_is_font_monospaced" translatable="false">pref_key_is_font_monospaced</string>
<bool name="pref_default_is_font_monospaced" translatable="false">false</bool>

<string name="pref_key_remove_indent_from_body" translatable="false">pref_key_remove_indent_from_body</string>
<bool name="pref_default_remove_indent_from_body" translatable="false">true</bool>

<string name="pref_key_style_text" translatable="false">pref_key_style_text</string>
<bool name="pref_default_style_text" translatable="false">true</bool>

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@
<string name="color_scheme">Color scheme</string>
<string name="monospaced_font">Monospaced font</string>
<string name="monospaced_font_summary">Use monospaced font for note\'s content and notebook\'s preface</string>
<string name="remove_indent_from_body">Remove indentation from body of items</string>
<string name="reversed_note_click_action">Swap note click and long-click actions</string>
<string name="reversed_note_click_action_summary">Click to select note, long-click to open</string>
<string name="look_and_feel">Look &amp; Feel</string>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/xml/prefs_screen_look_and_feel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
android:summary="@string/monospaced_font_summary"
android:defaultValue="@bool/pref_default_is_font_monospaced"/>

<SwitchPreference
android:key="@string/pref_key_remove_indent_from_body"
android:title="@string/remove_indent_from_body"
android:defaultValue="@bool/pref_default_remove_indent_from_body"/>

<SwitchPreference
android:key="@string/pref_key_style_text"
android:title="@string/style_text_title"
Expand Down