<commit_before>package com.kodroid.pilot.lib.android.uiTypeHandler;

import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;

import com.kodroid.pilot.lib.android.frameBacking.PilotFrameLayout;
import com.kodroid.pilot.lib.android.frameBacking.BackedByFrameUtils;
import com.kodroid.pilot.lib.stack.PilotFrame;
import com.kodroid.pilot.lib.sync.UITypeHandler;

import java.util.HashMap;
import java.util.Map;

public class UIViewTypeHandler implements UITypeHandler
{
    private final Displayer mDisplayer;
    private Map<Class<? extends PilotFrame>, Class<? extends PilotFrameLayout>> mFrameToViewMappings = new HashMap<>();

    //==================================================================//
    // Constructor
    //==================================================================//

    /**
     * @param topLevelViews An array of views that make up the main first level views of your app.
     * @param displayer A {@link UIViewTypeHandler.Displayer} that will
     *                  handle showing your views. You can use the provided
     *                  {@link UIViewTypeHandler.SimpleDisplayer} here if needed.
     */
    public UIViewTypeHandler(Class<? extends PilotFrameLayout>[] topLevelViews, Displayer displayer)
    {
        mDisplayer = displayer;
        setupRootViewAndPilotFrameMappings(topLevelViews);
    }

    //==================================================================//
    // UITypeHandler Interface
    //==================================================================//

    @SuppressWarnings("unchecked")
    @Override
    public boolean onFrame(PilotFrame frame)
    {
        Class<? extends PilotFrame> frameClass = frame.getClass();
        if(mFrameToViewMappings.containsKey(frameClass)) //does handle this frame type
        {
            if(mDisplayer.isViewVisibleForFrame(frameClass)) //view will always have a BackedByFrame set as set on creation and views not recreated unless inside a Fragment (not supporting PilotStack which is Fragment hosted atm)
                return true;
            else
            {
                Class<? extends PilotFrameLayout> viewClass = mFrameToViewMappings.get(frameClass);
                PilotFrameLayout newView = createView(viewClass);
                newView.setBackingPilotFrame(frame);
                newView.backingFrameSet(frame);
                mDisplayer.makeVisible(newView);
                return true;
            }
        }
        else
            return false;
    }

    /**
     * Will return true for all views by default. Subclasses should override and return false for any non-opaque, non-fullscreen views.
     *
     * @param frame
     * @return
     */
    @Override
    public boolean isFrameOpaque(PilotFrame frame) {
        return true;
    }

    @Override
    public void clearAllUI() {
        mDisplayer.clearAllUI();
    }

    //==================================================================//
    // Private
    //==================================================================//

    private void setupRootViewAndPilotFrameMappings(Class<? extends PilotFrameLayout>[] rootViews)
    {
        //get view classes that make up the root level of the app
        for(Class<? extends PilotFrameLayout> viewClass : rootViews)
        {
            if(!PilotFrameLayout.class.isAssignableFrom(viewClass))
                throw new RuntimeException("Passed class does not extend PilotFrameBackedFrameLayout:"+viewClass.getCanonicalName());
            Class<? extends PilotFrame> pilotFrameClass = BackedByFrameUtils.getPilotFrameClass(viewClass);
            mFrameToViewMappings.put(pilotFrameClass, viewClass);
        }
    }

    private <T extends PilotFrameLayout> T createView(Class<T> viewClass)
    {<commit_msg>Update UIViewTypeHandler.java<commit_after>package com.kodroid.pilot.lib.android.uiTypeHandler;

import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;

import com.kodroid.pilot.lib.android.frameBacking.PilotFrameLayout;
import com.kodroid.pilot.lib.android.frameBacking.BackedByFrameUtils;
import com.kodroid.pilot.lib.stack.PilotFrame;
import com.kodroid.pilot.lib.sync.UITypeHandler;

import java.util.HashMap;
import java.util.Map;

public class UIViewTypeHandler implements UITypeHandler
{
    private final Displayer mDisplayer;
    private Map<Class<? extends PilotFrame>, Class<? extends PilotFrameLayout>> mFrameToViewMappings = new HashMap<>();

    //==================================================================//
    // Constructor
    //==================================================================//

    /**
     * @param topLevelViews An array of views that make up the main first level views of your app.
     * @param displayer A {@link UIViewTypeHandler.Displayer} that will
     *                  handle showing your views. You can use the provided
     *                  {@link UIViewTypeHandler.SimpleDisplayer} here if needed.
     */
    public UIViewTypeHandler(Class<? extends PilotFrameLayout>[] topLevelViews, Displayer displayer)
    {
        mDisplayer = displayer;
        setupRootViewAndPilotFrameMappings(topLevelViews);
    }

    //==================================================================//
    // UITypeHandler Interface
    //==================================================================//

    @SuppressWarnings("unchecked")
    @Override
    public boolean onFrame(PilotFrame frame)
    {
        Class<? extends PilotFrame> frameClass = frame.getClass();
        if(mFrameToViewMappings.containsKey(frameClass)) //does handle this frame type
        {
            if(mDisplayer.isViewVisibleForFrame(frameClass)) //view will always have a BackedByFrame set as set on creation and views not recreated unless inside a Fragment (not supporting PilotStack which is Fragment hosted atm)
                return true;
            else
            {
                Class<? extends PilotFrameLayout> viewClass = mFrameToViewMappings.get(frameClass);
                PilotFrameLayout newView = createView(viewClass);
                newView.setBackingPilotFrame(frame);
                newView.backingFrameSet(frame);
                mDisplayer.makeVisible(newView);
                return true;
            }
        }
        else
            return false;
    }

    /**
     * Will return true for all views by default. Subclasses should override and return false for any non-opaque, non-fullscreen views.
     *
     * @param frame
     * @return
     */
    @Override
    public boolean isFrameOpaque(PilotFrame frame) {
        return true;
    }

    @Override
    public void clearAllUI() {
        mDisplayer.clearAllUI();
    }

    //==================================================================//
    // Private
    //==================================================================//

    private void setupRootViewAndPilotFrameMappings(Class<? extends PilotFrameLayout>[] rootViews)
    {
        //get view classes that make up the root level of the app
        for(Class<? extends PilotFrameLayout> viewClass : rootViews)
        {
            if(!PilotFrameLayout.class.isAssignableFrom(viewClass))
                throw new RuntimeException("Passed class does not extend PilotFrameLayout:"+viewClass.getCanonicalName());
            Class<? extends PilotFrame> pilotFrameClass = BackedByFrameUtils.getPilotFrameClass(viewClass);
            mFrameToViewMappings.put(pilotFrameClass, viewClass);
        }
    }

    private <T extends PilotFrameLayout> T createView(Class<T> viewClass)
    {
{'fraction_non_alphanumeric': 0.17896945063589878, 'fraction_numerical': 0.0, 'mean_word_length': 3.085698982324585, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3026097', 'n_tokens_mistral': 1984, 'n_tokens_neox': 1863, 'n_words': 471}