-
Notifications
You must be signed in to change notification settings - Fork 12
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
Update android side #68
Open
alibek228k
wants to merge
10
commits into
icerockdev:develop
Choose a base branch
from
alibek228k:master
base: develop
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
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fa51a32
refresh ActivityResultApi
alibek228k ac64d63
cleanup
alibek228k 49b7ed0
fix fragment cast bug
alibek228k e658bef
fix problem with toByteAttay android
alibek228k e293202
fix merge conflict
alibek228k f6809ab
cleanup code
alibek228k 7bc3ac0
prepare to merge
alibek228k bb02818
Merge remote-tracking branch 'moko/develop'
af6fab0
Fix fragment tags after merge
db585a3
Fix detekt errors
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
zipStorePath=wrapper/dists | ||
zipStoreBase=GRADLE_USER_HOME |
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
12 changes: 0 additions & 12 deletions
12
media/src/androidMain/kotlin/dev/icerock/moko/media/FileMediaExt.kt
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -4,12 +4,11 @@ | |
|
||
package dev.icerock.moko.media.picker | ||
|
||
import android.app.Activity | ||
import android.content.Intent | ||
import android.os.Environment | ||
import android.annotation.SuppressLint | ||
import android.content.ContentResolver | ||
import android.provider.OpenableColumns | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import androidx.fragment.app.Fragment | ||
import com.nbsp.materialfilepicker.MaterialFilePicker | ||
import com.nbsp.materialfilepicker.ui.FilePickerActivity | ||
import dev.icerock.moko.media.FileMedia | ||
import java.io.File | ||
|
||
|
@@ -19,49 +18,83 @@ class FilePickerFragment : Fragment() { | |
retainInstance = true | ||
} | ||
|
||
private val codeCallbackMap = mutableMapOf<Int, CallbackData>() | ||
private var callback: CallbackData? = null | ||
|
||
fun pickFile(callback: (Result<FileMedia>) -> Unit) { | ||
val requestCode = codeCallbackMap.keys.maxOrNull() ?: 0 | ||
|
||
codeCallbackMap[requestCode] = CallbackData(callback) | ||
|
||
// TODO нужно убрать использование внешней зависимости, сделать конфигурацию способа | ||
// выбора файла из вне (аргументом в контроллер передавать) | ||
val externalStorage = Environment.getExternalStorageDirectory() | ||
MaterialFilePicker().withSupportFragment(this) | ||
.withCloseMenu(true) | ||
.withRootPath(externalStorage.absolutePath) | ||
.withRequestCode(requestCode) | ||
.start() | ||
} | ||
@SuppressLint("Range") | ||
private val pickDocument = | ||
registerForActivityResult(ActivityResultContracts.OpenDocument()) { uri -> | ||
val callbackData = callback ?: return@registerForActivityResult | ||
callback = null | ||
|
||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | ||
super.onActivityResult(requestCode, resultCode, data) | ||
val callback = callbackData.callback | ||
|
||
val callbackData = codeCallbackMap[requestCode] ?: return | ||
codeCallbackMap.remove(requestCode) | ||
if (uri == null) { | ||
callback.invoke(Result.failure(CanceledException())) | ||
return@registerForActivityResult | ||
} | ||
|
||
val callback = callbackData.callback | ||
if (uri.path == null) { | ||
callback.invoke(Result.failure(java.lang.IllegalStateException("File is null"))) | ||
return@registerForActivityResult | ||
} | ||
|
||
if (resultCode == Activity.RESULT_CANCELED) { | ||
callback.invoke(Result.failure(CanceledException())) | ||
return | ||
} | ||
val fileNameWithExtension = if (uri.scheme == ContentResolver.SCHEME_CONTENT) { | ||
val cursor = requireContext().contentResolver.query(uri, null, null, null, null) | ||
cursor?.use { | ||
if (!it.moveToFirst()) null | ||
else it.getString(it.getColumnIndex(OpenableColumns.DISPLAY_NAME)) | ||
} | ||
} else null | ||
|
||
processResult(callback, data) | ||
} | ||
uri.path?.let { path -> | ||
val file = File(path) | ||
val name = file.name | ||
val byteArray = requireContext() | ||
.contentResolver | ||
.openInputStream(uri) | ||
?.readBytes() ?: return@registerForActivityResult | ||
|
||
private fun processResult( | ||
callback: (Result<FileMedia>) -> Unit, | ||
data: Intent? | ||
) { | ||
val filePath = data?.getStringExtra(FilePickerActivity.RESULT_FILE_PATH) | ||
callback( | ||
Result.success( | ||
FileMedia( | ||
fileNameWithExtension ?: name, | ||
uri.toString(), | ||
byteArray | ||
) | ||
) | ||
) | ||
} | ||
} | ||
|
||
filePath?.let { path -> | ||
val name = File(path).name | ||
callback(Result.success(FileMedia(name, path))) | ||
|
||
fun pickFile(callback: (Result<FileMedia>) -> Unit) { | ||
this.callback?.let { | ||
it.callback.invoke(Result.failure(IllegalStateException("Callback should be null"))) | ||
this.callback = null | ||
} | ||
|
||
this.callback = CallbackData(callback) | ||
|
||
pickDocument.launch( | ||
arrayOf( | ||
"application/pdf", | ||
"application/octet-stream", | ||
"application/doc", | ||
"application/msword", | ||
"application/ms-doc", | ||
"application/vnd.ms-excel", | ||
"application/vnd.ms-powerpoint", | ||
"application/json", | ||
"application/zip", | ||
"text/plain", | ||
"text/html", | ||
"text/xml", | ||
"audio/mpeg", | ||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document", | ||
"application/vnd.openxmlformats-officedocument.presentationml.presentation", | ||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" | ||
Comment on lines
+80
to
+95
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. maybe use something like |
||
) | ||
) | ||
} | ||
|
||
class CallbackData(val callback: (Result<FileMedia>) -> Unit) | ||
|
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.
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.
why we should read stream in this time? can we read it later?