aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/contracts
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2021-09-25 15:55:07 -0700
committerAllan Wang <me@allanwang.ca>2021-09-25 15:55:07 -0700
commit8db1930d7623ba071123f5978153679da7161278 (patch)
tree5ce5894c321fae7f0a2f5a9ab65c3dca031db724 /app/src/main/kotlin/com/pitchedapps/frost/contracts
parent1ac15f84b05e83d3c3482cf1498c74123852d658 (diff)
downloadfrost-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')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/contracts/ActivityContract.kt8
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/contracts/FileChooser.kt67
2 files changed, 68 insertions, 7 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/contracts/ActivityContract.kt b/app/src/main/kotlin/com/pitchedapps/frost/contracts/ActivityContract.kt
index 84edfee9..756b1f3d 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/contracts/ActivityContract.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/contracts/ActivityContract.kt
@@ -17,18 +17,12 @@
package com.pitchedapps.frost.contracts
import com.mikepenz.iconics.typeface.IIcon
-import com.pitchedapps.frost.activities.MainActivity
import com.pitchedapps.frost.fragments.BaseFragment
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.BroadcastChannel
-/**
- * All the contracts for [MainActivity]
- */
-interface ActivityContract : FileChooserActivityContract
-
@UseExperimental(ExperimentalCoroutinesApi::class)
-interface MainActivityContract : ActivityContract, MainFabContract {
+interface MainActivityContract : MainFabContract {
val fragmentChannel: BroadcastChannel<Int>
val headerBadgeChannel: BroadcastChannel<String>
fun setTitle(res: Int)
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
}