diff --git a/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java b/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java index bc703f45..00db6405 100644 --- a/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java @@ -8,6 +8,7 @@ import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; +import android.text.Spanned; import android.text.method.ScrollingMovementMethod; import android.util.Base64; import android.util.Log; @@ -42,7 +43,24 @@ import java.io.IOException; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; +import java.util.Collection; +import java.util.Collections; import java.util.Objects; +import io.noties.markwon.AbstractMarkwonPlugin; +import io.noties.markwon.Markwon; +import io.noties.markwon.core.CorePlugin; +import io.noties.markwon.core.MarkwonTheme; +import io.noties.markwon.ext.strikethrough.StrikethroughPlugin; +import io.noties.markwon.ext.tables.TablePlugin; +import io.noties.markwon.ext.tasklist.TaskListPlugin; +import io.noties.markwon.html.HtmlPlugin; +import io.noties.markwon.image.DefaultMediaDecoder; +import io.noties.markwon.image.ImageItem; +import io.noties.markwon.image.ImagesPlugin; +import io.noties.markwon.image.SchemeHandler; +import io.noties.markwon.image.gif.GifMediaDecoder; +import io.noties.markwon.image.svg.SvgMediaDecoder; +import io.noties.markwon.linkify.LinkifyPlugin; import retrofit2.Call; import retrofit2.Callback; @@ -65,6 +83,9 @@ public class FileViewActivity extends BaseActivity implements BottomSheetFileVie private LinearLayout pdfViewFrame; private byte[] decodedPdf; private Boolean pdfNightMode; + private String singleFileName; + private AppUtil appUtil; + private TinyDB tinyDb; @Override protected int getLayoutResourceId() { @@ -77,11 +98,12 @@ public class FileViewActivity extends BaseActivity implements BottomSheetFileVie super.onCreate(savedInstanceState); appCtx = getApplicationContext(); + appUtil = new AppUtil(); + tinyDb = new TinyDB(appCtx); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); - final TinyDB tinyDb = new TinyDB(appCtx); String repoFullName = tinyDb.getString("repoFullName"); String repoBranch = tinyDb.getString("repoBranch"); String[] parts = repoFullName.split("/"); @@ -91,6 +113,8 @@ public class FileViewActivity extends BaseActivity implements BottomSheetFileVie final String loginUid = tinyDb.getString("loginUid"); final String instanceToken = "token " + tinyDb.getString(loginUid + "-token"); + tinyDb.putBoolean("enableMarkdownInFileView", false); + ImageView closeActivity = findViewById(R.id.close); singleFileContents = findViewById(R.id.singleFileContents); singleCodeContents = findViewById(R.id.singleCodeContents); @@ -100,7 +124,7 @@ public class FileViewActivity extends BaseActivity implements BottomSheetFileVie pdfViewFrame = findViewById(R.id.pdfViewFrame); singleFileContentsFrame = findViewById(R.id.singleFileContentsFrame); - String singleFileName = getIntent().getStringExtra("singleFileName"); + singleFileName = getIntent().getStringExtra("singleFileName"); TextView toolbar_title = findViewById(R.id.toolbar_title); toolbar_title.setMovementMethod(new ScrollingMovementMethod()); @@ -119,7 +143,6 @@ public class FileViewActivity extends BaseActivity implements BottomSheetFileVie } catch(UnsupportedEncodingException e) { - assert singleFileName != null; Log.i("singleFileName", singleFileName); } @@ -132,8 +155,6 @@ public class FileViewActivity extends BaseActivity implements BottomSheetFileVie private void getSingleFileContents(String instanceUrl, String token, final String owner, String repo, final String filename, String ref) { - final TinyDB tinyDb = new TinyDB(appCtx); - Call call = RetrofitClient.getInstance(instanceUrl, ctx).getApiInterface().getSingleFileContents(token, owner, repo, filename, ref); call.enqueue(new Callback() { @@ -143,7 +164,6 @@ public class FileViewActivity extends BaseActivity implements BottomSheetFileVie if(response.code() == 200) { - AppUtil appUtil = new AppUtil(); assert response.body() != null; if(!response.body().getContent().equals("")) { @@ -277,6 +297,13 @@ public class FileViewActivity extends BaseActivity implements BottomSheetFileVie MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.generic_nav_dotted_menu, menu); + inflater.inflate(R.menu.files_view_menu, menu); + + String fileExtension = FileUtils.getExtension(singleFileName); + if(!fileExtension.equalsIgnoreCase("md")) { + menu.getItem(0).setVisible(false); + } + return true; } @@ -287,11 +314,82 @@ public class FileViewActivity extends BaseActivity implements BottomSheetFileVie switch(id) { case android.R.id.home: + finish(); return true; case R.id.genericMenu: + BottomSheetFileViewerFragment bottomSheet = new BottomSheetFileViewerFragment(); bottomSheet.show(getSupportFragmentManager(), "fileViewerBottomSheet"); + return true; + case R.id.markdown: + + final Markwon markwon = Markwon.builder(Objects.requireNonNull(ctx)).usePlugin(CorePlugin.create()) + .usePlugin(ImagesPlugin.create(plugin -> { + plugin.addSchemeHandler(new SchemeHandler() { + + @NonNull + @Override + public ImageItem handle(@NonNull String raw, @NonNull Uri uri) { + + final int resourceId = ctx.getResources().getIdentifier( + raw.substring("drawable://".length()), + "drawable", + ctx.getPackageName()); + + final Drawable drawable = ctx.getDrawable(resourceId); + + assert drawable != null; + return ImageItem.withResult(drawable); + } + + @NonNull + @Override + public Collection supportedSchemes() { + + return Collections.singleton("drawable"); + } + }); + plugin.placeholderProvider(drawable -> null); + plugin.addMediaDecoder(GifMediaDecoder.create(false)); + plugin.addMediaDecoder(SvgMediaDecoder.create(ctx.getResources())); + plugin.addMediaDecoder(SvgMediaDecoder.create()); + plugin.defaultMediaDecoder(DefaultMediaDecoder.create(ctx.getResources())); + plugin.defaultMediaDecoder(DefaultMediaDecoder.create()); + })) + .usePlugin(new AbstractMarkwonPlugin() { + @Override + public void configureTheme(@NonNull MarkwonTheme.Builder builder) { + + builder.codeTextColor(tinyDb.getInt("codeBlockColor")).codeBackgroundColor(tinyDb.getInt("codeBlockBackground")) + .linkColor(getResources().getColor(R.color.lightBlue)); + } + }) + .usePlugin(TablePlugin.create(ctx)) + .usePlugin(TaskListPlugin.create(ctx)) + .usePlugin(HtmlPlugin.create()) + .usePlugin(StrikethroughPlugin.create()) + .usePlugin(LinkifyPlugin.create()) + .build(); + + if(!tinyDb.getBoolean("enableMarkdownInFileView")) { + + singleCodeContents.setVisibility(View.GONE); + singleFileContentsFrame.setVisibility(View.VISIBLE); + singleFileContents.setVisibility(View.VISIBLE); + Spanned bodyWithMD = markwon.toMarkdown(appUtil.decodeBase64(tinyDb.getString("downloadFileContents"))); + markwon.setParsedMarkdown(singleFileContents, bodyWithMD); + tinyDb.putBoolean("enableMarkdownInFileView", true); + } + else { + + singleCodeContents.setVisibility(View.VISIBLE); + singleFileContentsFrame.setVisibility(View.GONE); + singleFileContents.setVisibility(View.GONE); + singleCodeContents.setSource(appUtil.decodeBase64(tinyDb.getString("downloadFileContents"))); + tinyDb.putBoolean("enableMarkdownInFileView", false); + } + return true; default: return super.onOptionsItemSelected(item); @@ -312,8 +410,6 @@ public class FileViewActivity extends BaseActivity implements BottomSheetFileVie private void requestFileDownload() { - final TinyDB tinyDb = new TinyDB(appCtx); - if(!tinyDb.getString("downloadFileContents").isEmpty()) { int CREATE_REQUEST_CODE = 40; @@ -340,9 +436,7 @@ public class FileViewActivity extends BaseActivity implements BottomSheetFileVie super.onActivityResult(requestCode, resultCode, data); - final TinyDB tinyDb = new TinyDB(appCtx); - - if (requestCode == 40 && resultCode == RESULT_OK) { + if(requestCode == 40 && resultCode == RESULT_OK) { try { @@ -361,7 +455,7 @@ public class FileViewActivity extends BaseActivity implements BottomSheetFileVie Toasty.info(ctx, getString(R.string.downloadFileSaved)); } - catch (IOException e) { + catch(IOException e) { Log.e("errorFileDownloading", Objects.requireNonNull(e.getMessage())); } diff --git a/app/src/main/res/drawable/ic_markdown.xml b/app/src/main/res/drawable/ic_markdown.xml new file mode 100644 index 00000000..2317f368 --- /dev/null +++ b/app/src/main/res/drawable/ic_markdown.xml @@ -0,0 +1,4 @@ + + + diff --git a/app/src/main/res/menu/files_view_menu.xml b/app/src/main/res/menu/files_view_menu.xml new file mode 100644 index 00000000..f991130b --- /dev/null +++ b/app/src/main/res/menu/files_view_menu.xml @@ -0,0 +1,12 @@ + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b722aeed..62e2121f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -465,6 +465,7 @@ Filter Branches + Markdown Copy Issue URL URL copied to clipboard