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 all 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 @@ -51,13 +58,16 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
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.TitleUi
import com.example.speechbuddy.compose.utils.TopAppBarUi
import com.example.speechbuddy.domain.models.Category
import com.example.speechbuddy.ui.models.DialogState
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 Down Expand Up @@ -87,6 +97,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()
}
}

if (creationResultMessage != null) {
LaunchedEffect(key1 = creationResultMessage) {
Expand Down Expand Up @@ -122,13 +153,41 @@ fun SymbolCreationScreen(
Spacer(modifier = Modifier.height(30.dp))

AddPhotoButton(
onClick = { galleryLauncher.launch("image/*") },
onClick = { viewModel.updateDialogState("show") },
isError = isPhotoInputError,
viewModel = viewModel
)

if (viewModel.dialogState == DialogState.SHOW) {
PhotoOptionDialog(
onDismissRequest = { viewModel.updateDialogState("hide") },
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 = { viewModel.updateDialogState("hide") }
)
}

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

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

// Symbol Creation Button
ButtonUi(
text = stringResource(id = R.string.create),
onClick = { viewModel.createSymbol(context) },
Expand Down Expand Up @@ -244,15 +304,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 @@ -266,6 +327,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
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,14 @@ enum class SymbolCreationErrorType {
CATEGORY,
PHOTO_INPUT,
CONNECTION
}

enum class PhotoType {
GALLERY,
CAMERA
}

enum class DialogState {
SHOW,
HIDE
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import com.example.speechbuddy.domain.models.Category
import com.example.speechbuddy.domain.models.Symbol
import com.example.speechbuddy.repository.SymbolRepository
import com.example.speechbuddy.repository.WeightTableRepository
import com.example.speechbuddy.ui.models.DialogState
import com.example.speechbuddy.ui.models.PhotoType
import com.example.speechbuddy.ui.models.SymbolCreationError
import com.example.speechbuddy.ui.models.SymbolCreationErrorType
import com.example.speechbuddy.ui.models.SymbolCreationUiState
import com.example.speechbuddy.utils.Constants.Companion.DEFAULT_SYMBOL_COUNT
import com.example.speechbuddy.utils.Status
import com.example.speechbuddy.utils.isValidSymbolText
import dagger.hilt.android.lifecycle.HiltViewModel
Expand Down Expand Up @@ -60,12 +61,28 @@ class SymbolCreationViewModel @Inject internal constructor(

var photoInputBitmap by mutableStateOf<Bitmap?>(null)

var photoType by mutableStateOf<PhotoType?>(null)

var dialogState by mutableStateOf<DialogState?>(DialogState.HIDE)

var symbolTextInput by mutableStateOf("")
private set

var categoryInput by mutableStateOf<Category?>(null)
private set

fun updateDialogState(updateState: String) {
when (updateState) {
"show" -> {
dialogState = DialogState.SHOW
}

"hide" -> {
dialogState = DialogState.HIDE
}
}
}

fun expandCategory() {
_uiState.update { currentState ->
currentState.copy(
Expand All @@ -89,6 +106,12 @@ class SymbolCreationViewModel @Inject internal constructor(
if (photoInputUri != null) photoInputBitmap = uriToBitmap(photoInputUri, context)
}

fun updatePhotoInputBitmap(bitmap: Bitmap) {
photoInputBitmap = bitmap
validatePhotoInput()
}


fun setSymbolText(input: String) {
symbolTextInput = input
if (_uiState.value.error?.type == SymbolCreationErrorType.SYMBOL_TEXT) validateSymbolText()
Expand All @@ -100,13 +123,23 @@ class SymbolCreationViewModel @Inject internal constructor(
}

private fun validatePhotoInput() {
if (photoInputUri != null) {
if (photoInputBitmap != null) {
_uiState.update { currentState ->
currentState.copy(
isValidPhotoInput = true,
error = null
)
}
} else {
_uiState.update { currentState ->
currentState.copy(
isValidPhotoInput = false,
error = SymbolCreationError(
type = SymbolCreationErrorType.PHOTO_INPUT,
messageId = R.string.no_photo
)
)
}
}
}

Expand Down Expand Up @@ -209,7 +242,10 @@ class SymbolCreationViewModel @Inject internal constructor(
)
)
}
} else if (photoInputUri == null || photoInputBitmap == null) {
} else if (
(photoType == PhotoType.GALLERY && photoInputUri == null)
|| (photoType == PhotoType.CAMERA && photoInputBitmap == null)
) {
_uiState.update { currentState ->
currentState.copy(
isValidPhotoInput = false,
Expand All @@ -220,7 +256,6 @@ class SymbolCreationViewModel @Inject internal constructor(
)
}
} else {
// If guest-mode
if (sessionManager.userId.value == GUEST) {
viewModelScope.launch {
val symbolId = repository.getNextSymbolId().first()
Expand Down
3 changes: 3 additions & 0 deletions frontend/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@
<string name="no_photo">사진을 선택해주세요</string>
<string name="create_symbol_success">상징이 성공적으로 추가되었습니다</string>
<string name="create_symbol_error">상징 생성에 실패하였습니다\n다시 시도해 주세요</string>
<string name="choose_photo_option">사진을 어떻게 추가하실 건가요?</string>
<string name="choose_from_gallery">사진 보관함에서 사진 선택하기</string>
<string name="take_photo">사진 촬영하기</string>

<!-- Settings -->
<string name="settings">설정</string>
Expand Down