Skip to content

Commit b6d8c3b

Browse files
committed
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.
1 parent fb181ff commit b6d8c3b

File tree

5 files changed

+108
-30
lines changed

5 files changed

+108
-30
lines changed

app/res/menu/file_view.xml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,20 @@
1616
-->
1717
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
1818

19-
<item
20-
android:id="@+id/m_wrap"
21-
android:showAsAction="never"
22-
android:title="@string/enable_wrapping"/>
23-
<item
24-
android:id="@+id/m_share"
25-
android:icon="@drawable/menu_share"
26-
android:showAsAction="never"
27-
android:title="@string/share"/>
19+
<item
20+
android:id="@+id/m_wrap"
21+
android:showAsAction="never"
22+
android:title="@string/enable_wrapping"/>
23+
<item
24+
android:id="@+id/m_render_markdown"
25+
android:enabled="false"
26+
android:showAsAction="never"
27+
android:title="@string/render_markdown"
28+
android:visible="false"/>
29+
<item
30+
android:id="@+id/m_share"
31+
android:icon="@drawable/menu_share"
32+
android:showAsAction="never"
33+
android:title="@string/share"/>
2834

2935
</menu>

app/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,5 +258,7 @@
258258
<string name="show_password">Show password</string>
259259
<string name="write">Write</string>
260260
<string name="preview">Preview</string>
261+
<string name="show_raw_markdown">Show raw markdown</string>
262+
<string name="render_markdown">Render markdown</string>
261263

262264
</resources>

app/src/main/java/com/github/mobile/ui/ref/BranchFileViewActivity.java

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.github.mobile.Intents.EXTRA_HEAD;
2020
import static com.github.mobile.Intents.EXTRA_PATH;
2121
import static com.github.mobile.Intents.EXTRA_REPOSITORY;
22+
import static com.github.mobile.util.PreferenceUtils.RENDER_MARKDOWN;
2223
import static com.github.mobile.util.PreferenceUtils.WRAP;
2324
import android.content.Intent;
2425
import android.os.Bundle;
@@ -112,12 +113,20 @@ public static Intent createIntent(Repository repository, String branch,
112113

113114
private String branch;
114115

116+
private boolean isMarkdownFile;
117+
118+
private String renderedMarkdown;
119+
120+
private Blob blob;
121+
115122
private ProgressBar loadingBar;
116123

117124
private WebView codeView;
118125

119126
private SourceEditor editor;
120127

128+
private MenuItem markdownItem;
129+
121130
@Inject
122131
private AvatarLoader avatars;
123132

@@ -139,6 +148,7 @@ protected void onCreate(Bundle savedInstanceState) {
139148
codeView = finder.find(id.wv_code);
140149

141150
file = CommitUtils.getName(path);
151+
isMarkdownFile = isMarkdown(file);
142152
editor = new SourceEditor(codeView);
143153
editor.setWrap(PreferenceUtils.getCodePreferences(this).getBoolean(
144154
WRAP, false));
@@ -161,6 +171,17 @@ public boolean onCreateOptionsMenu(final Menu optionsMenu) {
161171
else
162172
wrapItem.setTitle(string.enable_wrapping);
163173

174+
markdownItem = optionsMenu.findItem(id.m_render_markdown);
175+
if (isMarkdownFile) {
176+
markdownItem.setEnabled(blob != null);
177+
markdownItem.setVisible(true);
178+
if (PreferenceUtils.getCodePreferences(this).getBoolean(
179+
RENDER_MARKDOWN, true))
180+
markdownItem.setTitle(string.show_raw_markdown);
181+
else
182+
markdownItem.setTitle(string.render_markdown);
183+
}
184+
164185
return true;
165186
}
166187

@@ -181,6 +202,22 @@ public boolean onOptionsItemSelected(MenuItem item) {
181202
case id.m_share:
182203
shareFile();
183204
return true;
205+
case id.m_render_markdown:
206+
if (editor.isMarkdown()) {
207+
item.setTitle(string.render_markdown);
208+
editor.setMarkdown(false);
209+
editor.setSource(file, blob);
210+
} else {
211+
item.setTitle(string.show_raw_markdown);
212+
editor.setMarkdown(true);
213+
if (renderedMarkdown != null)
214+
editor.setSource(file, renderedMarkdown, false);
215+
else
216+
loadMarkdown();
217+
}
218+
PreferenceUtils.save(PreferenceUtils.getCodePreferences(this)
219+
.edit().putBoolean(RENDER_MARKDOWN, editor.isMarkdown()));
220+
return true;
184221
default:
185222
return super.onOptionsItemSelected(item);
186223
}
@@ -203,7 +240,12 @@ public void onLoadFinished(Loader<CharSequence> loader,
203240
ViewUtils.setGone(loadingBar, true);
204241
ViewUtils.setGone(codeView, false);
205242

206-
editor.setMarkdown(true).setSource(file, rendered.toString(), false);
243+
if (!TextUtils.isEmpty(rendered)) {
244+
renderedMarkdown = rendered.toString();
245+
if (markdownItem != null)
246+
markdownItem.setEnabled(true);
247+
editor.setMarkdown(true).setSource(file, renderedMarkdown, false);
248+
}
207249
}
208250

209251
@Override
@@ -216,23 +258,39 @@ private void shareFile() {
216258
"https://github.com/" + id + "/blob/" + branch + '/' + path));
217259
}
218260

