<commit_before>/*
 * Copyright (c) 2019 toastkidjp.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
 */
package jp.toastkid.yobidashi.media.image.preview

import android.graphics.ColorMatrix
import android.graphics.ColorMatrixColorFilter

/**
 * @author toastkidjp
 */
class ColorFilterUseCase(private val viewModel: ImagePreviewFragmentViewModel) {

    private var lastFilter: ImageColorFilter? = null

    fun applyAlpha(alpha: Float) {
        lastFilter = null
        viewModel.newColorFilter(
                ColorMatrixColorFilter(
                        ColorMatrix().also {
                            it.set(
                                    floatArrayOf(
                                            1f,0f,0f,alpha,000f,
                                            0f,1f,0f,alpha,000f,
                                            0f,0f,1f,alpha,000f,
                                            0f,0f,0f,1f,000f
                                    )
                            )
                        }
                )
        )
    }

    fun reverseFilter() {
        applyFilter(ImageColorFilter.REVERSE)
    }

    fun sepia() {
        applyFilter(ImageColorFilter.SEPIA)
    }

    fun grayScale() {
        applyFilter(ImageColorFilter.GRAY_SCALE)
    }

    private fun applyFilter(imageColorFilter: ImageColorFilter) {
        if (lastFilter == imageColorFilter) {
            clearFilter()
            return
        }
        lastFilter = imageColorFilter
        viewModel.newColorFilter(imageColorFilter)
    }

    private fun clearFilter() {
        viewModel.clearFilter()
    }
}<commit_msg>bugfix #506 Clear last filter.<commit_after>/*
 * Copyright (c) 2019 toastkidjp.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
 */
package jp.toastkid.yobidashi.media.image.preview

import android.graphics.ColorMatrix
import android.graphics.ColorMatrixColorFilter

/**
 * @author toastkidjp
 */
class ColorFilterUseCase(private val viewModel: ImagePreviewFragmentViewModel) {

    private var lastFilter: ImageColorFilter? = null

    fun applyAlpha(alpha: Float) {
        lastFilter = null
        viewModel.newColorFilter(
                ColorMatrixColorFilter(
                        ColorMatrix().also {
                            it.set(
                                    floatArrayOf(
                                            1f,0f,0f,alpha,000f,
                                            0f,1f,0f,alpha,000f,
                                            0f,0f,1f,alpha,000f,
                                            0f,0f,0f,1f,000f
                                    )
                            )
                        }
                )
        )
    }

    fun reverseFilter() {
        applyFilter(ImageColorFilter.REVERSE)
    }

    fun sepia() {
        applyFilter(ImageColorFilter.SEPIA)
    }

    fun grayScale() {
        applyFilter(ImageColorFilter.GRAY_SCALE)
    }

    private fun applyFilter(imageColorFilter: ImageColorFilter) {
        if (lastFilter == imageColorFilter) {
            clearFilter()
            return
        }
        lastFilter = imageColorFilter
        viewModel.newColorFilter(imageColorFilter)
    }

    private fun clearFilter() {
        viewModel.clearFilter()
        lastFilter = null
    }
}
{'fraction_non_alphanumeric': 0.07408397967370955, 'fraction_numerical': 0.018454132120887937, 'mean_word_length': 1.5390359809911744, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7154018', 'n_tokens_mistral': 1001, 'n_tokens_neox': 921, 'n_words': 196}