Skip to content

Commit

Permalink
[FEAT/#106] 로그아웃 서버통신 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
leeeyubin committed Jul 17, 2024
1 parent 5dd7534 commit b055e21
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 8 deletions.
10 changes: 8 additions & 2 deletions app/src/main/java/com/terning/point/di/DataSourceModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ package com.terning.point.di
import com.terning.data.datasource.AuthDataSource
import com.terning.data.datasource.CalendarDataSource
import com.terning.data.datasource.FilteringDataSource
import com.terning.data.datasource.InternDataSource
import com.terning.data.datasource.HomeDataSource
import com.terning.data.datasource.InternDataSource
import com.terning.data.datasource.MyPageDataSource
import com.terning.data.datasource.SearchDataSource
import com.terning.data.datasource.TokenReissueDataSource
import com.terning.data.datasourceimpl.AuthDataSourceImpl
import com.terning.data.datasourceimpl.CalendarDataSourceImpl
import com.terning.data.datasourceimpl.FilteringDataSourceImpl
import com.terning.data.datasourceimpl.InternDataSourceImpl
import com.terning.data.datasourceimpl.HomeDataSourceImpl
import com.terning.data.datasourceimpl.InternDataSourceImpl
import com.terning.data.datasourceimpl.MyPageDataSourceImpl
import com.terning.data.datasourceimpl.SearchDataSourceImpl
import com.terning.data.datasourceimpl.TokenReissueDataSourceImpl
import dagger.Binds
Expand Down Expand Up @@ -53,4 +55,8 @@ abstract class DataSourceModule {
@Binds
@Singleton
abstract fun bindHomeDataSource(homeDataSourceImpl: HomeDataSourceImpl): HomeDataSource

@Binds
@Singleton
abstract fun bindMyPageDataSource(myPageDataSourceImpl: MyPageDataSourceImpl): MyPageDataSource
}
10 changes: 8 additions & 2 deletions app/src/main/java/com/terning/point/di/RepositoryModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ package com.terning.point.di
import com.terning.data.repositoryimpl.AuthRepositoryImpl
import com.terning.data.repositoryimpl.CalendarRepositoryImpl
import com.terning.data.repositoryimpl.FilteringRepositoryImpl
import com.terning.data.repositoryimpl.InternRepositoryImpl
import com.terning.data.repositoryimpl.HomeRepositoryImpl
import com.terning.data.repositoryimpl.InternRepositoryImpl
import com.terning.data.repositoryimpl.MyPageRepositoryImpl
import com.terning.data.repositoryimpl.SearchRepositoryImpl
import com.terning.data.repositoryimpl.TokenReissueRepositoryImpl
import com.terning.data.repositoryimpl.TokenRepositoryImpl
import com.terning.domain.repository.AuthRepository
import com.terning.domain.repository.CalendarRepository
import com.terning.domain.repository.FilteringRepository
import com.terning.domain.repository.InternRepository
import com.terning.domain.repository.HomeRepository
import com.terning.domain.repository.InternRepository
import com.terning.domain.repository.MyPageRepository
import com.terning.domain.repository.SearchRepository
import com.terning.domain.repository.TokenReissueRepository
import com.terning.domain.repository.TokenRepository
Expand Down Expand Up @@ -57,4 +59,8 @@ abstract class RepositoryModule {
@Binds
@Singleton
abstract fun bindHomeRepository(homeRepositoryImpl: HomeRepositoryImpl): HomeRepository

@Binds
@Singleton
abstract fun bindMyPageRepository(myPageRepositoryImpl: MyPageRepositoryImpl): MyPageRepository
}
8 changes: 7 additions & 1 deletion app/src/main/java/com/terning/point/di/ServiceModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package com.terning.point.di

import com.terning.data.service.AuthService
import com.terning.data.service.CalendarService
import com.terning.data.service.InternService
import com.terning.data.service.FilteringService
import com.terning.data.service.HomeService
import com.terning.data.service.InternService
import com.terning.data.service.MyPageService
import com.terning.data.service.SearchService
import com.terning.data.service.TokenReissueService
import com.terning.point.di.qualifier.JWT
Expand Down Expand Up @@ -55,4 +56,9 @@ object ServiceModule {
@Singleton
fun provideHomeService(@JWT retrofit: Retrofit): HomeService =
retrofit.create(HomeService::class.java)

@Provides
@Singleton
fun provideMyPageService(@JWT retrofit: Retrofit): MyPageService =
retrofit.create(MyPageService::class.java)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.terning.data.datasource

import com.terning.data.dto.NonDataBaseResponse

interface MyPageDataSource {
suspend fun patchLogout(): NonDataBaseResponse
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.terning.data.datasourceimpl

import com.terning.data.datasource.MyPageDataSource
import com.terning.data.dto.NonDataBaseResponse
import com.terning.data.service.MyPageService
import javax.inject.Inject

class MyPageDataSourceImpl @Inject constructor(
private val myPageService: MyPageService
) : MyPageDataSource {
override suspend fun patchLogout(): NonDataBaseResponse = myPageService.patchLogout()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.terning.data.repositoryimpl

import com.terning.data.datasource.MyPageDataSource
import com.terning.domain.repository.MyPageRepository
import javax.inject.Inject

class MyPageRepositoryImpl @Inject constructor(
private val myPageDataSource: MyPageDataSource
) : MyPageRepository {
override suspend fun patchLogout(): Result<Unit> =
runCatching {
myPageDataSource.patchLogout()
}
}
10 changes: 10 additions & 0 deletions data/src/main/java/com/terning/data/service/MyPageService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.terning.data.service

import com.terning.data.dto.NonDataBaseResponse
import retrofit2.http.PATCH

interface MyPageService {
@PATCH("api/v1/mypage/logout")
suspend fun patchLogout(): NonDataBaseResponse

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.terning.domain.repository

interface MyPageRepository {
suspend fun patchLogout() : Result<Unit>
}
1 change: 1 addition & 0 deletions feature/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ dependencies {
implementation(libs.timber)
implementation(libs.ossLicense)
implementation(libs.lottie)
implementation(libs.process.phoenix)

// Compose Preview
debugImplementation(libs.compose.ui.tooling)
Expand Down
17 changes: 16 additions & 1 deletion feature/src/main/java/com/terning/feature/mypage/MyPageRoute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.terning.core.designsystem.component.bottomsheet.MyPageLogoutBottomSheet
import com.terning.core.designsystem.component.bottomsheet.MyPageQuitBottomSheet
import com.terning.core.designsystem.component.image.TerningImage
Expand All @@ -30,13 +32,16 @@ import com.terning.core.designsystem.theme.TerningTheme
import com.terning.core.designsystem.theme.White
import com.terning.core.extension.customShadow
import com.terning.core.extension.noRippleClickable
import com.terning.core.state.UiState
import com.terning.feature.R
import com.terning.feature.mypage.component.MyPageItem

@Composable
fun MyPageRoute(
viewModel: MyPageViewModel = hiltViewModel(),
) {
val state by viewModel.state.collectAsStateWithLifecycle()
val context = LocalContext.current

var showLogoutBottomSheet by remember { mutableStateOf(false) }
var showQuitBottomSheet by remember { mutableStateOf(false) }
Expand All @@ -46,7 +51,7 @@ fun MyPageRoute(
onDismiss = { showLogoutBottomSheet = false },
onLogoutClick = {
showLogoutBottomSheet = false
viewModel.patchLogout()
viewModel.logoutKakao()
},
)
}
Expand All @@ -61,6 +66,16 @@ fun MyPageRoute(
)
}

when (state.isSuccess) {
is UiState.Success -> {
viewModel.restartApp(context)
}

is UiState.Loading -> {}
is UiState.Empty -> {}
is UiState.Failure -> {}
}

MyPageScreen(
onLogoutClick = { showLogoutBottomSheet = true },
onQuitClick = { showQuitBottomSheet = true }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.terning.feature.mypage

import com.terning.core.state.UiState

data class MyPageState(
val isSuccess: UiState<Boolean> = UiState.Loading
)
Original file line number Diff line number Diff line change
@@ -1,14 +1,63 @@
package com.terning.feature.mypage

import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.Looper
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.jakewharton.processphoenix.ProcessPhoenix
import com.kakao.sdk.user.UserApiClient
import com.terning.core.state.UiState
import com.terning.domain.repository.MyPageRepository
import com.terning.domain.repository.TokenRepository
import com.terning.feature.main.MainActivity
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class MyPageViewModel @Inject constructor() : ViewModel() {
class MyPageViewModel @Inject constructor(
private val myPageRepository: MyPageRepository,
private val tokenRepository: TokenRepository
) : ViewModel() {

fun patchLogout() {
private val _state: MutableStateFlow<MyPageState> = MutableStateFlow(MyPageState())
val state: StateFlow<MyPageState> get() = _state.asStateFlow()

fun logoutKakao() {
UserApiClient.instance.logout { error ->
if (error == null) {
patchLogout()
} else {
_state.value = _state.value.copy(isSuccess = UiState.Failure(error.toString()))
}
}
}

private fun patchLogout() {
viewModelScope.launch {
myPageRepository.patchLogout().onSuccess {
tokenRepository.clearInfo()
_state.value = _state.value.copy(isSuccess = UiState.Success(true))
}.onFailure {
_state.value = _state.value.copy(isSuccess = UiState.Failure(it.toString()))
}
}
}

fun restartApp(context: Context) {
Handler(Looper.getMainLooper()).post {
Handler(Looper.getMainLooper()).post {
ProcessPhoenix.triggerRebirth(
context,
Intent(context, MainActivity::class.java)
)
}
}
}

fun patchQuit() {
Expand Down

0 comments on commit b055e21

Please sign in to comment.