From 63ea740e26d1724d8819f76d0d82854079922e75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A1=B0=ED=9D=AC=EC=9A=B0?= <84004687+huiwoo-jo@users.noreply.github.com> Date: Fri, 20 Sep 2024 22:02:28 +0900 Subject: [PATCH] =?UTF-8?q?build=20:=20mvvm=20=ED=8C=A8=ED=84=B4=20?= =?UTF-8?q?=EA=B5=AC=EC=A1=B0=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/view/schedule/DatePickerDialog.kt | 46 ++++++++------ .../ui/view/schedule/ScheduleClickListener.kt | 7 +++ .../ui/view/schedule/ScheduleFragment.kt | 61 +++++++++++-------- .../ui/view/schedule/ScheduleViewModel.kt | 10 +-- 4 files changed, 72 insertions(+), 52 deletions(-) create mode 100644 app/src/main/java/com/dongyang/android/youdongknowme/ui/view/schedule/ScheduleClickListener.kt diff --git a/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/schedule/DatePickerDialog.kt b/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/schedule/DatePickerDialog.kt index ed22f8cb..89d625c9 100644 --- a/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/schedule/DatePickerDialog.kt +++ b/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/schedule/DatePickerDialog.kt @@ -5,27 +5,24 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.view.WindowManager import android.widget.NumberPicker -import androidx.fragment.app.DialogFragment +import androidx.fragment.app.activityViewModels import com.dongyang.android.youdongknowme.R import com.dongyang.android.youdongknowme.databinding.DialogDatepickerBinding import com.google.android.material.bottomsheet.BottomSheetDialog import com.google.android.material.bottomsheet.BottomSheetDialogFragment -import org.koin.androidx.viewmodel.ext.android.viewModel +import com.prolificinteractive.materialcalendarview.CalendarDay import org.threeten.bp.LocalDate class DatePickerDialog( - val year: Int, - val month: Int, - private val dateSelectedListener: ScheduleFragment, + private val year: Int, + private val month: Int, + private val listener: ScheduleClickListener ) : BottomSheetDialogFragment() { private var _binding: DialogDatepickerBinding? = null private val binding get() = _binding!! - private val viewModel: ScheduleViewModel by viewModel() - override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -52,7 +49,9 @@ class DatePickerDialog( with(binding.numberpickerDialogDatepickerYear) { minValue = currentYear - 1 maxValue = currentYear + 1 - displayedValues=((minValue..maxValue).map{"$it 년"}.toTypedArray()) + displayedValues = + ((minValue..maxValue).map { "$it${getString(R.string.calendar_year)}" } + .toTypedArray()) } // 월 최소/최대값 설정 및 출력 방식 설정 @@ -65,29 +64,36 @@ class DatePickerDialog( maxValue = if (newYear == currentYear + 1) 2 else 12 } - displayedValues=((minValue..maxValue).map{"$it 월"}.toTypedArray()) + displayedValues = + ((minValue..maxValue).map { "$it${getString(R.string.calendar_month)}" } + .toTypedArray()) } // 초기 설정 binding.numberpickerDialogDatepickerYear.value = year binding.numberpickerDialogDatepickerMonth.value = month + return view + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + + super.onViewCreated(view, savedInstanceState) + binding.tvDialogPermissionComplete.setOnClickListener { - viewModel.setDatePicker( - binding.numberpickerDialogDatepickerYear.value, - binding.numberpickerDialogDatepickerMonth.value - ) - dateSelectedListener.onDateSelected( - binding.numberpickerDialogDatepickerYear.value, - binding.numberpickerDialogDatepickerMonth.value - ) + val year = binding.numberpickerDialogDatepickerYear.value + val month = binding.numberpickerDialogDatepickerMonth.value + val date = CalendarDay.from(year, month, 1) + + listener.buttonClick(date = date) + dismiss() } - - return view } + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + return BottomSheetDialog(requireContext(), R.style.CustomBottomSheetDialogTheme) } diff --git a/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/schedule/ScheduleClickListener.kt b/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/schedule/ScheduleClickListener.kt new file mode 100644 index 00000000..869a6291 --- /dev/null +++ b/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/schedule/ScheduleClickListener.kt @@ -0,0 +1,7 @@ +package com.dongyang.android.youdongknowme.ui.view.schedule + +import com.prolificinteractive.materialcalendarview.CalendarDay + +interface ScheduleClickListener { + fun buttonClick(date: CalendarDay) +} diff --git a/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/schedule/ScheduleFragment.kt b/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/schedule/ScheduleFragment.kt index 9a2942e7..34038d32 100644 --- a/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/schedule/ScheduleFragment.kt +++ b/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/schedule/ScheduleFragment.kt @@ -13,13 +13,9 @@ import com.prolificinteractive.materialcalendarview.CalendarDay import org.koin.androidx.viewmodel.ext.android.viewModel import org.threeten.bp.LocalDate -interface OnDateSelectedListener { - fun onDateSelected(year: Int, month: Int) -} - /* 학사 일정 화면 */ class ScheduleFragment : BaseFragment(), - OnDateSelectedListener { + ScheduleClickListener { override val layoutResourceId: Int = R.layout.fragment_schedule override val viewModel: ScheduleViewModel by viewModel() @@ -32,12 +28,27 @@ class ScheduleFragment : BaseFragment + val inputText: LocalDate = day.date + val calendarHeaderElements = inputText.toString().split("-").toTypedArray() + + year = calendarHeaderElements[0] + month = calendarHeaderElements[1] - // 선택된 연도와 월을 사용하여 CalendarView 업데이트 - val date = CalendarDay.from(year, month, 1) - binding.mvScheduleCalendar.currentDate = date - binding.mvScheduleCalendar.setCurrentDate(date, true) + val calendarHeaderBuilder = StringBuilder() + calendarHeaderBuilder.append(year) + .append(getString(R.string.calendar_year)) + .append(" ") + .append(month) + .append(getString(R.string.calendar_month)) + calendarHeaderBuilder.toString() + } } override fun initStartView() { @@ -90,6 +101,10 @@ class ScheduleFragment : BaseFragment + updateCalendarViewHeader(date) + } + viewModel.scheduleList.observe(viewLifecycleOwner) { adapter.submitList(it) } @@ -99,6 +114,13 @@ class ScheduleFragment : BaseFragment @@ -109,7 +131,7 @@ class ScheduleFragment : BaseFragment - val inputText: LocalDate = day.date - val calendarHeaderElements = inputText.toString().split("-").toTypedArray() - - year = calendarHeaderElements[0] - month = calendarHeaderElements[1] - - val calendarHeaderBuilder = StringBuilder() - calendarHeaderBuilder.append(year) - .append(getString(R.string.calendar_year)) - .append(" ") - .append(month) - .append(getString(R.string.calendar_month)) - calendarHeaderBuilder.toString() - } + setCalenderHeader() } } diff --git a/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/schedule/ScheduleViewModel.kt b/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/schedule/ScheduleViewModel.kt index cd48fc28..8b4c045b 100644 --- a/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/schedule/ScheduleViewModel.kt +++ b/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/schedule/ScheduleViewModel.kt @@ -36,14 +36,14 @@ class ScheduleViewModel(private val scheduleRepository: ScheduleRepository) : Ba private val _pickMonth = MutableLiveData() val pickMonth: LiveData = _pickMonth + private val _selectedDate = MutableLiveData() + val selectedDate: LiveData = _selectedDate + fun setPickedDate(date: CalendarDay) { + _selectedDate.value = date _pickYear.value = date.year _pickMonth.value = date.month - } - - fun setDatePicker(year: Int, month: Int) { - _pickYear.value = year - _pickMonth.value = month + getSchedules() } fun getSchedules() {