<commit_before>package com.marverenic.music.viewmodel;

import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.AppCompatEditText;
import android.text.InputType;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import com.android.databinding.library.baseAdapters.BR;
import com.marverenic.music.JockeyApplication;
import com.marverenic.music.R;
import com.marverenic.music.data.store.MusicStore;
import com.marverenic.music.data.store.PlaylistStore;
import com.marverenic.music.instances.Album;
import com.marverenic.music.instances.Artist;
import com.marverenic.music.instances.Genre;
import com.marverenic.music.instances.Playlist;
import com.marverenic.music.instances.Song;
import com.marverenic.music.instances.playlistrules.AutoPlaylistRule;
import com.marverenic.music.instances.playlistrules.RuleEnumeration;

import java.util.Calendar;
import java.util.Formatter;
import java.util.List;

import javax.inject.Inject;

import rx.Subscription;

import static android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE;

public class RuleViewModel extends BaseObservable {

    private static final String TAG = "RuleViewModel";

    private Context mContext;

    @Inject MusicStore mMusicStore;
    @Inject PlaylistStore mPlaylistStore;

    private AutoPlaylistRule.Factory mFactory;
    private RuleEnumeration mEnumeratedRule;

    private FilterAdapter mFilterAdapter;
    private ValueAdapter<?> mValueAdapter;
    private Subscription mValueSubscription;

    private OnRemovalListener mRemovalListener;
    private List<AutoPlaylistRule> mRules;
    private int mIndex;

    public RuleViewModel(Context context) {
        mContext = context;
        JockeyApplication.getComponent(context).inject(this);
        mEnumeratedRule = RuleEnumeration.IS;
    }

    public void setRule(List<AutoPlaylistRule> rules, int index) {
        if (rules.equals(mRules) && index == mIndex) {
            return;
        }

        mRules = rules;
        mIndex = index;
        mFactory = new AutoPlaylistRule.Factory(rules.get(index));
        mEnumeratedRule = RuleEnumeration.from(mFactory.getField(), mFactory.getMatch());

        if (mFilterAdapter != null) {
            mFilterAdapter.onTypeChanged();
        }

        setupValueAdapter();

        // Setup everything but the value spinner (since the adapter has to be made asynchronously)
        notifyPropertyChanged(BR.selectedType);
        notifyPropertyChanged(BR.selectedFilter);
        notifyPropertyChanged(BR.valueSpinnerVisibility);
        notifyPropertyChanged(BR.valueTextVisibility);
        notifyPropertyChanged(BR.valueText);
    }

    public void setOnRemovalListener(OnRemovalListener listener) {
        mRemovalListener = listener;
    }

    private void apply() {
        mRules.set(mIndex, mFactory.build());
    }

    @Bindable
    public int getSelectedType() {
        return mFactory.getType();
    }

