package com.mick88.dittimetable.utils;

import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.view.View;
import android.widget.TextView;

import com.mick88.dittimetable.utils.ViewTraverser.ForeachAction;

/**
 * Applies selected font to the view and all views
 * @author Michal
 *
 */
public class FontApplicator
{
	final private Typeface font;
	
	public FontApplicator(Typeface font)
	{
		this.font = font;
	}
	
	public FontApplicator(AssetManager assets, String assetFontName)
	{
		this.font = Typeface.createFromAsset(assets, assetFontName);
	}
	
	/**
	 * Applies font to the view and/or its children
	 * @param root
	 * @return
	 */
	public FontApplicator applyFont(View root)
	{
		if (root == null) return this;
		new ViewTraverser(root).traverse(new ForeachAction<View>()
		{
			
			@Override
			public void onElement(View element)
			{
				if (element instanceof TextView)
				{
					((TextView) element).setTypeface(font);
				}
			}
		});
		
		return this;
	}
}

@@ -108,16 +108,57 @@
 peface;%0A
+import android.support.v4.util.LruCache;%0A
 import a
@@ -175,16 +175,16 @@
 w.View;%0A
-
 import a
@@ -390,14 +390,98 @@
 %0A%7B%0A%09
-final 
+private static LruCache%3CString, Typeface%3E fontCache = new LruCache%3CString, Typeface%3E(1);%0A%09
 priv
@@ -634,29 +634,90 @@
 tName)%0A%09%7B%0A%09%09
-this.
+font = fontCache.get(assetFontName); %0A%09%09if (font == null) %0A%09%09%7B%0A%09%09%09
 font = Typef
@@ -760,16 +760,59 @@
 tName);%0A
+%09%09%09fontCache.put(assetFontName, font);%0A%09%09%7D%0A
 %09%7D%0A%09%0A%09/*

Create font cache