From 17ca65afbbe421e70229374a914d43bef0d28c9d Mon Sep 17 00:00:00 2001 From: sanghee Date: Sun, 12 Jun 2022 14:17:02 +0900 Subject: [PATCH] =?UTF-8?q?chore:=20CalendarViewModel=EC=97=90=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=B6=94=EA=B0=80=20(ios-h/airbnb#32)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ViewModel/CalendarViewModel.swift | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/iOS/airbnb/Present/Main/Calendar/ViewModel/CalendarViewModel.swift b/iOS/airbnb/Present/Main/Calendar/ViewModel/CalendarViewModel.swift index 795d29484..a07aabcb9 100644 --- a/iOS/airbnb/Present/Main/Calendar/ViewModel/CalendarViewModel.swift +++ b/iOS/airbnb/Present/Main/Calendar/ViewModel/CalendarViewModel.swift @@ -11,12 +11,40 @@ final class CalendarViewModel { static let weeks = ["일", "월", "화", "수", "목", "금", "토"] let today = Date() // 오늘의 날짜 - let dateOfToday = Date() // 날짜를 계산하는 기준 날짜 + var dateOfToday = Date() // 날짜를 계산하는 기준 날짜 let dateComponents = DateComponents() let dateStringOfToday: String? = nil - let totalDates = [String]() // dates를 담는 배열 - let daysCountInMonth = 0 // 해당 월이 며칠까지 있는지 - let weekdayStarting = 0 // 시작일 + var totalDates = [String]() // dates를 담는 배열 + var daysCountInMonth = 0 // 해당 월이 며칠까지 있는지 + var weekdayStarting = 0 // 시작일 + let calendarManager = CalendarManager() + func previousMonth() { + dateOfToday = calendarManager.minusMonth(date: dateOfToday) + self.calculateMonth() + } + func nextMonth() { + dateOfToday = calendarManager.plusMonth(date: dateOfToday) + self.calculateMonth() + } + + // 월별 날짜를 계산하고 뷰에 표시 + func calculateMonth() { + let firstDayOfMonth = calendarManager.firstOfMonth(date: dateOfToday) // 첫째날의 전 일.. 예를 들면 7월 1일 이전인 6월 30일 나옴. + let firstWeekday = calendarManager.weekDay(date: firstDayOfMonth) // 해당 월에서 첫번째로 시작하는 요일 + + daysCountInMonth = calendarManager.calendar.range(of: .day, in: .month, for: firstDayOfMonth)!.count + weekdayStarting = 1 - firstWeekday + + self.totalDates.removeAll() + + for day in weekdayStarting...daysCountInMonth { + if day < 1 { + self.totalDates.append("") + } else { + self.totalDates.append(String(day)) + } + } + } }