Skip to content

Commit

Permalink
Add all media types support to the media picker
Browse files Browse the repository at this point in the history
  • Loading branch information
irfano committed Apr 3, 2023
1 parent e8de8bb commit ac21e3c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ class MediaPickerViewModel @Inject constructor(
UiStringText(String.format(resourceProvider.getString(R.string.cab_selected), numSelected))
}
else -> {
if (isImagePicker() && isVideoPicker()) {
if (mediaPickerSetup.allowedTypes.size > 1) {
// "image + video" picker, or "image + video + audio" picker
UiStringRes(R.string.photo_picker_use_media)
} else if (isVideoPicker()) {
UiStringRes(R.string.photo_picker_use_video)
Expand Down Expand Up @@ -324,14 +325,8 @@ class MediaPickerViewModel @Inject constructor(
)
}

private fun hasPermission() = when {
mediaPickerSetup.requiresPhotosVideosPermissions -> permissionsHandler.hasPhotosVideosPermission()
mediaPickerSetup.requiresMusicAudioPermissions -> permissionsHandler.hasMusicAudioPermission()
else -> false
}

fun refreshData(forceReload: Boolean) {
if (hasPermission()) {
if (!needPhotosVideoPermission() && !needMusicAudioPermission()) {
launch(bgDispatcher) {
loadActions.send(LoadAction.Refresh(forceReload))
}
Expand Down Expand Up @@ -370,9 +365,7 @@ class MediaPickerViewModel @Inject constructor(
_domainModel.value = domainModel
}
}
if ((!mediaPickerSetup.requiresPhotosVideosPermissions && !mediaPickerSetup.requiresMusicAudioPermissions)
|| hasPermission()
) {
if (!needPhotosVideoPermission() && !needMusicAudioPermission()) {
launch(bgDispatcher) {
loadActions.send(LoadAction.Start())
}
Expand Down Expand Up @@ -574,12 +567,9 @@ class MediaPickerViewModel @Inject constructor(
// No permission is required, so there is no need to check permissions.
return
}
val isAlwaysDenied = if (mediaPickerSetup.requiresPhotosVideosPermissions) {
isPhotosVideosAlwaysDenied
} else {
isMusicAudioAlwaysDenied
}
if (hasPermission()) {
val isAlwaysDenied = (mediaPickerSetup.requiresPhotosVideosPermissions && isPhotosVideosAlwaysDenied) ||
(mediaPickerSetup.requiresMusicAudioPermissions && isMusicAudioAlwaysDenied)
if (!needPhotosVideoPermission() && !needMusicAudioPermission()) {
_softAskRequest.value = SoftAskRequest(show = false, isAlwaysDenied = isAlwaysDenied)
if (_domainModel.value?.domainItems.isNullOrEmpty()) {
refreshData(false)
Expand All @@ -600,17 +590,13 @@ class MediaPickerViewModel @Inject constructor(
clickIcon(icon)
}

private fun getRequiredPermissionName(): String {
val permissionName = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (mediaPickerSetup.requiresPhotosVideosPermissions) {
R.string.permission_images
} else if (mediaPickerSetup.requiresMusicAudioPermissions) {
R.string.permission_audio
} else {
R.string.unknown
}
} else {
R.string.permission_storage
private fun getRequiredPermissionsNames(): String {
val permissionName = when {
Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU -> R.string.permission_storage
needPhotosVideoPermission() && needMusicAudioPermission() -> R.string.permission_images_video_audio
needPhotosVideoPermission() -> R.string.permission_images
needMusicAudioPermission() -> R.string.permission_audio
else -> R.string.unknown
}
return resourceProvider.getString(permissionName)
}
Expand All @@ -619,19 +605,28 @@ class MediaPickerViewModel @Inject constructor(
private fun isVideoPicker() = mediaPickerSetup.allowedTypes.contains(VIDEO)
private fun isAudioPicker() = mediaPickerSetup.allowedTypes.contains(AUDIO)

private fun needPhotosVideoPermission() =
mediaPickerSetup.requiresPhotosVideosPermissions && !permissionsHandler.hasPhotosVideosPermission()

private fun needMusicAudioPermission() =
mediaPickerSetup.requiresMusicAudioPermissions && !permissionsHandler.hasMusicAudioPermission()

private fun buildSoftAskView(softAskRequest: SoftAskRequest?): SoftAskViewUiModel {
if (softAskRequest != null && softAskRequest.show) {
mediaPickerTracker.trackShowPermissionsScreen(mediaPickerSetup, softAskRequest.isAlwaysDenied)
val appName = "<strong>${resourceProvider.getString(R.string.app_name)}</strong>"
val label = if (softAskRequest.isAlwaysDenied) {
val permission = ("<strong>${getRequiredPermissionName()}</strong>")
val permission = ("<strong>${getRequiredPermissionsNames()}</strong>")
String.format(
resourceProvider.getString(R.string.media_picker_soft_ask_media_permissions_denied),
appName,
permission
)
} else {
val description = when {
isImagePicker() && isVideoPicker() && isAudioPicker() -> {
R.string.photo_picker_soft_ask_photos_videos_audio_label
}
isImagePicker() && isVideoPicker() -> R.string.photo_picker_soft_ask_photos_videos_label
isImagePicker() -> R.string.photo_picker_soft_ask_photos_label
isVideoPicker() -> R.string.photo_picker_soft_ask_videos_label
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class MediaPickerLauncher @Inject constructor(
primaryDataSource = DEVICE,
availableDataSources = setOf(),
canMultiselect = canMultiselect,
requiresPhotosVideosPermissions = false,
requiresPhotosVideosPermissions = allowedTypes.contains(IMAGE) || allowedTypes.contains(VIDEO),
requiresMusicAudioPermissions = allowedTypes.contains(AUDIO),
allowedTypes = allowedTypes,
cameraSetup = HIDDEN,
Expand Down
4 changes: 3 additions & 1 deletion WordPress/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2804,6 +2804,7 @@
<string name="photo_picker_wordpress_media_library">Choose from WordPress Media Library</string>
<string name="photo_picker_gif">Choose from Tenor</string>
<string name="photo_picker_soft_ask_label">%s needs permissions to access your photos</string>
<string name="photo_picker_soft_ask_photos_videos_audio_label">%s needs permission to access your music, audio, photos and videos</string>
<string name="photo_picker_soft_ask_photos_videos_label">%s needs permission to access your photos and videos</string>
<string name="photo_picker_soft_ask_photos_label">%s needs permission to access your photos</string>
<string name="photo_picker_soft_ask_videos_label">%s needs permission to access your videos</string>
Expand All @@ -2829,7 +2830,7 @@
<string name="media_insert_failed_with_reason">Media insert failed: %s</string>
<string name="media_insert_failed">Media insert failed.</string>
<string name="media_loading_failed">Media loading failed</string>
<string name="media_picker_soft_ask_media_permissions_denied">%1$s was denied access to your photos. To fix this, edit your permissions and turn on %2$s.</string>
<string name="media_picker_soft_ask_media_permissions_denied">%1$s was denied access to your media files. To fix this, edit your permissions and turn on %2$s.</string>

<!-- GIFs -->
<string name="gif_powered_by_tenor">Powered by Tenor</string>
Expand Down Expand Up @@ -2857,6 +2858,7 @@
<string name="permission_images">Photos and videos</string>
<string name="permission_video">Photos and videos</string>
<string name="permission_audio">Music and audio</string>
<string name="permission_images_video_audio">Photos and videos &amp; Music and audio</string>
<string name="permission_camera">Camera</string>
<string name="permission_microphone">Microphone</string>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,32 @@ class MediaPickerViewModelTest : BaseUnitTest() {
private var uiStates = mutableListOf<MediaPickerUiState>()
private lateinit var actions: Channel<LoadAction>
private var navigateEvents = mutableListOf<Event<MediaNavigationEvent>>()
private val singleSelectMediaPickerSetup = buildMediaPickerSetup(false, setOf(IMAGE))
private val multiSelectMediaPickerSetup = buildMediaPickerSetup(true, setOf(IMAGE, VIDEO))
private val singleSelectVideoPickerSetup = buildMediaPickerSetup(false, setOf(VIDEO))
private val singleSelectAudioPickerSetup = buildMediaPickerSetup(false, setOf(AUDIO))
private val multiSelectFilePickerSetup = buildMediaPickerSetup(true, setOf(IMAGE, VIDEO, AUDIO, DOCUMENT))
private val singleSelectMediaPickerSetup = buildMediaPickerSetup(
false,
setOf(IMAGE),
requiresPhotosVideosPermission = true
)
private val multiSelectMediaPickerSetup = buildMediaPickerSetup(
true,
setOf(IMAGE, VIDEO),
requiresPhotosVideosPermission = true
)
private val singleSelectVideoPickerSetup = buildMediaPickerSetup(
false,
setOf(VIDEO),
requiresPhotosVideosPermission = true
)
private val singleSelectAudioPickerSetup = buildMediaPickerSetup(
false,
setOf(AUDIO),
requiresMusicAudioPermission = true
)
private val multiSelectFilePickerSetup = buildMediaPickerSetup(
true,
setOf(IMAGE, VIDEO, AUDIO, DOCUMENT),
requiresPhotosVideosPermission = true,
requiresMusicAudioPermission = true
)
private val site = SiteModel()
private lateinit var firstItem: MediaItem
private lateinit var secondItem: MediaItem
Expand Down Expand Up @@ -950,8 +971,8 @@ class MediaPickerViewModelTest : BaseUnitTest() {
allowedTypes: Set<MediaType>,
cameraSetup: CameraSetup = HIDDEN,
editingEnabled: Boolean = true,
requiresPhotosVideosPermission: Boolean = true,
requiresMusicAudioPermission: Boolean = true
requiresPhotosVideosPermission: Boolean = false,
requiresMusicAudioPermission: Boolean = false
) = MediaPickerSetup(
primaryDataSource = DEVICE,
availableDataSources = setOf(),
Expand Down

0 comments on commit ac21e3c

Please sign in to comment.