19
19
import static com .github .mobile .Intents .EXTRA_HEAD ;
20
20
import static com .github .mobile .Intents .EXTRA_PATH ;
21
21
import static com .github .mobile .Intents .EXTRA_REPOSITORY ;
22
+ import static com .github .mobile .util .PreferenceUtils .RENDER_MARKDOWN ;
22
23
import static com .github .mobile .util .PreferenceUtils .WRAP ;
23
24
import android .content .Intent ;
24
25
import android .os .Bundle ;
@@ -112,12 +113,20 @@ public static Intent createIntent(Repository repository, String branch,
112
113
113
114
private String branch ;
114
115
116
+ private boolean isMarkdownFile ;
117
+
118
+ private String renderedMarkdown ;
119
+
120
+ private Blob blob ;
121
+
115
122
private ProgressBar loadingBar ;
116
123
117
124
private WebView codeView ;
118
125
119
126
private SourceEditor editor ;
120
127
128
+ private MenuItem markdownItem ;
129
+
121
130
@ Inject
122
131
private AvatarLoader avatars ;
123
132
@@ -139,6 +148,7 @@ protected void onCreate(Bundle savedInstanceState) {
139
148
codeView = finder .find (id .wv_code );
140
149
141
150
file = CommitUtils .getName (path );
151
+ isMarkdownFile = isMarkdown (file );
142
152
editor = new SourceEditor (codeView );
143
153
editor .setWrap (PreferenceUtils .getCodePreferences (this ).getBoolean (
144
154
WRAP , false ));
@@ -161,6 +171,17 @@ public boolean onCreateOptionsMenu(final Menu optionsMenu) {
161
171
else
162
172
wrapItem .setTitle (string .enable_wrapping );
163
173
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
+
164
185
return true ;
165
186
}
166
187
@@ -181,6 +202,22 @@ public boolean onOptionsItemSelected(MenuItem item) {
181
202
case id .m_share :
182
203
shareFile ();
183
204
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 ;
184
221
default :
185
222
return super .onOptionsItemSelected (item );
186
223
}
@@ -203,7 +240,12 @@ public void onLoadFinished(Loader<CharSequence> loader,
203
240
ViewUtils .setGone (loadingBar , true );
204
241
ViewUtils .setGone (codeView , false );
205
242
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
+ }
207
249
}
208
250
209
251
@ Override
@@ -216,23 +258,39 @@ private void shareFile() {
216
258
"https://github.com/" + id + "/blob/" + branch + '/' + path ));
217
259
}
218
260
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
+
219
274
private void loadContent () {
275
+ ViewUtils .setGone (loadingBar , false );
276
+ ViewUtils .setGone (codeView , true );
277
+
220
278
new RefreshBlobTask (repo , sha , this ) {
221
279
222
280
@ Override
223
281
protected void onSuccess (Blob blob ) throws Exception {
224
282
super .onSuccess (blob );
225
283
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 {
236
294
ViewUtils .setGone (loadingBar , true );
237
295
ViewUtils .setGone (codeView , false );
238
296
0 commit comments