Skip to content

Commit

Permalink
feat/#11: 회원 정보 조회
Browse files Browse the repository at this point in the history
  • Loading branch information
youjin09222 committed May 3, 2024
1 parent 1938b2e commit 18d2d22
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package com.sopt.now.compose
package com.sopt.now.compose.presentation

import androidx.compose.runtime.livedata.observeAsState

import android.os.Bundle
import androidx.activity.ComponentActivity
Expand Down Expand Up @@ -26,6 +28,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.sopt.now.compose.BottomNavigationItem
import com.sopt.now.compose.ui.theme.NOWSOPTAndroidTheme

class MainActivity : ComponentActivity() {
Expand All @@ -37,26 +40,17 @@ class MainActivity : ComponentActivity() {
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
val viewModel: UserInfoViewModel = remember { UserInfoViewModel() }

val userId = intent.getStringExtra("userId").toString()
val userPw = intent.getStringExtra("userPw").toString()
val userName = intent.getStringExtra("userName").toString()
val userDescription = intent.getStringExtra("userDescription").toString()

MainView(
userId = userId,
userPw = userPw,
userName = userName,
userDescription = userDescription
)
MainView(viewModel)
}
}
}
}
}

@Composable
fun MainView(userId: String, userPw: String, userName: String, userDescription: String) {
fun MainView(viewModel: UserInfoViewModel) {
var selectedItem by remember { mutableIntStateOf(0) }
val items = listOf(
BottomNavigationItem(
Expand Down Expand Up @@ -85,33 +79,53 @@ fun MainView(userId: String, userPw: String, userName: String, userDescription:
)
}
}
},
}
) { innerPadding ->
Column(
modifier = Modifier
.padding(innerPadding),
modifier = Modifier.padding(innerPadding),
verticalArrangement = Arrangement.spacedBy(16.dp),
) {
when(selectedItem) {
0 -> {
HomeView()
}
1 -> {
Text(text ="Search")
}
2 -> {
MyPageView(userId, userPw, userName, userDescription)
}
0 -> SetHomeView(viewModel)
1 -> Text(text ="Search")
2 -> SetMyPageView(viewModel)
}

}
}
}

// 사용자 데이터 적용
@Composable
fun SetHomeView(viewModel: UserInfoViewModel){

viewModel.userInfo()

viewModel.userInfoLiveData.observeAsState().value?.let { userInfo ->
HomeView(
userName = userInfo.data.nickname,
userPhone = userInfo.data.phone,
)
}
}

@Composable
fun SetMyPageView(viewModel: UserInfoViewModel){

viewModel.userInfo()

viewModel.userInfoLiveData.observeAsState().value?.let { userInfo ->
MyPageView(
userId = userInfo.data.authenticationId,
userPw = userInfo.data.nickname,
userPhone = userInfo.data.phone,
)
}
}

@Preview(showBackground = true)
@Composable
fun MainPreview() {
NOWSOPTAndroidTheme {
MainView("Id1234", "Password123", "UserName", "ISTP 입니다!")
MyPageView("", "", "")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.sopt.now.compose.presentation

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.google.gson.Gson
import com.sopt.now.compose.data.BaseState
import com.sopt.now.compose.data.ServicePool
import com.sopt.now.compose.data.dto.response.ResponseAuthDto
import com.sopt.now.compose.data.dto.response.ResponseUserInfoDto
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class UserInfoViewModel : ViewModel() {
private val userService by lazy { ServicePool.userService }
val liveData = MutableLiveData<BaseState>()
val userInfoLiveData = MutableLiveData<ResponseUserInfoDto?>()

fun userInfo() {
userService.userInfo().enqueue(object : Callback<ResponseUserInfoDto> {
override fun onResponse(
call: Call<ResponseUserInfoDto>,
response: Response<ResponseUserInfoDto>,
) {
if (response.isSuccessful) {
val data: ResponseUserInfoDto? = response.body()
val userId = response.headers()["location"]
userInfoLiveData.value = data

liveData.value = BaseState(
isSuccess = true,
message = response.message()
)
Log.d("UserInfo", "data: $data, userId: $userId")
} else {
val error = response.errorBody()?.string()
val gson = Gson()
try {
val errorResponse = gson.fromJson(error, ResponseAuthDto::class.java)
liveData.value = BaseState(
isSuccess = false,
message = "회원 정보 조회 실패: ${errorResponse.message}" // 에러 메시지 사용
)
} catch (e: Exception) {
liveData.value = BaseState(
isSuccess = false,
message = "회원 정보 조회 실패: 에러 메시지 파싱 실패"
)
}
}
}

override fun onFailure(call: Call<ResponseUserInfoDto>, t: Throwable) {
liveData.value = BaseState(
isSuccess = false,
message = "서버 에러"
)
}
})
}
}

0 comments on commit 18d2d22

Please sign in to comment.