<commit_before>package org.commcare.android.adapters;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import org.commcare.android.view.SquareButtonWithNotification;
import org.commcare.dalvik.R;

import java.util.HashMap;

/**
 * Created by dancluna on 3/19/15.
 */
public class HomeScreenAdapter extends BaseAdapter {
    //region Buttons

    static final int[] buttonsResources = new int[]{
            R.layout.home_start_button,
            R.layout.home_savedforms_button,
            R.layout.home_incompleteforms_button,
            R.layout.home_sync_button,
            R.layout.home_disconnect_button,
    };

    static final HashMap<Integer, Integer> buttonsIDsToResources = new HashMap<Integer, Integer>() {{
                put(R.id.home_start_sqbn,R.layout.home_start_button);
                put(R.id.home_savedforms_sqbn,R.layout.home_savedforms_button);
                put(R.id.home_sync_sqbn,R.layout.home_sync_button);
                put(R.id.home_disconnect_sqbn,R.layout.home_disconnect_button);
                put(R.id.home_incompleteforms_sqbn,R.layout.home_incompleteforms_button);
    }};

    //endregion

    //region Private variables

    final View.OnClickListener[] buttonListeners = new View.OnClickListener[buttonsResources.length];

    final SquareButtonWithNotification[] buttons = new SquareButtonWithNotification[buttonsResources.length];

    private Context context;

    private boolean[] hiddenButtons = new boolean[buttonsResources.length];

    //endregion

    //region Constructors

    public HomeScreenAdapter(Context c) { this.context = c; }

    //endregion

    //region Public API

    public void setOnClickListenerForButton(int androidCode, boolean lookupID, View.OnClickListener listener){
        int buttonIndex = getButtonIndex(androidCode, lookupID);
        setOnClickListenerForButton(buttonIndex, listener);
    }

    public SquareButtonWithNotification getButton(int androidCode, boolean lookupID){
        return buttons[getButtonIndex(androidCode, lookupID)];
    }

    public void setNotificationTextForButton(int androidCode, boolean lookupID, String notificationText) {
        SquareButtonWithNotification button = getButton(androidCode, lookupID);
        if (button != null) button.setNotificationText(notificationText);
    }

    @Override
    public int getCount() {
        return buttonsResources.length;
    }

    @Override
    public Object getItem(int position) {
        return buttons[position];
    }

    @Override
    public long getItemId(int position) {
        return buttonsResources[position];
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(position < 0 || position >= getCount()) return null;
        if(convertView != null) {
            return convertView;
        } else {
            // going to assume we already created at least 1 button, if not, we'll just create them all here...
            SquareButtonWithNotification view = buttons[position];
            if(view == null) {
                Log.i("HomeScrnAdpt","Creating all buttons because got a null in position " + position);
                for (int i = 0; i < buttons.length; i++) {
                    if (buttons[i] != null) continue;
                    SquareButtonWithNotification button = (SquareButtonWithNotification) LayoutInflater.from(context)
                            .inflate(buttonsResources[i], parent, false);
                    buttons[i] = button;
                    Log.i("HomeScrnAdpt","Added button " + button + "to position " + i);

                    View.OnClickListener listener = buttonListeners[i];
                    // creating now, but set a clickListener before, so we'll add it to this button...
                    if(listener != null) {
                        button.setOnClickListener(listener);
                        Log.i("HomeScrnAdpt","Added onClickListener " + listener + " to button in position " + i);
                    }
                    if( i == position ) view = button;
                }
            }

            return view;
        }
    }

    public void setButtonVisibility(int androidCode, boolean lookupID, boolean isButtonHidden){
        hiddenButtons[getButtonIndex(androidCode, lookupID)] = isButtonHidden;
    }

    //endregion

    //region Private methods

    private void setOnClickListenerForButton(int buttonIndex, View.OnClickListener listener) {
        buttonListeners[buttonIndex] = listener;
        SquareButtonWithNotification button = (SquareButtonWithNotification) getItem(buttonIndex);
        if(button != null){
            button.setOnClickListener(listener);
        }
    }<commit_msg>Changes to make hiding buttons work (REC 2.0 now works)<commit_after>package org.commcare.android.adapters;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import org.commcare.android.view.SquareButtonWithNotification;
import org.commcare.dalvik.R;

import java.util.HashMap;
import java.util.LinkedList;

/**
 * Created by dancluna on 3/19/15.
 */
public class HomeScreenAdapter extends BaseAdapter {
    //region Buttons

