diff options
author | Allan Wang <me@allanwang.ca> | 2021-09-25 15:55:07 -0700 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2021-09-25 15:55:07 -0700 |
commit | 8db1930d7623ba071123f5978153679da7161278 (patch) | |
tree | 5ce5894c321fae7f0a2f5a9ab65c3dca031db724 /app/src/main/kotlin/com/pitchedapps/frost/contracts/FileChooser.kt | |
parent | 1ac15f84b05e83d3c3482cf1498c74123852d658 (diff) | |
download | frost-8db1930d7623ba071123f5978153679da7161278.tar.gz frost-8db1930d7623ba071123f5978153679da7161278.tar.bz2 frost-8db1930d7623ba071123f5978153679da7161278.zip |
Create hilt web file chooser implementation
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/contracts/FileChooser.kt')
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/contracts/FileChooser.kt | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/contracts/FileChooser.kt b/app/src/main/kotlin/com/pitchedapps/frost/contracts/FileChooser.kt index 4853e7ff..21cb4948 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/contracts/FileChooser.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/contracts/FileChooser.kt @@ -26,12 +26,78 @@ import ca.allanwang.kau.permissions.kauRequestPermissions import ca.allanwang.kau.utils.string import com.pitchedapps.frost.R import com.pitchedapps.frost.utils.L +import dagger.Binds +import dagger.Module +import dagger.hilt.InstallIn +import dagger.hilt.android.components.ActivityComponent +import dagger.hilt.android.scopes.ActivityScoped +import javax.inject.Inject /** * Created by Allan Wang on 2017-07-04. */ const val MEDIA_CHOOSER_RESULT = 67 +interface WebFileChooser { + fun openMediaPicker( + filePathCallback: ValueCallback<Array<Uri>?>, + fileChooserParams: WebChromeClient.FileChooserParams + ) + + fun onActivityResultWeb( + requestCode: Int, + resultCode: Int, + intent: Intent? + ): Boolean +} + +class WebFileChooserImpl @Inject internal constructor(private val activity: Activity) : + WebFileChooser { + private var filePathCallback: ValueCallback<Array<Uri>?>? = null + + override fun openMediaPicker( + filePathCallback: ValueCallback<Array<Uri>?>, + fileChooserParams: WebChromeClient.FileChooserParams + ) { + activity.kauRequestPermissions(PERMISSION_WRITE_EXTERNAL_STORAGE) { granted, _ -> + if (!granted) { + L.d { "Failed to get write permissions" } + filePathCallback.onReceiveValue(null) + return@kauRequestPermissions + } + this.filePathCallback = filePathCallback + val intent = Intent() + intent.type = fileChooserParams.acceptTypes.firstOrNull() + intent.action = Intent.ACTION_GET_CONTENT + activity.startActivityForResult( + Intent.createChooser(intent, activity.string(R.string.pick_image)), + MEDIA_CHOOSER_RESULT + ) + } + } + + override fun onActivityResultWeb( + requestCode: Int, + resultCode: Int, + intent: Intent? + ): Boolean { + L.d { "FileChooser On activity results web $requestCode" } + if (requestCode != MEDIA_CHOOSER_RESULT) return false + val data = intent?.data + filePathCallback?.onReceiveValue(if (data != null) arrayOf(data) else null) + filePathCallback = null + return true + } +} + +@Module +@InstallIn(ActivityComponent::class) +interface WebFileChooserModule { + @Binds + @ActivityScoped + fun webFileChooser(to: WebFileChooserImpl): WebFileChooser +} + interface FileChooserActivityContract { fun openFileChooser( filePathCallback: ValueCallback<Array<Uri>?>, @@ -59,6 +125,7 @@ class FileChooserDelegate : FileChooserContract { ) { kauRequestPermissions(PERMISSION_WRITE_EXTERNAL_STORAGE) { granted, _ -> if (!granted) { + L.d { "Failed to get write permissions" } filePathCallback.onReceiveValue(null) return@kauRequestPermissions } |