-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DROID-1760 Tech | Fix | Unable to upload a picture or video (#487)
- Loading branch information
1 parent
2ec651b
commit 5af9384
Showing
15 changed files
with
253 additions
and
427 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
app/src/main/java/com/anytypeio/anytype/other/MediaPermissionHelper.kt
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.anytypeio.anytype.other | ||
|
||
import androidx.activity.result.ActivityResultLauncher | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import androidx.fragment.app.Fragment | ||
import com.anytypeio.anytype.core_utils.ext.FilePickerUtils.getPermissionToRequestByMime | ||
import com.anytypeio.anytype.core_utils.ext.FilePickerUtils.getPermissionToRequestForFiles | ||
import com.anytypeio.anytype.core_utils.ext.FilePickerUtils.getPermissionToRequestForImages | ||
import com.anytypeio.anytype.core_utils.ext.FilePickerUtils.getPermissionToRequestForVideos | ||
import com.anytypeio.anytype.core_utils.ext.FilePickerUtils.hasPermission | ||
import com.anytypeio.anytype.core_utils.ext.Mimetype | ||
|
||
class MediaPermissionHelper( | ||
private val fragment: Fragment, | ||
private val onPermissionDenied: () -> Unit, | ||
private val onPermissionSuccess: (Mimetype, Int?) -> Unit | ||
) { | ||
private var mimeType: Mimetype? = null | ||
private var requestCode: Int? = null | ||
|
||
private val permissionReadStorage: ActivityResultLauncher<Array<String>> | ||
|
||
init { | ||
permissionReadStorage = fragment.registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { grantResults -> | ||
val readResult = when (mimeType) { | ||
Mimetype.MIME_VIDEO_ALL -> grantResults[getPermissionToRequestForVideos()] | ||
Mimetype.MIME_IMAGE_ALL -> grantResults[getPermissionToRequestForImages()] | ||
Mimetype.MIME_FILE_ALL -> grantResults[getPermissionToRequestForFiles()] | ||
null -> false | ||
} | ||
if (readResult == true) { | ||
val type = requireNotNull(mimeType) { | ||
"mimeType should be initialized" | ||
} | ||
onPermissionSuccess(type, requestCode) | ||
} else { | ||
onPermissionDenied() | ||
} | ||
} | ||
} | ||
|
||
fun openFilePicker(mimeType: Mimetype, requestCode: Int?) { | ||
this.mimeType = mimeType | ||
this.requestCode = requestCode | ||
val context = fragment.context ?: return | ||
val hasPermission = mimeType.hasPermission(context) | ||
if (hasPermission) { | ||
onPermissionSuccess(mimeType, requestCode) | ||
} else { | ||
val permission = mimeType.getPermissionToRequestByMime() | ||
permissionReadStorage.launch(arrayOf(permission)) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.