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

[FEAT] 회원탈퇴 #161

Merged
merged 1 commit into from
Oct 14, 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
@@ -1,11 +1,11 @@
package com.polzzak_android.data.remote.service

import com.polzzak_android.data.remote.model.response.EmptyDataResponse
import com.polzzak_android.data.remote.model.response.ProfileDto
import com.polzzak_android.data.remote.model.response.ProfileResponse
import com.polzzak_android.data.remote.model.response.UserResponse
import okhttp3.MultipartBody
import retrofit2.Response
import retrofit2.http.DELETE
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.Multipart
Expand Down Expand Up @@ -43,4 +43,9 @@ interface UserService {
@Header("Content-Type") contentType: String,
@Path("nickname") nickname: String
): Response<EmptyDataResponse>

@DELETE("/api/v1/users")
suspend fun deleteUser(
@Header("Authorization") authorization: String,
): Response<EmptyDataResponse>
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.polzzak_android.data.repository

import android.net.Uri
import com.polzzak_android.data.remote.model.ApiResult
import com.polzzak_android.data.remote.model.response.ProfileDto
import com.polzzak_android.data.remote.model.response.UserInfoDto
import com.polzzak_android.data.remote.service.UserService
import com.polzzak_android.data.remote.util.createHeaderAuthorization
import com.polzzak_android.data.remote.util.requestCatching
import com.polzzak_android.presentation.feature.auth.model.SocialLoginType
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
Expand All @@ -32,7 +30,11 @@ class UserRepository @Inject constructor(
userService.requestProfile(authorization = authorization)
}

