From 7a4726660fcc22ee713dd919968643f78ac1d938 Mon Sep 17 00:00:00 2001 From: "Jihee.Han" Date: Thu, 6 Jun 2024 18:21:14 +0900 Subject: [PATCH] =?UTF-8?q?[Fix]=20Firestore=EC=97=90=EC=84=9C=20=EC=84=B8?= =?UTF-8?q?=EC=85=98=20=EC=A0=95=EB=B3=B4=EB=A5=BC=20=EB=B0=9B=EC=95=84?= =?UTF-8?q?=EC=98=A4=EB=8A=94=20=EC=9E=84=EC=8B=9C=20=EA=B5=AC=ED=98=84?= =?UTF-8?q?=EC=9D=84=20=EC=A0=9C=EA=B1=B0=ED=95=98=EA=B3=A0,=20Remote=20Co?= =?UTF-8?q?nfig=EC=97=90=EC=84=9C=20=EA=B0=80=EC=A0=B8=EC=98=A4=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EB=B3=B5=EA=B5=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../datasource/SessionRemoteDataSourceImpl.kt | 40 +++++++++++++------ .../java/com/yapp/data/model/SessionEntity.kt | 36 +++++++---------- .../yapp/domain/firebase/RemoteConfigData.kt | 8 ++++ .../main/java/com/yapp/domain/model/Member.kt | 2 +- .../java/com/yapp/domain/model/Session.kt | 7 +--- .../usecases/CheckSessionPasswordUseCase.kt | 14 +++---- .../yapp/domain/usecases/SetSessionUseCase.kt | 1 - .../signup/password/PassWordViewModel.kt | 6 +-- 8 files changed, 60 insertions(+), 54 deletions(-) diff --git a/data/src/main/java/com/yapp/data/datasource/SessionRemoteDataSourceImpl.kt b/data/src/main/java/com/yapp/data/datasource/SessionRemoteDataSourceImpl.kt index a06d1c8e..f6ed085d 100644 --- a/data/src/main/java/com/yapp/data/datasource/SessionRemoteDataSourceImpl.kt +++ b/data/src/main/java/com/yapp/data/datasource/SessionRemoteDataSourceImpl.kt @@ -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 @@ -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() @@ -47,23 +65,19 @@ class SessionRemoteDataSourceImpl @Inject constructor( } } + @OptIn(ExperimentalCoroutinesApi::class) override suspend fun getAllSession(): List { 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>(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) + } } } } diff --git a/data/src/main/java/com/yapp/data/model/SessionEntity.kt b/data/src/main/java/com/yapp/data/model/SessionEntity.kt index eb888649..e676bd08 100644 --- a/data/src/main/java/com/yapp/data/model/SessionEntity.kt +++ b/data/src/main/java/com/yapp/data/model/SessionEntity.kt @@ -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 ) } @@ -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 ) -} \ No newline at end of file +} diff --git a/domain/src/main/java/com/yapp/domain/firebase/RemoteConfigData.kt b/domain/src/main/java/com/yapp/domain/firebase/RemoteConfigData.kt index 64115db7..2c7a8db8 100644 --- a/domain/src/main/java/com/yapp/domain/firebase/RemoteConfigData.kt +++ b/domain/src/main/java/com/yapp/domain/firebase/RemoteConfigData.kt @@ -9,6 +9,12 @@ sealed class RemoteConfigData { override val defaultValue: String = "fail" } + object SessionList : RemoteConfigData() { + override val key: String = ATTENDANCE_SESSION_LIST + override val defaultValue: String = "" + } + + object Config : RemoteConfigData() { override val key: String = ATTENDANCE_CONFIG override val defaultValue: String = "" @@ -46,6 +52,7 @@ sealed class RemoteConfigData { 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" @@ -56,6 +63,7 @@ sealed class RemoteConfigData { 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, diff --git a/domain/src/main/java/com/yapp/domain/model/Member.kt b/domain/src/main/java/com/yapp/domain/model/Member.kt index 97e29c5a..ff09d991 100644 --- a/domain/src/main/java/com/yapp/domain/model/Member.kt +++ b/domain/src/main/java/com/yapp/domain/model/Member.kt @@ -9,4 +9,4 @@ data class Member( val position: PositionType, val team: Team, val attendances: AttendanceList -) \ No newline at end of file +) diff --git a/domain/src/main/java/com/yapp/domain/model/Session.kt b/domain/src/main/java/com/yapp/domain/model/Session.kt index 9b8c6fa2..b1398ec2 100644 --- a/domain/src/main/java/com/yapp/domain/model/Session.kt +++ b/domain/src/main/java/com/yapp/domain/model/Session.kt @@ -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" } - -} \ No newline at end of file +} diff --git a/domain/src/main/java/com/yapp/domain/usecases/CheckSessionPasswordUseCase.kt b/domain/src/main/java/com/yapp/domain/usecases/CheckSessionPasswordUseCase.kt index 50ba423c..b51b065d 100644 --- a/domain/src/main/java/com/yapp/domain/usecases/CheckSessionPasswordUseCase.kt +++ b/domain/src/main/java/com/yapp/domain/usecases/CheckSessionPasswordUseCase.kt @@ -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 { - return sessionRepository.getSession(id = sessionId).mapCatching { session -> - check(session != null) { "${sessionId}에 해당하는 Session이 존재하지 않습니다" } - - inputPassword == session.code + suspend operator fun invoke(inputPassword: String): Result { + return remoteConfigRepository.getSessionPassword().mapCatching { sessionPassword -> + inputPassword == sessionPassword } } -} \ No newline at end of file +} diff --git a/domain/src/main/java/com/yapp/domain/usecases/SetSessionUseCase.kt b/domain/src/main/java/com/yapp/domain/usecases/SetSessionUseCase.kt index 4a4778cb..cc59ab7b 100644 --- a/domain/src/main/java/com/yapp/domain/usecases/SetSessionUseCase.kt +++ b/domain/src/main/java/com/yapp/domain/usecases/SetSessionUseCase.kt @@ -31,7 +31,6 @@ class SetSessionUseCase @Inject constructor( type = type, startTime = dateParser.parse(rawDate = rawDate), description = description, - code = code ) ) } diff --git a/presentation/src/main/java/com/yapp/presentation/ui/member/signup/password/PassWordViewModel.kt b/presentation/src/main/java/com/yapp/presentation/ui/member/signup/password/PassWordViewModel.kt index 47b70706..978c7b3e 100644 --- a/presentation/src/main/java/com/yapp/presentation/ui/member/signup/password/PassWordViewModel.kt +++ b/presentation/src/main/java/com/yapp/presentation/ui/member/signup/password/PassWordViewModel.kt @@ -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 @@ -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 -> {