261+
private void loadMarkdown() {
262+
ViewUtils.setGone(loadingBar, false);
263+
ViewUtils.setGone(codeView, true);
264+
265+
String markdown = new String(
266+
EncodingUtils.fromBase64(blob.getContent()));
267+
Bundle args = new Bundle();
268+
args.putCharSequence(ARG_TEXT, markdown);
269+
if (repo instanceof Serializable)
270+
args.putSerializable(ARG_REPO, (Serializable) repo);
271+
getSupportLoaderManager().restartLoader(0, args, this);
272+
}
273+
219274
private void loadContent() {
275+
ViewUtils.setGone(loadingBar, false);
276+
ViewUtils.setGone(codeView, true);
277+
220278
new RefreshBlobTask(repo, sha, this) {
221279

222280
@Override
223281
protected void onSuccess(Blob blob) throws Exception {
224282
super.onSuccess(blob);
225283

226-
if (isMarkdown(file)) {
227-
String markdown = new String(EncodingUtils.fromBase64(blob
228-
.getContent()));
229-
Bundle args = new Bundle();
230-
args.putCharSequence(ARG_TEXT, markdown);
231-
if (repo instanceof Serializable)
232-
args.putSerializable(ARG_REPO, (Serializable) repo);
233-
getSupportLoaderManager().restartLoader(0, args,
234-
BranchFileViewActivity.this);
235-
} else {
284+
if (markdownItem != null)
285+
markdownItem.setEnabled(true);
286+
287+
BranchFileViewActivity.this.blob = blob;
288+
if (isMarkdownFile
289+
&& PreferenceUtils.getCodePreferences(
290+
BranchFileViewActivity.this).getBoolean(
291+
RENDER_MARKDOWN, true))
292+
loadMarkdown();
293+
else {
236294
ViewUtils.setGone(loadingBar, true);
237295
ViewUtils.setGone(codeView, false);
238296

app/src/main/java/com/github/mobile/util/PreferenceUtils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public class PreferenceUtils {
3232
*/
3333
public static final String WRAP = "wrap";
3434

35+
/**
36+
* Preference to render markdown
37+
*/
38+
public static final String RENDER_MARKDOWN = "renderMarkdown";
39+
3540
/**
3641
* Get code browsing preferences
3742
*

app/src/main/java/com/github/mobile/util/SourceEditor.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ public boolean getWrap() {
118118
return wrap;
119119
}
120120

121+
/**
122+
* @return markdown
123+
*/
124+
public boolean isMarkdown() {
125+
return markdown;
126+
}
127+
121128
/**
122129
* Set whether lines should wrap
123130
*
@@ -126,12 +133,7 @@ public boolean getWrap() {
126133
*/
127134
public SourceEditor setWrap(final boolean wrap) {
128135
this.wrap = wrap;
129-
if (name != null && content != null) {
130-
if (markdown)
131-
view.loadData(content, "text/html", null);
132-
else
133-
view.loadUrl(URL_PAGE);
134-
}
136+
loadSource();
135137
return this;
136138
}
137139

@@ -159,14 +161,19 @@ public SourceEditor setSource(final String name, final String content,
159161
this.name = name;
160162
this.content = content;
161163
this.encoded = encoded;
162-
if (markdown)
163-
view.loadData(content, "text/html", null);
164-
else
165-
view.loadUrl(URL_PAGE);
164+
loadSource();
166165

167166
return this;
168167
}
169168

169+
private void loadSource() {
170+
if (name != null && content != null)
171+
if (markdown)
172+
view.loadData(content, "text/html", null);
173+
else
174+
view.loadUrl(URL_PAGE);
175+
}
176+
170177
/**
171178
* Bind blob content to current {@link WebView}
172179
*

0 commit comments

Comments
 (0)