11package com.quickfilemanager.data.repository
22
3- import android.content.Context
43import android.os.Environment
54import android.os.StatFs
6- import android.os.storage.StorageManager
75import com.quickfilemanager.domain.model.FileItem
86import com.quickfilemanager.domain.model.OperationResult
97import com.quickfilemanager.domain.model.StorageInfo
@@ -53,14 +51,14 @@ class FileRepository @Inject constructor() {
5351
5452 // 使用反射获取外部存储目录
5553 try {
56- val storageManager = Environment .getExternalStorageDirectory().let {
54+ val storageDir = Environment .getExternalStorageDirectory().let {
5755 it.parentFile?.let { parent ->
5856 File (parent, " storage" )
5957 } ? : File (" /storage" )
6058 }
6159
62- if (storageManager .exists()) {
63- storageManager .listFiles()?.forEach { file ->
60+ if (storageDir .exists()) {
61+ storageDir .listFiles()?.forEach { file ->
6462 if (file.isDirectory && file.canRead()) {
6563 val name = file.name
6664 if (name != " emulated" && name != " self" ) {
@@ -192,8 +190,17 @@ class FileRepository @Inject constructor() {
192190 val target = File (targetDir, source.name)
193191
194192 if (source.isDirectory) {
195- copyDirectory(source, target) { current, total, name ->
196- emit(OperationResult .Progress (current, total, name))
193+ val files = source.listFiles() ? : emptyArray()
194+ var current = 0
195+ files.forEachIndexed { index, file ->
196+ val dest = File (target, file.name)
197+ if (file.isDirectory) {
198+ file.copyRecursively(dest, overwrite = true )
199+ } else {
200+ file.copyTo(dest, overwrite = true )
201+ }
202+ current++
203+ emit(OperationResult .Progress (current, files.size, file.name))
197204 }
198205 } else {
199206 source.copyTo(target, overwrite = true )
@@ -205,23 +212,6 @@ class FileRepository @Inject constructor() {
205212 }
206213 }
207214
208- private fun copyDirectory (source : File , target : File , onProgress : (Int , Int , String ) -> Unit ) {
209- if (! target.exists()) {
210- target.mkdirs()
211- }
212-
213- val files = source.listFiles() ? : return
214- files.forEachIndexed { index, file ->
215- val dest = File (target, file.name)
216- if (file.isDirectory) {
217- copyDirectory(file, dest, onProgress)
218- } else {
219- file.copyTo(dest, overwrite = true )
220- }
221- onProgress(index + 1 , files.size, file.name)
222- }
223- }
224-
225215 /* *
226216 * 移动文件
227217 */
@@ -237,23 +227,16 @@ class FileRepository @Inject constructor() {
237227 if (source.renameTo(target)) {
238228 OperationResult .Success (" 已移动到: ${target.absolutePath} " )
239229 } else {
240- copyAndDelete(source, target)
230+ // 复制后删除
231+ source.copyRecursively(target, overwrite = true )
232+ source.deleteRecursively()
233+ OperationResult .Success (" 已移动到: ${target.absolutePath} " )
241234 }
242235 } catch (e: Exception ) {
243236 OperationResult .Error (" 移动失败: ${e.message} " , e)
244237 }
245238 }
246239
247- private fun copyAndDelete (source : File , target : File ): OperationResult {
248- return try {
249- source.copyRecursively(target, overwrite = true )
250- source.deleteRecursively()
251- OperationResult .Success (" 已移动到: ${target.absolutePath} " )
252- } catch (e: Exception ) {
253- OperationResult .Error (" 移动失败: ${e.message} " , e)
254- }
255- }
256-
257240 /* *
258241 * 获取父目录路径
259242 */
0 commit comments