Skip to content

Commit

Permalink
[Feature/#9] myProfile 기능에 viewModel 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
gaeun5744 committed Jun 28, 2023
1 parent c14d32f commit 3459f2d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,28 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.viewModels
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import org.android.go.sopt.databinding.FragmentMyProfileBinding
import org.android.go.sopt.present.loginPage.LoginActivity
import org.android.go.sopt.util.MySharedPreferences
import org.android.go.sopt.remote.ServicePool
import org.android.go.sopt.present.viewModel.LoginPageViewModel
import org.android.go.sopt.util.MyApplication
import org.android.go.sopt.util.ViewModelFactory

class MyProfileFragment : Fragment() {
private var _binding: FragmentMyProfileBinding? = null
private val binding: FragmentMyProfileBinding
get() = requireNotNull(_binding) { "앗 ! _binding이 null이다 !" }

private val myProfileService = ServicePool.loginPageService
private val viewModel: LoginPageViewModel by viewModels { ViewModelFactory() }

override fun onCreateView( // 뷰를 만든다 << 이때 초기화하면 좋음
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View { // 이제 반환하는 View가 Null일 수 없기 때문에, ?를 지워주셔도 됩니다.
): View {
_binding = FragmentMyProfileBinding.inflate(inflater, container, false)
return binding.root
}
Expand All @@ -33,6 +35,7 @@ class MyProfileFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
getProfile()
observeMyProfile()
clickLogOut()
}

Expand All @@ -42,22 +45,16 @@ class MyProfileFragment : Fragment() {
}

private fun getProfile() {
/*myProfileService.myProfile(
MySharedPreferences.getUserId(requireContext())
).enqueue(object : retrofit2.Callback<MyProfileDto> {
override fun onResponse(call: Call<MyProfileDto>, response: Response<MyProfileDto>) {
if (response.isSuccessful) {
binding.name.text = "이름 : ${response.body()?.data?.name}"
binding.specialty.text = "특기 : ${response.body()?.data?.skill}"
} else {
requireContext().makeToastMessage("서버 실패")
}
}
viewModel.getMyProfile(MyApplication.mySharedPreferences.getUserId())
}

override fun onFailure(call: Call<MyProfileDto>, t: Throwable) {
requireContext().makeToastMessage("서버 실패")
private fun observeMyProfile() {
viewModel.getMyProfile.observe(this) {
with(binding) {
name.text = "이름 : ${it.data.name}"
specialty.text = "특기 : ${it.data.skill}"
}
})*/
}
}

private fun clickLogOut() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.android.go.sopt.present.viewModel

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import org.android.go.sopt.RequestSignUpDto
import org.android.go.sopt.remote.remoteData.model.MyProfileDto
import org.android.go.sopt.remote.remoteData.model.RequestLogInDto
import org.android.go.sopt.remote.remoteData.repoImpl.LoginPageRepoImpl
import org.android.go.sopt.util.Event
Expand All @@ -18,6 +20,9 @@ class LoginPageViewModel(private val loginPageRepoImpl: LoginPageRepoImpl) : Vie
private val _signUpResult = MutableLiveData<Boolean>()
val signUpResult: LiveData<Boolean> get() = _signUpResult

private val _getMyProfile = MutableLiveData<MyProfileDto>()
val getMyProfile: LiveData<MyProfileDto> get() = _getMyProfile

fun login(request: RequestLogInDto) = viewModelScope.launch {
kotlin.runCatching {
loginPageRepoImpl.login(request)
Expand All @@ -38,4 +43,14 @@ class LoginPageViewModel(private val loginPageRepoImpl: LoginPageRepoImpl) : Vie
_signUpResult.value = false
}
}

fun getMyProfile(userId: String) = viewModelScope.launch {
kotlin.runCatching {
loginPageRepoImpl.myProfile(userId)
}.onSuccess { response ->
_getMyProfile.value = response
}.onFailure {
Log.d("error", "서버 통신 실패")
}
}
}

0 comments on commit 3459f2d

Please sign in to comment.