suspend fun updateUserInfo(accessToken: String, profileInfo: ProfileDto, profileImagePath: String?): ApiResult<Unit> =
suspend fun updateUserInfo(
accessToken: String,
profileInfo: ProfileDto,
profileImagePath: String?
): ApiResult<Unit> =
requestCatching {
val authorization = createHeaderAuthorization(accessToken = accessToken)
val profilePart = createSignUpPart(profile = profileInfo)
Expand All @@ -42,8 +44,13 @@ class UserRepository @Inject constructor(
.apply {
profileImagePart?.let { addPart(it) }
}.build()
val contentType = "multipart/form-data; charset=utf-8; boundary=${multipartBody.boundary}"
userService.updateUserInfo(authorization = authorization, contentType = contentType, profile = multipartBody)
val contentType =
"multipart/form-data; charset=utf-8; boundary=${multipartBody.boundary}"
userService.updateUserInfo(
authorization = authorization,
contentType = contentType,
profile = multipartBody
)
}

private fun createSignUpPart(
Expand Down Expand Up @@ -73,6 +80,15 @@ class UserRepository @Inject constructor(
requestCatching {
val authorization = createHeaderAuthorization(accessToken = accessToken)
val contentType = "application/json;charset=UTF-8"
userService.updateUserNickname(authorization = authorization, contentType = contentType, nickname = nickname)
userService.updateUserNickname(
authorization = authorization,
contentType = contentType,
nickname = nickname
)
}

suspend fun deleteUser(accessToken: String): ApiResult<Unit?> = requestCatching {
val authorization = createHeaderAuthorization(accessToken = accessToken)
userService.deleteUser(authorization = authorization)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import androidx.fragment.app.DialogFragment
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.polzzak_android.R
import com.polzzak_android.common.util.livedata.EventWrapperObserver
import com.polzzak_android.data.remote.model.isAccessTokenException
import com.polzzak_android.databinding.FragmentMyAccountDeleteBinding
import com.polzzak_android.presentation.common.base.BaseFragment
import com.polzzak_android.presentation.common.model.ButtonCount
import com.polzzak_android.presentation.common.model.CommonButtonModel
import com.polzzak_android.presentation.common.model.ModelState
import com.polzzak_android.presentation.common.util.SpannableBuilder
import com.polzzak_android.presentation.common.util.getAccessTokenOrNull
import com.polzzak_android.presentation.common.util.handleInvalidToken
import com.polzzak_android.presentation.common.util.logout
import com.polzzak_android.presentation.component.PolzzakSnackBar
Expand All @@ -23,8 +25,9 @@ import com.polzzak_android.presentation.component.errorOf
import com.polzzak_android.presentation.component.toolbar.ToolbarData
import com.polzzak_android.presentation.component.toolbar.ToolbarHelper
import com.polzzak_android.presentation.feature.myPage.accountmanagement.MyAccountManagementFragment.Companion.ARGUMENT_NICKNAME_KEY
import timber.log.Timber
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MyAccountDeleteFragment : BaseFragment<FragmentMyAccountDeleteBinding>() {
override val layoutResId: Int = R.layout.fragment_my_account_delete

Expand Down Expand Up @@ -88,7 +91,7 @@ class MyAccountDeleteFragment : BaseFragment<FragmentMyAccountDeleteBinding>() {
onConfirmListener = {
object : OnButtonClickListener {
override fun setBusinessLogic() {
myAccountDeleteViewModel.deleteAccount()
myAccountDeleteViewModel.deleteAccount(getAccessTokenOrNull() ?: "")
}

override fun getReturnValue(value: Any) {
Expand Down Expand Up @@ -148,19 +151,23 @@ class MyAccountDeleteFragment : BaseFragment<FragmentMyAccountDeleteBinding>() {

is ModelState.Success -> {
dismissCurrentDialog()
Timber.d("회원탈퇴")
logout()
}

is ModelState.Error -> {
dismissCurrentDialog()
when {
it.exception.isAccessTokenException() -> handleInvalidToken()
else -> PolzzakSnackBar.errorOf(binding.root, it.exception).show()
}
}
}
}

myAccountDeleteViewModel.errorEventLiveData.observe(
viewLifecycleOwner,
EventWrapperObserver {
when {
it.isAccessTokenException() -> handleInvalidToken()
else -> PolzzakSnackBar.errorOf(binding.root, it).show()
}
})
}

private fun showDialog(dialogFragment: DialogFragment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,26 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.polzzak_android.common.util.livedata.EventWrapper
import com.polzzak_android.data.repository.UserRepository
import com.polzzak_android.presentation.common.model.ModelState
import com.polzzak_android.presentation.feature.myPage.accountmanagement.model.MyAccountDeleteMenuModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import javax.inject.Inject

class MyAccountDeleteViewModel : ViewModel() {
@HiltViewModel
class MyAccountDeleteViewModel @Inject constructor(
private val userRepository: UserRepository
) : ViewModel() {
private val _myAccountMenuLiveData = MutableLiveData<MyAccountDeleteMenuModel>()
val myAccountMenuLiveData: LiveData<MyAccountDeleteMenuModel> = _myAccountMenuLiveData

private val _deleteAccountLiveData = MutableLiveData<ModelState<Unit>>()
val deleteAccountLiveData: LiveData<ModelState<Unit>> = _deleteAccountLiveData
private val _errorEventLiveData = MutableLiveData<EventWrapper<Exception>>()
val errorEventLiveData: LiveData<EventWrapper<Exception>> = _errorEventLiveData
private var deleteAccountJob: Job? = null

fun toggleDeleteSocialAccountData() {
Expand Down Expand Up @@ -46,15 +54,15 @@ class MyAccountDeleteViewModel : ViewModel() {
}
}

fun deleteAccount() {
fun deleteAccount(accessToken: String) {
if (deleteAccountJob?.isCompleted == false) return
deleteAccountJob = viewModelScope.launch {
//TODO 회원탈퇴 API 호출
_deleteAccountLiveData.value = ModelState.Loading()
delay(2000)
//onSuccess
_deleteAccountLiveData.value = ModelState.Success(Unit)
userRepository.deleteUser(accessToken = accessToken).onSuccess {
_deleteAccountLiveData.value = ModelState.Success(Unit)
}.onError { exception, _ ->
_deleteAccountLiveData.value = ModelState.Error(exception)
_errorEventLiveData.value = EventWrapper(exception)
}
}

}
}