package io.github.droidkaigi.confsched.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.databinding.DataBindingUtil;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;

import io.github.droidkaigi.confsched.R;
import io.github.droidkaigi.confsched.databinding.ViewSettingSwitchRowBinding;
import io.github.droidkaigi.confsched.util.PrefUtil;

public class SettingSwitchRowView extends RelativeLayout {

    private static final String TAG = SettingSwitchRowView.class.getSimpleName();

    private ViewSettingSwitchRowBinding binding;

    private String prefKey;

    public SettingSwitchRowView(Context context) {
        this(context, null);
    }

    public SettingSwitchRowView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SettingSwitchRowView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.view_setting_switch_row, this, true);

        if (!isInEditMode()) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SettingSwitchRow);

            String title = a.getString(R.styleable.SettingSwitchRow_settingTitle);
            String description = a.getString(R.styleable.SettingSwitchRow_settingDescription);

            binding.settingTitle.setText(title);
            binding.settingDescription.setText(description);

            binding.getRoot().setOnClickListener(v -> switchSetting());
            binding.settingSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> setSetting(isChecked));

            a.recycle();
        }
    }

    public void init(String prefKey, boolean defaultValue) {
        this.prefKey = prefKey;
        boolean isChecked = PrefUtil.get(getContext(), prefKey, defaultValue);
        binding.settingSwitch.setChecked(isChecked);
    }

    private void setSetting(boolean isChecked) {
        if (TextUtils.isEmpty(prefKey)) {
            Log.d(TAG, "PrefKey must be set. Call init()");
        } else {
            PrefUtil.put(getContext(), prefKey, isChecked);
        }
    }

    private void switchSetting() {
        if (TextUtils.isEmpty(prefKey)) {
            Log.d(TAG, "PrefKey must be set. Call init()");
        } else {
            boolean newValule = !PrefUtil.get(getContext(), prefKey, true);
            setSetting(newValule);
            binding.settingSwitch.setChecked(newValule);
        }
    }

}

@@ -282,16 +282,87 @@
 flater;%0A
+import android.widget.Checkable;%0Aimport android.widget.CompoundButton;%0A
 import a
@@ -623,16 +623,37 @@
 eLayout 
+implements Checkable 
 %7B%0A%0A    p
@@ -2722,10 +2722,1157 @@
 %0A    %7D%0A%0A
+    public void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener) %7B%0A        binding.settingSwitch.setOnCheckedChangeListener(listener);%0A    %7D%0A%0A    @Override%0A    public void setEnabled(boolean enabled) %7B%0A        super.setEnabled(enabled);%0A        binding.getRoot().setEnabled(enabled);%0A        binding.settingSwitch.setEnabled(enabled);%0A        if (enabled) %7B%0A            binding.settingTitle.setTextColor(getResources().getColor(R.color.black));%0A            binding.settingDescription.setTextColor(getResources().getColor(R.color.grey600));%0A        %7D else %7B%0A            int disabledTextColor = getResources().getColor(R.color.black_alpha_30);%0A            binding.settingTitle.setTextColor(disabledTextColor);%0A            binding.settingDescription.setTextColor(disabledTextColor);%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void setChecked(boolean checked) %7B%0A        setSetting(checked);%0A        binding.settingSwitch.setChecked(checked);%0A    %7D%0A%0A    @Override%0A    public boolean isChecked() %7B%0A        return binding.settingSwitch.isChecked();%0A    %7D%0A%0A    @Override%0A    public void toggle() %7B%0A        switchSetting();%0A    %7D%0A
 %7D%0A

Add methods in SettingSwitchRowView