    public Spinner.OnItemSelectedListener getTypeSelectedListener() {
        return new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                if (mFactory.getType() == pos) {
                    return;
                }

                mFactory.setType(pos);

                apply();
                mEnumeratedRule = RuleEnumeration.from(mFactory.getField(), mFactory.getMatch());
                mFilterAdapter.onTypeChanged();

                setupValueAdapter();

                notifyPropertyChanged(BR.valueTextVisibility);
                notifyPropertyChanged(BR.valueSpinnerVisibility);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {}
        };
    }

    @Bindable
    public int getSelectedFilter() {
        RuleEnumeration[] values = RuleEnumeration.values();

        for (int i = 0; i < values.length; i++) {
            RuleEnumeration filter = values[i];
            if (filter.equals(mEnumeratedRule)) {
                return i;
            }
        }
        return -1;
    }

    public Spinner.OnItemSelectedListener getFilterSelectedListener() {
        return new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                mEnumeratedRule = mFilterAdapter.mFilters.get(pos);

                if (mFactory.getField() == mEnumeratedRule.getField()
                        && mFactory.getMatch() == mEnumeratedRule.getMatch()) {
                    return;
                }

                mFactory.setField(mEnumeratedRule.getField());
                mFactory.setMatch(mEnumeratedRule.getMatch());
                apply();

                setupValueAdapter();

                notifyPropertyChanged(BR.valueTextVisibility);<commit_msg>Reset value when input type changes<commit_after>package com.marverenic.music.viewmodel;

import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.AppCompatEditText;
import android.text.InputType;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import com.android.databinding.library.baseAdapters.BR;
import com.marverenic.music.JockeyApplication;
import com.marverenic.music.R;
import com.marverenic.music.data.store.MusicStore;
import com.marverenic.music.data.store.PlaylistStore;
import com.marverenic.music.instances.Album;
import com.marverenic.music.instances.Artist;
import com.marverenic.music.instances.Genre;
import com.marverenic.music.instances.Playlist;
import com.marverenic.music.instances.Song;
import com.marverenic.music.instances.playlistrules.AutoPlaylistRule;
import com.marverenic.music.instances.playlistrules.RuleEnumeration;

import java.util.Calendar;
import java.util.Formatter;
import java.util.List;

import javax.inject.Inject;

import rx.Subscription;

import static android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE;

public class RuleViewModel extends BaseObservable {

    private static final String TAG = "RuleViewModel";

    private Context mContext;

    @Inject MusicStore mMusicStore;
    @Inject PlaylistStore mPlaylistStore;

    private AutoPlaylistRule.Factory mFactory;
    private RuleEnumeration mEnumeratedRule;

    private FilterAdapter mFilterAdapter;
    private ValueAdapter<?> mValueAdapter;
    private Subscription mValueSubscription;

    private OnRemovalListener mRemovalListener;
    private List<AutoPlaylistRule> mRules;
    private int mIndex;

    public RuleViewModel(Context context) {
        mContext = context;
        JockeyApplication.getComponent(context).inject(this);
        mEnumeratedRule = RuleEnumeration.IS;
    }

    public void setRule(List<AutoPlaylistRule> rules, int index) {
        if (rules.equals(mRules) && index == mIndex) {
            return;
        }

        mRules = rules;
        mIndex = index;
        mFactory = new AutoPlaylistRule.Factory(rules.get(index));
        mEnumeratedRule = RuleEnumeration.from(mFactory.getField(), mFactory.getMatch());

        if (mFilterAdapter != null) {
            mFilterAdapter.onTypeChanged();
        }

        setupValueAdapter();

        // Setup everything but the value spinner (since the adapter has to be made asynchronously)
        notifyPropertyChanged(BR.selectedType);
        notifyPropertyChanged(BR.selectedFilter);
        notifyPropertyChanged(BR.valueSpinnerVisibility);
        notifyPropertyChanged(BR.valueTextVisibility);
        notifyPropertyChanged(BR.valueText);
    }

    public void setOnRemovalListener(OnRemovalListener listener) {
        mRemovalListener = listener;
    }

    private void apply() {
        mRules.set(mIndex, mFactory.build());
    }

    @Bindable
    public int getSelectedType() {
        return mFactory.getType();
    }

    public Spinner.OnItemSelectedListener getTypeSelectedListener() {
        return new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                if (mFactory.getType() == pos) {
                    return;
                }

                mFactory.setType(pos);

                apply();
                mEnumeratedRule = RuleEnumeration.from(mFactory.getField(), mFactory.getMatch());
                mFilterAdapter.onTypeChanged();

                setupValueAdapter();

                notifyPropertyChanged(BR.valueTextVisibility);
                notifyPropertyChanged(BR.valueSpinnerVisibility);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {}
        };
    }

    @Bindable
    public int getSelectedFilter() {
        RuleEnumeration[] values = RuleEnumeration.values();

        for (int i = 0; i < values.length; i++) {
            RuleEnumeration filter = values[i];
            if (filter.equals(mEnumeratedRule)) {
                return i;
            }
        }
        return -1;
    }

    public Spinner.OnItemSelectedListener getFilterSelectedListener() {
        return new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                RuleEnumeration nextRule = mFilterAdapter.mFilters.get(pos);

                if (nextRule.getField() == mEnumeratedRule.getField()
                        && nextRule.getMatch() == mEnumeratedRule.getMatch()) {
                    return;
                }

                if (nextRule.getInputType() != mEnumeratedRule.getInputType()) {
                    mFactory.setValue("");
                }

                mEnumeratedRule = nextRule;
                mFactory.setField(mEnumeratedRule.getField());
                mFactory.setMatch(mEnumeratedRule.getMatch());
                apply();

                setupValueAdapter();

                notifyPropertyChanged(BR.valueText);
                notifyPropertyChanged(BR.valueTextVisibility);
{'fraction_non_alphanumeric': 0.08944893069482651, 'fraction_numerical': 0.000709912148371639, 'mean_word_length': 2.9336823734729496, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, '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': '2404710', 'n_tokens_mistral': 3102, 'n_tokens_neox': 2994, 'n_words': 582}