-
Notifications
You must be signed in to change notification settings - Fork 10
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
Fix #6 and support for delete. #12
Open
deakjahn
wants to merge
1
commit into
chaudharydeepanshu:master
Choose a base branch
from
deakjahn:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 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 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 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 |
---|---|---|
|
@@ -15,8 +15,11 @@ import java.io.File | |
//private var isSourceFileTemp : Boolean = false | ||
private var destinationSaveFileInfo: DestinationSaveFileInfo? = null | ||
private var destinationSaveFilesInfo: MutableList<DestinationSaveFileInfo> = mutableListOf() | ||
private var destinationDeleteFileInfo: DestinationDeleteFileInfo? = null | ||
private var destinationDeleteFilesInfo: MutableList<DestinationDeleteFileInfo> = mutableListOf() | ||
|
||
var fileSaveJob: Job? = null | ||
var fileDeleteJob: Job? = null | ||
|
||
data class DestinationSaveFileInfo( | ||
val file: File, | ||
|
@@ -46,6 +49,8 @@ data class SaveFileInfo(val filePath: String?, val fileData: ByteArray?, val fil | |
result = 31 * result + fileName.hashCode() | ||
return result | ||
} | ||
|
||
override fun toString() = "SaveFileInfo(filePath=$filePath fileName=$fileName)" | ||
} | ||
|
||
// For saving single file. | ||
|
@@ -139,6 +144,7 @@ fun saveMultipleFiles( | |
|
||
val begin = System.nanoTime() | ||
|
||
destinationSaveFilesInfo.clear() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug mentioned in #6. |
||
saveFilesInfo.indices.map { index -> | ||
|
||
val saveFileInfo = saveFilesInfo.elementAt(index) | ||
|
@@ -397,4 +403,100 @@ fun processMultipleSaveFile( | |
} | ||
|
||
return true | ||
} | ||
} | ||
|
||
data class DestinationDeleteFileInfo( | ||
val fileUri: String, | ||
) | ||
|
||
data class DeleteFileInfo(val filePath: String) { | ||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (javaClass != other?.javaClass) return false | ||
|
||
other as DeleteFileInfo | ||
|
||
if (filePath != other.filePath) return false | ||
|
||
return true | ||
} | ||
|
||
override fun hashCode(): Int { | ||
var result = filePath.hashCode() | ||
return result | ||
} | ||
|
||
override fun toString() = "DeleteFileInfo(filePath=$filePath)" | ||
} | ||
|
||
// For deleting multiple files. | ||
fun deleteMultipleFiles( | ||
deleteFilesInfo: List<DeleteFileInfo>, | ||
context: Activity, | ||
) { | ||
val utils = Utils() | ||
|
||
val begin = System.nanoTime() | ||
|
||
destinationDeleteFilesInfo.clear() | ||
deleteFilesInfo.indices.map { index -> | ||
|
||
val deleteFileInfo = deleteFilesInfo.elementAt(index) | ||
destinationDeleteFilesInfo.add( | ||
DestinationDeleteFileInfo( | ||
fileUri = deleteFileInfo.filePath, | ||
) | ||
) | ||
} | ||
|
||
deleteMultipleFilesInDirectory(context) | ||
} | ||
|
||
// Delete multiple files in directory. | ||
fun deleteMultipleFilesInDirectory( | ||
context: Activity | ||
): Boolean { | ||
|
||
val coroutineScope = CoroutineScope(Dispatchers.IO) | ||
fileDeleteJob = coroutineScope.launch { | ||
|
||
val utils = Utils() | ||
|
||
val begin = System.nanoTime() | ||
|
||
if (destinationDeleteFilesInfo.isNotEmpty()) { | ||
|
||
val deletedFilesPaths: List<String> = utils.deleteMultipleFilesOnBackground( | ||
destinationDeleteFilesInfo, fileDeleteResult, context | ||
) | ||
|
||
if (deletedFilesPaths.isNotEmpty()) { | ||
utils.finishSuccessfully( | ||
deletedFilesPaths, fileDeleteResult | ||
) | ||
} else { | ||
utils.finishWithError( | ||
"files_delete_failed", | ||
"deleted files paths list was empty", | ||
"deleted files paths list was empty", | ||
fileDeleteResult | ||
) | ||
} | ||
|
||
} else { | ||
utils.finishWithError( | ||
"destinationDeleteFilesInfo_not_found", | ||
"destinationDeleteFilesInfo is empty", | ||
"destinationDeleteFilesInfo is empty", | ||
fileDeleteResult | ||
) | ||
} | ||
|
||
|
||
val end = System.nanoTime() | ||
println("Elapsed time in nanoseconds: ${end - begin}") | ||
|
||
} | ||
|
||
return true | ||
} |
This file contains 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 |
---|---|---|
|
@@ -9,6 +9,7 @@ import android.database.Cursor | |
import android.net.Uri | ||
import android.os.Build | ||
import android.os.ext.SdkExtensions.getExtensionVersion | ||
import android.provider.DocumentsContract | ||
import android.provider.OpenableColumns | ||
import android.util.Log | ||
import android.webkit.MimeTypeMap | ||
|
@@ -258,7 +259,7 @@ class Utils { | |
val saveFileNamePrefix: String = | ||
destinationSaveFilesInfo.elementAt(index).saveFileNamePrefix | ||
val documentFileNewFile = outputFolder!!.createFile( | ||
sourceFileMimeType ?: "application/random", saveFileNamePrefix | ||
sourceFileMimeType ?: "application/octet-stream", saveFileNamePrefix | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no |
||
) | ||
val destinationFileUri: Uri = documentFileNewFile!!.uri | ||
savedFilesPaths.add(withContext(Dispatchers.IO) { | ||
|
@@ -315,6 +316,50 @@ class Utils { | |
Log.d(LOG_TAG, "Canceled File Saving") | ||
} | ||
|
||
suspend fun deleteMultipleFilesOnBackground( | ||
destinationDeleteFilesInfo: List<DestinationDeleteFileInfo>, | ||
resultCallback: MethodChannel.Result?, | ||
context: Context, | ||
): List<String> { | ||
val deletedFilesPaths: MutableList<String> = mutableListOf() | ||
val uiScope = CoroutineScope(Dispatchers.Main) | ||
withContext(uiScope.coroutineContext) { | ||
try { | ||
Log.d(LOG_TAG, "Deleting file on background...") | ||
destinationDeleteFilesInfo.indices.map { index -> | ||
yield() | ||
val deleteFileUri = destinationDeleteFilesInfo.elementAt(index).fileUri | ||
if (DocumentsContract.deleteDocument(context.contentResolver, Uri.parse(deleteFileUri))) { | ||
deletedFilesPaths.add(deleteFileUri) | ||
} | ||
} | ||
Log.d(LOG_TAG, "...deleted file on background, result: $deletedFilesPaths") | ||
} catch (e: SecurityException) { | ||
Log.e(LOG_TAG, "deleteMultipleFilesOnBackground", e) | ||
finishWithError( | ||
"security_exception", e.localizedMessage, e.toString(), resultCallback | ||
) | ||
} catch (e: Exception) { | ||
Log.e(LOG_TAG, "deleteMultipleFilesOnBackground failed", e) | ||
finishWithError( | ||
"delete_file_failed", e.localizedMessage, e.toString(), resultCallback | ||
) | ||
} catch (e: Error) { | ||
Log.e(LOG_TAG, "deleteMultipleFilesOnBackground failed", e) | ||
finishWithError( | ||
"delete_file_failed", e.localizedMessage, e.toString(), resultCallback | ||
) | ||
} | ||
} | ||
return deletedFilesPaths | ||
} | ||
|
||
fun cancelDelete( | ||
) { | ||
fileDeleteJob?.cancel() | ||
Log.d(LOG_TAG, "Canceled File Delete") | ||
} | ||
|
||
fun cancelDirectoryDocumentsPicker( | ||
) { | ||
directoryDocumentsPickerJob?.cancel() | ||
|
@@ -344,6 +389,10 @@ class Utils { | |
fileSavingResult = null | ||
println("fileSaving result cleared") | ||
} | ||
if (resultCallback == fileDeleteResult && fileDeleteResult != null) { | ||
fileDeleteResult = null | ||
println("fileDelete result cleared") | ||
} | ||
if (resultCallback == fileMetadataResult && fileMetadataResult != null) { | ||
fileMetadataResult = null | ||
println("fileMetadata result cleared") | ||
|
This file contains 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 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to dump a possibly large binary file into the log.