Android Open Source - wally Filter Dialog Fragment






From Project

Back to project page wally.

License

The source code is released under:

Apache License

If you think the Android project wally listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2014 Freddie (Musenkishi) Lust-Hed
 */*  w w  w . j a  v  a2s.c  om*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.musenkishi.wally.fragments;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.musenkishi.wally.R;
import com.musenkishi.wally.base.WallyApplication;
import com.musenkishi.wally.models.Filter;
import com.musenkishi.wally.models.ListFilterGroup;
import com.musenkishi.wally.models.Size;
import com.musenkishi.wally.models.filters.FilterAspectRatioKeys;
import com.musenkishi.wally.models.filters.FilterBoards;
import com.musenkishi.wally.models.filters.FilterBoardsKeys;
import com.musenkishi.wally.models.filters.FilterPurity;
import com.musenkishi.wally.models.filters.FilterPurityKeys;
import com.musenkishi.wally.models.filters.FilterResolutionKeys;

/**
 * DialogFragment responsible for showing applicable filters to the user.
 *
 * Created by Musenkishi on 2014-03-11.
 */
public class FilterDialogFragment extends MaterialDialogFragment {

    public static final String TAG = "FilterDialogFragment";

    private static final String STATE_HAS_ANYTHING_CHANGED = TAG + ".state.hasAnythingChanged";
    private static final int NUM_OF_RETRIES = 2;

    private Spinner spinnerAspectRatio;
    private CheckBox checkBoxBoardGeneral;
    private CheckBox checkBoxBoardAnime;
    private CheckBox checkBoxBoardPeople;
    private CheckBox checkBoxPuritySFW;
    private CheckBox checkBoxPuritySketchy;
    private Spinner spinnerResolution;

    private boolean hasAnythingChanged = false;

    private int numberOfRetriesOnCategory = 0;
    private int numberOfRetriesOnRating = 0;

