Skip to content

Commit

Permalink
build : mvvm 패턴 구조로 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
huiwoo-jo committed Sep 20, 2024
1 parent 7e4ab80 commit 63ea740
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
Expand All @@ -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())
}

// 월 최소/최대값 설정 및 출력 방식 설정
Expand All @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.dongyang.android.youdongknowme.ui.view.schedule

import com.prolificinteractive.materialcalendarview.CalendarDay

interface ScheduleClickListener {
fun buttonClick(date: CalendarDay)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<FragmentScheduleBinding, ScheduleViewModel>(),
OnDateSelectedListener {
ScheduleClickListener {

override val layoutResourceId: Int = R.layout.fragment_schedule
override val viewModel: ScheduleViewModel by viewModel()
Expand All @@ -32,12 +28,27 @@ class ScheduleFragment : BaseFragment<FragmentScheduleBinding, ScheduleViewModel
private var _month = LocalDate.now().month.toString()
private var month = _month

override fun onDateSelected(year: Int, month: Int) {
override fun buttonClick(date: CalendarDay) {
viewModel.setPickedDate(date)
}

// 연/월 방식으로 타이틀 처리
fun setCalenderHeader() {
binding.mvScheduleCalendar.setTitleFormatter { day ->
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() {
Expand Down Expand Up @@ -90,6 +101,10 @@ class ScheduleFragment : BaseFragment<FragmentScheduleBinding, ScheduleViewModel
viewModel.getSchedules()
}

viewModel.selectedDate.observe(viewLifecycleOwner) { date ->
updateCalendarViewHeader(date)
}

viewModel.scheduleList.observe(viewLifecycleOwner) {
adapter.submitList(it)
}
Expand All @@ -99,6 +114,13 @@ class ScheduleFragment : BaseFragment<FragmentScheduleBinding, ScheduleViewModel
}
}

private fun updateCalendarViewHeader(date: CalendarDay) {
binding.mvScheduleCalendar.setCurrentDate(date)

setCalenderHeader()
}


override fun initAfterBinding() {

binding.mvScheduleCalendar.setOnMonthChangedListener { _, date ->
Expand All @@ -109,7 +131,7 @@ class ScheduleFragment : BaseFragment<FragmentScheduleBinding, ScheduleViewModel
val dialog = DatePickerDialog(
year = year.toInt(),
month = month.toInt(),
dateSelectedListener = this
listener = this@ScheduleFragment
)
dialog.show(requireActivity().supportFragmentManager, "CustomDialog")
}
Expand All @@ -121,21 +143,6 @@ class ScheduleFragment : BaseFragment<FragmentScheduleBinding, ScheduleViewModel
.commit()
}

// 연/월 방식으로 타이틀 처리
binding.mvScheduleCalendar.setTitleFormatter { day ->
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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ class ScheduleViewModel(private val scheduleRepository: ScheduleRepository) : Ba
private val _pickMonth = MutableLiveData<Int>()
val pickMonth: LiveData<Int> = _pickMonth

private val _selectedDate = MutableLiveData<CalendarDay>()
val selectedDate: LiveData<CalendarDay> = _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() {
Expand Down

0 comments on commit 63ea740

Please sign in to comment.