From 18d2d222f3952299bffca89af9b83e11ba64e538 Mon Sep 17 00:00:00 2001 From: youjin09222 Date: Fri, 3 May 2024 23:31:14 +0900 Subject: [PATCH] =?UTF-8?q?feat/#11:=20=ED=9A=8C=EC=9B=90=20=EC=A0=95?= =?UTF-8?q?=EB=B3=B4=20=EC=A1=B0=ED=9A=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{ => presentation}/MainActivity.kt | 68 +++++++++++-------- .../compose/presentation/UserInfoViewModel.kt | 62 +++++++++++++++++ 2 files changed, 103 insertions(+), 27 deletions(-) rename app/src/main/java/com/sopt/now/compose/{ => presentation}/MainActivity.kt (69%) create mode 100644 app/src/main/java/com/sopt/now/compose/presentation/UserInfoViewModel.kt diff --git a/app/src/main/java/com/sopt/now/compose/MainActivity.kt b/app/src/main/java/com/sopt/now/compose/presentation/MainActivity.kt similarity index 69% rename from app/src/main/java/com/sopt/now/compose/MainActivity.kt rename to app/src/main/java/com/sopt/now/compose/presentation/MainActivity.kt index d6075a3..95d401d 100644 --- a/app/src/main/java/com/sopt/now/compose/MainActivity.kt +++ b/app/src/main/java/com/sopt/now/compose/presentation/MainActivity.kt @@ -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 @@ -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() { @@ -37,18 +40,9 @@ 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) } } } @@ -56,7 +50,7 @@ class MainActivity : ComponentActivity() { } @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( @@ -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("", "", "") } } \ No newline at end of file diff --git a/app/src/main/java/com/sopt/now/compose/presentation/UserInfoViewModel.kt b/app/src/main/java/com/sopt/now/compose/presentation/UserInfoViewModel.kt new file mode 100644 index 0000000..812278c --- /dev/null +++ b/app/src/main/java/com/sopt/now/compose/presentation/UserInfoViewModel.kt @@ -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() + val userInfoLiveData = MutableLiveData() + + fun userInfo() { + userService.userInfo().enqueue(object : Callback { + override fun onResponse( + call: Call, + response: Response, + ) { + 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, t: Throwable) { + liveData.value = BaseState( + isSuccess = false, + message = "서버 에러" + ) + } + }) + } +} \ No newline at end of file