<commit_before>package com.gallery.core.delegate

import android.content.ContentResolver
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ImageView
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.kotlin.expand.app.scanFileExpand
import androidx.kotlin.expand.app.squareExpand
import androidx.kotlin.expand.content.drawableExpand
import androidx.kotlin.expand.content.findIdByUriExpand
import androidx.kotlin.expand.content.openVideoExpand
import androidx.kotlin.expand.net.deleteExpand
import androidx.kotlin.expand.net.isFileExistsExpand
import androidx.kotlin.expand.view.hideExpand
import androidx.kotlin.expand.view.showExpand
import androidx.lifecycle.ViewModelStoreOwner
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator
import com.gallery.core.GalleryBundle
import com.gallery.core.ScanArgs
import com.gallery.core.ScanArgs.Companion.putScanArgs
import com.gallery.core.ScanArgs.Companion.scanArgs
import com.gallery.core.callback.IGalleryCallback
import com.gallery.core.callback.IGalleryImageLoader
import com.gallery.core.callback.IGalleryInterceptor
import com.gallery.core.crop.ICrop
import com.gallery.core.expand.*
import com.gallery.core.ui.adapter.GalleryAdapter
import com.gallery.core.ui.widget.SimpleDivider
import com.gallery.scan.ScanImpl
import com.gallery.scan.ScanView
import com.gallery.scan.args.CursorLoaderArgs
import com.gallery.scan.args.ScanEntityFactory
import com.gallery.scan.args.file.*
import com.gallery.scan.types.SCAN_ALL
import com.gallery.scan.types.Sort
import com.gallery.scan.types.isScanAllExpand

/**
 * 图库代理
 */
