-
Notifications
You must be signed in to change notification settings - Fork 4
Feat: custom projections support #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
de492a6
feat: custom projections support.
ItzNotABug 409ffd9
update: sample app.
ItzNotABug 033d951
misc: fixes.
ItzNotABug d580c3e
fix: compatibility.
ItzNotABug 56475e9
update: sample.
ItzNotABug d35cf45
update: sample for `listFiles().size` vs `count()`.
ItzNotABug 6ab8e53
Update dfc/src/main/java/com/lazygeniouz/dfc/file/internals/TreeDocum…
ItzNotABug 0de0dad
address comments.
ItzNotABug 59949da
improve: api.
ItzNotABug File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
app/src/main/java/com/lazygeniouz/filecompat/example/TestFileGenerator.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package com.lazygeniouz.filecompat.example | ||
|
|
||
| import android.content.Context | ||
| import android.net.Uri | ||
| import com.lazygeniouz.dfc.file.DocumentFileCompat | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.async | ||
| import kotlinx.coroutines.awaitAll | ||
| import kotlinx.coroutines.coroutineScope | ||
| import kotlinx.coroutines.sync.Semaphore | ||
| import kotlinx.coroutines.sync.withPermit | ||
| import java.util.Random | ||
| import java.util.concurrent.atomic.AtomicInteger | ||
|
|
||
| object TestFileGenerator { | ||
|
|
||
| private val extensions = listOf("txt", "pdf", "jpg", "png", "mp4", "mp3", "doc", "zip", "apk") | ||
| private val random = Random() | ||
|
|
||
| suspend fun generateTestFiles(context: Context, directoryUri: Uri, fileCount: Int): String { | ||
| return try { | ||
| val directory = DocumentFileCompat.fromTreeUri(context, directoryUri) | ||
| ?: return "Failed to access directory" | ||
|
|
||
| val startTime = System.currentTimeMillis() | ||
| val successCount = AtomicInteger(0) | ||
| val failCount = AtomicInteger(0) | ||
|
|
||
| val semaphore = Semaphore(10) | ||
|
|
||
| coroutineScope { | ||
| val jobs = (1..fileCount).map { index -> | ||
| async(Dispatchers.IO) { | ||
| semaphore.withPermit { | ||
| val fileName = "test_file_$index" | ||
| val extension = extensions.random() | ||
| val sizeInKb = random.nextInt(100) + 1 // 1KB to 100KB | ||
|
|
||
| val file = directory.createFile( | ||
| "application/octet-stream", | ||
| "$fileName.$extension" | ||
| ) | ||
| if (file != null) { | ||
| val success = writeRandomData(context, file.uri, sizeInKb) | ||
| if (success) successCount.incrementAndGet() else failCount.incrementAndGet() | ||
| } else { | ||
| failCount.incrementAndGet() | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| jobs.awaitAll() | ||
| } | ||
|
|
||
| val elapsedTime = (System.currentTimeMillis() - startTime) / 1000.0 | ||
|
|
||
| buildString { | ||
| append("Test Files Generation Complete!\n\n") | ||
| append("Total: $fileCount files\n") | ||
| append("Success: ${successCount.get()}\n") | ||
| append("Failed: ${failCount.get()}\n") | ||
| append("Time: ${elapsedTime}s\n\n") | ||
| append("Files have random sizes between 1KB-100KB\n") | ||
| append("Extensions: ${extensions.joinToString(", ")}") | ||
| } | ||
| } catch (e: Exception) { | ||
| "Error generating files: ${e.message}" | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Write random data to a file to achieve desired size. | ||
| */ | ||
| private fun writeRandomData(context: Context, fileUri: Uri, sizeInKb: Int): Boolean { | ||
| return try { | ||
| context.contentResolver.openOutputStream(fileUri)?.use { output -> | ||
| val bufferSize = 8192 // 8KB buffer | ||
| val buffer = ByteArray(bufferSize) | ||
| val totalBytes = sizeInKb * 1024 | ||
| var written = 0 | ||
|
|
||
| while (written < totalBytes) { | ||
| random.nextBytes(buffer) | ||
| val toWrite = minOf(bufferSize, totalBytes - written) | ||
| output.write(buffer, 0, toWrite) | ||
| written += toWrite | ||
| } | ||
| output.flush() | ||
| } | ||
| true | ||
| } catch (e: Exception) { | ||
| false | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.