fix: camera option in file picker will now ask for camera permissions if not granted. (#8)

Signed-off-by: Alex Yong <alexjyong@gmail.com>
This commit is contained in:
Alex Yong 2025-09-05 18:40:22 -04:00 committed by GitHub
parent d4eaea0709
commit d3cd1545b8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 38 additions and 2 deletions

View File

@ -1,8 +1,10 @@
package chat.revolt.screens.chat.views.channel package chat.revolt.screens.chat.views.channel
import android.Manifest
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.Activity import android.app.Activity
import android.content.ContentValues import android.content.ContentValues
import android.content.pm.PackageManager
import android.content.res.Configuration import android.content.res.Configuration
import android.net.Uri import android.net.Uri
import android.os.Environment import android.os.Environment
@ -103,6 +105,7 @@ import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.em import androidx.compose.ui.unit.em
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.core.content.ContextCompat
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import androidx.hilt.navigation.compose.hiltViewModel import androidx.hilt.navigation.compose.hiltViewModel
import chat.revolt.R import chat.revolt.R
@ -374,6 +377,38 @@ fun ChannelScreen(
} }
} }
val cameraPermissionLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.RequestPermission()
) { isGranted ->
if (isGranted) {
openCameraCallback()
}
else {
Toast.makeText(
context,
context.getString(
R.string.file_picker_chip_camera_permission_denied
),
Toast.LENGTH_SHORT
).show()
}
}
val checkAndRequestCameraPermission = {
when {
ContextCompat.checkSelfPermission(
context,
Manifest.permission.CAMERA
) == PackageManager.PERMISSION_GRANTED -> {
openCameraCallback()
}
else -> {
cameraPermissionLauncher.launch(Manifest.permission.CAMERA)
}
}
}
val openDocumentPickerCallback = { val openDocumentPickerCallback = {
pickFileLauncher.launch(arrayOf("*/*")) pickFileLauncher.launch(arrayOf("*/*"))
} }
@ -1134,7 +1169,7 @@ fun ChannelScreen(
}, },
text = { Text(stringResource(R.string.file_picker_chip_camera)) }, text = { Text(stringResource(R.string.file_picker_chip_camera)) },
onClick = { onClick = {
openCameraCallback() checkAndRequestCameraPermission()
viewModel.activePane = viewModel.activePane =
ChannelScreenActivePane.None ChannelScreenActivePane.None
} }
@ -1250,7 +1285,7 @@ fun ChannelScreen(
ChannelScreenActivePane.None ChannelScreenActivePane.None
}, },
onOpenCamera = { onOpenCamera = {
openCameraCallback() checkAndRequestCameraPermission()
viewModel.activePane = viewModel.activePane =
ChannelScreenActivePane.None ChannelScreenActivePane.None
}, },

View File

@ -597,6 +597,7 @@
<string name="file_picker_chip_camera">Camera</string> <string name="file_picker_chip_camera">Camera</string>
<string name="file_picker_chip_camera_none_installed">No camera app installed</string> <string name="file_picker_chip_camera_none_installed">No camera app installed</string>
<string name="file_picker_chip_camera_failed">Failed to open camera</string> <string name="file_picker_chip_camera_failed">Failed to open camera</string>
<string name="file_picker_chip_camera_permission_denied">Camera permission is required to take photos</string>
<string name="inline_media_picker_current_description">Currently selected media</string> <string name="inline_media_picker_current_description">Currently selected media</string>
<string name="inline_media_picker_no_media_placeholder">Pick media…</string> <string name="inline_media_picker_no_media_placeholder">Pick media…</string>