Adapt example app to show heading configuration options
This commit is contained in:
		
							parent
							
								
									11f072edc2
								
							
						
					
					
						commit
						f9b98405c7
					
				@ -36,4 +36,6 @@ dependencies {
 | 
			
		||||
 | 
			
		||||
    compile 'com.google.dagger:dagger:2.10'
 | 
			
		||||
    annotationProcessor 'com.google.dagger:dagger-compiler:2.10'
 | 
			
		||||
 | 
			
		||||
    implementation "com.android.support:appcompat-v7:27.0.2"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -1,11 +1,15 @@
 | 
			
		||||
package ru.noties.markwon;
 | 
			
		||||
 | 
			
		||||
import android.content.Context;
 | 
			
		||||
import android.graphics.Color;
 | 
			
		||||
import android.graphics.Typeface;
 | 
			
		||||
import android.net.Uri;
 | 
			
		||||
import android.os.Handler;
 | 
			
		||||
import android.os.SystemClock;
 | 
			
		||||
import android.support.annotation.FontRes;
 | 
			
		||||
import android.support.annotation.NonNull;
 | 
			
		||||
import android.support.annotation.Nullable;
 | 
			
		||||
import android.support.v4.content.res.ResourcesCompat;
 | 
			
		||||
 | 
			
		||||
import java.util.concurrent.ExecutorService;
 | 
			
		||||
import java.util.concurrent.Future;
 | 
			
		||||
@ -14,13 +18,14 @@ import javax.inject.Inject;
 | 
			
		||||
 | 
			
		||||
import ru.noties.debug.Debug;
 | 
			
		||||
import ru.noties.markwon.spans.AsyncDrawable;
 | 
			
		||||
import ru.noties.markwon.spans.SpannableTheme;
 | 
			
		||||
import ru.noties.markwon.spans.heading.HeadingConfig;
 | 
			
		||||
import ru.noties.markwon.spans.heading.HeadingTypeConfig;
 | 
			
		||||
 | 
			
		||||
@ActivityScope
 | 
			
		||||
public class MarkdownRenderer {
 | 
			
		||||
 | 
			
		||||
    interface MarkdownReadyListener {
 | 
			
		||||
        void onMarkdownReady(CharSequence markdown);
 | 
			
		||||
    }
 | 
			
		||||
    private final Context context;
 | 
			
		||||
 | 
			
		||||
    @Inject
 | 
			
		||||
    AsyncDrawable.Loader loader;
 | 
			
		||||
@ -34,14 +39,15 @@ public class MarkdownRenderer {
 | 
			
		||||
    private Future<?> task;
 | 
			
		||||
 | 
			
		||||
    @Inject
 | 
			
		||||
    MarkdownRenderer() {
 | 
			
		||||
    MarkdownRenderer(Context context) {
 | 
			
		||||
        this.context = context;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void render(
 | 
			
		||||
            @NonNull final Context context,
 | 
			
		||||
            @Nullable final Uri uri,
 | 
			
		||||
            @NonNull final String markdown,
 | 
			
		||||
            @NonNull final MarkdownReadyListener listener) {
 | 
			
		||||
        @NonNull final Context context,
 | 
			
		||||
        @Nullable final Uri uri,
 | 
			
		||||
        @NonNull final String markdown,
 | 
			
		||||
        @NonNull final MarkdownReadyListener listener) {
 | 
			
		||||
        cancel();
 | 
			
		||||
        task = service.submit(new Runnable() {
 | 
			
		||||
            @Override
 | 
			
		||||
@ -55,9 +61,10 @@ public class MarkdownRenderer {
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                final SpannableConfiguration configuration = SpannableConfiguration.builder(context)
 | 
			
		||||
                        .asyncDrawableLoader(loader)
 | 
			
		||||
                        .urlProcessor(urlProcessor)
 | 
			
		||||
                        .build();
 | 
			
		||||
                    .theme(getSpannableTheme())
 | 
			
		||||
                    .asyncDrawableLoader(loader)
 | 
			
		||||
                    .urlProcessor(urlProcessor)
 | 
			
		||||
                    .build();
 | 
			
		||||
 | 
			
		||||
                final long start = SystemClock.uptimeMillis();
 | 
			
		||||
 | 
			
		||||
@ -92,4 +99,25 @@ public class MarkdownRenderer {
 | 
			
		||||
    private boolean isCancelled() {
 | 
			
		||||
        return task == null || task.isCancelled();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private HeadingConfig getHeadingConfig() {
 | 
			
		||||
        final HeadingTypeConfig h1 = new HeadingTypeConfig(-1, Color.RED, getTypeface(R.font.opensans_semibold));
 | 
			
		||||
        final HeadingTypeConfig h2 = new HeadingTypeConfig(-1, Color.BLUE, getTypeface(R.font.opensans_regular));
 | 
			
		||||
 | 
			
		||||
        return new HeadingConfig(h1, h2);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private SpannableTheme getSpannableTheme() {
 | 
			
		||||
        return SpannableTheme.builderWithDefaults(context)
 | 
			
		||||
            .headingConfig(getHeadingConfig(), context.getResources().getDisplayMetrics().density)
 | 
			
		||||
            .build();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private Typeface getTypeface(@FontRes int font){
 | 
			
		||||
        return ResourcesCompat.getFont(context, font);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    interface MarkdownReadyListener {
 | 
			
		||||
        void onMarkdownReady(CharSequence markdown);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										25
									
								
								app/src/main/res/font/app_font.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								app/src/main/res/font/app_font.xml
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,25 @@
 | 
			
		||||
<?xml version="1.0" encoding="utf-8"?>
 | 
			
		||||
<font-family
 | 
			
		||||
    xmlns:android="http://schemas.android.com/apk/res/android"
 | 
			
		||||
    xmlns:app="http://schemas.android.com/apk/res-auto">
 | 
			
		||||
 | 
			
		||||
    <!-- Regular -->
 | 
			
		||||
    <font
 | 
			
		||||
        android:font="@font/opensans_regular"
 | 
			
		||||
        android:fontStyle="normal"
 | 
			
		||||
        android:fontWeight="400"
 | 
			
		||||
 | 
			
		||||
        app:font="@font/opensans_regular"
 | 
			
		||||
        app:fontStyle="normal"
 | 
			
		||||
        app:fontWeight="400"/>
 | 
			
		||||
 | 
			
		||||
    <!-- Semi-bold -->
 | 
			
		||||
    <font
 | 
			
		||||
        android:font="@font/opensans_semibold"
 | 
			
		||||
        android:fontStyle="normal"
 | 
			
		||||
        android:fontWeight="700"
 | 
			
		||||
 | 
			
		||||
        app:font="@font/opensans_semibold"
 | 
			
		||||
        app:fontStyle="normal"
 | 
			
		||||
        app:fontWeight="700"/>
 | 
			
		||||
</font-family>
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								app/src/main/res/font/opensans_regular.ttf
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								app/src/main/res/font/opensans_regular.ttf
									
									
									
									
									
										Executable file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								app/src/main/res/font/opensans_semibold.ttf
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								app/src/main/res/font/opensans_semibold.ttf
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user