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

[Fix] Firestore에서 세션 정보를 받아오는 임시 구현을 제거하고, Remote Config에서 가져오도록 복구 #343

Merged
merged 1 commit into from
Jun 24, 2024
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
@@ -1,9 +1,17 @@
package com.yapp.data.datasource

import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.ktx.Firebase
import com.google.firebase.remoteconfig.ktx.remoteConfig
import com.google.firebase.remoteconfig.ktx.remoteConfigSettings
import com.yapp.data.BuildConfig
import com.yapp.data.model.SessionEntity
import com.yapp.data.util.sessionRef
import com.yapp.domain.firebase.RemoteConfigData
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import javax.inject.Inject
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
Expand All @@ -12,6 +20,16 @@ class SessionRemoteDataSourceImpl @Inject constructor(
private val fireStore: FirebaseFirestore,
) : SessionRemoteDataSource {

private val firebaseRemoteConfig = Firebase.remoteConfig
private val configSettings = remoteConfigSettings {
minimumFetchIntervalInSeconds = if (BuildConfig.DEBUG) 60 else 3600
}

init {
firebaseRemoteConfig.setDefaultsAsync(RemoteConfigData.defaultMaps)
firebaseRemoteConfig.setConfigSettingsAsync(configSettings)
}

override suspend fun setSession(session: SessionEntity) {
return suspendCancellableCoroutine { cancellableContinuation ->
fireStore.sessionRef()
Expand Down Expand Up @@ -47,23 +65,19 @@ class SessionRemoteDataSourceImpl @Inject constructor(
}
}

@OptIn(ExperimentalCoroutinesApi::class)
override suspend fun getAllSession(): List<SessionEntity> {
return suspendCancellableCoroutine { cancellableContinuation ->
fireStore.sessionRef()
.get()
.addOnSuccessListener { documents ->
if (documents.isEmpty) {
cancellableContinuation.resume(emptyList())
return@addOnSuccessListener
firebaseRemoteConfig.fetchAndActivate().addOnSuccessListener {
val entities = firebaseRemoteConfig.getString(RemoteConfigData.SessionList.key)
.let { jsonString ->
Json.decodeFromString<List<SessionEntity>>(jsonString)
}

documents.toObjects(SessionEntity::class.java).also { entity ->
cancellableContinuation.resume(entity)
}
}
.addOnFailureListener { exception ->
cancellableContinuation.resumeWithException(exception)
}
cancellableContinuation.resume(value = entities, onCancellation = null)
}.addOnFailureListener { exception ->
cancellableContinuation.resumeWithException(exception)
}
}
}
}
36 changes: 15 additions & 21 deletions data/src/main/java/com/yapp/data/model/SessionEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,27 @@ import com.google.firebase.firestore.PropertyName
import com.yapp.domain.model.Session
import com.yapp.domain.model.types.NeedToAttendType
import com.yapp.domain.util.DateParser
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class SessionEntity(
@PropertyName("sessionId")
val sessionId: Int? = null,
@PropertyName("title")
val title: String? = null,
@PropertyName("type")
val type: String? = null,
@PropertyName("startTime")
val startTime: String? = null,
@PropertyName("description")
val description: String? = null,
@PropertyName("code")
val code: String? = null
@SerialName("session_id")
val sessionId: Int,
val title: String,
val type: String,
@SerialName("date")
val startTime: String,
val description: String,
)

fun SessionEntity.toDomain(dateParser: DateParser): Session {
return Session(
sessionId = sessionId!!,
title = title!!,
type = NeedToAttendType.valueOf(type!!),
startTime = dateParser.parse(rawDate = startTime!!),
description = description!!,
code = code!!
sessionId = sessionId,
title = title,
type = NeedToAttendType.valueOf(type),
startTime = dateParser.parse(rawDate = startTime),
description = description
)
}

Expand All @@ -39,7 +34,6 @@ fun Session.toData(dateParser: DateParser): SessionEntity {
title = title,
type = type.name,
startTime = dateParser.format(date = startTime),
description = description,
code = code
description = description
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ sealed class RemoteConfigData<T> {
override val defaultValue: String = "fail"
}

object SessionList : RemoteConfigData<String>() {
override val key: String = ATTENDANCE_SESSION_LIST
override val defaultValue: String = ""
}


object Config : RemoteConfigData<String>() {
override val key: String = ATTENDANCE_CONFIG
override val defaultValue: String = ""
Expand Down Expand Up @@ -46,6 +52,7 @@ sealed class RemoteConfigData<T> {

companion object {
private const val ATTENDANCE_MAGINOTLINE_TIME = "attendance_maginotline_time"
private const val ATTENDANCE_SESSION_LIST = "attendance_session_list"
private const val ATTENDANCE_SELECT_TEAMS = "attendance_select_teams"
private const val ATTENDANCE_CONFIG = "config"
private const val ATTENDANCE_QR_PASSWORD = "attendance_qr_password"
Expand All @@ -56,6 +63,7 @@ sealed class RemoteConfigData<T> {

val defaultMaps = mapOf(
MaginotlineTime.defaultValue to MaginotlineTime.key,
SessionList.defaultValue to SessionList.key,
AttendanceSelectTeams.defaultValue to AttendanceSelectTeams.key,
Config.defaultValue to Config.key,
QrPassword.defaultValue to QrPassword.key,
Expand Down
2 changes: 1 addition & 1 deletion domain/src/main/java/com/yapp/domain/model/Member.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ data class Member(
val position: PositionType,
val team: Team,
val attendances: AttendanceList
)
)
7 changes: 2 additions & 5 deletions domain/src/main/java/com/yapp/domain/model/Session.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@ data class Session(
val title: String,
val type: NeedToAttendType,
val startTime: LocalDateTime,
val description: String,
val code: String
val description: String
) {

val monthAndDay: String
get() = String.format(TWO_DIGIT, startTime.month.value) + "." + String.format(TWO_DIGIT, startTime.dayOfMonth)

companion object {
private const val TWO_DIGIT = "%02d"
}

}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.yapp.domain.usecases

import com.yapp.domain.repository.SessionRepository
import com.yapp.domain.repository.RemoteConfigRepository
import javax.inject.Inject

class CheckSessionPasswordUseCase @Inject constructor(
private val sessionRepository: SessionRepository
private val remoteConfigRepository: RemoteConfigRepository
) {
suspend operator fun invoke(sessionId: Int, inputPassword: String): Result<Boolean> {
return sessionRepository.getSession(id = sessionId).mapCatching { session ->
check(session != null) { "${sessionId}에 해당하는 Session이 존재하지 않습니다" }

inputPassword == session.code
suspend operator fun invoke(inputPassword: String): Result<Boolean> {
return remoteConfigRepository.getSessionPassword().mapCatching { sessionPassword ->
inputPassword == sessionPassword
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class SetSessionUseCase @Inject constructor(
type = type,
startTime = dateParser.parse(rawDate = rawDate),
description = description,
code = code
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.google.firebase.FirebaseNetworkException
import com.yapp.common.base.BaseViewModel
import com.yapp.domain.usecases.CheckSessionPasswordUseCase
import com.yapp.domain.usecases.CheckSignUpPasswordUseCase
import com.yapp.presentation.common.AttendanceBundle
import com.yapp.presentation.ui.member.signup.password.PasswordContract.PasswordSideEffect
import com.yapp.presentation.ui.member.signup.password.PasswordContract.PasswordUiEvent
import com.yapp.presentation.ui.member.signup.password.PasswordContract.PasswordUiState
Expand Down Expand Up @@ -72,10 +71,7 @@ internal class PassWordViewModel @Inject constructor(
private fun checkSessionPassword(password: String) = viewModelScope.launch {
setEffect(PasswordSideEffect.KeyboardHide)

// TODO AttendanceBundle을 사용하지 않고 Navagation의 Argument로 넘어온 Id를 사용하도록 수정할것
val sessionId = AttendanceBundle.upComingSession!!.sessionId

checkSessionPasswordUseCase(sessionId = sessionId, inputPassword = password)
checkSessionPasswordUseCase(inputPassword = password)
.onSuccess { isPasswordValid ->
when (isPasswordValid) {
true -> {
Expand Down
Loading