package com.zxhy.simplefilesmanager.internalstorage

import androidx.lifecycle.viewModelScope
import com.zxhy.simplefilesmanager.simpleCommon.SimpleCommonViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File

class InternalStorageViewModel : SimpleCommonViewModel() {
    fun copyFiles(
        files: List<File>, currentDir: File,
        finish: () -> Unit
    ) = viewModelScope.launch(Dispatchers.IO) {
        runCatching {
            files.forEach {
                it.copyRecursively(File(currentDir, it.name), true)
            }
        }
        finish.invoke()
    }

    fun moveFiles(
        files: List<File>, currentDir: File,
        finish: () -> Unit
    ) = viewModelScope.launch(Dispatchers.IO) {
        runCatching {
            files.forEach {
                it.copyRecursively(File(currentDir, it.name), true)
                it.deleteRecursively()
            }
        }
        finish.invoke()
    }
}