Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add option to switch back to raw markdown

Markdown files are rendered by default when opened
but can still be toggled back to raw mode which is
saved as a preference.
  • Loading branch information...
commit b6d8c3b9f625c5466e96bfd4ec4c061e57f1456f 1 parent fb181ff
@kevinsawicki kevinsawicki authored
View
24 app/res/menu/file_view.xml
@@ -16,14 +16,20 @@
-->
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
- <item
- android:id="@+id/m_wrap"
- android:showAsAction="never"
- android:title="@string/enable_wrapping"/>
- <item
- android:id="@+id/m_share"
- android:icon="@drawable/menu_share"
- android:showAsAction="never"
- android:title="@string/share"/>
+ <item
+ android:id="@+id/m_wrap"
+ android:showAsAction="never"
+ android:title="@string/enable_wrapping"/>
+ <item
+ android:id="@+id/m_render_markdown"
+ android:enabled="false"
+ android:showAsAction="never"
+ android:title="@string/render_markdown"
+ android:visible="false"/>
+ <item
+ android:id="@+id/m_share"
+ android:icon="@drawable/menu_share"
+ android:showAsAction="never"
+ android:title="@string/share"/>
</menu>
View
2  app/res/values/strings.xml
@@ -258,5 +258,7 @@
<string name="show_password">Show password</string>
<string name="write">Write</string>
<string name="preview">Preview</string>
+ <string name="show_raw_markdown">Show raw markdown</string>
+ <string name="render_markdown">Render markdown</string>
</resources>
View
80 app/src/main/java/com/github/mobile/ui/ref/BranchFileViewActivity.java
@@ -19,6 +19,7 @@
import static com.github.mobile.Intents.EXTRA_HEAD;
import static com.github.mobile.Intents.EXTRA_PATH;
import static com.github.mobile.Intents.EXTRA_REPOSITORY;
+import static com.github.mobile.util.PreferenceUtils.RENDER_MARKDOWN;
import static com.github.mobile.util.PreferenceUtils.WRAP;
import android.content.Intent;
import android.os.Bundle;
@@ -112,12 +113,20 @@ public static Intent createIntent(Repository repository, String branch,
private String branch;
+ private boolean isMarkdownFile;
+
+ private String renderedMarkdown;
+
+ private Blob blob;
+
private ProgressBar loadingBar;
private WebView codeView;
private SourceEditor editor;
+ private MenuItem markdownItem;
+
@Inject
private AvatarLoader avatars;
@@ -139,6 +148,7 @@ protected void onCreate(Bundle savedInstanceState) {
codeView = finder.find(id.wv_code);
file = CommitUtils.getName(path);
+ isMarkdownFile = isMarkdown(file);
editor = new SourceEditor(codeView);
editor.setWrap(PreferenceUtils.getCodePreferences(this).getBoolean(
WRAP, false));
@@ -161,6 +171,17 @@ public boolean onCreateOptionsMenu(final Menu optionsMenu) {
else
wrapItem.setTitle(string.enable_wrapping);
+ markdownItem = optionsMenu.findItem(id.m_render_markdown);
+ if (isMarkdownFile) {
+ markdownItem.setEnabled(blob != null);
+ markdownItem.setVisible(true);
+ if (PreferenceUtils.getCodePreferences(this).getBoolean(
+ RENDER_MARKDOWN, true))
+ markdownItem.setTitle(string.show_raw_markdown);
+ else
+ markdownItem.setTitle(string.render_markdown);
+ }
+
return true;
}
@@ -181,6 +202,22 @@ public boolean onOptionsItemSelected(MenuItem item) {
case id.m_share:
shareFile();
return true;
+ case id.m_render_markdown:
+ if (editor.isMarkdown()) {
+ item.setTitle(string.render_markdown);
+ editor.setMarkdown(false);
+ editor.setSource(file, blob);
+ } else {
+ item.setTitle(string.show_raw_markdown);
+ editor.setMarkdown(true);
+ if (renderedMarkdown != null)
+ editor.setSource(file, renderedMarkdown, false);
+ else
+ loadMarkdown();
+ }
+ PreferenceUtils.save(PreferenceUtils.getCodePreferences(this)
+ .edit().putBoolean(RENDER_MARKDOWN, editor.isMarkdown()));
+ return true;
default:
return super.onOptionsItemSelected(item);
}
@@ -203,7 +240,12 @@ public void onLoadFinished(Loader<CharSequence> loader,
ViewUtils.setGone(loadingBar, true);
ViewUtils.setGone(codeView, false);
- editor.setMarkdown(true).setSource(file, rendered.toString(), false);
+ if (!TextUtils.isEmpty(rendered)) {
+ renderedMarkdown = rendered.toString();
+ if (markdownItem != null)
+ markdownItem.setEnabled(true);
+ editor.setMarkdown(true).setSource(file, renderedMarkdown, false);
+ }
}
@Override
@@ -216,23 +258,39 @@ private void shareFile() {
"https://github.com/" + id + "/blob/" + branch + '/' + path));
}
+ private void loadMarkdown() {
+ ViewUtils.setGone(loadingBar, false);
+ ViewUtils.setGone(codeView, true);
+
+ String markdown = new String(
+ EncodingUtils.fromBase64(blob.getContent()));
+ Bundle args = new Bundle();
+ args.putCharSequence(ARG_TEXT, markdown);
+ if (repo instanceof Serializable)
+ args.putSerializable(ARG_REPO, (Serializable) repo);
+ getSupportLoaderManager().restartLoader(0, args, this);
+ }
+
private void loadContent() {
+ ViewUtils.setGone(loadingBar, false);
+ ViewUtils.setGone(codeView, true);
+
new RefreshBlobTask(repo, sha, this) {
@Override
protected void onSuccess(Blob blob) throws Exception {
super.onSuccess(blob);
- if (isMarkdown(file)) {
- String markdown = new String(EncodingUtils.fromBase64(blob
- .getContent()));
- Bundle args = new Bundle();
- args.putCharSequence(ARG_TEXT, markdown);
- if (repo instanceof Serializable)
- args.putSerializable(ARG_REPO, (Serializable) repo);
- getSupportLoaderManager().restartLoader(0, args,
- BranchFileViewActivity.this);
- } else {
+ if (markdownItem != null)
+ markdownItem.setEnabled(true);
+
+ BranchFileViewActivity.this.blob = blob;
+ if (isMarkdownFile
+ && PreferenceUtils.getCodePreferences(
+ BranchFileViewActivity.this).getBoolean(
+ RENDER_MARKDOWN, true))
+ loadMarkdown();
+ else {
ViewUtils.setGone(loadingBar, true);
ViewUtils.setGone(codeView, false);
View
5 app/src/main/java/com/github/mobile/util/PreferenceUtils.java
@@ -33,6 +33,11 @@
public static final String WRAP = "wrap";
/**
+ * Preference to render markdown
+ */
+ public static final String RENDER_MARKDOWN = "renderMarkdown";
+
+ /**
* Get code browsing preferences
*
* @param context
View
27 app/src/main/java/com/github/mobile/util/SourceEditor.java
@@ -119,6 +119,13 @@ public boolean getWrap() {
}
/**
+ * @return markdown
+ */
+ public boolean isMarkdown() {
+ return markdown;
+ }
+
+ /**
* Set whether lines should wrap
*
* @param wrap
@@ -126,12 +133,7 @@ public boolean getWrap() {
*/
public SourceEditor setWrap(final boolean wrap) {
this.wrap = wrap;
- if (name != null && content != null) {
- if (markdown)
- view.loadData(content, "text/html", null);
- else
- view.loadUrl(URL_PAGE);
- }
+ loadSource();
return this;
}
@@ -159,14 +161,19 @@ public SourceEditor setSource(final String name, final String content,
this.name = name;
this.content = content;
this.encoded = encoded;
- if (markdown)
- view.loadData(content, "text/html", null);
- else
- view.loadUrl(URL_PAGE);
+ loadSource();
return this;
}
+ private void loadSource() {
+ if (name != null && content != null)
+ if (markdown)
+ view.loadData(content, "text/html", null);
+ else
+ view.loadUrl(URL_PAGE);
+ }
+
/**
* Bind blob content to current {@link WebView}
*
Please sign in to comment.
Something went wrong with that request. Please try again.