diff --git a/app/src/main/java/umc/com/mobile/project/data/model/plan/SavelicenseRequest.kt b/app/src/main/java/umc/com/mobile/project/data/model/plan/SavelicenseRequest.kt index 08022d8..478df2e 100644 --- a/app/src/main/java/umc/com/mobile/project/data/model/plan/SavelicenseRequest.kt +++ b/app/src/main/java/umc/com/mobile/project/data/model/plan/SavelicenseRequest.kt @@ -4,17 +4,13 @@ import java.time.LocalDate data class SaveInfo( - val name: String, - val date: String + val name: String, + val date: String ) - - - data class SavelicenseRequest( - val info : List, - - ) + val info: List +) diff --git a/app/src/main/java/umc/com/mobile/project/data/model/plan/SemesterTimeResponse.kt b/app/src/main/java/umc/com/mobile/project/data/model/plan/SemesterTimeResponse.kt index 2b3b917..7a9e492 100644 --- a/app/src/main/java/umc/com/mobile/project/data/model/plan/SemesterTimeResponse.kt +++ b/app/src/main/java/umc/com/mobile/project/data/model/plan/SemesterTimeResponse.kt @@ -1,14 +1,17 @@ package umc.com.mobile.project.data.model.plan data class SemesterTimeResponse( - val isSuccess: Boolean, - val code: String, - val message: String, - val result: List, -) + val isSuccess: Boolean, + val code: String, + val message: String, + val result: List, +) { + + +} data class semesterResult( - val hakkiNum: String, - val hakkiText: String, + val hakkiNum: String, + val hakkiText: String, ) diff --git a/app/src/main/java/umc/com/mobile/project/ui/plan/PlanSemesterAdapter.kt b/app/src/main/java/umc/com/mobile/project/ui/plan/PlanSemesterAdapter.kt index 1360383..912f19e 100644 --- a/app/src/main/java/umc/com/mobile/project/ui/plan/PlanSemesterAdapter.kt +++ b/app/src/main/java/umc/com/mobile/project/ui/plan/PlanSemesterAdapter.kt @@ -7,28 +7,31 @@ import umc.com.mobile.project.data.model.plan.semesterResult import umc.com.mobile.project.databinding.ItemChooseSemesterBinding class PlanSemesterAdapter( semesterList: List): RecyclerView.Adapter(){ - var semesterList: List = semesterList - set(value) { - field = value - notifyDataSetChanged() - //데이터가 바뀌었다 알려줌. - } + var semesterList: List = semesterList + set(value) { + field = value + notifyDataSetChanged() + //데이터가 바뀌었다 알려줌. + } - override fun getItemCount(): Int { - return semesterList?.size ?: 0 - } + override fun getItemCount(): Int { + return semesterList?.size ?: 0 + } - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PlanSemesterAdapter.NonSubjectViewHolder { - val itemBinding = ItemChooseSemesterBinding.inflate(LayoutInflater.from(parent.context), parent, false) - return PlanSemesterAdapter.NonSubjectViewHolder(itemBinding) - } + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PlanSemesterAdapter.NonSubjectViewHolder { + val itemBinding = ItemChooseSemesterBinding.inflate(LayoutInflater.from(parent.context), parent, false) + return PlanSemesterAdapter.NonSubjectViewHolder(itemBinding) + } - override fun onBindViewHolder(holder: PlanSemesterAdapter.NonSubjectViewHolder, position: Int) { + override fun onBindViewHolder(holder: PlanSemesterAdapter.NonSubjectViewHolder, position: Int) { - holder.itemBinding.planTimeSemesterItem.text =semesterList[position]?.hakkiText.toString() + val semesterInfo = semesterList[position] + if (semesterInfo != null) { + holder.itemBinding.planTimeSemesterItem.text = semesterInfo.hakkiText + } - } + } - class NonSubjectViewHolder(val itemBinding: ItemChooseSemesterBinding) : RecyclerView.ViewHolder(itemBinding.root) + class NonSubjectViewHolder(val itemBinding: ItemChooseSemesterBinding) : RecyclerView.ViewHolder(itemBinding.root) } \ No newline at end of file diff --git a/app/src/main/java/umc/com/mobile/project/ui/plan/PlanSemesterFragment.kt b/app/src/main/java/umc/com/mobile/project/ui/plan/PlanSemesterFragment.kt index d1f412e..c173abb 100644 --- a/app/src/main/java/umc/com/mobile/project/ui/plan/PlanSemesterFragment.kt +++ b/app/src/main/java/umc/com/mobile/project/ui/plan/PlanSemesterFragment.kt @@ -16,42 +16,59 @@ import umc.com.mobile.project.databinding.FragmentPlanlicenseBinding import android.widget.EditText import androidx.core.view.setMargins import androidx.lifecycle.Observer -import umc.com.mobile.project.data.model.plan.SaveInfo +import androidx.recyclerview.widget.LinearLayoutManager + import umc.com.mobile.project.data.model.plan.SavelicenseRequest import umc.com.mobile.project.data.model.plan.semesterResult import umc.com.mobile.project.databinding.SemesterChooseBinding import java.time.LocalDate class PlanSemesterFragment : Fragment() { - private var _binding: SemesterChooseBinding? = null - private val viewModel: PlanViewModel by viewModels() - private val binding get() = _binding!! + private var _binding: SemesterChooseBinding? = null + private val viewModel: PlanViewModel by viewModels() + private val binding get() = _binding!! + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + _binding = SemesterChooseBinding.inflate(inflater, container, false) + + + + + + return binding.root + } - override fun onCreateView( - inflater: LayoutInflater, - container: ViewGroup?, - savedInstanceState: Bundle? - ): View { - _binding = SemesterChooseBinding.inflate(inflater, container, false) + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + val adapter = PlanSemesterAdapter(listOf()) // 초기 데이터로 빈 리스트 사용 - return binding.root - } + // RecyclerView에 어댑터와 레이아웃 매니저 설정 + binding.recyclerViewPlanSemester.adapter = adapter + binding.recyclerViewPlanSemester.layoutManager = LinearLayoutManager(context) + viewModel.planSemesterInfo.observe(viewLifecycleOwner) { semesterInfo -> + // 데이터가 업데이트되면 어댑터의 리스트 업데이트 + adapter.semesterList = semesterInfo?.result ?: emptyList() + adapter.notifyDataSetChanged() - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) + } - } + viewModel.getSemesterInfo() // 데이터 로드 + } - override fun onDestroyView() { - super.onDestroyView() - _binding = null - } + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } } diff --git a/app/src/main/java/umc/com/mobile/project/ui/plan/PlanSettingFragment.kt b/app/src/main/java/umc/com/mobile/project/ui/plan/PlanSettingFragment.kt index ee95e4a..51b01b1 100644 --- a/app/src/main/java/umc/com/mobile/project/ui/plan/PlanSettingFragment.kt +++ b/app/src/main/java/umc/com/mobile/project/ui/plan/PlanSettingFragment.kt @@ -17,58 +17,57 @@ import umc.com.mobile.project.ui.gradInfo.adapter.GradInfoVPAdapter import umc.com.mobile.project.ui.gradInfo.viewmodel.GradInfoViewModel class PlanSettingFragment : Fragment() { - private var _binding:PlanTimeTabMainBinding? = null - private val viewModel: PlanViewModel by viewModels() - private val binding get() = _binding!! + private var _binding:PlanTimeTabMainBinding? = null + private val viewModel: PlanViewModel by viewModels() + private val binding get() = _binding!! - private lateinit var viewPager : ViewPager2 - private lateinit var tabLayout : TabLayout + private lateinit var viewPager : ViewPager2 + private lateinit var tabLayout : TabLayout - override fun onCreateView( - inflater: LayoutInflater, - container: ViewGroup?, - savedInstanceState: Bundle? - ): View { - _binding = PlanTimeTabMainBinding.inflate(inflater, container, false) + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + _binding = PlanTimeTabMainBinding.inflate(inflater, container, false) - initTabLayout() - initViewPager() + initTabLayout() + initViewPager() - return binding.root - } + return binding.root + } - private fun initTabLayout() { + private fun initTabLayout() { - val tabTitle = arrayOf("시간표", "자격증", "자유") + val tabTitle = arrayOf("시간표", "자격증", "자유") - viewPager = binding.viewPagerTimeTabMain - tabLayout = binding.tabLayoutPlanTime + viewPager = binding.viewPagerTimeTabMain + tabLayout = binding.tabLayoutPlanTime - val adapter = PlanVPAdapter(this) + val adapter = PlanVPAdapter(this) + adapter.addFragment(PlanTimeFragment()) + adapter.addFragment(PlanlicenseFragment()) + adapter.addFragment(PlanFreeFragment()) - adapter.addFragment(PlanlicenseFragment()) - adapter.addFragment(PlanTimeFragment()) - adapter.addFragment(PlanFreeFragment()) + viewPager.orientation = ViewPager2.ORIENTATION_HORIZONTAL + viewPager.adapter = adapter - viewPager.orientation = ViewPager2.ORIENTATION_HORIZONTAL - viewPager.adapter = adapter + TabLayoutMediator(tabLayout, viewPager + ) { tab, position -> tab.text = tabTitle[position] }.attach() + } - TabLayoutMediator(tabLayout, viewPager - ) { tab, position -> tab.text = tabTitle[position] }.attach() - } + private fun initViewPager() { - private fun initViewPager() { + binding.viewPagerTimeTabMain.isUserInputEnabled = false + } - binding.viewPagerTimeTabMain.isUserInputEnabled = false - } - - override fun onDestroyView() { - super.onDestroyView() - _binding = null - } + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } } \ No newline at end of file diff --git a/app/src/main/java/umc/com/mobile/project/ui/plan/PlanTimeFragment.kt b/app/src/main/java/umc/com/mobile/project/ui/plan/PlanTimeFragment.kt index 5964ae4..8def1b7 100644 --- a/app/src/main/java/umc/com/mobile/project/ui/plan/PlanTimeFragment.kt +++ b/app/src/main/java/umc/com/mobile/project/ui/plan/PlanTimeFragment.kt @@ -17,54 +17,64 @@ import umc.com.mobile.project.ui.common.NavigationUtil.navigate import umc.com.mobile.project.ui.plan.PlanViewModel class PlanTimeFragment : Fragment() { - private var _binding: PlanSubjectListBinding? = null - private val viewModel: PlanViewModel by viewModels() - private val binding get() = _binding!! + private var _binding: PlanSubjectListBinding? = null + private val viewModel: PlanViewModel by viewModels() + private val binding get() = _binding!! - override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? - ): View { - _binding = PlanSubjectListBinding.inflate(inflater, container, false) - binding.planSubjectListSemester.setOnClickListener{ - navigate(R.id.action_planTimeFragment_to_planSemesterFragment) - } + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? + ): View { + _binding = PlanSubjectListBinding.inflate(inflater, container, false) - viewModel.getListTimeInfo() - val adapter = viewModel.listTimeInfo.value?.let { PlanRecyclerAdapter(it.result) } - binding.recyclerView.adapter = adapter - binding.recyclerView.layoutManager = - LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) + viewModel.getListTimeInfo() + val adapter = viewModel.listTimeInfo.value?.let { PlanRecyclerAdapter(it.result) } + binding.recyclerView.adapter = adapter + binding.recyclerView.layoutManager = + LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) - viewModel.listTimeInfo.observe(viewLifecycleOwner) { timenewList -> - if (timenewList != null) { - if (adapter != null) { - adapter.timeList=timenewList.result - adapter.notifyDataSetChanged() - } - } + viewModel.listTimeInfo.observe(viewLifecycleOwner) { timenewList -> + if (timenewList != null) { + if (adapter != null) { + adapter.timeList=timenewList.result + adapter.notifyDataSetChanged() + } + } - } + } - return binding.root - } + return binding.root + } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + binding.planSubjectListSemester.setOnClickListener { - override fun onDestroyView() { - super.onDestroyView() - _binding = null - } + findNavController().navigate(R.id.action_planTimeFragment_to_planSemesterFragment) + } + } + + + + + + + + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } } diff --git a/app/src/main/java/umc/com/mobile/project/ui/plan/PlanTrackFragment.kt b/app/src/main/java/umc/com/mobile/project/ui/plan/PlanTrackFragment.kt index be1cf89..75f1bf6 100644 --- a/app/src/main/java/umc/com/mobile/project/ui/plan/PlanTrackFragment.kt +++ b/app/src/main/java/umc/com/mobile/project/ui/plan/PlanTrackFragment.kt @@ -16,7 +16,6 @@ import umc.com.mobile.project.databinding.FragmentPlanlicenseBinding import android.widget.EditText import androidx.core.view.setMargins import androidx.lifecycle.Observer -import umc.com.mobile.project.data.model.plan.SaveInfo import umc.com.mobile.project.data.model.plan.SavelicenseRequest import umc.com.mobile.project.data.model.plan.semesterResult import umc.com.mobile.project.databinding.PlanTimeChooseTrackBinding @@ -24,35 +23,35 @@ import umc.com.mobile.project.databinding.SemesterChooseBinding import java.time.LocalDate class PlanTrackFragment : Fragment() { - private var _binding: PlanTimeChooseTrackBinding? = null - private val viewModel: PlanViewModel by viewModels() - private val binding get() = _binding!! + private var _binding: PlanTimeChooseTrackBinding? = null + private val viewModel: PlanViewModel by viewModels() + private val binding get() = _binding!! - override fun onCreateView( - inflater: LayoutInflater, - container: ViewGroup?, - savedInstanceState: Bundle? - ): View { - _binding = PlanTimeChooseTrackBinding.inflate(inflater, container, false) + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + _binding = PlanTimeChooseTrackBinding.inflate(inflater, container, false) - return binding.root - } + return binding.root + } - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) - } + } - override fun onDestroyView() { - super.onDestroyView() - _binding = null - } + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } } diff --git a/app/src/main/java/umc/com/mobile/project/ui/plan/PlanViewModel.kt b/app/src/main/java/umc/com/mobile/project/ui/plan/PlanViewModel.kt index dc83956..268e519 100644 --- a/app/src/main/java/umc/com/mobile/project/ui/plan/PlanViewModel.kt +++ b/app/src/main/java/umc/com/mobile/project/ui/plan/PlanViewModel.kt @@ -10,7 +10,6 @@ import retrofit2.Callback import retrofit2.Response import umc.com.mobile.project.data.model.plan.BringlicenseResponse import umc.com.mobile.project.data.model.plan.ListTimeResponse -import umc.com.mobile.project.data.model.plan.PlanTrackResponse import umc.com.mobile.project.data.model.plan.SaveInfo import umc.com.mobile.project.data.model.plan.SavelicenseRequest @@ -23,237 +22,202 @@ import umc.com.mobile.project.data.network.api.PlanApi class PlanViewModel : ViewModel() { - //api - private val planApiService = ApiClient.createService() + //api + private val planApiService = ApiClient.createService() - private val _hakki: MutableLiveData = MutableLiveData() - val hakki: LiveData - get() = _hakki + private val _hakki: MutableLiveData = MutableLiveData() + val hakki: LiveData + get() = _hakki - private val _track: MutableLiveData = MutableLiveData() - val track: LiveData - get() = _track + private val _track: MutableLiveData = MutableLiveData() + val track: LiveData + get() = _track - private val _planSemesterInfo : MutableLiveData = MutableLiveData() - val planSemesterInfo :LiveData - get()=_planSemesterInfo + private val _planSemesterInfo : MutableLiveData = MutableLiveData() + val planSemesterInfo :LiveData + get()=_planSemesterInfo - private val _planTrackInfo : MutableLiveData = MutableLiveData() - val planTrackInfo :LiveData - get()=_planTrackInfo + private val _planTimeStatus: MutableLiveData = MutableLiveData() + val planTimeStatus: LiveData + get() = _planTimeStatus - private val _planTimeStatus: MutableLiveData = MutableLiveData() - val planTimeStatus: LiveData - get() = _planTimeStatus + private val _bringLicenseInfo = MutableLiveData() - private val _bringLicenseInfo = MutableLiveData() + + private val _listTimeInfo = MutableLiveData() + + val listTimeInfo: LiveData + get() = _listTimeInfo + + val bringLicenseInfo: LiveData + get() = _bringLicenseInfo + + + + + + private val _licenseInfo: MutableLiveData = MutableLiveData() + val licenseInfo: LiveData + get() = _licenseInfo - private val _listTimeInfo = MutableLiveData() - - val listTimeInfo: LiveData - get() = _listTimeInfo - - val bringLicenseInfo: LiveData - get() = _bringLicenseInfo - - - - - - private val _licenseInfo: MutableLiveData = MutableLiveData() - val licenseInfo: LiveData - get() = _licenseInfo - - - private val _error: MutableLiveData = MutableLiveData() - val error: LiveData - get() = _error - - - private val _savelicenseInfo : MutableLiveData =MutableLiveData() - val savelicenseInfo : LiveData - get()=_savelicenseInfo - - - // 기존에 있던 text LiveData - private val _text = MutableLiveData().apply { - value = "This is Plan Fragment" - } - val text: LiveData = _text - - // 새로 추가된 isFilledAllOptions LiveData - private val _isFilledAllOptions = MutableLiveData().apply { - value = false // 초기값 설정 - } - val isFilledAllOptions: LiveData = _isFilledAllOptions - - // isFilledAllOptions의 값을 업데이트하는 메서드 - fun updateIsFilledAllOptions(isFilled: Boolean) { - _isFilledAllOptions.value = isFilled - } - - - fun getListTimeInfo() { - _hakki.value?.let { - _track.value?.let { it1 -> - planApiService.getListTime(it, it1).enqueue(object : Callback { - override fun onResponse( - call: Call, - response: Response - ) { - if (response.isSuccessful) { - if (response.body() != null) { - _listTimeInfo.postValue(response.body()) - Log.d("Planlicense", "${response.body()}") - } else { - _error.postValue("서버 응답이 올바르지 않습니다.") - } - } else { - _error.postValue("사용자 정보를 가져오지 못했습니다.") - try { - throw response.errorBody()?.string()?.let { - RuntimeException(it) - } ?: RuntimeException("Unknown error") - } catch (e: Exception) { - Log.e("PlanInfo", "PlanResponse API 오류: ${e.message}") - } - } - } - - override fun onFailure(call: Call, t: Throwable) { - _error.postValue("네트워크 오류: ${t.message}") - Log.d("gradInfo", "completion: ${t.message}") - } - }) - } - } - } - - - fun getLicenseInfo() { - planApiService.getUPlicense().enqueue(object : Callback { - override fun onResponse( - call: Call, - response: Response - ) { - if (response.isSuccessful) { - if (response.body() != null) { - _licenseInfo.postValue(response.body()) - Log.d("Planlicense", "${response.body()}") - } else { - _error.postValue("서버 응답이 올바르지 않습니다.") - } - } else { - _error.postValue("사용자 정보를 가져오지 못했습니다.") - try { - throw response.errorBody()?.string()?.let { - RuntimeException(it) - } ?: RuntimeException("Unknown error") - } catch (e: Exception) { - Log.e("PlanInfo", "PlanResponse API 오류: ${e.message}") - } - } - } - - override fun onFailure(call: Call, t: Throwable) { - _error.postValue("네트워크 오류: ${t.message}") - Log.d("gradInfo", "completion: ${t.message}") - } - }) - } - - - fun saveLicense(request: List) { - - planApiService.saveLicense(request).enqueue(object : Callback { - override fun onResponse(call: Call, response: Response - ) { - if (response.isSuccessful) { - _bringLicenseInfo.postValue(response.body()) - Log.d("PlanViewModel", "License saved successfully: ${response.body()}") - } else { - _error.postValue("자격증 정보 저장 실패: ${response.errorBody()?.string()}") - Log.e( - "PlanViewModel", - "Error saving license: ${response.errorBody()?.string()}" - ) - } - } - - override fun onFailure(call: Call, t: Throwable) { - _error.postValue("네트워크 오류: ${t.message}") - Log.e("PlanViewModel", "Network error: ${t.message}") - } - }) - } - - - - fun getSemesterInfo() { - planApiService.getSemesterInfo().enqueue(object : Callback { - override fun onResponse( - call: Call, - response: Response - ) { - if (response.isSuccessful) { - if (response.body() != null) { - _planSemesterInfo.postValue(response.body()) - Log.d("PlanSemester", "${response.body()}") - } else { - _error.postValue("서버 응답이 올바르지 않습니다.") - } - } else { - _error.postValue("사용자 정보를 가져오지 못했습니다.") - try { - throw response.errorBody()?.string()?.let { - RuntimeException(it) - } ?: RuntimeException("Unknown error") - } catch (e: Exception) { - Log.e("PlanInfo", "PlanResponse API 오류: ${e.message}") - } - } - } - - override fun onFailure(call: Call, t: Throwable) { - _error.postValue("네트워크 오류: ${t.message}") - Log.d("gradInfo", "completion: ${t.message}") - } - }) - } - - - fun getTrackInfo(hakki: String) { - planApiService.getTrackInfo(hakki).enqueue(object : Callback { - override fun onResponse(call: Call, response: Response) { - if (response.isSuccessful) { - if (response.body() != null) { - _planTrackInfo.postValue(response.body()) - Log.d("PlanTrackInfo", "${response.body()}") - } else { - _error.postValue("서버 응답이 올바르지 않습니다.") - } - } else { - _error.postValue("사용자 정보를 가져오지 못했습니다.") - try { - throw response.errorBody()?.string()?.let { - RuntimeException(it) - } ?: RuntimeException("Unknown error") - } catch (e: Exception) { - Log.e("PlanInfo", "PlanResponse API 오류: ${e.message}") - } - } - } - - override fun onFailure(call: Call, t: Throwable) { - _error.postValue("네트워크 오류: ${t.message}") - Log.d("gradInfo", "completion: ${t.message}") - } - }) - } - + private val _error: MutableLiveData = MutableLiveData() + val error: LiveData + get() = _error + + + private val _savelicenseInfo : MutableLiveData =MutableLiveData() + val savelicenseInfo : LiveData + get()=_savelicenseInfo + + + // 기존에 있던 text LiveData + private val _text = MutableLiveData().apply { + value = "This is Plan Fragment" + } + val text: LiveData = _text + + // 새로 추가된 isFilledAllOptions LiveData + private val _isFilledAllOptions = MutableLiveData().apply { + value = false // 초기값 설정 + } + val isFilledAllOptions: LiveData = _isFilledAllOptions + + // isFilledAllOptions의 값을 업데이트하는 메서드 + fun updateIsFilledAllOptions(isFilled: Boolean) { + _isFilledAllOptions.value = isFilled + } + + + fun getListTimeInfo() { + _hakki.value?.let { + _track.value?.let { it1 -> + planApiService.getListTime(it, it1).enqueue(object : Callback { + override fun onResponse( + call: Call, + response: Response + ) { + if (response.isSuccessful) { + if (response.body() != null) { + _listTimeInfo.postValue(response.body()) + Log.d("Planlicense", "${response.body()}") + } else { + _error.postValue("서버 응답이 올바르지 않습니다.") + } + } else { + _error.postValue("사용자 정보를 가져오지 못했습니다.") + try { + throw response.errorBody()?.string()?.let { + RuntimeException(it) + } ?: RuntimeException("Unknown error") + } catch (e: Exception) { + Log.e("PlanInfo", "PlanResponse API 오류: ${e.message}") + } + } + } + + override fun onFailure(call: Call, t: Throwable) { + _error.postValue("네트워크 오류: ${t.message}") + Log.d("gradInfo", "completion: ${t.message}") + } + }) + } + } + } + + + fun getLicenseInfo() { + planApiService.getUPlicense().enqueue(object : Callback { + override fun onResponse( + call: Call, + response: Response + ) { + if (response.isSuccessful) { + if (response.body() != null) { + _licenseInfo.postValue(response.body()) + Log.d("Planlicense", "${response.body()}") + } else { + _error.postValue("서버 응답이 올바르지 않습니다.") + } + } else { + _error.postValue("사용자 정보를 가져오지 못했습니다.") + try { + throw response.errorBody()?.string()?.let { + RuntimeException(it) + } ?: RuntimeException("Unknown error") + } catch (e: Exception) { + Log.e("PlanInfo", "PlanResponse API 오류: ${e.message}") + } + } + } + + override fun onFailure(call: Call, t: Throwable) { + _error.postValue("네트워크 오류: ${t.message}") + Log.d("gradInfo", "completion: ${t.message}") + } + }) + } + + + fun saveLicense(request: List) { + + planApiService.saveLicense(request).enqueue(object : Callback { + override fun onResponse(call: Call, response: Response + ) { + if (response.isSuccessful) { + _bringLicenseInfo.postValue(response.body()) + Log.d("PlanViewModel", "License saved successfully: ${response.body()}") + } else { + _error.postValue("자격증 정보 저장 실패: ${response.errorBody()?.string()}") + Log.e( + "PlanViewModel", + "Error saving license: ${response.errorBody()?.string()}" + ) + } + } + + override fun onFailure(call: Call, t: Throwable) { + _error.postValue("네트워크 오류: ${t.message}") + Log.e("PlanViewModel", "Network error: ${t.message}") + } + }) + } + + + + fun getSemesterInfo() { + planApiService.getSemesterInfo().enqueue(object : Callback { + override fun onResponse( + call: Call, + response: Response + ) { + if (response.isSuccessful) { + if (response.body() != null) { + _planSemesterInfo.postValue(response.body()) + Log.d("PlanSemester", "${response.body()}") + } else { + _error.postValue("서버 응답이 올바르지 않습니다.") + } + } else { + _error.postValue("사용자 정보를 가져오지 못했습니다.") + try { + throw response.errorBody()?.string()?.let { + RuntimeException(it) + } ?: RuntimeException("Unknown error") + } catch (e: Exception) { + Log.e("PlanInfo", "PlanResponse API 오류: ${e.message}") + } + } + } + + override fun onFailure(call: Call, t: Throwable) { + _error.postValue("네트워크 오류: ${t.message}") + Log.d("gradInfo", "completion: ${t.message}") + } + }) + } } @@ -270,3 +234,4 @@ class PlanViewModel : ViewModel() { + diff --git a/app/src/main/java/umc/com/mobile/project/ui/plan/PlanlicenseFragment.kt b/app/src/main/java/umc/com/mobile/project/ui/plan/PlanlicenseFragment.kt index 5ca427b..92f2d90 100644 --- a/app/src/main/java/umc/com/mobile/project/ui/plan/PlanlicenseFragment.kt +++ b/app/src/main/java/umc/com/mobile/project/ui/plan/PlanlicenseFragment.kt @@ -15,125 +15,125 @@ import umc.com.mobile.project.R import umc.com.mobile.project.databinding.FragmentPlanlicenseBinding class PlanlicenseFragment : Fragment() { - private var _binding: FragmentPlanlicenseBinding? = null - private val viewModel: PlanViewModel by viewModels() - private val binding get() = _binding!! + private var _binding: FragmentPlanlicenseBinding? = null + private val viewModel: PlanViewModel by viewModels() + private val binding get() = _binding!! - override fun onCreateView( - inflater: LayoutInflater, - container: ViewGroup?, - savedInstanceState: Bundle? - ): View { - _binding = FragmentPlanlicenseBinding.inflate(inflater, container, false) + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + _binding = FragmentPlanlicenseBinding.inflate(inflater, container, false) - viewModel.getLicenseInfo() // api 연결 + viewModel.getLicenseInfo() // api 연결 - // UPlicenseResponse-자격증 정보 불러오기 - viewModel.licenseInfo.observe(viewLifecycleOwner) { licenseInfo -> - licenseInfo?.result?.let { resultList -> - // 첫 번째 Result 객체에 접근하여 데이터 매핑 - resultList.getOrNull(0)?.let { firstResult -> - binding.planLicenseName.setText(firstResult.name) - binding.planLicenseDate.setText(firstResult.date) - } + // UPlicenseResponse-자격증 정보 불러오기 + viewModel.licenseInfo.observe(viewLifecycleOwner) { licenseInfo -> + licenseInfo?.result?.let { resultList -> + // 첫 번째 Result 객체에 접근하여 데이터 매핑 + resultList.getOrNull(0)?.let { firstResult -> + binding.planLicenseName.setText(firstResult.name) + binding.planLicenseDate.setText(firstResult.date) + } - // 두 번째 Result 객체에 접근하여 데이터 매핑 - resultList.getOrNull(1)?.let { secondResult -> - binding.planLicenseName2.setText(secondResult.name) - binding.planLicenseDate2.setText(secondResult.date) + // 두 번째 Result 객체에 접근하여 데이터 매핑 + resultList.getOrNull(1)?.let { secondResult -> + binding.planLicenseName2.setText(secondResult.name) + binding.planLicenseDate2.setText(secondResult.date) - } + } - resultList.getOrNull(2)?.let { thirdResult -> - binding.planLicenseName3.setText(thirdResult.name) - binding.planLicenseDate3.setText(thirdResult.date) + resultList.getOrNull(2)?.let { thirdResult -> + binding.planLicenseName3.setText(thirdResult.name) + binding.planLicenseDate3.setText(thirdResult.date) - } + } - resultList.getOrNull(3)?.let { fourthResult -> - binding.planLicenseName4.setText(fourthResult.name) - binding.planLicenseDate4.setText(fourthResult.date) - } - } - } + resultList.getOrNull(3)?.let { fourthResult -> + binding.planLicenseName4.setText(fourthResult.name) + binding.planLicenseDate4.setText(fourthResult.date) + } + } + } - return binding.root - } + return binding.root + } - private fun submitData() { + private fun submitData() { - val name = binding.planLicenseName.text.toString() - val date = binding.planLicenseDate.text.toString() + val name = binding.planLicenseName.text.toString() + val date = binding.planLicenseDate.text.toString() - // SaveInfo 객체 리스트 생성 - val saveInfoList = listOf(SaveInfo(name, date)) + // SaveInfo 객체 리스트 생성 + val saveInfoList = listOf(SaveInfo(name, date)) - // SavelicenseRequest 객체 생성 + // SavelicenseRequest 객체 생성 - // API 호출 - viewModel.saveLicense(saveInfoList) + // API 호출 + viewModel.saveLicense(saveInfoList) - } + } - private fun setupEditTextListener(){ - val textWatcher = object : TextWatcher { - override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { - // 필요 없음 - } + private fun setupEditTextListener(){ + val textWatcher = object : TextWatcher { + override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { + // 필요 없음 + } - override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { - // 입력이 변경될 때마다 호출됩니다. - checkIfAnyInputIsFilled() - } + override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { + // 입력이 변경될 때마다 호출됩니다. + checkIfAnyInputIsFilled() + } - override fun afterTextChanged(s: Editable?) { - // 필요 없음 - } - } - binding.planLicenseName.addTextChangedListener(textWatcher) - } + override fun afterTextChanged(s: Editable?) { + // 필요 없음 + } + } + binding.planLicenseName.addTextChangedListener(textWatcher) + } - private fun checkIfAnyInputIsFilled() { - val isAnyFieldFilled = binding.planLicenseName.text.trim().isNotEmpty() - //버튼 활성화 업데이트 - binding.licenseButtonStore.isEnabled = isAnyFieldFilled + private fun checkIfAnyInputIsFilled() { + val isAnyFieldFilled = binding.planLicenseName.text.trim().isNotEmpty() + //버튼 활성화 업데이트 + binding.licenseButtonStore.isEnabled = isAnyFieldFilled - // 버튼 색상도 업데이트 - val colorResId = if (isAnyFieldFilled) R.color.skyBlue else R.color.gray - val color = ContextCompat.getColor(requireContext(), colorResId) - binding.licenseButtonStore.backgroundTintList = ColorStateList.valueOf(color) + // 버튼 색상도 업데이트 + val colorResId = if (isAnyFieldFilled) R.color.skyBlue else R.color.gray + val color = ContextCompat.getColor(requireContext(), colorResId) + binding.licenseButtonStore.backgroundTintList = ColorStateList.valueOf(color) - } + } - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - setupEditTextListener() - checkIfAnyInputIsFilled() // 초기 상태 확인 - binding.licenseButtonStore.setOnClickListener { - submitData() // 사용자 입력을 기반으로 API 호출 - } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + setupEditTextListener() + checkIfAnyInputIsFilled() // 초기 상태 확인 + binding.licenseButtonStore.setOnClickListener { + submitData() // 사용자 입력을 기반으로 API 호출 + } - } + } - override fun onDestroyView() { - super.onDestroyView() - _binding = null - } -} + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } + }