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/182 messaging token #183

Merged
merged 7 commits into from
Aug 29, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ internal fun NavGraphBuilder.myPageGraph(
composable<Withdrawal> {
WithdrawalScreen(
navigateToUp = navigateToUp,
navigateToLogin = navigateToLogin
navigateToLogin = navigateToLogin,
snackBarHostState = snackBarHostState
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.easyhz.noffice.core.network.api.auth

import com.easyhz.noffice.core.network.model.request.sign.LoginRequest
import com.easyhz.noffice.core.network.model.request.token.MessagingToken
import com.easyhz.noffice.core.network.model.response.auth.UserResponse
import com.easyhz.noffice.core.network.util.NofficeResult
import retrofit2.http.Body
import retrofit2.http.DELETE
import retrofit2.http.HTTP
import retrofit2.http.Header
import retrofit2.http.POST

Expand All @@ -25,7 +27,13 @@ interface AuthService {
/* fcm ํ† ํฐ ์ €์žฅ */
@POST("/api/v1/notifications/fcm-token")
suspend fun registerMessagingToken(
@Body fcmToken: String
@Body messagingToken: MessagingToken
): NofficeResult<Unit>

/* fcm ํ† ํฐ ์‚ญ์ œ */
@HTTP(method = "DELETE", path = "/api/v1/notifications/fcm-token", hasBody = true)
suspend fun deleteMessagingToken(
@Body messagingToken: MessagingToken
): NofficeResult<Unit>

/* ํšŒ์› ํƒˆํ‡ด */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.easyhz.noffice.core.network.model.request.token

data class MessagingToken(
val token: String
)
2 changes: 2 additions & 0 deletions data/notification/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ android {
dependencies {
implementation(projects.core.common)
implementation(projects.core.network)

implementation(projects.core.datastore)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ package com.easyhz.noffice.data.notification.repository.messaging

interface CloudMessagingRepository {
suspend fun getToken(): Result<String>
suspend fun registerToken(token: String): Result<Unit>
suspend fun registerMessagingToken(token: String): Result<Unit>
suspend fun deleteMessagingToken(token: String): Result<Unit>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.easyhz.noffice.data.notification.repository.messaging
import com.easyhz.noffice.core.common.di.Dispatcher
import com.easyhz.noffice.core.common.di.NofficeDispatchers
import com.easyhz.noffice.core.network.api.auth.AuthService
import com.easyhz.noffice.core.network.model.request.token.MessagingToken
import com.easyhz.noffice.core.network.util.toResult
import com.google.firebase.messaging.FirebaseMessaging
import kotlinx.coroutines.CoroutineDispatcher
Expand All @@ -21,7 +22,11 @@ class CloudMessagingRepositoryImpl @Inject constructor(
}
}

override suspend fun registerToken(token: String): Result<Unit> = withContext(dispatcher) {
return@withContext authService.registerMessagingToken(token).toResult()
override suspend fun registerMessagingToken(token: String): Result<Unit> = withContext(dispatcher) {
return@withContext authService.registerMessagingToken(MessagingToken(token)).toResult()
}

override suspend fun deleteMessagingToken(token: String): Result<Unit> = withContext(dispatcher) {
return@withContext authService.deleteMessagingToken(MessagingToken(token)).toResult()
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.easyhz.noffice.data.notification.service

import android.util.Log
import com.easyhz.noffice.core.common.util.errorLogging
import com.easyhz.noffice.core.datastore.datasource.user.UserLocalDataSource
import com.easyhz.noffice.core.network.api.auth.AuthService
import com.easyhz.noffice.core.network.model.request.token.MessagingToken
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -15,6 +18,9 @@ class CloudMessagingService: FirebaseMessagingService() {
@Inject
lateinit var authService: AuthService

@Inject
lateinit var userLocalDataSource: UserLocalDataSource

@Inject
lateinit var notificationService: NotificationService

Expand All @@ -32,13 +38,17 @@ class CloudMessagingService: FirebaseMessagingService() {

private fun registerToken(token: String) {
CoroutineScope(Dispatchers.IO).launch {
val response = authService.registerMessagingToken(token)
val memberId = userLocalDataSource.getMemberId().getOrNull()
if (memberId == null || memberId == -1) {
Log.w(this.javaClass.name, "memberId is $memberId")
return@launch
}
val response = authService.registerMessagingToken(MessagingToken(token))
response.onSuccess {
Log.d(this.javaClass.name, "Success registering token")
}.onFailure { e ->
Log.e(this.javaClass.name, "Error registering token", e)
errorLogging(this.javaClass.name, "Error registering token", e)
}
}
}

}
1 change: 1 addition & 0 deletions domain/my-page/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ dependencies {
implementation(projects.data.notification)
implementation(projects.data.organization)

implementation(projects.domain.notification)
implementation(projects.domain.organization)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@ package com.easyhz.noffice.domain.my_page.usecase

import android.content.Context
import com.easyhz.noffice.core.common.base.BaseUseCase
import com.easyhz.noffice.core.common.util.errorLogging
import com.easyhz.noffice.data.auth.repository.auth.AuthRepository
import com.easyhz.noffice.domain.notification.usecase.DeleteMessagingTokenUseCase
import javax.inject.Inject

class LogoutUseCase @Inject constructor(
private val authRepository: AuthRepository
private val authRepository: AuthRepository,
private val deleteMessagingTokenUseCase: DeleteMessagingTokenUseCase
): BaseUseCase<Context, Unit>() {
override suspend fun invoke(param: Context): Result<Unit> {
return authRepository.logout(param)
override suspend fun invoke(param: Context): Result<Unit> = runCatching {
deleteMessagingToken()
authRepository.logout(param)
}

private suspend fun deleteMessagingToken() {
deleteMessagingTokenUseCase(Unit).onFailure { e ->
errorLogging(this.javaClass.name, "deleteMessagingToken", e)
throw e
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.easyhz.noffice.domain.my_page.usecase

import android.content.Context
import com.easyhz.noffice.core.common.base.BaseUseCase
import com.easyhz.noffice.core.common.util.errorLogging
import com.easyhz.noffice.data.auth.repository.auth.AuthRepository
import com.easyhz.noffice.domain.notification.usecase.DeleteMessagingTokenUseCase
import javax.inject.Inject

class WithdrawUseCase @Inject constructor(
private val authRepository: AuthRepository,
private val deleteMessagingTokenUseCase: DeleteMessagingTokenUseCase
): BaseUseCase<Context, Unit>() {
override suspend fun invoke(param: Context): Result<Unit> = runCatching {
deleteMessagingToken()
authRepository.withdraw(param)
}

private suspend fun deleteMessagingToken() {
deleteMessagingTokenUseCase(Unit).onFailure { e ->
errorLogging(this.javaClass.name, "deleteMessagingToken", e)
throw e
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.easyhz.noffice.domain.notification.usecase

import com.easyhz.noffice.core.common.base.BaseUseCase
import com.easyhz.noffice.data.notification.repository.messaging.CloudMessagingRepository
import javax.inject.Inject

class DeleteMessagingTokenUseCase @Inject constructor(
private val cloudMessagingRepository: CloudMessagingRepository,
) : BaseUseCase<Unit, Unit>() {
override suspend fun invoke(param: Unit): Result<Unit> = runCatching {
val token = getMessagingToken().getOrThrow()
deleteMessagingToken(token)
}

private suspend fun getMessagingToken(): Result<String> {
return cloudMessagingRepository.getToken()
}

private suspend fun deleteMessagingToken(token: String): Result<Unit> {
return cloudMessagingRepository.deleteMessagingToken(token)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ class RegisterMessagingTokenUseCase @Inject constructor(
}

private suspend fun registerMessagingToken(token: String): Result<Unit> {
return cloudMessagingRepository.registerToken(token)
return cloudMessagingRepository.registerMessagingToken(token)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.easyhz.noffice.feature.my_page.contract.detail.withdrawal

import androidx.annotation.StringRes
import com.easyhz.noffice.core.common.base.UiSideEffect

sealed class WithdrawalSideEffect: UiSideEffect() {
data object NavigateToUp: WithdrawalSideEffect()
data object NavigateToLogin: WithdrawalSideEffect()
data class ShowSnackBar(@StringRes val stringId: Int): WithdrawalSideEffect()
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.easyhz.noffice.feature.my_page.contract.menu

import android.net.Uri
import androidx.annotation.StringRes
import com.easyhz.noffice.core.common.base.UiSideEffect

sealed class MenuSideEffect: UiSideEffect() {
Expand All @@ -11,4 +12,5 @@ sealed class MenuSideEffect: UiSideEffect() {
data object NavigateToConsentToInformation: MenuSideEffect()
data object NavigateToWithdrawal: MenuSideEffect()
data object NavigateToLogin: MenuSideEffect()
data class ShowSnackBar(@StringRes val stringId: Int): MenuSideEffect()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.content.Context
import android.net.Uri
import androidx.lifecycle.viewModelScope
import com.easyhz.noffice.core.common.base.BaseViewModel
import com.easyhz.noffice.core.common.error.handleError
import com.easyhz.noffice.core.common.util.errorLogging
import com.easyhz.noffice.domain.my_page.usecase.LogoutUseCase
import com.easyhz.noffice.feature.my_page.contract.menu.MenuIntent
import com.easyhz.noffice.feature.my_page.contract.menu.MenuSideEffect
Expand Down Expand Up @@ -88,6 +90,9 @@ class MyPageMenuViewModel @Inject constructor(
logoutUseCase.invoke(context)
.onSuccess {
postSideEffect { MenuSideEffect.NavigateToLogin }
}.onFailure {
errorLogging(this.javaClass.name, "handleLogout", it)
showSnackBar(it.handleError())
}.also {
reduce { copy(isLoading = false) }
}
Expand All @@ -96,4 +101,8 @@ class MyPageMenuViewModel @Inject constructor(
private fun updateLogoutState(isShowLogoutDialog: Boolean = false, isLoading: Boolean) {
reduce { copy(isShowLogoutDialog = isShowLogoutDialog, isLoading = isLoading) }
}

private fun showSnackBar(stringId: Int) {
postSideEffect { MenuSideEffect.ShowSnackBar(stringId) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ fun MyPageScreen(
is MenuSideEffect.NavigateToConsentToInformation -> { navigateToConsent() }
is MenuSideEffect.NavigateToWithdrawal -> { navigateToWithdrawal() }
is MenuSideEffect.NavigateToLogin -> { navigateToLogin() }
is MenuSideEffect.ShowSnackBar -> {
snackBarHostState.showSnackbar(
message = context.getString(sideEffect.stringId),
withDismissAction = true
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Icon
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
Expand Down Expand Up @@ -52,6 +53,7 @@ import com.easyhz.noffice.feature.my_page.util.WithdrawalType
fun WithdrawalScreen(
modifier: Modifier = Modifier,
viewModel: WithdrawalViewModel = hiltViewModel(),
snackBarHostState: SnackbarHostState,
navigateToUp: () -> Unit,
navigateToLogin: () -> Unit
) {
Expand Down Expand Up @@ -169,6 +171,12 @@ fun WithdrawalScreen(
when(sideEffect) {
is WithdrawalSideEffect.NavigateToUp -> { navigateToUp() }
is WithdrawalSideEffect.NavigateToLogin -> { navigateToLogin() }
is WithdrawalSideEffect.ShowSnackBar -> {
snackBarHostState.showSnackbar(
message = context.getString(sideEffect.stringId),
withDismissAction = true
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import android.content.Context
import androidx.lifecycle.viewModelScope
import com.easyhz.noffice.core.common.base.BaseViewModel
import com.easyhz.noffice.core.common.error.NofficeError
import com.easyhz.noffice.core.common.error.handleError
import com.easyhz.noffice.core.common.util.errorLogging
import com.easyhz.noffice.domain.my_page.usecase.WithdrawUserCase
import com.easyhz.noffice.domain.my_page.usecase.WithdrawUseCase
import com.easyhz.noffice.feature.my_page.contract.detail.withdrawal.WithdrawalIntent
import com.easyhz.noffice.feature.my_page.contract.detail.withdrawal.WithdrawalSideEffect
import com.easyhz.noffice.feature.my_page.contract.detail.withdrawal.WithdrawalState
Expand All @@ -15,7 +16,7 @@ import javax.inject.Inject

@HiltViewModel
class WithdrawalViewModel @Inject constructor(
private val withdrawUserCase: WithdrawUserCase
private val withdrawUseCase: WithdrawUseCase
): BaseViewModel<WithdrawalState, WithdrawalIntent, WithdrawalSideEffect>(
initialState = WithdrawalState.init()
) {
Expand All @@ -33,13 +34,14 @@ class WithdrawalViewModel @Inject constructor(

private fun onClickWithdrawalButton(context: Context) = viewModelScope.launch {
reduce { copy(isLoading = true) }
withdrawUserCase.invoke(context).onSuccess {
withdrawUseCase.invoke(context).onSuccess {
navigateToLogIn()
}.onFailure {
if (it is NofficeError.NoContent) {
navigateToLogIn()
} else {
errorLogging(this.javaClass.name, "withdraw", it)
showSnackBar(it.handleError())
}
}.also {
reduce { copy(isLoading = false) }
Expand All @@ -53,4 +55,8 @@ class WithdrawalViewModel @Inject constructor(
private fun onClickBackButton() {
postSideEffect { WithdrawalSideEffect.NavigateToUp }
}

private fun showSnackBar(stringId: Int) {
postSideEffect { WithdrawalSideEffect.ShowSnackBar(stringId) }
}
}
Loading