<commit_before>/*
 * Copyright (c) 2022 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.image.view

import android.Manifest
import androidx.activity.compose.BackHandler
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStoreOwner
import coil.compose.AsyncImage
import coil.request.ImageRequest
import jp.toastkid.image.Image
import jp.toastkid.image.R
import jp.toastkid.image.list.BucketLoader
import jp.toastkid.image.list.ImageFilterUseCase
import jp.toastkid.image.list.ImageLoader
import jp.toastkid.image.list.ImageLoaderUseCase
import jp.toastkid.image.preview.ImagePreviewUi
import jp.toastkid.lib.ContentViewModel
import jp.toastkid.lib.preference.PreferenceApplier
import jp.toastkid.lib.viewmodel.PageSearcherViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

@Composable
fun ImageListUi() {
    val context = LocalContext.current

    val contentResolver = context.contentResolver ?: return

    val preferenceApplier = PreferenceApplier(context)

    val preview = remember { mutableStateOf(false) }

    val images = remember { mutableStateListOf<Image>() }

    val backHandlerState = remember { mutableStateOf(false) }

    val imageLoader = ImageLoader(contentResolver)
    val imageLoaderUseCase = remember {
        ImageLoaderUseCase(
            preferenceApplier,
            {
                images.clear()
                images.addAll(it)
            },
            BucketLoader(contentResolver),
            imageLoader,
            backHandlerState,
            { }
        )
    }

    val imageFilterUseCase = remember {
        ImageFilterUseCase(
            preferenceApplier,
            {
                images.clear()
                images.addAll(it)
            },
            imageLoaderUseCase,
            imageLoader,
            { }
        )
    }

    val requestPermissionLauncher =
        rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) {
            if (it) {
                imageLoaderUseCase()
                return@rememberLauncherForActivityResult
            }

            (context as? ViewModelStoreOwner)?.let { viewModelStoreOwner ->
                ViewModelProvider(viewModelStoreOwner).get(ContentViewModel::class.java)
                    .snackShort(R.string.message_audio_file_is_not_found)
            }
        }<commit_msg>refactor #506 Extract to local variable.<commit_after>/*
 * Copyright (c) 2022 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.image.view

import android.Manifest
import androidx.activity.compose.BackHandler
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStoreOwner
import coil.compose.AsyncImage
import coil.request.ImageRequest
import jp.toastkid.image.Image
import jp.toastkid.image.R
import jp.toastkid.image.list.BucketLoader
import jp.toastkid.image.list.ImageFilterUseCase
import jp.toastkid.image.list.ImageLoader
import jp.toastkid.image.list.ImageLoaderUseCase
import jp.toastkid.image.preview.ImagePreviewUi
import jp.toastkid.lib.ContentViewModel
import jp.toastkid.lib.preference.PreferenceApplier
import jp.toastkid.lib.viewmodel.PageSearcherViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

@Composable
fun ImageListUi() {
    val context = LocalContext.current

    val contentResolver = context.contentResolver ?: return

    val preferenceApplier = PreferenceApplier(context)

    val preview = remember { mutableStateOf(false) }

    val images = remember { mutableStateListOf<Image>() }

    val backHandlerState = remember { mutableStateOf(false) }

    val imageLoader = ImageLoader(contentResolver)
    val imageLoaderUseCase = remember {
        ImageLoaderUseCase(
            preferenceApplier,
            {
                images.clear()
                images.addAll(it)
            },
            BucketLoader(contentResolver),
            imageLoader,
            backHandlerState,
            { }
        )
    }

    val imageFilterUseCase = remember {
        ImageFilterUseCase(
            preferenceApplier,
            {
                images.clear()
                images.addAll(it)
            },
            imageLoaderUseCase,
            imageLoader,
            { }
        )
    }

    val contentViewModel = (context as? ViewModelStoreOwner)?.let { viewModelStoreOwner ->
        ViewModelProvider(viewModelStoreOwner).get(ContentViewModel::class.java)
    }

    val requestPermissionLauncher =
        rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) {
            if (it) {
                imageLoaderUseCase()
                return@rememberLauncherForActivityResult
            }

            contentViewModel?.snackShort(R.string.message_audio_file_is_not_found)
        }
{'fraction_non_alphanumeric': 0.07390534042014679, 'fraction_numerical': 0.00240445456846368, 'mean_word_length': 3.64063417498532, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '2493183', 'n_tokens_mistral': 2276, 'n_tokens_neox': 2178, 'n_words': 383}