<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.app.Dialog
import android.content.ContentResolver
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AlertDialog
import androidx.core.graphics.drawable.toBitmap
import androidx.core.net.toUri
import androidx.core.os.bundleOf
import androidx.core.view.isVisible
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.LinearSnapHelper
import jp.toastkid.lib.ContentViewModel
import jp.toastkid.lib.preference.PreferenceApplier
import jp.toastkid.yobidashi.R
import jp.toastkid.yobidashi.databinding.DialogImagePreviewBinding
import jp.toastkid.yobidashi.media.image.Image
import jp.toastkid.yobidashi.media.image.preview.attach.AttachMenuPopup
import jp.toastkid.yobidashi.media.image.preview.attach.AttachToAnyAppUseCase
import jp.toastkid.yobidashi.media.image.preview.attach.AttachToThisAppBackgroundUseCase
import jp.toastkid.yobidashi.media.image.preview.attach.MenuActionUseCase

/**
 * @author toastkidjp
 */
class ImagePreviewDialogFragment  : DialogFragment() {

    private lateinit var binding: DialogImagePreviewBinding

    private lateinit var contentResolver: ContentResolver

    private lateinit var contentViewModel: ContentViewModel

    private var attachMenuPopup: AttachMenuPopup? = null

    private var pathFinder: () -> String? = { null }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        setStyle(STYLE_NO_TITLE, R.style.FullScreenDialogStyle)

        val activityContext = activity
                ?: return super.onCreateDialog(savedInstanceState)

        binding = DataBindingUtil.inflate(
                LayoutInflater.from(activityContext),
                LAYOUT_ID,
                null,
                false
        )

        binding.dialog = this
        binding.moduleEdit.dialog = this

        fitPhotoView()

        applyColorToButtons()

        val adapter = Adapter()
        binding.photo.adapter = adapter
        LinearSnapHelper().attachToRecyclerView(binding.photo)

        pathFinder = {
            findLayoutManager()?.let {
                adapter.getPath(it.findFirstVisibleItemPosition())
            }
        }

        contentViewModel = ViewModelProvider(activityContext).get(ContentViewModel::class.java)

        val viewModel =
                ViewModelProvider(this).get(ImagePreviewFragmentViewModel::class.java)
        binding.moduleEdit.colorFilterUseCase = ColorFilterUseCase(viewModel)
        viewModel.colorFilter.observe(this, Observer {
            findCurrentImageView()?.colorFilter = it
        })

        initializeContrastSlider()
        initializeAlphaSlider()

        binding.moduleEdit.imageRotationUseCase = ImageRotationUseCase(
                viewModel,<commit_msg>refactor #506 Remove redundant SAM-constructor.<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.app.Dialog
import android.content.ContentResolver
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AlertDialog
import androidx.core.graphics.drawable.toBitmap
import androidx.core.net.toUri
import androidx.core.os.bundleOf
import androidx.core.view.isVisible
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.LinearSnapHelper
import jp.toastkid.lib.ContentViewModel
import jp.toastkid.lib.preference.PreferenceApplier
import jp.toastkid.yobidashi.R
import jp.toastkid.yobidashi.databinding.DialogImagePreviewBinding
import jp.toastkid.yobidashi.media.image.Image
import jp.toastkid.yobidashi.media.image.preview.attach.AttachMenuPopup
import jp.toastkid.yobidashi.media.image.preview.attach.AttachToAnyAppUseCase
import jp.toastkid.yobidashi.media.image.preview.attach.AttachToThisAppBackgroundUseCase
import jp.toastkid.yobidashi.media.image.preview.attach.MenuActionUseCase

/**
 * @author toastkidjp
 */
class ImagePreviewDialogFragment  : DialogFragment() {

    private lateinit var binding: DialogImagePreviewBinding

    private lateinit var contentResolver: ContentResolver

    private lateinit var contentViewModel: ContentViewModel

    private var attachMenuPopup: AttachMenuPopup? = null

    private var pathFinder: () -> String? = { null }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        setStyle(STYLE_NO_TITLE, R.style.FullScreenDialogStyle)

        val activityContext = activity
                ?: return super.onCreateDialog(savedInstanceState)

        binding = DataBindingUtil.inflate(
                LayoutInflater.from(activityContext),
                LAYOUT_ID,
                null,
                false
        )

        binding.dialog = this
        binding.moduleEdit.dialog = this

        fitPhotoView()

        applyColorToButtons()

        val adapter = Adapter()
        binding.photo.adapter = adapter
        LinearSnapHelper().attachToRecyclerView(binding.photo)

        pathFinder = {
            findLayoutManager()?.let {
                adapter.getPath(it.findFirstVisibleItemPosition())
            }
        }

        contentViewModel = ViewModelProvider(activityContext).get(ContentViewModel::class.java)

        val viewModel =
                ViewModelProvider(this).get(ImagePreviewFragmentViewModel::class.java)
        binding.moduleEdit.colorFilterUseCase = ColorFilterUseCase(viewModel)
        viewModel.colorFilter.observe(this, {
            findCurrentImageView()?.colorFilter = it
        })

        initializeContrastSlider()
        initializeAlphaSlider()

        binding.moduleEdit.imageRotationUseCase = ImageRotationUseCase(
                viewModel,
{'fraction_non_alphanumeric': 0.07545300113250283, 'fraction_numerical': 0.0026896942242355605, 'mean_word_length': 3.71, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2020316', 'n_tokens_mistral': 2007, 'n_tokens_neox': 1866, 'n_words': 358}