Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/onapp take photo #60

Merged
merged 18 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions frontend/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-feature
android:name="android.hardware.camera"
android:required="false" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
android:name=".BaseApplication"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.example.speechbuddy.compose.symbolcreation

import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.net.Uri
import android.widget.Toast
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.result.contract.ActivityResultContracts.RequestPermission
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
Expand All @@ -27,6 +30,10 @@ import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowDropDown
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonColors
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.DropdownMenu
Expand All @@ -41,6 +48,8 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ImageBitmap
Expand All @@ -49,14 +58,18 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat
import androidx.hilt.navigation.compose.hiltViewModel
import com.example.speechbuddy.R
import com.example.speechbuddy.compose.utils.ButtonUi
import com.example.speechbuddy.compose.utils.TextFieldUi
import com.example.speechbuddy.compose.utils.TopAppBarUi
import com.example.speechbuddy.compose.utils.TitleUi
import com.example.speechbuddy.compose.utils.TopAppBarUi
import com.example.speechbuddy.domain.models.Category
import com.example.speechbuddy.ui.SpeechBuddyTheme
import com.example.speechbuddy.ui.models.PhotoType
import com.example.speechbuddy.ui.models.SymbolCreationErrorType
import com.example.speechbuddy.ui.models.SymbolCreationUiState
import com.example.speechbuddy.utils.Constants
Expand All @@ -74,6 +87,8 @@ fun SymbolCreationScreen(
val uiState by viewModel.uiState.collectAsState()
val categories by viewModel.categories.observeAsState(emptyList())

val showDialog = remember { mutableStateOf(false) }
89645321 marked this conversation as resolved.
Show resolved Hide resolved

val isSymbolTextError = uiState.error?.type == SymbolCreationErrorType.SYMBOL_TEXT
val isCategoryError = uiState.error?.type == SymbolCreationErrorType.CATEGORY
val isPhotoInputError = uiState.error?.type == SymbolCreationErrorType.PHOTO_INPUT
Expand All @@ -86,6 +101,27 @@ fun SymbolCreationScreen(
rememberLauncherForActivityResult(contract = ActivityResultContracts.GetContent()) { uri: Uri? ->
viewModel.setPhotoInputUri(uri, context)
}
// Take photo
val cameraLauncher =
rememberLauncherForActivityResult(contract = ActivityResultContracts.TakePicturePreview()) { bitmap: Bitmap? ->
bitmap?.let {
viewModel.updatePhotoInputBitmap(bitmap)
}
}
// Request permission launcher
val requestPermissionLauncher = rememberLauncherForActivityResult(
contract = RequestPermission()
) { isGranted: Boolean ->
if (isGranted) {
cameraLauncher.launch(null)
} else {
Toast.makeText(
context,
"Camera permission is required to take photos",
Toast.LENGTH_SHORT
).show()
}
}

Surface(
modifier = modifier.fillMaxSize()
Expand Down Expand Up @@ -114,13 +150,41 @@ fun SymbolCreationScreen(
Spacer(modifier = Modifier.height(30.dp))

AddPhotoButton(
onClick = { galleryLauncher.launch("image/*") },
onClick = { showDialog.value = true },
isError = isPhotoInputError,
viewModel = viewModel
)

if (showDialog.value) {
PhotoOptionDialog(
onDismissRequest = { showDialog.value = false },
onCameraClick = {
// Check if permission is already granted
when (PackageManager.PERMISSION_GRANTED) {
ContextCompat.checkSelfPermission(
context,
android.Manifest.permission.CAMERA
) -> {
cameraLauncher.launch(null)
}

else -> {
requestPermissionLauncher.launch(android.Manifest.permission.CAMERA)
}
}
viewModel.photoType = PhotoType.CAMERA
},
onGalleryClick = {
galleryLauncher.launch("image/*")
viewModel.photoType = PhotoType.GALLERY
},
onCancelClick = { showDialog.value = false }
)
}

Spacer(modifier = Modifier.height(30.dp))

// Symbol Category Field
DropdownUi(
selectedValue = viewModel.categoryInput,
onValueChange = { viewModel.setCategory(it) },
Expand Down Expand Up @@ -148,6 +212,7 @@ fun SymbolCreationScreen(
isValid = uiState.isValidSymbolText
)

// Symbol Creation Button
ButtonUi(
text = stringResource(id = R.string.create),
onClick = { viewModel.createSymbol(context) },
Expand Down Expand Up @@ -240,15 +305,16 @@ private fun AddPhotoButton(
val color =
if (isError) MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.surfaceVariant

Box(contentAlignment = Alignment.Center,
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.size(140.dp)
.clickable { onClick() }
.clickable(onClick = onClick)
.border(
border = BorderStroke(1.dp, color), shape = RoundedCornerShape(10.dp)
)) {
)
) {
if (viewModel.photoInputBitmap != null) {
// Display the loaded image if the photo input bitmap is not null
SymbolPreview(
bitmap = viewModel.photoInputBitmap!!.asImageBitmap(),
text = viewModel.symbolTextInput
Expand All @@ -262,6 +328,94 @@ private fun AddPhotoButton(
}
}

@Composable
fun PhotoOptionDialog(
onDismissRequest: () -> Unit,
onCameraClick: () -> Unit,
onGalleryClick: () -> Unit,
onCancelClick: () -> Unit
) {
AlertDialog(
onDismissRequest = onDismissRequest,
confirmButton = {},
dismissButton = {},
title = {
Box(
modifier = Modifier.fillMaxWidth(),
contentAlignment = Alignment.Center
) {
Text(
text = stringResource(id = R.string.choose_photo_option),
style = MaterialTheme.typography.bodyLarge
)
}
},
text = {
Column(
verticalArrangement = Arrangement.spacedBy(14.dp)
) {
PhotoOptionButton(
onClick = {
onCameraClick()
onDismissRequest()
},
text = stringResource(id = R.string.take_photo),
colors = ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.primary,
contentColor = MaterialTheme.colorScheme.onPrimary
)
)

PhotoOptionButton(
onClick = {
onGalleryClick()
onDismissRequest()
},
text = stringResource(id = R.string.choose_from_gallery),
colors = ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.primary,
contentColor = MaterialTheme.colorScheme.onPrimary
)
)

PhotoOptionButton(
onClick = {
onCancelClick()
onDismissRequest()
},
text = stringResource(id = R.string.cancel),
colors = ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.surfaceVariant,
contentColor = MaterialTheme.colorScheme.onSurfaceVariant
)
)
}
},
containerColor = MaterialTheme.colorScheme.inverseOnSurface
)
}

@Composable
private fun PhotoOptionButton(
onClick: () -> Unit,
text: String,
colors: ButtonColors
) {
Button(
onClick = onClick,
modifier = Modifier
.fillMaxWidth()
.height(48.dp),
shape = RoundedCornerShape(10.dp),
colors = colors
) {
Text(
text = text,
style = MaterialTheme.typography.bodyLarge
)
}
}

@Composable
private fun SymbolPreview(
bitmap: ImageBitmap,
Expand Down Expand Up @@ -302,4 +456,17 @@ private fun SymbolPreview(
}
}
}
}

@Preview
@Composable
fun PhotoOptionDialogPreview() {
SpeechBuddyTheme {
PhotoOptionDialog(
onDismissRequest = {},
onCameraClick = {},
onGalleryClick = {},
onCancelClick = {}
)
}
}
89645321 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.speechbuddy.ui.models

data class SymbolCreationUiState (
data class SymbolCreationUiState(
val isValidSymbolText: Boolean = false,
val isValidCategory: Boolean = false,
val isValidPhotoInput: Boolean = false,
Expand All @@ -18,4 +18,9 @@ enum class SymbolCreationErrorType {
CATEGORY,
PHOTO_INPUT,
CONNECTION
}

enum class PhotoType {
GALLERY,
CAMERA
}
Loading