    private CompoundButton.OnCheckedChangeListener categoryCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (!hasAtLeastOneCategoryChecked()){
                compoundButton.setChecked(true);
                numberOfRetriesOnCategory++;
                if (numberOfRetriesOnCategory == NUM_OF_RETRIES) {
                    Toast.makeText(compoundButton.getContext(), R.string.you_must_have_at_least_one_category_checked, Toast.LENGTH_SHORT).show();
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            numberOfRetriesOnCategory = 0;
                        }
                    }, 2 * 1000);
                }
            }
        }
    };

    private CompoundButton.OnCheckedChangeListener ratingCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (!hasAtLeastOneRatingChecked()){
                compoundButton.setChecked(true);
                numberOfRetriesOnRating++;
                if (numberOfRetriesOnRating == NUM_OF_RETRIES) {
                    Toast.makeText(compoundButton.getContext(), R.string.you_must_have_at_least_one_rating_checked, Toast.LENGTH_SHORT).show();
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            numberOfRetriesOnRating = 0;
                        }
                    }, 2 * 1000);
                }
            }
        }
    };

    public FilterDialogFragment(){
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        if (savedInstanceState != null
                && savedInstanceState.containsKey(STATE_HAS_ANYTHING_CHANGED)) {
            hasAnythingChanged = savedInstanceState.getBoolean(STATE_HAS_ANYTHING_CHANGED);
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        if (getActivity() != null) {

            setContentView(R.layout.dialog_content_filter);
            final Dialog dialog = super.onCreateDialog(savedInstanceState);

            checkBoxBoardGeneral = (CheckBox) dialog.findViewById(R.id.filter_boards_general);
            checkBoxBoardAnime = (CheckBox) dialog.findViewById(R.id.filter_boards_anime);
            checkBoxBoardPeople = (CheckBox) dialog.findViewById(R.id.filter_boards_high_resolution);
            checkBoxPuritySFW = (CheckBox) dialog.findViewById(R.id.filter_purity_sfw);
            checkBoxPuritySketchy = (CheckBox) dialog.findViewById(R.id.filter_purity_sketchy);
            spinnerAspectRatio = (Spinner) dialog.findViewById(R.id.filter_aspect_ratio_spinner);
            spinnerResolution = (Spinner) dialog.findViewById(R.id.filter_resolution_spinner);

            int[] titleIds = new int[]{
                    R.id.filter_title_categories,
                    R.id.filter_title_rating,
                    R.id.filter_title_aspect_ratio,
                    R.id.filter_title_resolution
            };
            colorizeTextViews(titleIds, dialog);

            setupFilterViews();
            return dialog;
        } else {
            return null;
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        saveChanges();
        outState.putBoolean(STATE_HAS_ANYTHING_CHANGED, hasAnythingChanged);
        super.onSaveInstanceState(outState);
    }

    private void colorizeTextViews(int[] resourceIds, Dialog dialog) {
        for (int id : resourceIds) {
            TextView textView = (TextView) dialog.findViewById(id);
            textView.setTextColor(getPrimaryColor());
        }
    }

    private void setupFilterViews() {
        setupBoardCheckBoxes();
        setupPurityCheckBoxes();
        setupAspectRatioSpinner();
        setupResolutionSpinner();
    }

    private void setupBoardCheckBoxes() {
        FilterBoards filterBoards = new FilterBoards(WallyApplication.getDataProviderInstance().getBoards(FilterBoardsKeys.PARAMETER_KEY));
        checkBoxBoardGeneral.setChecked(filterBoards.isGeneralChecked());
        checkBoxBoardAnime.setChecked(filterBoards.isAnimeChecked());
        checkBoxBoardPeople.setChecked(filterBoards.isPeopleChecked());
        checkBoxBoardGeneral.setOnCheckedChangeListener(categoryCheckedChangeListener);
        checkBoxBoardAnime.setOnCheckedChangeListener(categoryCheckedChangeListener);
        checkBoxBoardPeople.setOnCheckedChangeListener(categoryCheckedChangeListener);
    }

    private void setupPurityCheckBoxes() {
        FilterPurity filterPurity = new FilterPurity(WallyApplication.getDataProviderInstance().getPurity(FilterPurityKeys.PARAMETER_KEY));
        checkBoxPuritySFW.setChecked(filterPurity.isSfwChecked());
        checkBoxPuritySketchy.setChecked(filterPurity.isSketchyChecked());
        checkBoxPuritySFW.setOnCheckedChangeListener(ratingCheckedChangeListener);
        checkBoxPuritySketchy.setOnCheckedChangeListener(ratingCheckedChangeListener);
    }

    private void setupAspectRatioSpinner() {
        ListFilterGroup aspectRatioFilterGroup = new ListFilterGroup(FilterAspectRatioKeys.PARAMETER_KEY, FilterAspectRatioKeys.getOrderedList());
        ArrayAdapter filterAdapter = new ArrayAdapter(getActivity(), R.layout.view_filter_list_item, aspectRatioFilterGroup.getFilters());
        spinnerAspectRatio.setAdapter(filterAdapter);
        Filter<String, String> defaultAspectRatio = WallyApplication.getDataProviderInstance().getAspectRatio(aspectRatioFilterGroup.getTag());
        int defaultAspectRationPosition = aspectRatioFilterGroup.getFilters().indexOf(defaultAspectRatio);
        spinnerAspectRatio.setSelection(defaultAspectRationPosition);
    }

    private void setupResolutionSpinner() {
        final ListFilterGroup resolutionFilterGroup = new ListFilterGroup(FilterResolutionKeys.PARAMETER_KEY, FilterResolutionKeys.getOrderedList());
        Filter<String, String> defaultResolution = WallyApplication.getDataProviderInstance().getResolution(resolutionFilterGroup.getTag());
        if (defaultResolution.isCustom()){
            defaultResolution.setKey(defaultResolution.getValue() + "");
            FilterResolutionKeys.RES_CUSTOM.setKey(defaultResolution.getKey());
            FilterResolutionKeys.RES_CUSTOM.setValue(defaultResolution.getValue());
        }
        final ArrayAdapter filterAdapter = new ArrayAdapter(getActivity(), R.layout.view_filter_list_item, resolutionFilterGroup.getFilters());
        spinnerResolution.setAdapter(filterAdapter);
        int defaultResolutionPosition = resolutionFilterGroup.getFilters().indexOf(defaultResolution);
        spinnerResolution.setSelection(defaultResolutionPosition, false);
        spinnerResolution.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if (resolutionFilterGroup.getFilter(i).isCustom()) {
                    final CustomResolutionDialogFragment customResolutionDialogFragment = new CustomResolutionDialogFragment();
                    customResolutionDialogFragment.setPrimaryColor(getPrimaryColor());
                    customResolutionDialogFragment.setTitle(R.string.dialog_custom_resolution_title);
                    customResolutionDialogFragment.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Size size = customResolutionDialogFragment.getSize();
                            FilterResolutionKeys.RES_CUSTOM.setKey(size + "");
                            FilterResolutionKeys.RES_CUSTOM.setValue(size.toString());
                            filterAdapter.notifyDataSetChanged();
                        }
                    });
                    customResolutionDialogFragment.setNegativeButton(R.string.cancel, null);
                    customResolutionDialogFragment.show(getFragmentManager(), CustomResolutionDialogFragment.TAG);
                } else {
                    //Clear custom filter
                    FilterResolutionKeys.RES_CUSTOM.setKey("Custom");
                    FilterResolutionKeys.RES_CUSTOM.setValue("");
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                Log.d(TAG, "onNothingSelected");
            }
        });
    }

    /**
     * Returns true if any filters has been changed.
     */
    public boolean saveChanges() {

        FilterBoards currentFilterBoards = new FilterBoards(checkBoxBoardGeneral.isChecked(), checkBoxBoardAnime.isChecked(), checkBoxBoardPeople.isChecked());
        FilterBoards savedFilterBoards = new FilterBoards(WallyApplication.getDataProviderInstance().getBoards(FilterBoardsKeys.PARAMETER_KEY));
        if (!currentFilterBoards.equals(savedFilterBoards)){
            WallyApplication.getDataProviderInstance().setBoards(FilterBoardsKeys.PARAMETER_KEY, currentFilterBoards.getFormattedValue());
            hasAnythingChanged = true;
        }

        FilterPurity currentFilterPurity = new FilterPurity(checkBoxPuritySFW.isChecked(), checkBoxPuritySketchy.isChecked());
        FilterPurity savedFilterPurity = new FilterPurity(WallyApplication.getDataProviderInstance().getPurity(FilterPurityKeys.PARAMETER_KEY));
        if (!currentFilterPurity.equals(savedFilterPurity)){
            WallyApplication.getDataProviderInstance().setPurity(FilterPurityKeys.PARAMETER_KEY, currentFilterPurity.getFormattedValue());
            hasAnythingChanged = true;
        }

        Filter<String, String> filterAspectRatio = (Filter<String, String>) spinnerAspectRatio.getSelectedItem();
        if (!WallyApplication.getDataProviderInstance().getAspectRatio(FilterAspectRatioKeys.PARAMETER_KEY).equals(filterAspectRatio)){
            WallyApplication.getDataProviderInstance().setAspectRatio(FilterAspectRatioKeys.PARAMETER_KEY, filterAspectRatio);
            hasAnythingChanged = true;
        }

        Filter<String, String> filterResolution = (Filter<String, String>) spinnerResolution.getSelectedItem();
        if (!WallyApplication.getDataProviderInstance().getResolution(FilterResolutionKeys.PARAMETER_KEY).equals(filterResolution)){
            WallyApplication.getDataProviderInstance().setResolution(FilterResolutionKeys.PARAMETER_KEY, filterResolution);
            hasAnythingChanged = true;
        }

        return hasAnythingChanged;
    }

    private boolean hasAtLeastOneRatingChecked() {
        return checkBoxPuritySFW.isChecked() || checkBoxPuritySketchy.isChecked();
    }

    private boolean hasAtLeastOneCategoryChecked() {
        return checkBoxBoardGeneral.isChecked() ||
                checkBoxBoardAnime.isChecked() ||
                checkBoxBoardPeople.isChecked();
    }
}




