Skip to content

Commit

Permalink
[MERGE] #227 -> develop
Browse files Browse the repository at this point in the history
[Feat/#227] Edit Profile & Participant Profile 관련 수정
  • Loading branch information
chattymin authored Mar 8, 2024
2 parents 00b693c + 75ff1bd commit 9480bb1
Show file tree
Hide file tree
Showing 30 changed files with 154 additions and 110 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
<activity
android:name="com.going.presentation.profile.edit.ProfileEditActivity"
android:exported="false"
android:windowSoftInputMode="adjustResize"
android:screenOrientation="portrait" />

<activity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package com.going.data.datasource

import com.going.data.dto.BaseResponse
import com.going.data.dto.NonDataBaseResponse
import com.going.data.dto.request.ParticipantProfileRequestDto
import com.going.data.dto.request.UserProfileRequestDto
import com.going.data.dto.response.ParticipantProfileResponseDto
import com.going.data.dto.response.UserProfileResponseDto
import com.going.domain.entity.request.ParticipantProfileRequestModel
import com.going.domain.entity.request.UserProfileRequestModel
import retrofit2.http.Body

interface ProfileDataSource {
suspend fun getUserProfile(): BaseResponse<UserProfileResponseDto>

suspend fun patchUserProfile(
@Body userProfileResponseModel: UserProfileRequestDto
): NonDataBaseResponse

suspend fun getParticipantProfile(
participantProfileRequestModel: ParticipantProfileRequestDto
): BaseResponse<ParticipantProfileResponseDto>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package com.going.data.datasourceImpl

import com.going.data.datasource.ProfileDataSource
import com.going.data.dto.BaseResponse
import com.going.data.dto.NonDataBaseResponse
import com.going.data.dto.request.ParticipantProfileRequestDto
import com.going.data.dto.request.UserProfileRequestDto
import com.going.data.dto.response.ParticipantProfileResponseDto
import com.going.data.dto.response.UserProfileResponseDto
import com.going.data.service.ProfileService
Expand All @@ -15,6 +17,10 @@ class ProfileDataSourceImpl @Inject constructor(
override suspend fun getUserProfile(): BaseResponse<UserProfileResponseDto> =
profileService.getUserProfile()

override suspend fun patchUserProfile(request: UserProfileRequestDto): NonDataBaseResponse =
profileService.patchUserProfile(request)


override suspend fun getParticipantProfile(participantProfileRequestDto: ParticipantProfileRequestDto): BaseResponse<ParticipantProfileResponseDto> =
profileService.getParticipantProfile(participantProfileRequestDto.participantId)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.going.data.dto.request

import com.going.domain.entity.request.TokenReissueRequestModel
import com.going.domain.entity.request.UserProfileRequestModel
import com.going.domain.entity.response.UserProfileResponseModel
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class UserProfileRequestDto(
@SerialName("name")
val name: String,
@SerialName("intro")
val intro: String,
)

fun UserProfileRequestModel.toDto() = UserProfileRequestDto(name, intro)

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.going.data.dto.response

import com.going.domain.entity.request.UserProfileRequestModel
import com.going.domain.entity.response.UserProfileResponseModel
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

Expand All @@ -13,5 +14,5 @@ data class UserProfileResponseDto(
@SerialName("result")
val result: Int,
) {
fun toProfileModel() = UserProfileRequestModel(name, intro, result)
fun toProfileModel() = UserProfileResponseModel(name, intro, result)
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package com.going.data.repositoryImpl

import com.going.data.datasource.ProfileDataSource
import com.going.data.dto.request.toDto
import com.going.data.dto.request.toParticipantRequestDto
import com.going.domain.entity.request.ParticipantProfileRequestModel
import com.going.domain.entity.request.UserProfileRequestModel
import com.going.domain.entity.response.ParticipantProfileResponseModel
import com.going.domain.entity.response.UserProfileResponseModel
import com.going.domain.repository.ProfileRepository
import javax.inject.Inject

class ProfileRepositoryImpl @Inject constructor(
private val profileDataSource: ProfileDataSource
) : ProfileRepository {
override suspend fun getUserProfile(): Result<UserProfileRequestModel> =
override suspend fun getUserProfile(): Result<UserProfileResponseModel> =
runCatching {
profileDataSource.getUserProfile().data.toProfileModel()
}

override suspend fun patchUserProfile(userProfileResponseModel: UserProfileRequestModel): Result<Unit> =
runCatching {
profileDataSource.patchUserProfile(userProfileResponseModel.toDto())
}


override suspend fun getParticipantProfile(
participantProfileRequestModel: ParticipantProfileRequestModel
): Result<ParticipantProfileResponseModel> =
Expand Down
10 changes: 10 additions & 0 deletions data/src/main/java/com/going/data/service/ProfileService.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
package com.going.data.service

import com.going.data.dto.BaseResponse
import com.going.data.dto.NonDataBaseResponse
import com.going.data.dto.request.ParticipantProfileRequestDto
import com.going.data.dto.request.StartInviteTripRequestDto
import com.going.data.dto.request.UserProfileRequestDto
import com.going.data.dto.response.ParticipantProfileResponseDto
import com.going.data.dto.response.UserProfileResponseDto
import com.going.domain.entity.request.ParticipantProfileRequestModel
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.PATCH
import retrofit2.http.Path

interface ProfileService {
@GET("api/users/profile")
suspend fun getUserProfile(): BaseResponse<UserProfileResponseDto>

@PATCH("api/users/profile")
suspend fun patchUserProfile(
@Body request: UserProfileRequestDto,
): NonDataBaseResponse

@GET("api/trips/participants/{participantId}")
suspend fun getParticipantProfile(
@Path("participantId") participantId: Long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ package com.going.domain.entity.request
data class UserProfileRequestModel(
val name: String,
val intro: String,
val result: Int,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.going.domain.entity.response

data class UserProfileResponseModel(
val name: String,
val intro: String,
val result: Int,
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package com.going.domain.repository
import com.going.domain.entity.request.ParticipantProfileRequestModel
import com.going.domain.entity.request.UserProfileRequestModel
import com.going.domain.entity.response.ParticipantProfileResponseModel
import com.going.domain.entity.response.UserProfileResponseModel

interface ProfileRepository {
suspend fun getUserProfile(): Result<UserProfileRequestModel>
suspend fun getUserProfile(): Result<UserProfileResponseModel>

suspend fun patchUserProfile(userProfileResponseModel: UserProfileRequestModel): Result<Unit>

suspend fun getParticipantProfile(participantProfileRequestModel: ParticipantProfileRequestModel): Result<ParticipantProfileResponseModel>
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.going.domain.entity.AuthState
import com.going.presentation.R
import com.going.presentation.dashboard.DashBoardActivity
import com.going.presentation.databinding.ActivitySplashBinding
import com.going.presentation.onboarding.signin.SignInActivity
import com.going.presentation.tendency.splash.TendencySplashActivity
import com.going.presentation.util.navigateToScreenClear
import com.going.ui.base.BaseActivity
import com.going.ui.extension.setNavigationBarColorFromResource
Expand Down Expand Up @@ -46,12 +44,8 @@ class SplashActivity : BaseActivity<ActivitySplashBinding>(R.layout.activity_spl

private fun observeUserState() {
viewModel.userState.flowWithLifecycle(lifecycle).onEach { state ->
when (state) {
AuthState.LOADING -> return@onEach
AuthState.SUCCESS -> navigateToScreenClear<DashBoardActivity>()
AuthState.FAILURE -> navigateToScreenClear<SignInActivity>()
AuthState.OTHER_PAGE -> navigateToScreenClear<TendencySplashActivity>()
}
if (state) navigateToScreenClear<DashBoardActivity>()
else navigateToScreenClear<SignInActivity>()
}.launchIn(lifecycleScope)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,33 @@ package com.going.presentation.onboarding.splash
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModel
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.viewModelScope
import com.going.domain.entity.AuthState
import com.going.domain.repository.AuthRepository
import com.going.domain.repository.TokenRepository
import com.going.presentation.util.toErrorCode
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class SplashViewModel @Inject constructor(
private val authRepository: AuthRepository,
private val tokenRepository: TokenRepository,
) : ViewModel() {

private val _userState = MutableStateFlow(AuthState.LOADING)
val userState: StateFlow<AuthState> = _userState
private val _userState = MutableSharedFlow<Boolean>()
val userState: SharedFlow<Boolean>
get() = _userState

private fun getHasAccessToken(): Boolean = tokenRepository.getAccessToken().isNotBlank()

fun initSplash(lifecycleOwner: LifecycleOwner) {
lifecycleOwner.lifecycleScope.launch {
delay(DELAY_TIME)
if (getHasAccessToken()) {
getUserState()
} else {
_userState.value = AuthState.FAILURE
}
_userState.emit(getHasAccessToken())
}
}

private fun getUserState() =
viewModelScope.launch {
authRepository.getSplash().onSuccess {
_userState.value = AuthState.SUCCESS
}.onFailure {
val errorCode = toErrorCode(it)

_userState.value = when (errorCode) {
TENDENCY -> AuthState.OTHER_PAGE
else -> AuthState.FAILURE
}
}
}

companion object {
private const val DELAY_TIME = 2200L

private const val TENDENCY = "e4045"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.going.presentation.R
import com.going.presentation.databinding.ActivityProfileEditBinding
import com.going.presentation.designsystem.snackbar.customSnackBar
import com.going.ui.base.BaseActivity
import com.going.ui.extension.setOnSingleClickListener
import com.going.ui.extension.toast
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
Expand All @@ -30,6 +32,8 @@ class ProfileEditActivity :
observeInfoTextChanged()
observeIsValueChanged()
initBackBtnClickListener()
initProfileEditBtnClickListener()
observeIsChangedSuccess()
}

private fun setEtNameArguments() {
Expand Down Expand Up @@ -82,6 +86,21 @@ class ProfileEditActivity :
}
}

private fun initProfileEditBtnClickListener() {
binding.btnProfileEditFinish.setOnSingleClickListener {
viewModel.patchUserInfo()
}
}

private fun observeIsChangedSuccess() {
viewModel.isChangedSuccess.flowWithLifecycle(lifecycle).onEach {
if (it) {
toast(getString(R.string.edit_profile_finish))
finish()
} else customSnackBar(binding.root, getString(R.string.server_error))
}.launchIn(lifecycleScope)
}

companion object {
private const val NICKNAME = "NICKNAME"
private const val INFO = "INFO"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
package com.going.presentation.profile.edit

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.going.domain.entity.request.UserProfileRequestModel
import com.going.domain.repository.ProfileRepository
import com.going.presentation.onboarding.signup.SignUpViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

class ProfileEditViewModel : ViewModel() {
@HiltViewModel
class ProfileEditViewModel @Inject constructor(
private val profileRepository: ProfileRepository,
) : ViewModel() {
private val _isValueChanged = MutableStateFlow(false)
val isValueChanged: StateFlow<Boolean> = _isValueChanged

private val _isChangedSuccess = MutableSharedFlow<Boolean>()
val isChangedSuccess: SharedFlow<Boolean>
get() = _isChangedSuccess

private var isNameChanged = false
private var isInfoChanged = false

Expand Down Expand Up @@ -44,6 +59,17 @@ class ProfileEditViewModel : ViewModel() {
nowName.length <= getMaxNameLen() && nowInfo.length <= getMaxInfoLen() && (isInfoChanged || isNameChanged)
}

fun patchUserInfo() {
viewModelScope.launch {
profileRepository.patchUserProfile(UserProfileRequestModel(nowName, nowInfo))
.onSuccess {
_isChangedSuccess.emit(true)
}.onFailure {
_isChangedSuccess.emit(false)
}
}
}

fun getMaxNameLen() = SignUpViewModel.MAX_NAME_LEN

fun getMaxInfoLen() = SignUpViewModel.MAX_INFO_LEN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class ProfileActivity :
initRestartBtnClickListener()
}

override fun onResume() {
super.onResume()
getUserInfo()
}

private fun getUserInfo() {
profileViewModel.getUserInfoState()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.going.presentation.profile.my
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.going.domain.entity.request.UserProfileRequestModel
import com.going.domain.entity.response.UserProfileResponseModel
import com.going.domain.repository.ProfileRepository
import com.going.ui.state.UiState
import dagger.hilt.android.lifecycle.HiltViewModel
Expand All @@ -16,8 +17,8 @@ class ProfileViewModel @Inject constructor(
private val profileRepository: ProfileRepository,
) : ViewModel() {

private val _userInfoState = MutableStateFlow<UiState<UserProfileRequestModel>>(UiState.Empty)
val userInfoState: StateFlow<UiState<UserProfileRequestModel>> = _userInfoState
private val _userInfoState = MutableStateFlow<UiState<UserProfileResponseModel>>(UiState.Empty)
val userInfoState: StateFlow<UiState<UserProfileResponseModel>> = _userInfoState

val profileId = MutableStateFlow(0)

Expand Down
Loading

0 comments on commit 9480bb1

Please sign in to comment.