diff --git a/CoNet/Main/Home/CalendarDateFormatter.swift b/CoNet/Main/Home/CalendarDateFormatter.swift index 359a8d8..ddf4e0c 100644 --- a/CoNet/Main/Home/CalendarDateFormatter.swift +++ b/CoNet/Main/Home/CalendarDateFormatter.swift @@ -8,9 +8,9 @@ import Foundation class CalendarDateFormatter { - private var calendar = Calendar.current // Calendar 구조체를 현재 달력으로 초기화 - private let dateFormatter = DateFormatter() // 원하는 String 타입으로 변화시켜줄 formatter - private var nowCalendarDate = Date() // 현재 시간 + var calendar = Calendar.current // Calendar 구조체를 현재 달력으로 초기화 + let dateFormatter = DateFormatter() // 원하는 String 타입으로 변화시켜줄 formatter + var nowCalendarDate = Date() // 현재 시간 private(set) var days = [String]() // 달력에 표시할 날짜를 담을 배열 init() { diff --git a/CoNet/Main/Home/CalendarViewController.swift b/CoNet/Main/Home/CalendarViewController.swift index e37e86d..c080f1c 100644 --- a/CoNet/Main/Home/CalendarViewController.swift +++ b/CoNet/Main/Home/CalendarViewController.swift @@ -11,7 +11,7 @@ import UIKit class CalendarViewController: UIViewController { var meetingId = 0 - + var selectedDate: String? // MARK: UIComponents // 이전 달로 이동 버튼 @@ -76,7 +76,8 @@ class CalendarViewController: UIViewController { format.dateFormat = "yyyy. MM" // api 호출 - getMonthPlanAPI(date: format.string(from: Date())) + getMonthPlanAPI(date: format.string(from: calendarDateFormatter.nowCalendarDate)) + } private func setupCollectionView() { @@ -176,6 +177,22 @@ class CalendarViewController: UIViewController { // api: 특정 달 약속 조회 getMonthPlanAPI(date: changedHeader) + + // 캘린더에서 날짜 선택 + selectDateOnCalendar() + } + + func selectDateOnCalendar() { + if let selectedDate = selectedDate { + let dateComponents = selectedDate.split(separator: ".").map { String($0) } + if dateComponents.count == 3 { + let day = dateComponents[2] + if let index = calendarDateFormatter.days.firstIndex(of: day) { + let indexPath = IndexPath(item: index, section: 0) + calendarCollectionView.selectItem(at: indexPath, animated: false, scrollPosition: []) + } + } + } } } @@ -207,6 +224,8 @@ extension CalendarViewController: UICollectionViewDataSource, UICollectionViewDe // yyyy. MM. dd 형식 let clickDate = calendarDateFormatter.changeDateType(date: calendarDate) + calendarDay + selectedDate = clickDate + if let parentVC = parent { if parentVC is HomeViewController { // 부모 뷰컨트롤러가 HomeViewController @@ -282,6 +301,12 @@ extension CalendarViewController: UICollectionViewDataSource, UICollectionViewDe cell.reloadPlanMark() } + // 셀의 날짜가 선택된 날짜와 일치하는지 확인 + let cellDate = calendarDateFormatter.changeDateType(date: calendarDate) + calendarDateFormatter.formatNumberToTwoDigit(Int(cellDay) ?? 0) + if cellDate == selectedDate { + collectionView.selectItem(at: indexPath, animated: false, scrollPosition: []) + } + return cell } } diff --git a/CoNet/Main/Home/HomeViewController.swift b/CoNet/Main/Home/HomeViewController.swift index f2982c0..f9e1574 100644 --- a/CoNet/Main/Home/HomeViewController.swift +++ b/CoNet/Main/Home/HomeViewController.swift @@ -133,7 +133,7 @@ class HomeViewController: UIViewController { self.planNum.text = String(count) self.dayPlanData = plans self.dayPlanCollectionView.reloadData() - self.layoutConstraints() + self.updataPlanCollectionViewHeight() } } @@ -143,7 +143,7 @@ class HomeViewController: UIViewController { self.waitingPlanNum.text = String(count) self.waitingPlanData = plans self.waitingPlanCollectionView.reloadData() - self.layoutConstraints() + self.updataPlanCollectionViewHeight() } } @@ -173,6 +173,19 @@ class HomeViewController: UIViewController { dayPlanLabel.text = month + "월 " + day + "일의 약속" } } + + // 높이 업데이트 + func updataPlanCollectionViewHeight() { + let dayPlanHeight = (dayPlanData.count - 1) * 100 + 90 + dayPlanCollectionView.snp.updateConstraints { make in + make.height.equalTo(dayPlanHeight) + } + + let waitingPlanHeight = waitingPlanData.count * 100 - 1 + waitingPlanCollectionView.snp.updateConstraints { make in + make.height.equalTo(waitingPlanHeight) + } + } } extension HomeViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { diff --git a/CoNet/Main/Meeting/MeetingMain/MeetingMainViewController.swift b/CoNet/Main/Meeting/MeetingMain/MeetingMainViewController.swift index f73ed68..f27fc72 100644 --- a/CoNet/Main/Meeting/MeetingMain/MeetingMainViewController.swift +++ b/CoNet/Main/Meeting/MeetingMain/MeetingMainViewController.swift @@ -138,7 +138,11 @@ class MeetingMainViewController: UIViewController { format.locale = Locale(identifier: "ko_KR") format.timeZone = TimeZone(abbreviation: "KST") - dayPlanAPI(date: format.string(from: Date())) + if let selectedDate = calendarVC.selectedDate { + dayPlanAPI(date: selectedDate) + } else { + dayPlanAPI(date: format.string(from: Date())) + } } override func viewWillDisappear(_ animated: Bool) { @@ -185,7 +189,7 @@ class MeetingMainViewController: UIViewController { self.planNum.text = String(count) self.dayPlanData = plans self.dayPlanCollectionView.reloadData() - self.layoutContraints() + self.updataPlanCollectionViewHeight() } } @@ -312,6 +316,13 @@ class MeetingMainViewController: UIViewController { present(getMemberBottomSheet, animated: true, completion: nil) } + + func updataPlanCollectionViewHeight() { + let dayPlanHeight = dayPlanData.count * 80 - 1 + dayPlanCollectionView.snp.updateConstraints { make in + make.height.equalTo(dayPlanHeight) + } + } } // 사이드바 화면 전환