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

[AN/USER] feat: 학생 인증 API 연동(#456) #470

Merged
merged 7 commits into from
Sep 29, 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 @@ -4,6 +4,6 @@ import kotlinx.serialization.Serializable

@Serializable
data class SendVerificationRequest(
val userName: String,
val schoolId: Int,
val username: String,
val schoolId: Long,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.festago.festago.data.dto

import com.festago.festago.model.StudentVerificationCode
import kotlinx.serialization.Serializable

@Serializable
data class VerificationRequest(
val code: String,
) {
companion object {
fun from(code: StudentVerificationCode) = VerificationRequest(code.value)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.festago.festago.data.repository

import com.festago.festago.data.dto.SendVerificationRequest
import com.festago.festago.data.dto.VerificationRequest
import com.festago.festago.data.service.StudentVerificationRetrofitService
import com.festago.festago.data.util.runCatchingWithErrorHandler
import com.festago.festago.model.StudentVerificationCode
import com.festago.festago.repository.StudentVerificationRepository
import javax.inject.Inject
Expand All @@ -10,12 +13,18 @@ class StudentVerificationDefaultRepository @Inject constructor(
) : StudentVerificationRepository {

override suspend fun sendVerificationCode(userName: String, schoolId: Long): Result<Unit> {
// TODO: API 연동 작업 필요
return Result.success(Unit)
studentVerificationRetrofitService.sendVerificationCode(
SendVerificationRequest(userName, schoolId),
).runCatchingWithErrorHandler()
.getOrElse { error -> return Result.failure(error) }
.let { return Result.success(Unit) }
}

override suspend fun requestVerificationCodeConfirm(code: StudentVerificationCode): Result<Unit> {
// TODO: API 연동 작업 필요
return Result.success(Unit)
studentVerificationRetrofitService.requestVerification(
VerificationRequest.from(code),
).runCatchingWithErrorHandler()
.getOrElse { error -> return Result.failure(error) }
.let { return Result.success(Unit) }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.festago.festago.data.service

import com.festago.festago.data.dto.SendVerificationRequest
import com.festago.festago.data.dto.VerificationRequest
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.POST
Expand All @@ -10,4 +11,9 @@ interface StudentVerificationRetrofitService {
suspend fun sendVerificationCode(
@Body sendVerificationRequest: SendVerificationRequest,
): Response<Unit>

@POST("/students/verification")
suspend fun requestVerification(
@Body verificationRequest: VerificationRequest,
): Response<Unit>
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import com.festago.festago.R
import com.festago.festago.databinding.ActivityStudentVerificationBinding
import com.festago.festago.presentation.ui.customview.OkDialogFragment
import com.festago.festago.presentation.ui.home.HomeActivity
import com.festago.festago.presentation.util.repeatOnStarted
import dagger.hilt.android.AndroidEntryPoint
import java.time.LocalTime
Expand Down Expand Up @@ -42,7 +44,7 @@ class StudentVerificationActivity : AppCompatActivity() {

private fun initRequestVerificationCodeBtn(schoolId: Long) {
binding.btnRequestVerificationCode.setOnClickListener {
vm.sendVerificationCode(binding.tieVerificationCode.text.toString(), schoolId)
vm.sendVerificationCode(binding.tieUserName.text.toString(), schoolId)
}
}

Expand All @@ -62,8 +64,9 @@ class StudentVerificationActivity : AppCompatActivity() {
private fun handleUiState(uiState: StudentVerificationUiState) {
binding.uiState = uiState
when (uiState) {
is StudentVerificationUiState.Loading -> Unit
is StudentVerificationUiState.Success -> handleSuccess(uiState)
is StudentVerificationUiState.Loading, StudentVerificationUiState.Error -> Unit
is StudentVerificationUiState.Error -> Unit
}
}

Expand All @@ -81,15 +84,31 @@ class StudentVerificationActivity : AppCompatActivity() {

private fun handleEvent(event: StudentVerificationEvent) {
when (event) {
is StudentVerificationEvent.VerificationSuccess -> Unit
is StudentVerificationEvent.VerificationFailure -> Unit
is StudentVerificationEvent.CodeTimeOut -> Unit
is StudentVerificationEvent.VerificationSuccess -> handleVerificationSuccess()
is StudentVerificationEvent.VerificationFailure -> showDialog(FAILURE_VERIFICATION)
is StudentVerificationEvent.VerificationTimeOut -> showDialog(TIME_OUT_VERIFICATION)
is StudentVerificationEvent.SendingEmailFailure -> showDialog(FAILURE_SENDING_EMAIL)
}
}

companion object {
private fun handleVerificationSuccess() {
val intent = HomeActivity.getIntent(this).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP)
}
finishAffinity()
startActivity(intent)
}

private fun showDialog(message: String) {
val dialog = OkDialogFragment.newInstance(message)
dialog.show(supportFragmentManager, OkDialogFragment::class.java.name)
}

companion object {
private const val KEY_SCHOOL_ID = "KEY_SCHOOL_ID"
private const val FAILURE_VERIFICATION = "인증에 실패하였습니다"
private const val TIME_OUT_VERIFICATION = "인증 시간이 만료되었습니다"
private const val FAILURE_SENDING_EMAIL = "이메일 입력을 확인해주세요"

fun getIntent(context: Context, schoolId: Long): Intent {
return Intent(context, StudentVerificationActivity::class.java).apply {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.festago.festago.presentation.ui.studentverification

sealed interface StudentVerificationEvent {
object CodeTimeOut : StudentVerificationEvent
object VerificationTimeOut : StudentVerificationEvent
object VerificationFailure : StudentVerificationEvent
object VerificationSuccess : StudentVerificationEvent
object SendingEmailFailure : StudentVerificationEvent
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class StudentVerificationViewModel @Inject constructor(
setTimer()
}
.onFailure {
_event.emit(StudentVerificationEvent.SendingEmailFailure)
analyticsHelper.logNetworkFailure(
KEY_SEND_VERIFICATION_CODE_LOG,
it.message.toString(),
Expand Down Expand Up @@ -129,7 +130,7 @@ class StudentVerificationViewModel @Inject constructor(
val state = uiState.value as? StudentVerificationUiState.Success ?: return@launch

if (state.remainTime == MIN_REMAIN_TIME) {
_event.emit(StudentVerificationEvent.CodeTimeOut)
_event.emit(StudentVerificationEvent.VerificationTimeOut)
return@launch
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/student_verification_btn_student_verification"
android:textSize="12sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@+id/glEnd"
app:layout_constraintTop_toBottomOf="@+id/tilEmail" />

Expand Down Expand Up @@ -110,13 +112,13 @@
android:id="@+id/tvTimerVerificationCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginEnd="12dp"
android:textColor="@color/md_theme_light_primary"
android:textSize="20sp"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="@id/glEnd"
app:layout_constraintTop_toBottomOf="@id/tilVerificationCode"
app:layout_constraintBottom_toBottomOf="@+id/btnRequestVerificationCode"
app:layout_constraintEnd_toStartOf="@+id/btnRequestVerificationCode"
app:layout_constraintTop_toTopOf="@+id/btnRequestVerificationCode"
tools:text="00:24" />

<Button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class StudentVerificationViewModelTest {
`인증 코드를 확인하면`()

// then
assertThat(awaitItem()).isEqualTo(StudentVerificationEvent.CodeTimeOut)
assertThat(awaitItem()).isEqualTo(StudentVerificationEvent.VerificationTimeOut)
cancelAndIgnoreRemainingEvents()
}
}
Expand Down