Java Source Code List

com.musenkishi.wally.activities.ImageDetailsActivity.java
com.musenkishi.wally.activities.MainActivity.java
com.musenkishi.wally.adapters.RecyclerImagesAdapter.java
com.musenkishi.wally.adapters.RecyclerSavedImagesAdapter.java
com.musenkishi.wally.adapters.SmartFragmentPagerAdapter.java
com.musenkishi.wally.adapters.SmartFragmentStatePagerAdapter.java
com.musenkishi.wally.anim.BaseItemAnimator.java
com.musenkishi.wally.anim.ScaleInOutItemAnimator.java
com.musenkishi.wally.anim.interpolator.EaseInOutBezierInterpolator.java
com.musenkishi.wally.anim.interpolator.FastOutLinearInInterpolator.java
com.musenkishi.wally.anim.interpolator.FastOutSlowInInterpolator.java
com.musenkishi.wally.anim.interpolator.LinearOutSlowInInterpolator.java
com.musenkishi.wally.base.BaseActivity.java
com.musenkishi.wally.base.BaseFragment.java
com.musenkishi.wally.base.GridFragment.java
com.musenkishi.wally.base.WallyApplication.java
com.musenkishi.wally.dataprovider.DataProvider.java
com.musenkishi.wally.dataprovider.FileManager.java
com.musenkishi.wally.dataprovider.NetworkDataProvider.java
com.musenkishi.wally.dataprovider.SharedPreferencesDataProvider.java
com.musenkishi.wally.dataprovider.models.DataProviderError.java
com.musenkishi.wally.dataprovider.models.SaveImageRequest.java
com.musenkishi.wally.dataprovider.okhttp.OkHttpStreamFetcher.java
com.musenkishi.wally.dataprovider.okhttp.OkHttpUrlLoader.java
com.musenkishi.wally.dataprovider.util.Parser.java
com.musenkishi.wally.fragments.CustomResolutionDialogFragment.java
com.musenkishi.wally.fragments.FilterDialogFragment.java
com.musenkishi.wally.fragments.ImageZoomFragment.java
com.musenkishi.wally.fragments.LatestFragment.java
com.musenkishi.wally.fragments.MaterialDialogFragment.java
com.musenkishi.wally.fragments.RandomImagesFragment.java
com.musenkishi.wally.fragments.SavedImagesFragment.java
com.musenkishi.wally.fragments.SearchFragment.java
com.musenkishi.wally.fragments.ToplistFragment.java
com.musenkishi.wally.models.Author.java
com.musenkishi.wally.models.ExceptionReporter.java
com.musenkishi.wally.models.Filter.java
com.musenkishi.wally.models.ImagePage.java
com.musenkishi.wally.models.Image.java
com.musenkishi.wally.models.ListFilterGroup.java
com.musenkishi.wally.models.Rating.java
com.musenkishi.wally.models.SavedImageData.java
com.musenkishi.wally.models.Size.java
com.musenkishi.wally.models.Tag.java
com.musenkishi.wally.models.filters.FilterAspectRatioKeys.java
com.musenkishi.wally.models.filters.FilterBoardsKeys.java
com.musenkishi.wally.models.filters.FilterBoards.java
com.musenkishi.wally.models.filters.FilterGroup.java
com.musenkishi.wally.models.filters.FilterGroupsStructure.java
com.musenkishi.wally.models.filters.FilterPurityKeys.java
com.musenkishi.wally.models.filters.FilterPurity.java
com.musenkishi.wally.models.filters.FilterResOptKeys.java
com.musenkishi.wally.models.filters.FilterResOpt.java
com.musenkishi.wally.models.filters.FilterResolutionKeys.java
com.musenkishi.wally.models.filters.FilterTimeSpanKeys.java
com.musenkishi.wally.muzei.WallyArtSource.java
com.musenkishi.wally.notification.NotificationProvider.java
com.musenkishi.wally.observers.FileChangeReceiver.java
com.musenkishi.wally.observers.FiltersChangeReceiver.java
com.musenkishi.wally.util.Blur.java
com.musenkishi.wally.util.PaletteLoader.java
com.musenkishi.wally.util.PaletteRequest.java
com.musenkishi.wally.util.SparseBooleanArrayParcelable.java
com.musenkishi.wally.util.TextClickableSpan.java
com.musenkishi.wally.util.TextLinkBuilder.java
com.musenkishi.wally.util.TypefaceSpan.java
com.musenkishi.wally.views.AutoGridView.java
com.musenkishi.wally.views.GridRecyclerView.java
com.musenkishi.wally.views.ObservableScrollView.java
com.musenkishi.wally.views.TabBarView.java
com.musenkishi.wally.views.TabView.java
com.musenkishi.wally.views.swipeclearlayout.SwipeClearLayout.java
net.margaritov.preference.colorpicker.dialog.ColorPickerDialogFragment.java
net.margaritov.preference.colorpicker.drawable.AlphaPatternDrawable.java
net.margaritov.preference.colorpicker.preference.ColorPickerPreference.java
net.margaritov.preference.colorpicker.view.ColorPanelView.java
net.margaritov.preference.colorpicker.view.ColorPickerView.java
nl.codesoup.cubicbezier.CubicBezierInterpolator.java