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

[AN] feat: 마이페이지 내정보 API 연결 #194

Merged
merged 1 commit into from
Jul 24, 2024
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
Expand Up @@ -21,6 +21,7 @@ import com.woowacourse.friendogly.domain.usecase.DeleteClubUseCase
import com.woowacourse.friendogly.domain.usecase.DeleteLocalDataUseCase
import com.woowacourse.friendogly.domain.usecase.GetClubMineUseCase
import com.woowacourse.friendogly.domain.usecase.GetJwtTokenUseCase
import com.woowacourse.friendogly.domain.usecase.GetMemberMineUseCase
import com.woowacourse.friendogly.domain.usecase.GetPetsMineUseCase
import com.woowacourse.friendogly.domain.usecase.KakaoLoginUseCase
import com.woowacourse.friendogly.domain.usecase.PostClubParticipationUseCase
Expand Down Expand Up @@ -89,6 +90,8 @@ class AppModule(context: Context) {
val postMemberUseCase: PostMemberUseCase = PostMemberUseCase(repository = memberRepository)
val getPetsMineUseCase: GetPetsMineUseCase = GetPetsMineUseCase(repository = petRepository)
val postPetUseCase: PostPetUseCase = PostPetUseCase(repository = petRepository)
val getMemberMineUseCase: GetMemberMineUseCase =
GetMemberMineUseCase(repository = memberRepository)

companion object {
private var instance: AppModule? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ import com.woowacourse.friendogly.domain.model.Member
fun MemberDto.toDomain(): Member {
return Member(
id = id,
name = name,
tag = tag,
email = email,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ package com.woowacourse.friendogly.data.model

data class MemberDto(
val id: Long,
val name: String,
val tag: String,
val email: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ class MemberRepositoryImpl(
name: String,
email: String,
): Result<Member> = source.postMember(name = name, email = email).mapCatching { result -> result.toDomain() }

override suspend fun getMemberMine(): Result<Member> = source.getMemberMine().mapCatching { result -> result.toDomain() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ interface MemberDataSource {
name: String,
email: String,
): Result<MemberDto>

suspend fun getMemberMine(): Result<MemberDto>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ package com.woowacourse.friendogly.domain.model

data class Member(
val id: Long,
val name: String,
val tag: String,
val email: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ interface MemberRepository {
name: String,
email: String,
): Result<Member>

suspend fun getMemberMine(): Result<Member>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.woowacourse.friendogly.domain.usecase

import com.woowacourse.friendogly.domain.model.Member
import com.woowacourse.friendogly.domain.repository.MemberRepository

class GetMemberMineUseCase(
private val repository: MemberRepository,
) {
suspend operator fun invoke(): Result<Member> = repository.getMemberMine()
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import com.woowacourse.friendogly.presentation.ui.mypage.adapter.PetProfileAdapt

class MyPageFragment : BaseFragment<FragmentMyPageBinding>(R.layout.fragment_my_page) {
private val viewModel: MyPageViewModel by viewModels {
MyPageViewModel.factory(getPetsMineUseCase = AppModule.getInstance().getPetsMineUseCase)
MyPageViewModel.factory(
getPetsMineUseCase = AppModule.getInstance().getPetsMineUseCase,
getMemberMineUseCase = AppModule.getInstance().getMemberMineUseCase,
)
}

private val adapter: PetProfileAdapter by lazy { PetProfileAdapter(viewModel) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import java.time.LocalDate
data class MyPageUiState(
val nickname: String = "",
val email: String = "",
val tag: String = "",
val profilePath: String? = null,
val pets: List<Pet> = emptyList(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.woowacourse.friendogly.domain.usecase.GetMemberMineUseCase
import com.woowacourse.friendogly.domain.usecase.GetPetsMineUseCase
import com.woowacourse.friendogly.presentation.base.BaseViewModel
import com.woowacourse.friendogly.presentation.base.BaseViewModelFactory
Expand All @@ -14,6 +15,7 @@ import java.time.LocalDate

class MyPageViewModel(
private val getPetsMineUseCase: GetPetsMineUseCase,
private val getMemberMineUseCase: GetMemberMineUseCase,
) : BaseViewModel(), MyPageActionHandler {
private val _uiState: MutableLiveData<MyPageUiState> = MutableLiveData(MyPageUiState())
val uiState: LiveData<MyPageUiState> get() = _uiState
Expand All @@ -23,20 +25,27 @@ class MyPageViewModel(
val navigateAction: LiveData<Event<MyPageNavigationAction>> get() = _navigateAction

init {
fetchMemberMine()
fetchPetMine()
}

fun fetchPetMine() {
val state = _uiState.value ?: return
private fun fetchMemberMine() {
viewModelScope.launch {
getMemberMineUseCase().onSuccess { member ->
val state = _uiState.value ?: return@launch
_uiState.value =
state.copy(nickname = member.name, email = member.email, tag = member.tag)
}.onFailure {
// TODO 예외 처리
}
}
}

fun fetchPetMine() {
viewModelScope.launch {
getPetsMineUseCase().onSuccess { pets ->
_uiState.value =
state.copy(
nickname = "손흥민",
email = "tottenham@gmail.com",
pets = pets,
)
val state = _uiState.value ?: return@launch
_uiState.value = state.copy(pets = pets)
}.onFailure {
// TODO 예외 처리
}
Expand Down Expand Up @@ -91,10 +100,14 @@ class MyPageViewModel(
),
)

fun factory(getPetsMineUseCase: GetPetsMineUseCase): ViewModelProvider.Factory {
fun factory(
getPetsMineUseCase: GetPetsMineUseCase,
getMemberMineUseCase: GetMemberMineUseCase,
): ViewModelProvider.Factory {
return BaseViewModelFactory { _ ->
MyPageViewModel(
getPetsMineUseCase = getPetsMineUseCase,
getMemberMineUseCase = getMemberMineUseCase,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ApiClient {
object Member {
private const val BASE_URL = "/members"
const val POST_MEMBER = BASE_URL
const val GET_MEMBER_MINE = "$BASE_URL/mine"
}

object Pet {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import com.woowacourse.friendogly.remote.model.request.PostMembersRequest
import com.woowacourse.friendogly.remote.model.response.BaseResponse
import com.woowacourse.friendogly.remote.model.response.MemberResponse
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST

interface MemberService {
@POST(ApiClient.Member.POST_MEMBER)
suspend fun postMember(
@Body body: PostMembersRequest,
): BaseResponse<MemberResponse>

@GET(ApiClient.Member.GET_MEMBER_MINE)
suspend fun getMemberMine(): BaseResponse<MemberResponse>
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ import com.woowacourse.friendogly.remote.model.response.MemberResponse
fun MemberResponse.toData(): MemberDto {
return MemberDto(
id = id,
name = name,
tag = tag,
email = email,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ class MemberDataSourceImpl(
val body = PostMembersRequest(name = name, email = email)
service.postMember(body = body).data.toData()
}

override suspend fun getMemberMine(): Result<MemberDto> =
runCatching {
service.getMemberMine().data.toData()
}
}
71 changes: 48 additions & 23 deletions android/app/src/main/res/layout/fragment_my_page.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,46 +49,71 @@
android:id="@+id/iv_user_profile"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginTop="5dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:scaleType="centerCrop"
android:src="@drawable/img_dog"
app:glideProfile1000="@{vm.uiState.profilePath}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toTopOf="parent"
app:layout_goneMarginTop="16dp" />

<TextView
android:id="@+id/tv_user_name"
style="@style/Theme.AppCompat.TextView.Medium.Black.Size18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@{vm.uiState.nickname}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/iv_user_profile" />

<TextView
android:id="@+id/tv_user_email"
style="@style/Theme.AppCompat.TextView.Medium.Gray07.Size14"
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@{vm.uiState.email}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_user_name" />
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="@+id/iv_user_profile"
app:layout_constraintStart_toEndOf="@+id/iv_user_profile"
app:layout_constraintTop_toTopOf="@+id/iv_user_profile">

<TextView
android:id="@+id/tv_user_name"
style="@style/Theme.AppCompat.TextView.Medium.Black.Size18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{vm.uiState.nickname}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="손흥민" />

<TextView
android:id="@+id/tv_user_name_tag"
style="@style/Theme.AppCompat.TextView.Medium.Gray07.Size14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:text="@{@string/user_name_tag(vm.uiState.tag)}"
app:layout_constraintBottom_toBottomOf="@+id/tv_user_name"
app:layout_constraintStart_toEndOf="@+id/tv_user_name"
app:layout_constraintTop_toTopOf="@+id/tv_user_name"
tools:text="#1234" />

<TextView
android:id="@+id/tv_user_email"
style="@style/Theme.AppCompat.TextView.Medium.Gray07.Size14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{vm.uiState.email}"
app:layout_constraintStart_toStartOf="@+id/tv_user_name"
app:layout_constraintTop_toBottomOf="@+id/tv_user_name"
tools:text="sonny@gmail.com" />


</androidx.constraintlayout.widget.ConstraintLayout>


<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="15dp"
android:layout_marginTop="16dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_user_email">
app:layout_constraintTop_toBottomOf="@+id/iv_user_profile">

<TextView
android:id="@+id/tv_edit_profile_btn"
Expand Down
2 changes: 2 additions & 0 deletions android/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
<string name="edit_profile_title">프로필 편집</string>
<string name="register_dog_title">등록하기</string>
<string name="empty_pet_description">등록하기를 통해 강아지를 추가해주세요</string>
<string name="user_name_tag">#%s</string>


<!--강아지 등록-->
<string name="dog_name_title">댕댕이 이름</string>
Expand Down
Loading