Skip to content

Commit

Permalink
[FEAT/#46] ocr post user check 서버통신 로직 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
b1urrrr committed Jan 13, 2023
1 parent 432b01e commit b7e051b
Show file tree
Hide file tree
Showing 13 changed files with 298 additions and 8 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ dependencies {
// shared preference
implementation 'androidx.security:security-crypto-ktx:1.1.0-alpha04'

// firebase
implementation platform('com.google.firebase:firebase-bom:31.1.1')
implementation 'com.google.firebase:firebase-analytics-ktx'

// firebase storage
implementation 'com.google.firebase:firebase-core:21.1.1'
implementation 'com.google.firebase:firebase-storage:20.1.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.keyneez.data.model.request

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class RequestPostStudentUserCheckDto(
@SerialName("user_school")
val school: String,
@SerialName("user_name")
val name: String,
@SerialName("user_ocr")
val ocrImg: String,
@SerialName("ocr_dir")
val ocrDir: Boolean
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.keyneez.data.model.request

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class RequestPostYouthUserCheckDto(
@SerialName("user_name")
val name : String,
@SerialName("user_birth")
val birth : String,
@SerialName("user_ocr")
val ocrImg: String,
@SerialName("ocr_dir")
val ocrDir: Boolean
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.keyneez.data.model.response

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class ResponsePostUserCheckDto(
@SerialName("user_key")
val key: Int,
@SerialName("user_name")
val name: String,
@SerialName("user_age")
val age: String?,
@SerialName("user_gender")
val gender: String?,
@SerialName("user_birth")
val birthDate: String?,
@SerialName("user_school")
val school: String?,
@SerialName("user_character")
val character: Int?,
@SerialName("user_password")
val password: String?,
@SerialName("user_ocr")
val ocrImg: String?,
@SerialName("ocr_dir")
val ocrDir: Boolean?,
@SerialName("user_benefit")
val benefit: Boolean?,
val Characters: Character?
) {
@Serializable
data class Character(
@SerialName("character_key")
val key: Int,
val inter: String?,
val dispo: String?,
val character: String?,
@SerialName("character_img")
val img: String?,
@SerialName("character_desc")
val description: String?,
@SerialName("test_img")
val testImg: String?
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@ interface UserRepository {
suspend fun postPwdCheck(
requestPostPwdCheckDto: RequestPostPwdCheckDto
): Result<Unit>

suspend fun postStudentUserCheck(
requestPostStudentUserCheckDto: RequestPostStudentUserCheckDto
): Result<BaseResponse<ResponsePostUserCheckDto>>

suspend fun postYouthUserCheck(
requestPostYouthUserCheckDto: RequestPostYouthUserCheckDto
): Result<BaseResponse<ResponsePostUserCheckDto>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,14 @@ class UserRepositoryImpl @Inject constructor(
requestPostPwdCheckDto: RequestPostPwdCheckDto
): Result<Unit> =
kotlin.runCatching { userDataSource.postPwdCheck(requestPostPwdCheckDto) }

override suspend fun postStudentUserCheck(
requestPostStudentUserCheckDto: RequestPostStudentUserCheckDto
): Result<BaseResponse<ResponsePostUserCheckDto>> =
kotlin.runCatching { userDataSource.postStudentUserCheck(requestPostStudentUserCheckDto) }

override suspend fun postYouthUserCheck(
requestPostYouthUserCheckDto: RequestPostYouthUserCheckDto
): Result<BaseResponse<ResponsePostUserCheckDto>> =
kotlin.runCatching { userDataSource.postYouthUserCheck(requestPostYouthUserCheckDto) }
}
12 changes: 12 additions & 0 deletions app/src/main/java/com/keyneez/data/service/UserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,16 @@ interface UserService {
suspend fun postUserLogIn(
@Body request: RequestPostUserLogInDto
): BaseResponse<ResponsePostUserLogInDto>

// 학생증 유저 대조 (다날*학생)
@POST("user/check")
suspend fun postCheckStudentUser(
@Body request: RequestPostStudentUserCheckDto
): BaseResponse<ResponsePostUserCheckDto>

// 청소년증 유저 대조 (다날*학생)
@POST("user/check")
suspend fun postCheckYouthUser(
@Body request: RequestPostYouthUserCheckDto
): BaseResponse<ResponsePostUserCheckDto>
}
10 changes: 10 additions & 0 deletions app/src/main/java/com/keyneez/data/source/UserDataSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,14 @@ class UserDataSource @Inject constructor(
suspend fun postPwdCheck(
requestPostPwdCheckDto: RequestPostPwdCheckDto
): Unit = userService.postPwdCheck(requestPostPwdCheckDto)

suspend fun postStudentUserCheck(
requestPostStudentUserCheckDto: RequestPostStudentUserCheckDto
): BaseResponse<ResponsePostUserCheckDto> =
userService.postCheckStudentUser(requestPostStudentUserCheckDto)

suspend fun postYouthUserCheck(
requestPostYouthUserCheckDto: RequestPostYouthUserCheckDto
): BaseResponse<ResponsePostUserCheckDto> =
userService.postCheckYouthUser(requestPostYouthUserCheckDto)
}
21 changes: 17 additions & 4 deletions app/src/main/java/com/keyneez/presentation/ocr/OcrActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,23 @@ class OcrActivity : BindingActivity<ActivityOcrBinding>(R.layout.activity_ocr) {
isSuccess = true
}
else -> {
if (isSuccess && viewModel.idName.value == "" && word.length == 3) viewModel.setIdName(word)
if (isSuccess && viewModel.idName.value == "" && word.startsWith("명:")) viewModel.setIdName(word.substring(2, 5))
if (isSuccess && viewModel.isStudentId.value == true && viewModel.idSubEntry.value == "" && word.endsWith("학교")) viewModel.setIdSchool(word)
if (isSuccess && viewModel.isStudentId.value == false && viewModel.idSubEntry.value == "" && word.length == 14 && word.contains('-')) viewModel.setBirthDate(word)
if (isSuccess && viewModel.idName.value == "" && word.length == 3) viewModel.setIdName(
word
)
if (isSuccess && viewModel.idName.value == "" && word.startsWith("명:")) viewModel.setIdName(
word.substring(2, 5)
)
if (isSuccess && viewModel.idName.value == "" && word.startsWith(":") && word.length == 4) viewModel.setIdName(
word.substring(1, 4)
)
if (isSuccess && viewModel.isStudentId.value == true && viewModel.idSubEntry.value == "" && word.endsWith(
"학교"
)
) viewModel.setIdSchool(word)
if (isSuccess && viewModel.isStudentId.value == false && viewModel.idSubEntry.value == "" && word.length == 14 && word.contains(
'-'
)
) viewModel.setBirthDate(word)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class OcrViewModel @Inject constructor() : ViewModel() {
val isStudentId: LiveData<Boolean>
get() = _isStudentId

private val _imgUrl = MutableLiveData<String>()
val imgUrl: LiveData<String>
get() = _imgUrl

init {
_isVertical.value = false
_isPassive.value = false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
package com.keyneez.presentation.ocr.dialog

import android.app.Activity.RESULT_OK
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import com.keyneez.presentation.ocr.OcrActivity
import com.keyneez.presentation.ocr.guide.OcrGuideActivity
import com.keyneez.util.UiState
import com.keyneez.util.binding.BindingBottomSheetDialog
import com.keyneez.util.extension.hideKeyboard
import com.keyneez.util.extension.setOnSingleClickListener
import com.keyneez.util.extension.showSnackbar
import com.lab.keyneez.R
import com.lab.keyneez.databinding.BotSheetOcrResultBinding

class OcrResultFragment :
BindingBottomSheetDialog<BotSheetOcrResultBinding>(R.layout.bot_sheet_ocr_result) {
private val viewModel by viewModels<OcrResultViewModel>()

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.vm = (activity as OcrActivity).viewModel

initHideKeyboard()
initReshootBtnClickListener()
initConfirmBtnClickListener()
setupCheckUserState()
}

private fun initHideKeyboard() {
Expand All @@ -37,10 +43,35 @@ class OcrResultFragment :

private fun initConfirmBtnClickListener() {
binding.btnOcrResultConfirm.setOnSingleClickListener {
val intent = Intent(activity, OcrGuideActivity::class.java).apply {
requireActivity().setResult(RESULT_OK, this)
val avm = (activity as OcrActivity).viewModel
val isStudent = avm.isStudentId.value ?: true
val name = avm.idName.value.toString()
val subEntry = avm.idSubEntry.value.toString()
val img = avm.imgUrl.value.toString()
val isVertical = avm.isVertical.value ?: false

viewModel.postUserCheck(isStudent, name, subEntry, img, isVertical)
}
}

private fun setupCheckUserState() {
viewModel.stateMessage.observe(viewLifecycleOwner) {
when (it) {
is UiState.Success -> {
val intent = Intent(activity, OcrGuideActivity::class.java).apply {
requireActivity().setResult(Activity.RESULT_OK, this)
}
requireActivity().finish()
}
is UiState.Failure -> requireContext().showSnackbar(
binding.root,
getString(R.string.msg_error)
)
is UiState.Error -> requireContext().showSnackbar(
binding.root,
getString(R.string.msg_error)
)
}
requireActivity().finish()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package com.keyneez.presentation.ocr.dialog

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.keyneez.data.model.request.RequestPostStudentUserCheckDto
import com.keyneez.data.model.request.RequestPostYouthUserCheckDto
import com.keyneez.data.repository.UserRepository
import com.keyneez.util.UiState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import retrofit2.HttpException
import timber.log.Timber
import javax.inject.Inject

@HiltViewModel
class OcrResultViewModel @Inject constructor(
private val userRepository: UserRepository
) : ViewModel() {
private val _stateMessage = MutableLiveData<UiState>()
val stateMessage: LiveData<UiState>
get() = _stateMessage

fun postUserCheck(
isStudent: Boolean,
name: String,
subEntry: String,
img: String,
isVertical: Boolean
) {
// 학생증인 경우
if (isStudent) {
viewModelScope.launch {
userRepository.postStudentUserCheck(
RequestPostStudentUserCheckDto(
subEntry,
name,
img,
isVertical
)
)
.onSuccess { response ->
Timber.tag(successTag).d("response : $response")

if (response.data == null) {
_stateMessage.value = UiState.Failure(RESPONSE_NULL_CODE)
return@onSuccess
}

_stateMessage.value = UiState.Success
}
.onFailure {
Timber.tag(failTag).e("throwable: $it")
if (it is HttpException) {
Timber.tag(failTag).e("code : ${it.code()}")
Timber.tag(failTag).e("message : ${it.message()}")

when (it.code()) {
INVALID_TOKEN_CODE -> _stateMessage.value = UiState.Failure(
INVALID_TOKEN_CODE
)
CHECK_FAIL_CODE -> _stateMessage.value = UiState.Failure(
CHECK_FAIL_CODE
)
else -> _stateMessage.value = UiState.Error
}
} else _stateMessage.value = UiState.Error
}
}
}
// 청소년증인 경우
else {
viewModelScope.launch {
userRepository.postYouthUserCheck(RequestPostYouthUserCheckDto(name, subEntry, img, isVertical))
.onSuccess { response ->
Timber.tag(successTag).d("response : $response")

if (response.data == null) {
_stateMessage.value = UiState.Failure(RESPONSE_NULL_CODE)
return@onSuccess
}

_stateMessage.value = UiState.Success
}
.onFailure {
Timber.tag(failTag).e("throwable: $it")
if (it is HttpException) {
Timber.tag(failTag).e("code : ${it.code()}")
Timber.tag(failTag).e("message : ${it.message()}")

when (it.code()) {
INVALID_TOKEN_CODE -> _stateMessage.value = UiState.Failure(
INVALID_TOKEN_CODE
)
CHECK_FAIL_CODE -> _stateMessage.value = UiState.Failure(
CHECK_FAIL_CODE
)
else -> _stateMessage.value = UiState.Error
}
} else _stateMessage.value = UiState.Error
}
}
}
}

companion object {
const val RESPONSE_NULL_CODE = 100
const val INVALID_TOKEN_CODE = 401
const val CHECK_FAIL_CODE = 400

private const val successTag = "POST_CHECK_USER_SUCCESS"
private const val failTag = "POST_CHECK_USER_FAIL"
}
}
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
buildscript {
repositories {
google()
mavenCentral()
}

dependencies {
classpath 'com.google.gms:google-services:4.3.13'
}
Expand Down

0 comments on commit b7e051b

Please sign in to comment.