    static final int[] buttonsResources = new int[]{
            R.layout.home_start_button,
            R.layout.home_savedforms_button,
            R.layout.home_incompleteforms_button,
            R.layout.home_sync_button,
            R.layout.home_disconnect_button,
    };

    static final HashMap<Integer, Integer> buttonsIDsToResources = new HashMap<Integer, Integer>() {{
                put(R.id.home_start_sqbn,R.layout.home_start_button);
                put(R.id.home_savedforms_sqbn,R.layout.home_savedforms_button);
                put(R.id.home_sync_sqbn,R.layout.home_sync_button);
                put(R.id.home_disconnect_sqbn,R.layout.home_disconnect_button);
                put(R.id.home_incompleteforms_sqbn,R.layout.home_incompleteforms_button);
    }};

    //endregion

    //region Private variables

    final View.OnClickListener[] buttonListeners = new View.OnClickListener[buttonsResources.length];

    final SquareButtonWithNotification[] buttons = new SquareButtonWithNotification[buttonsResources.length];

    private Context context;

    private boolean[] hiddenButtons = new boolean[buttonsResources.length];
    private boolean isInitialized = false;

    private LinkedList<SquareButtonWithNotification> visibleButtons;

    //endregion

    //region Constructors

    public HomeScreenAdapter(Context c) { this.context = c; }

    //endregion

    //region Public API

    public void setOnClickListenerForButton(int androidCode, boolean lookupID, View.OnClickListener listener){
        int buttonIndex = getButtonIndex(androidCode, lookupID);
        setOnClickListenerForButton(buttonIndex, listener);
    }

    public SquareButtonWithNotification getButton(int androidCode, boolean lookupID){
        return buttons[getButtonIndex(androidCode, lookupID)];
    }

    public void setNotificationTextForButton(int androidCode, boolean lookupID, String notificationText) {
        SquareButtonWithNotification button = getButton(androidCode, lookupID);
        if (button != null) button.setNotificationText(notificationText);
    }

    @Override
    public int getCount() {
//        return buttonsResources.length;
        return visibleButtons == null ? buttonsResources.length : visibleButtons.size();
    }

    @Override
    public Object getItem(int position) {
        return buttons[position];
    }

    @Override
    public long getItemId(int position) {
        return buttonsResources[position];
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        if(!isInitialized){
            visibleButtons = new LinkedList<>();
            Log.i("HomeScrnAdpt","Creating all buttons because got a null in position " + position);
            for (int i = 0; i < buttons.length; i++) {
                if (buttons[i] != null) continue;
                SquareButtonWithNotification button = (SquareButtonWithNotification) LayoutInflater.from(context)
                        .inflate(buttonsResources[i], parent, false);
                buttons[i] = button;
                Log.i("HomeScrnAdpt","Added button " + button + "to position " + i);

                View.OnClickListener listener = buttonListeners[i];
                // creating now, but set a clickListener before, so we'll add it to this button...
                if(listener != null) {
                    button.setOnClickListener(listener);
                    Log.i("HomeScrnAdpt","Added onClickListener " + listener + " to button in position " + i);
                }
                if( i == position ) view = button;
                if(!hiddenButtons[i]) visibleButtons.add(button);
            }
            isInitialized = true;
        }
        if(position < 0 || position >= getCount()) return null;
        if(convertView != null) {
            return convertView;
        } else {
            SquareButtonWithNotification btn = visibleButtons.get(position);

            if(btn == null) {
                Log.i("HomeScrnAdpt","Unexpected null button");
            }

            return btn;
        }
    }

    public void setButtonVisibility(int androidCode, boolean lookupID, boolean isButtonHidden){
        int index = getButtonIndex(androidCode, lookupID);
        boolean toggled = isButtonHidden ^ hiddenButtons[index];
        hiddenButtons[index] = isButtonHidden;
        if (visibleButtons != null) {
            if(!toggled) return;
            if(isButtonHidden) {
                visibleButtons.remove(buttons[index]);
            } else {
                visibleButtons.add(index, buttons[index]);
            }
        }
    }

    //endregion

    //region Private methods

    private void setOnClickListenerForButton(int buttonIndex, View.OnClickListener listener) {
        buttonListeners[buttonIndex] = listener;
        SquareButtonWithNotification button = (SquareButtonWithNotification) getItem(buttonIndex);
        if(button != null){
            button.setOnClickListener(listener);
        }
    }
{'fraction_non_alphanumeric': 0.09021186846898667, 'fraction_numerical': 0.0016297574537436487, 'mean_word_length': 2.6437303527768075, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, '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': '4553390', 'n_tokens_mistral': 2721, 'n_tokens_neox': 2618, 'n_words': 621}