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

상징 생성 토스트 에러 해결 #57

Merged
merged 2 commits into from
Nov 23, 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
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
Expand All @@ -54,8 +55,8 @@ 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.models.SymbolCreationErrorType
import com.example.speechbuddy.ui.models.SymbolCreationUiState
Expand Down Expand Up @@ -87,6 +88,13 @@ fun SymbolCreationScreen(
viewModel.setPhotoInputUri(uri, context)
}

if (creationResultMessage != null) {
LaunchedEffect(key1 = creationResultMessage) {
val toastMessage = context.resources.getString(creationResultMessage!!)
Toast.makeText(context, toastMessage, Toast.LENGTH_SHORT).show()
}
}

Surface(
modifier = modifier.fillMaxSize()
) {
Expand Down Expand Up @@ -158,10 +166,6 @@ fun SymbolCreationScreen(
}
}
}
if (creationResultMessage != null) {
val toastMessage = stringResource(id = creationResultMessage!!)
Toast.makeText(context, toastMessage, Toast.LENGTH_SHORT).show()
}
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface SymbolDao {
fun getSymbols(): Flow<List<SymbolEntity>>

@Query("SELECT * FROM symbols ORDER BY id DESC LIMIT 1")
fun getLastSymbol(): Flow<List<SymbolEntity>>
fun getLastSymbol(): Flow<SymbolEntity>

@Query("SELECT * FROM symbols WHERE isFavorite = 1")
fun getFavoriteSymbols(): Flow<List<SymbolEntity>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,12 @@ class SymbolRepository @Inject constructor(
private val categoryMapper: CategoryMapper
) {


fun getSymbols(query: String) =
if (query.isBlank()) getAllSymbols()
else symbolDao.getSymbolsByQuery(query).map { symbolEntities ->
symbolEntities.map { symbolEntity -> symbolMapper.mapToDomainModel(symbolEntity) }
}

suspend fun deleteAllSymbols() {
symbolDao.deleteAllSymbols()
}

fun getLastSymbol() = symbolDao.getLastSymbol().map { symbolEntities ->
symbolEntities.first().let { symbolEntity -> symbolMapper.mapToDomainModel(symbolEntity) }
}

fun getCategories(query: String) =
if (query.isBlank()) getAllCategories()
else categoryDao.getCategoriesByQuery(query).map { categoryEntities ->
Expand Down Expand Up @@ -116,6 +107,13 @@ class SymbolRepository @Inject constructor(
symbolDao.updateSymbol(symbolEntity)
}

fun getNextSymbolId() =
symbolDao.getLastSymbol().map { symbol -> symbol.id +1 }

fun clearAllMySymbols() {
/* TODO: 내가 만든 상징들 모두 삭제 */
}

suspend fun insertSymbol(symbol: Symbol) {
val symbolEntity = symbolMapper.mapFromDomainModel(symbol)
symbolDao.insertSymbol(symbolEntity)
Expand All @@ -126,7 +124,12 @@ class SymbolRepository @Inject constructor(
categoryId: Int,
image: MultipartBody.Part
): Flow<Resource<MySymbol>> {
return mySymbolRemoteSource.createSymbolBackup(getAuthHeader(), symbolText, categoryId, image)
return mySymbolRemoteSource.createSymbolBackup(
getAuthHeader(),
symbolText,
categoryId,
image
)
.map { response ->
if (response.isSuccessful && response.code() == ResponseCode.SUCCESS.value) {
response.body()?.let { mySymbolDto ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ class Constants {
const val CODE_LENGTH = 6
const val MAXIMUM_LINES_FOR_SYMBOL_TEXT = 2
const val MAXIMUM_SYMBOL_TEXT_LENGTH = 20

const val DEFAULT_SYMBOL_COUNT = 500
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,16 @@ import com.example.speechbuddy.repository.WeightTableRepository
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
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
Expand Down Expand Up @@ -134,6 +131,7 @@ class SymbolCreationViewModel @Inject internal constructor(
}
}
}

private fun clearInput() {
symbolTextInput = ""
categoryInput = null
Expand Down Expand Up @@ -223,11 +221,10 @@ class SymbolCreationViewModel @Inject internal constructor(
}
} else {
// If guest-mode
if(sessionManager.userId.value ==-1){
if (sessionManager.userId.value == GUEST) {
viewModelScope.launch {
val lastSymbol = repository.getLastSymbol().first()
val symbolId = lastSymbol.id+1
val fileName = "symbol_${ symbolId }"
val symbolId = repository.getNextSymbolId().first()
val fileName = "symbol_${symbolId}"
bitmapToFile(context, photoInputBitmap!!, fileName)

val symbol = Symbol(
Expand All @@ -247,13 +244,12 @@ class SymbolCreationViewModel @Inject internal constructor(
// Notify user that the creation was successful
_creationResultMessage.postValue(R.string.create_symbol_success)
}
}
else { // If login-mode
} else { // If login-mode
// file processing
val tempFileName = "symbol_${System.currentTimeMillis()}"
val imageFile = bitmapToFile(context, photoInputBitmap!!, tempFileName)
val imagePart = fileToMultipartBodyPart(imageFile, "image")
viewModelScope.launch() {
viewModelScope.launch {
repository.createSymbolBackup(
symbolText = symbolTextInput,
categoryId = categoryInput!!.id,
Expand All @@ -262,7 +258,7 @@ class SymbolCreationViewModel @Inject internal constructor(
if (resource.status == Status.SUCCESS) {
// Store new symbol in local db
val symbolId = resource.data!!.id
val imageUrl = resource.data!!.imageUrl
val imageUrl = resource.data.imageUrl
val symbol = Symbol(
id = symbolId!!,
text = symbolTextInput,
Expand Down Expand Up @@ -302,4 +298,8 @@ class SymbolCreationViewModel @Inject internal constructor(
}
}
}

companion object {
const val GUEST = -1
}
}