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' |     compile 'com.google.dagger:dagger:2.10' | ||||||
|     annotationProcessor 'com.google.dagger:dagger-compiler: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; | package ru.noties.markwon; | ||||||
| 
 | 
 | ||||||
| import android.content.Context; | import android.content.Context; | ||||||
|  | import android.graphics.Color; | ||||||
|  | import android.graphics.Typeface; | ||||||
| import android.net.Uri; | import android.net.Uri; | ||||||
| import android.os.Handler; | import android.os.Handler; | ||||||
| import android.os.SystemClock; | import android.os.SystemClock; | ||||||
|  | import android.support.annotation.FontRes; | ||||||
| import android.support.annotation.NonNull; | import android.support.annotation.NonNull; | ||||||
| import android.support.annotation.Nullable; | import android.support.annotation.Nullable; | ||||||
|  | import android.support.v4.content.res.ResourcesCompat; | ||||||
| 
 | 
 | ||||||
| import java.util.concurrent.ExecutorService; | import java.util.concurrent.ExecutorService; | ||||||
| import java.util.concurrent.Future; | import java.util.concurrent.Future; | ||||||
| @ -14,13 +18,14 @@ import javax.inject.Inject; | |||||||
| 
 | 
 | ||||||
| import ru.noties.debug.Debug; | import ru.noties.debug.Debug; | ||||||
| import ru.noties.markwon.spans.AsyncDrawable; | 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 | @ActivityScope | ||||||
| public class MarkdownRenderer { | public class MarkdownRenderer { | ||||||
| 
 | 
 | ||||||
|     interface MarkdownReadyListener { |     private final Context context; | ||||||
|         void onMarkdownReady(CharSequence markdown); |  | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
|     @Inject |     @Inject | ||||||
|     AsyncDrawable.Loader loader; |     AsyncDrawable.Loader loader; | ||||||
| @ -34,7 +39,8 @@ public class MarkdownRenderer { | |||||||
|     private Future<?> task; |     private Future<?> task; | ||||||
| 
 | 
 | ||||||
|     @Inject |     @Inject | ||||||
|     MarkdownRenderer() { |     MarkdownRenderer(Context context) { | ||||||
|  |         this.context = context; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public void render( |     public void render( | ||||||
| @ -55,6 +61,7 @@ public class MarkdownRenderer { | |||||||
|                 } |                 } | ||||||
| 
 | 
 | ||||||
|                 final SpannableConfiguration configuration = SpannableConfiguration.builder(context) |                 final SpannableConfiguration configuration = SpannableConfiguration.builder(context) | ||||||
|  |                     .theme(getSpannableTheme()) | ||||||
|                     .asyncDrawableLoader(loader) |                     .asyncDrawableLoader(loader) | ||||||
|                     .urlProcessor(urlProcessor) |                     .urlProcessor(urlProcessor) | ||||||
|                     .build(); |                     .build(); | ||||||
| @ -92,4 +99,25 @@ public class MarkdownRenderer { | |||||||
|     private boolean isCancelled() { |     private boolean isCancelled() { | ||||||
|         return task == null || task.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
	 Daniel Leal
						Daniel Leal