diff --git a/README.md b/README.md index f3e3c340..22aae543 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,7 @@ Thanks to all the open source libraries, contributors and donators. - [ge0rg/MemorizingTrustManager](https://github.com/ge0rg/MemorizingTrustManager) - [mikaelhg/urlbuilder](https://github.com/mikaelhg/urlbuilder) - [ACRA/acra](https://github.com/ACRA/acra) +- [chrisvest/stormpot](https://github.com/chrisvest/stormpot) #### Icon sets - [feathericons/feather](https://github.com/feathericons/feather) diff --git a/app/build.gradle b/app/build.gradle index ac7dbf5d..103c84b0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -112,5 +112,6 @@ dependencies { implementation "org.codeberg.gitnex:tea4j:1.0.5" coreLibraryDesugaring "com.android.tools:desugar_jdk_libs:1.1.5" implementation 'androidx.biometric:biometric:1.1.0' + implementation 'com.github.chrisvest:stormpot:2.4.1' } 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 229ab3c8..80fa8cb0 100644 --- a/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java @@ -305,7 +305,7 @@ public class FileViewActivity extends BaseActivity implements BottomSheetFileVie if(!tinyDB.getBoolean("enableMarkdownInFileView")) { - new Markdown(ctx, EmojiParser.parseToUnicode(binding.contents.getContent()), binding.markdown); + Markdown.render(ctx, EmojiParser.parseToUnicode(binding.contents.getContent()), binding.markdown); binding.contents.setVisibility(View.GONE); binding.markdownFrame.setVisibility(View.VISIBLE); diff --git a/app/src/main/java/org/mian/gitnex/activities/IssueDetailActivity.java b/app/src/main/java/org/mian/gitnex/activities/IssueDetailActivity.java index 6a18e9a0..3760ab8a 100644 --- a/app/src/main/java/org/mian/gitnex/activities/IssueDetailActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/IssueDetailActivity.java @@ -582,7 +582,7 @@ public class IssueDetailActivity extends BaseActivity implements LabelsListAdapt viewBinding.issueTitle.setText(HtmlCompat.fromHtml(issueNumber_ + " " + EmojiParser.parseToUnicode(singleIssue.getTitle()), HtmlCompat.FROM_HTML_MODE_LEGACY)); String cleanIssueDescription = singleIssue.getBody().trim(); - new Markdown(ctx, EmojiParser.parseToUnicode(cleanIssueDescription), viewBinding.issueDescription); + Markdown.render(ctx, EmojiParser.parseToUnicode(cleanIssueDescription), viewBinding.issueDescription); RelativeLayout.LayoutParams paramsDesc = (RelativeLayout.LayoutParams) viewBinding.issueDescription.getLayoutParams(); diff --git a/app/src/main/java/org/mian/gitnex/adapters/DraftsAdapter.java b/app/src/main/java/org/mian/gitnex/adapters/DraftsAdapter.java index 5f2d04b5..a6db98d7 100644 --- a/app/src/main/java/org/mian/gitnex/adapters/DraftsAdapter.java +++ b/app/src/main/java/org/mian/gitnex/adapters/DraftsAdapter.java @@ -127,7 +127,7 @@ public class DraftsAdapter extends RecyclerView.Adapter()); - this.context = context; - this.markdown = markdown; - this.textView = textView; + private static final Pool rendererPool; - executorService.execute(new Renderer()); + static { + + Config config = new Config<>(); + + config.setBackgroundExpirationEnabled(true); + config.setPreciseLeakDetectionEnabled(true); + config.setSize(MAX_POOL_SIZE); + config.setAllocator(new Allocator() { + + @Override + public Renderer allocate(Slot slot) throws Exception { + return new Renderer(slot); + } + + @Override public void deallocate(Renderer poolable) throws Exception {} + + }); + + rendererPool = new BlazePool<>(config); } - private class Renderer implements Runnable { + public static void render(Context context, String markdown, TextView textView) { - @Override - public void run() { + try { + Renderer renderer = rendererPool.claim(timeout); + + if(renderer != null) { + renderer.setParameters(context, markdown, textView); + executorService.execute(renderer); + } + } catch(InterruptedException ignored) {} + } + + private static class Renderer implements Runnable, Poolable { + + private final Slot slot; + + private Markwon markwon; + + private Context context; + private String markdown; + private TextView textView; + + public Renderer(Slot slot) { + this.slot = slot; + } + + private void setup() { Prism4jTheme prism4jTheme = TinyDB.getInstance(context).getString("currentTheme").equals("dark") ? Prism4jThemeDarkula.create() : @@ -72,16 +122,56 @@ public class Markdown { @Override public void configureTheme(@NonNull MarkwonTheme.Builder builder) { builder.codeBlockTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/sourcecodeproregular.ttf")); + builder.codeBlockMargin((int) (context.getResources().getDisplayMetrics().density * 10)); + builder.blockMargin((int) (context.getResources().getDisplayMetrics().density * 10)); + builder.codeTextSize((int) (context.getResources().getDisplayMetrics().scaledDensity * 13)); builder.codeTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/sourcecodeproregular.ttf")); builder.linkColor(ResourcesCompat.getColor(context.getResources(), R.color.lightBlue, null)); } }); - Markwon markwon = builder.build(); - Spanned spanned = markwon.toMarkdown(markdown); + markwon = builder.build(); - textView.post(() -> markwon.setParsedMarkdown(textView, spanned)); + } + public void setParameters(Context context, String markdown, TextView textView) { + + this.context = context; + this.markdown = markdown; + this.textView = textView; + } + + @Override + public void run() { + + Objects.requireNonNull(context); + Objects.requireNonNull(markdown); + Objects.requireNonNull(textView); + + if(markwon == null) setup(); + + Spanned processedMarkdown = markwon.toMarkdown(markdown); + + TextView localReference = textView; + localReference.post(() -> localReference.setText(processedMarkdown)); + + release(); + + } + + @Override + public void release() { + + context = null; + markdown = null; + textView = null; + + slot.release(this); + + } + + public void expire() { + slot.expire(this); } } } diff --git a/app/src/main/java/org/mian/gitnex/helpers/SyntaxHighlightedArea.java b/app/src/main/java/org/mian/gitnex/helpers/views/SyntaxHighlightedArea.java similarity index 98% rename from app/src/main/java/org/mian/gitnex/helpers/SyntaxHighlightedArea.java rename to app/src/main/java/org/mian/gitnex/helpers/views/SyntaxHighlightedArea.java index 52686cfa..f7a65575 100644 --- a/app/src/main/java/org/mian/gitnex/helpers/SyntaxHighlightedArea.java +++ b/app/src/main/java/org/mian/gitnex/helpers/views/SyntaxHighlightedArea.java @@ -1,4 +1,4 @@ -package org.mian.gitnex.helpers; +package org.mian.gitnex.helpers.views; import android.content.Context; import android.graphics.Canvas; @@ -16,6 +16,8 @@ import androidx.annotation.ColorInt; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import org.mian.gitnex.core.MainGrammarLocator; +import org.mian.gitnex.helpers.AppUtil; +import org.mian.gitnex.helpers.TinyDB; import io.noties.markwon.syntax.Prism4jSyntaxHighlight; import io.noties.markwon.syntax.Prism4jTheme; import io.noties.markwon.syntax.Prism4jThemeDarkula; diff --git a/app/src/main/res/layout/activity_file_view.xml b/app/src/main/res/layout/activity_file_view.xml index 4b857eb3..bc0870e8 100644 --- a/app/src/main/res/layout/activity_file_view.xml +++ b/app/src/main/res/layout/activity_file_view.xml @@ -99,7 +99,7 @@ -