feat: "take a photo" button

Signed-off-by: Infi <wingit@geist.ga>
This commit is contained in:
Infi 2023-07-11 23:59:42 +02:00
parent 0d17f8c9cf
commit ab67ca35e1
2 changed files with 45 additions and 8 deletions

View File

@ -118,8 +118,9 @@ fun InbuiltMediaPicker(
val projection = arrayOf(
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.RESOLUTION,
MediaStore.Images.ImageColumns.ORIENTATION,
MediaStore.Images.ImageColumns.MIME_TYPE,
MediaStore.Video.VideoColumns.DURATION
MediaStore.Video.VideoColumns.DURATION,
)
val selection: String? = null
@ -143,6 +144,10 @@ fun InbuiltMediaPicker(
cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns._ID))
val resolution =
cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.RESOLUTION))
val orientation =
cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION))
val swapDimensions = orientation == 90 || orientation == 270
val isVideo =
cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.MIME_TYPE))
@ -164,11 +169,13 @@ fun InbuiltMediaPicker(
if (resolution == null) continue
val (width, height) = resolution.split("×").map { it.toInt() }
images.add(
Media(
uri = contentUri,
width = resolution.split("×")[0].toInt(),
height = resolution.split("×")[1].toInt(),
width = if (swapDimensions) height else width,
height = if (swapDimensions) width else height,
duration = videoDuration
)
)

View File

@ -1,7 +1,10 @@
package chat.revolt.screens.chat.views.channel
import android.content.ContentValues
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.MediaStore
import android.widget.Toast
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
@ -40,6 +43,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
@ -153,6 +157,17 @@ fun ChannelScreen(
}
}
val capturedPhotoUri = rememberSaveable { mutableStateOf<Uri?>(null) }
val pickCameraLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.TakePicture()
) { uriUpdated ->
if (uriUpdated) {
capturedPhotoUri.value?.let { uri ->
processFileUri(uri)
}
}
}
val scrollDownFABPadding by animateDpAsState(
if (viewModel.typingUsers.isNotEmpty()) 25.dp else 0.dp,
animationSpec = RevoltTweenDp,
@ -472,11 +487,26 @@ fun ChannelScreen(
viewModel.inbuiltFilePickerOpen = false
},
onOpenCamera = {
Toast.makeText(
context,
"This will open the camera one day",
Toast.LENGTH_SHORT
).show()
// Create a new content URI to store the captured image.
val contentResolver = context.contentResolver
val contentValues = ContentValues().apply {
put(
MediaStore.MediaColumns.DISPLAY_NAME,
"RVL_${System.currentTimeMillis()}.jpg"
)
put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
put(
MediaStore.MediaColumns.RELATIVE_PATH,
Environment.DIRECTORY_PICTURES
)
}
capturedPhotoUri.value = contentResolver.insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
contentValues
)
pickCameraLauncher.launch(capturedPhotoUri.value)
viewModel.inbuiltFilePickerOpen = false
},
onClose = {