Skip to content

Commit

Permalink
#210 UserResponse -> UserEntity 변환 로직 추가 및 bindingAdapter 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
tgyuuAn committed Aug 20, 2023
1 parent 037f321 commit 9a394b0
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ object BindingAdapter {
GlideApp.with(imageView.context).load(img).centerInside().skipMemoryCache(caching).into(imageView)
}

@BindingAdapter("loadUserImage", requireAll = false)
@JvmStatic
fun loadUserImage(imageView: ImageView, img: String) {
if (img.isEmpty()) {
imageView.setImageResource(R.drawable.default_user_image)
return
}
GlideApp.with(imageView.context).load(img).circleCrop().into(imageView)
}

@BindingAdapter("img")
@JvmStatic
fun loadImage(imageView: ImageView, img: Bitmap) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.android.mediproject.core.data.user

import com.android.mediproject.core.model.user.AccountState
import com.android.mediproject.core.model.user.remote.UserResponse
import com.android.mediproject.core.model.user.User
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow

interface UserInfoRepository {
val myAccountInfo: StateFlow<AccountState>
fun getMyAccountInfo(): Flow<Result<UserResponse>>
fun getMyAccountInfo(): Flow<Result<User>>

suspend fun updateMyAccountInfo(accountState: AccountState)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.android.mediproject.core.data.user

import com.android.mediproject.core.datastore.AppDataStore
import com.android.mediproject.core.model.user.AccountState
import com.android.mediproject.core.model.user.User
import com.android.mediproject.core.model.user.remote.toUser
import com.android.mediproject.core.network.datasource.user.UserInfoDataSource
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
Expand Down Expand Up @@ -57,13 +59,18 @@ class UserInfoRepositoryImpl @Inject constructor(
*/
override fun getMyAccountInfo() = channelFlow {
userInfoDataSource.getUserInfo().collectLatest {
it.onSuccess { userResponse ->
_myAccountInfo.value = AccountState.SignedIn(userResponse.userId, userResponse.nickname, userResponse.email)
}.onFailure {
_myAccountInfo.value = AccountState.Unknown
it.fold(
onSuccess = { userResponse ->
_myAccountInfo.value = AccountState.SignedIn(userResponse.userId, userResponse.nickname, userResponse.email)
Result.success(userResponse.toUser())
},
onFailure = {
_myAccountInfo.value = AccountState.Unknown
Result.failure(it)
},
).also {
trySend(it)
}
trySend(it)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.android.mediproject.core.model.user

data class User(
val nickName: String,
) {
var id: Long = 0
var email: String = ""
}
val profileUrl: String,
var id: Long = 0,
var email: String = "",
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.android.mediproject.core.model.user.remote

import com.android.mediproject.core.model.user.User
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

/**
Expand All @@ -13,10 +14,11 @@ import kotlinx.serialization.Serializable
*/
@Serializable
data class UserResponse(
val nickname: String, val userId: Long, val email: String, val message: String, @SerialName("profile_url") val profileUrl: String? = "",
val nickname: String, val userId: Long, val email: String, val message: String, @SerialName("profile_url") val profileUrl: String = "",
)

fun UserResponse.toUserDto() = User(nickName = nickname).apply {
id = userId
email = this@toUserDto.email
}
fun UserResponse.toUser() = User(
nickName = nickname, profileUrl = profileUrl,
id = userId,
email = this@toUser.email,
)
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ class MyPageFragment :

is UiState.Success -> {
setSuccessUserVisible()
binding.userDto = userState.data
binding.user = userState.data

}

is UiState.Error -> {
Expand All @@ -141,6 +142,7 @@ class MyPageFragment :
}
}


private fun setLoadingUserVisible() = binding.apply {
userNameTV.visibility = View.GONE
userImageIV.visibility = View.GONE
Expand Down
7 changes: 3 additions & 4 deletions feature/mypage/src/main/res/layout/fragment_my_page.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
type="com.android.mediproject.feature.mypage.MyPageViewModel" />

<variable
name="userDto"
name="user"
type="com.android.mediproject.core.model.user.User" />
</data>

Expand Down Expand Up @@ -152,7 +152,7 @@
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:src="@drawable/default_user_image"
loadUserImage="@{user.profileUrl}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

Expand All @@ -161,7 +161,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:text="@{userDto.nickName}"
android:text="@{user.nickName}"
android:textColor="@color/black"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="@id/userImageIV"
Expand Down Expand Up @@ -266,7 +266,6 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />

</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Expand Down

0 comments on commit 9a394b0

Please sign in to comment.