class ScanDelegate(
        private val fragment: Fragment,
        private val recyclerView: RecyclerView,
        private val emptyView: ImageView
) : IScanDelegate, ScanView<ScanFileEntity>, GalleryAdapter.OnGalleryItemClickListener {

    private var fileUri: Uri = Uri.EMPTY
    var parentId: Long = SCAN_ALL

    private val openCameraLauncher: ActivityResultLauncher<CameraUri> = fragment.requestCameraResultLauncherExpand({ cameraCanceled() }) { cameraSuccess() }
    private val cropLauncher: ActivityResultLauncher<Intent> =
            fragment.registerForActivityResult(ActivityResultContracts.StartActivityForResult())
            { intent -> galleryICrop.onCropResult(this, galleryBundle, intent) }
    private val cameraPermissionLauncher: ActivityResultLauncher<String> =
            fragment.requestPermissionResultLauncherExpand {
                if (it) {
                    permissionsGranted(PermissionCode.READ)
                } else {
                    permissionsDenied(PermissionCode.READ)
                }
            }

    private val writePermissionLauncher: ActivityResultLauncher<String> =
            fragment.requestPermissionResultLauncherExpand {
                if (it) {
                    permissionsGranted(PermissionCode.WRITE)
                } else {
                    permissionsDenied(PermissionCode.WRITE)
                }
            }

    private val scan: ScanImpl<ScanFileEntity> by lazy { ScanImpl(this) }
    private val galleryBundle: GalleryBundle by lazy { fragment.galleryArgs }
    private val galleryImageLoader: IGalleryImageLoader by lazy { fragment.galleryImageLoader }
    private val galleryCallback: IGalleryCallback by lazy { fragment.galleryCallback }
    private val galleryInterceptor: IGalleryInterceptor by lazy { fragment.galleryInterceptor }
    private val galleryICrop: ICrop by lazy { fragment.galleryCrop }
    private val galleryAdapter: GalleryAdapter by lazy { GalleryAdapter(activityNotNull.squareExpand(galleryBundle.spanCount), galleryBundle, galleryCallback, galleryImageLoader, this) }

    override val activity: FragmentActivity?
        get() = fragment.activity
    override val activityNotNull: FragmentActivity
        get() = fragment.requireActivity()
    override val currentEntities: ArrayList<ScanEntity>
        get() = galleryAdapter.currentList.filter { it.parent != GalleryAdapter.CAMERA } as ArrayList<ScanEntity>
    override val selectEntities: ArrayList<ScanEntity>
        get() = galleryAdapter.currentSelectList

    override fun onSaveInstanceState(outState: Bundle) {
        ScanArgs.newSaveInstance(parentId, fileUri, selectEntities).putScanArgs(outState)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        val selectList = savedInstanceState?.scanArgs?.let {
            parentId = it.parentId
            fileUri = it.fileUri
            it.selectList
        }
        galleryCallback.onGalleryCreated()
        fragment.view?.setBackgroundColor(galleryBundle.galleryRootBackground)
        emptyView.setImageDrawable(activityNotNull.drawableExpand(galleryBundle.photoEmptyDrawable))
        emptyView.setOnClickListener { v ->
            if (galleryInterceptor.onEmptyPhotoClick(v)) {
                cameraOpen()
            }
        }
        galleryAdapter.addSelectAll(selectList ?: galleryBundle.selectEntities)
        recyclerView.setHasFixedSize(true)
        recyclerView.layoutManager = when (galleryBundle.layoutManager) {
            LayoutManager.GRID -> GridLayoutManager(recyclerView.context, galleryBundle.spanCount, galleryBundle.orientation, false)
            LayoutManager.LINEAR -> LinearLayoutManager(recyclerView.context, galleryBundle.orientation, false)
        }
        recyclerView.addItemDecoration(SimpleDivider(galleryBundle.dividerWidth))
        if (recyclerView.itemAnimator is SimpleItemAnimator) {<commit_msg>update code<commit_after>package com.gallery.core.delegate

import android.content.ContentResolver
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ImageView
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.kotlin.expand.app.scanFileExpand
import androidx.kotlin.expand.app.squareExpand
import androidx.kotlin.expand.content.drawableExpand
import androidx.kotlin.expand.content.findIdByUriExpand
import androidx.kotlin.expand.content.openVideoExpand
import androidx.kotlin.expand.net.deleteExpand
import androidx.kotlin.expand.net.isFileExistsExpand
import androidx.kotlin.expand.view.hideExpand
import androidx.kotlin.expand.view.showExpand
import androidx.lifecycle.ViewModelStoreOwner
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator
import com.gallery.core.GalleryBundle
import com.gallery.core.ScanArgs
import com.gallery.core.ScanArgs.Companion.putScanArgs
import com.gallery.core.ScanArgs.Companion.scanArgs
import com.gallery.core.callback.IGalleryCallback
import com.gallery.core.callback.IGalleryImageLoader
import com.gallery.core.callback.IGalleryInterceptor
import com.gallery.core.crop.ICrop
import com.gallery.core.expand.*
import com.gallery.core.ui.adapter.GalleryAdapter
import com.gallery.core.ui.widget.SimpleDivider
import com.gallery.scan.ScanImpl
import com.gallery.scan.ScanView
import com.gallery.scan.args.CursorLoaderArgs
import com.gallery.scan.args.ScanEntityFactory
import com.gallery.scan.args.file.*
import com.gallery.scan.types.SCAN_ALL
import com.gallery.scan.types.Sort
import com.gallery.scan.types.isScanAllExpand

/**
 * 图库代理
 */
class ScanDelegate(
        private val fragment: Fragment,
        private val recyclerView: RecyclerView,
        private val emptyView: ImageView,
) : IScanDelegate, ScanView<ScanFileEntity>, GalleryAdapter.OnGalleryItemClickListener {

    private var fileUri: Uri = Uri.EMPTY
    var parentId: Long = SCAN_ALL

    private val openCameraLauncher: ActivityResultLauncher<CameraUri> = fragment.requestCameraResultLauncherExpand({ cameraCanceled() }) { cameraSuccess() }
    private val cropLauncher: ActivityResultLauncher<Intent> =
            fragment.registerForActivityResult(ActivityResultContracts.StartActivityForResult())
            { intent -> galleryICrop.onCropResult(this, galleryBundle, intent) }
    private val cameraPermissionLauncher: ActivityResultLauncher<String> =
            fragment.requestPermissionResultLauncherExpand {
                if (it) {
                    permissionsGranted(PermissionCode.READ)
                } else {
                    permissionsDenied(PermissionCode.READ)
                }
            }

    private val writePermissionLauncher: ActivityResultLauncher<String> =
            fragment.requestPermissionResultLauncherExpand {
                if (it) {
                    permissionsGranted(PermissionCode.WRITE)
                } else {
                    permissionsDenied(PermissionCode.WRITE)
                }
            }

    private val scan: ScanImpl<ScanFileEntity> by lazy { ScanImpl(this) }
    private val galleryBundle: GalleryBundle by lazy { fragment.galleryArgs }
    private val galleryImageLoader: IGalleryImageLoader by lazy { fragment.galleryImageLoader }
    private val galleryCallback: IGalleryCallback by lazy { fragment.galleryCallback }
    private val galleryInterceptor: IGalleryInterceptor by lazy { fragment.galleryInterceptor }
    private val galleryICrop: ICrop by lazy { fragment.galleryCrop }
    private val galleryAdapter: GalleryAdapter by lazy { GalleryAdapter(activityNotNull.squareExpand(galleryBundle.spanCount), galleryBundle, galleryCallback, galleryImageLoader, this) }

    override val activity: FragmentActivity?
        get() = fragment.activity
    override val activityNotNull: FragmentActivity
        get() = fragment.requireActivity()
    override val currentEntities: ArrayList<ScanEntity>
        get() = galleryAdapter.currentList.filterTo(ArrayList()) { it.parent != GalleryAdapter.CAMERA }
    override val selectEntities: ArrayList<ScanEntity>
        get() = galleryAdapter.currentSelectList

    override fun onSaveInstanceState(outState: Bundle) {
        ScanArgs.newSaveInstance(parentId, fileUri, selectEntities).putScanArgs(outState)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        val selectList = savedInstanceState?.scanArgs?.let {
            parentId = it.parentId
            fileUri = it.fileUri
            it.selectList
        }
        galleryCallback.onGalleryCreated()
        fragment.view?.setBackgroundColor(galleryBundle.galleryRootBackground)
        emptyView.setImageDrawable(activityNotNull.drawableExpand(galleryBundle.photoEmptyDrawable))
        emptyView.setOnClickListener { v ->
            if (galleryInterceptor.onEmptyPhotoClick(v)) {
                cameraOpen()
            }
        }
        galleryAdapter.addSelectAll(selectList ?: galleryBundle.selectEntities)
        recyclerView.setHasFixedSize(true)
        recyclerView.layoutManager = when (galleryBundle.layoutManager) {
            LayoutManager.GRID -> GridLayoutManager(recyclerView.context, galleryBundle.spanCount, galleryBundle.orientation, false)
            LayoutManager.LINEAR -> LinearLayoutManager(recyclerView.context, galleryBundle.orientation, false)
        }
        recyclerView.addItemDecoration(SimpleDivider(galleryBundle.dividerWidth))
        if (recyclerView.itemAnimator is SimpleItemAnimator) {
{'fraction_non_alphanumeric': 0.07524870334155259, 'fraction_numerical': 0.0, 'mean_word_length': 3.992359932088285, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 28, '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': '2558828', 'n_tokens_mistral': 3175, 'n_tokens_neox': 3004, 'n_words': 561}