Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 69f9cad6 authored by Mohammed Althaf T's avatar Mohammed Althaf T 😊
Browse files

Message: Replace files if its already exists

- Avoid copy and  then delete, use renameTo to move to new dir.
parent 586ecdf3
Loading
Loading
Loading
Loading
Loading
+18 −5
Original line number Diff line number Diff line
@@ -15,15 +15,28 @@
 */
package com.moez.QKSMS.util

import android.os.Handler
import android.os.Looper
import java.io.File

object FileUtils {

    fun moveDir(oldDir: File, newDir: File) {
        if (oldDir.exists() && oldDir.isDirectory) {
            newDir.apply { mkdirs() }
            oldDir.copyRecursively(newDir)
            oldDir.deleteRecursively()
    fun moveDir(sourceDir: File, destinationDir: File) {
        if (!sourceDir.exists() || !sourceDir.isDirectory) {
            return
        }

        // Perform the copy operation asynchronously using
        // a Handler to prevent blocking the main thread
        Handler(Looper.getMainLooper()).post {
            if (destinationDir.exists()) {
                val copySuccess = sourceDir.copyRecursively(destinationDir, true)
                if (copySuccess) {
                    sourceDir.deleteRecursively()
                }
            } else {
                sourceDir.renameTo(destinationDir)
            }
        }
    }