package ca.antonious.viewcelladapter.viewcells.builtins;

import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import ca.antonious.viewcelladapter.R;
import ca.antonious.viewcelladapter.annotations.BindListener;
import ca.antonious.viewcelladapter.internal.Function;
import ca.antonious.viewcelladapter.viewcells.AbstractViewCell;
import ca.antonious.viewcelladapter.viewcells.BaseViewHolder;

/**
 * Created by George on 2017-04-26.
 */

public class MaterialToggleSettingViewCell extends AbstractViewCell<MaterialToggleSettingViewCell.MaterialToggleSettingViewHolder> {
    private int settingId;
    private String settingName;
    private boolean isChecked;

    public MaterialToggleSettingViewCell(int settingId,
                                         String settingName,
                                         boolean isChecked)
    {
        this.settingId = settingId;
        this.settingName = settingName;
        this.isChecked = isChecked;
    }

    @Override
    public int getLayoutId() {
        return R.layout.material_toggle_setting_list_item;
    }

    @Override
    public int getItemId() {
        return settingId;
    }

    @Override
    public Function<View, BaseViewHolder> getViewHolderFactory() {
        return new Function<View, BaseViewHolder>() {
            @Override
            public BaseViewHolder apply(View view) {
                return new MaterialToggleSettingViewHolder(view);
            }
        };
    }

    @Override
    public void bindViewCell(MaterialToggleSettingViewHolder viewHolder) {
        viewHolder.setSettingName(settingName);
        viewHolder.setSwitchState(isChecked);
    }

    public interface OnSettingToggledListener {
        void onSettingToggled(int settingId, boolean isOn);
    }

    @BindListener(bindIfNull = true)
    public void bindOnSettingToggledListener(final MaterialToggleSettingViewHolder viewHolder, final OnSettingToggledListener listener) {
        viewHolder.setOnBackgroundClickedListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toggleSetting();
                viewHolder.setSwitchState(isChecked);
            }
        });

        viewHolder.setOnSwitchStateChangedListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
                if (listener != null) {
                    listener.onSettingToggled(settingId, checked);
                }
            }
        });
    }

    public void toggleSetting() {
        isChecked = !isChecked;
    }

    public class MaterialToggleSettingViewHolder extends BaseViewHolder {
        private TextView settingNameTextView;
        private Switch settingSwitch;

        public MaterialToggleSettingViewHolder(View itemView) {
            super(itemView);
            settingNameTextView = (TextView) itemView.findViewById(R.id.setting_name);
            settingSwitch = (Switch) itemView.findViewById(R.id.setting_switch);
        }

        public void setSettingName(String settingName) {
            settingNameTextView.setText(settingName);
        }

        public void setSwitchState(boolean isChecked) {
            settingSwitch.setChecked(isChecked);
        }

        public void setOnBackgroundClickedListener(View.OnClickListener onClickListener) {
            itemView.setOnClickListener(onClickListener);
        }

        public void setOnSwitchStateChangedListener(CompoundButton.OnCheckedChangeListener onSwitchStateChangedListener) {
            settingSwitch.setOnCheckedChangeListener(onSwitchStateChangedListener);
        }
    }
}

@@ -2157,41 +2157,8 @@
 ) %7B%0A
-                toggleSetting();%0A
     
@@ -2187,32 +2187,33 @@
 .setSwitchState(
+!
 isChecked);%0A    
@@ -2438,32 +2438,69 @@
 lean checked) %7B%0A
+                isChecked = checked;%0A
                 
@@ -2645,81 +2645,8 @@
  %7D%0A%0A
-    public void toggleSetting() %7B%0A        isChecked = !isChecked;%0A    %7D%0A%0A
     

Fix event handling logic in toggle setting view cel