Skip to content

Commit

Permalink
Start working on batch checksum comparing
Browse files Browse the repository at this point in the history
  • Loading branch information
T8RIN committed Jan 22, 2025
1 parent 009c739 commit 356c0be
Show file tree
Hide file tree
Showing 12 changed files with 541 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import ru.tech.imageresizershrinker.core.data.utils.fileSize
import ru.tech.imageresizershrinker.core.data.utils.getFilename
import ru.tech.imageresizershrinker.core.data.utils.getPath
import ru.tech.imageresizershrinker.core.data.utils.isExternalStorageWritable
import ru.tech.imageresizershrinker.core.data.utils.listFilesInDirectory
import ru.tech.imageresizershrinker.core.data.utils.openWriteableStream
import ru.tech.imageresizershrinker.core.data.utils.toUiPath
import ru.tech.imageresizershrinker.core.domain.dispatchers.DispatchersHolder
Expand Down Expand Up @@ -391,4 +392,10 @@ internal class AndroidFileController @Inject constructor(
}
}

override suspend fun listFilesInDirectory(
treeUri: String
): List<String> = withContext(ioDispatcher) {
context.listFilesInDirectory(treeUri.toUri()).map { it.toString() }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal class StreamWriteable(

override fun copyFrom(readable: Readable) = writeBytes(readable.readBytes())

override fun writeBytes(byteArray: ByteArray) = stream.write(byteArray)
override fun writeBytes(byteArray: ByteArray) = stream.buffered().write(byteArray)

override fun close() {
stream.flush()
Expand All @@ -41,7 +41,7 @@ internal class StreamReadable(
private val stream: InputStream?
) : Readable {

override fun readBytes(): ByteArray = stream?.readBytes() ?: ByteArray(0)
override fun readBytes(): ByteArray = stream?.buffered()?.readBytes() ?: ByteArray(0)

override fun copyTo(writeable: Writeable) = writeable.writeBytes(readBytes())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,76 @@ import android.content.Context
import android.net.Uri
import android.os.Build
import android.os.ParcelFileDescriptor
import android.provider.DocumentsContract
import android.provider.MediaStore
import android.provider.OpenableColumns
import androidx.core.net.toUri
import androidx.documentfile.provider.DocumentFile
import okio.use
import kotlinx.coroutines.coroutineScope
import ru.tech.imageresizershrinker.core.domain.model.ImageModel
import ru.tech.imageresizershrinker.core.resources.R
import java.net.URLDecoder
import java.util.LinkedList
import kotlin.io.use

fun ImageModel.toUri(): Uri? = when (data) {
is Uri -> data as Uri
is String -> (data as String).toUri()
else -> null
}

private fun isDirectory(mimeType: String): Boolean {
return DocumentsContract.Document.MIME_TYPE_DIR == mimeType
}

internal suspend fun Context.listFilesInDirectory(
rootUri: Uri
): List<Uri> = coroutineScope {
var childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(
rootUri,
DocumentsContract.getTreeDocumentId(rootUri)
)

val files: MutableList<Pair<Uri, Long>> = LinkedList()

val dirNodes: MutableList<Uri> = LinkedList()
dirNodes.add(childrenUri)
while (dirNodes.isNotEmpty()) {
childrenUri = dirNodes.removeAt(0)

contentResolver.query(
childrenUri,
arrayOf(
DocumentsContract.Document.COLUMN_DOCUMENT_ID,
DocumentsContract.Document.COLUMN_LAST_MODIFIED,
DocumentsContract.Document.COLUMN_MIME_TYPE,
),
null,
null,
null
).use {
while (it!!.moveToNext()) {
val docId = it.getString(0)
val lastModified = it.getLong(1)
val mime = it.getString(2)
if (isDirectory(mime)) {
val newNode = DocumentsContract.buildChildDocumentsUriUsingTree(rootUri, docId)
dirNodes.add(newNode)
} else {
files.add(
DocumentsContract.buildDocumentUriUsingTree(
rootUri,
docId
) to lastModified
)
}
}
}
}

files.sortedByDescending { it.second }.map { it.first }
}

fun Uri.fileSize(context: Context): Long? {
runCatching {
context.contentResolver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,6 @@ interface FileController {
metadata: M?
)

suspend fun listFilesInDirectory(treeUri: String): List<String>

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ru.tech.imageresizershrinker.core.resources.icons

import androidx.compose.material.icons.Icons
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.graphics.vector.path
import androidx.compose.ui.unit.dp

val Icons.Rounded.FolderCompare: ImageVector by lazy {
ImageVector.Builder(
name = "FolderCompare",
defaultWidth = 24.dp,
defaultHeight = 24.dp,
viewportWidth = 24f,
viewportHeight = 24f
).apply {
path(fill = SolidColor(Color(0xFF000000))) {
moveTo(13f, 19f)
curveTo(13f, 19.34f, 13.04f, 19.67f, 13.09f, 20f)
horizontalLineTo(4f)
curveTo(2.9f, 20f, 2f, 19.11f, 2f, 18f)
verticalLineTo(6f)
curveTo(2f, 4.89f, 2.89f, 4f, 4f, 4f)
horizontalLineTo(10f)
lineTo(12f, 6f)
horizontalLineTo(20f)
curveTo(21.1f, 6f, 22f, 6.89f, 22f, 8f)
verticalLineTo(13.81f)
curveTo(21.12f, 13.3f, 20.1f, 13f, 19f, 13f)
curveTo(15.69f, 13f, 13f, 15.69f, 13f, 19f)
moveTo(23f, 17f)
lineTo(20f, 14.5f)
verticalLineTo(16f)
horizontalLineTo(16f)
verticalLineTo(18f)
horizontalLineTo(20f)
verticalLineTo(19.5f)
lineTo(23f, 17f)
moveTo(18f, 18.5f)
lineTo(15f, 21f)
lineTo(18f, 23.5f)
verticalLineTo(22f)
horizontalLineTo(22f)
verticalLineTo(20f)
horizontalLineTo(18f)
verticalLineTo(18.5f)
close()
}
}.build()
}
2 changes: 2 additions & 0 deletions core/resources/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1566,4 +1566,6 @@
<string name="change_sticker">Change Sticker</string>
<string name="fit_width">Fit Width</string>
<string name="fit_height">Fit Height</string>
<string name="batch_compare">Batch Compare</string>
<string name="pick_files_to_checksum">Pick file/files to calculate it\'s checksum based on selected algorithm</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,18 @@

package ru.tech.imageresizershrinker.core.ui.utils.helper

import android.app.Activity
import android.content.Context
import android.net.Uri
import android.os.Build
import android.provider.DocumentsContract
import androidx.core.net.toUri
import androidx.documentfile.provider.DocumentFile
import kotlinx.coroutines.coroutineScope
import ru.tech.imageresizershrinker.core.domain.model.FileModel
import ru.tech.imageresizershrinker.core.domain.model.ImageModel
import ru.tech.imageresizershrinker.core.domain.model.SortType
import ru.tech.imageresizershrinker.core.resources.R
import ru.tech.imageresizershrinker.core.ui.utils.helper.ContextUtils.getFilename
import java.net.URLDecoder
import java.util.LinkedList


fun Uri?.toUiPath(
Expand All @@ -56,54 +53,6 @@ fun Uri?.toUiPath(
}
} ?: default

suspend fun Activity.listFilesInDirectory(
rootUri: Uri
): List<Uri> = coroutineScope {
var childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(
rootUri,
DocumentsContract.getTreeDocumentId(rootUri)
)

val files: MutableList<Pair<Uri, Long>> = LinkedList()

val dirNodes: MutableList<Uri> = LinkedList()
dirNodes.add(childrenUri)
while (dirNodes.isNotEmpty()) {
childrenUri = dirNodes.removeAt(0)

contentResolver.query(
childrenUri,
arrayOf(
DocumentsContract.Document.COLUMN_DOCUMENT_ID,
DocumentsContract.Document.COLUMN_LAST_MODIFIED,
DocumentsContract.Document.COLUMN_MIME_TYPE,
),
null,
null,
null
).use {
while (it!!.moveToNext()) {
val docId = it.getString(0)
val lastModified = it.getLong(1)
val mime = it.getString(2)
if (isDirectory(mime)) {
val newNode = DocumentsContract.buildChildDocumentsUriUsingTree(rootUri, docId)
dirNodes.add(newNode)
} else {
files.add(
DocumentsContract.buildDocumentUriUsingTree(
rootUri,
docId
) to lastModified
)
}
}
}
}

files.sortedByDescending { it.second }.map { it.first }
}

fun Uri.lastModified(context: Context): Long? = with(context.contentResolver) {
val query = query(this@lastModified, null, null, null, null)

Expand Down Expand Up @@ -152,10 +101,6 @@ fun List<Uri>.sortedByName(
context.getFilename(it)
}

private fun isDirectory(mimeType: String): Boolean {
return DocumentsContract.Document.MIME_TYPE_DIR == mimeType
}

fun ImageModel.toUri(): Uri? = when (data) {
is Uri -> data as Uri
is String -> (data as String).toUri()
Expand Down
Loading

0 comments on commit 356c0be

Please sign in to comment.