-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CHORE] Calendar View의 가독성을 향상시키기 위해 노력했습니다. #607
base: develop
Are you sure you want to change the base?
Changes from all commits
f72b3df
81fa5cb
052eaef
33b43a9
d2e0356
af3f5af
611e3e4
af5904b
51ad98e
c0723eb
dd3eab6
f896045
4d3933c
1497c6c
dd6c61b
5dc58de
34a46d2
25ad87d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,10 +11,6 @@ import UIKit | |
import FSCalendar | ||
import SnapKit | ||
|
||
protocol CalendarDelegate: AnyObject { | ||
func detectChangeButton(_ value: Bool) | ||
} | ||
|
||
final class CalendarView: UIView { | ||
|
||
private enum CalendarMoveType { | ||
|
@@ -66,18 +62,12 @@ final class CalendarView: UIView { | |
// MARK: - property | ||
|
||
private var selectStartDate: Date = Date() | ||
private let oneDayInterval: TimeInterval = 86400 | ||
private let sevenDaysInterval: TimeInterval = 604800 | ||
var changeButtonState: ((Bool) -> ())? | ||
var startDateText: String = "" | ||
var endDateText: String = "" | ||
private var tempStartDateText: String = "" | ||
private var tempEndDateText: String = "" | ||
private var startDateText: String = "" | ||
private var endDateText: String = "" | ||
var isFirstTap: Bool = false | ||
private weak var delegate: CalendarDelegate? | ||
|
||
let startDateTapPublisher = PassthroughSubject<String, Never>() | ||
let endDateTapPublisher = PassthroughSubject<String, Never>() | ||
let startDateTapPublisher: PassthroughSubject<String, Never> = PassthroughSubject() | ||
let endDateTapPublisher: PassthroughSubject<String, Never> = PassthroughSubject() | ||
|
||
// MARK: - init | ||
|
||
|
@@ -121,23 +111,13 @@ final class CalendarView: UIView { | |
} | ||
self.previousButton.addAction(action, for: .touchUpInside) | ||
} | ||
|
||
private func setupNextButton() { | ||
let action = UIAction { [weak self] _ in | ||
self?.changeMonth(with: CalendarMoveType.next) | ||
} | ||
self.nextButton.addAction(action, for: .touchUpInside) | ||
} | ||
|
||
func configureCalendarDelegate(_ delegate: CalendarDelegate) { | ||
self.delegate = delegate | ||
} | ||
|
||
func setupButtonState() { | ||
let hasDate = self.tempStartDateText != "" && self.tempEndDateText != "" | ||
self.delegate?.detectChangeButton(hasDate) | ||
// FIXME: - delegate로 통일 후 삭제해야함 | ||
self.changeButtonState?(hasDate) | ||
} | ||
|
||
private func setupDelegation() { | ||
self.calendar.delegate = self | ||
|
@@ -154,65 +134,79 @@ final class CalendarView: UIView { | |
} | ||
|
||
func setupDateRange() { | ||
self.startDateTapPublisher.send("20\(self.startDateText)") | ||
self.endDateTapPublisher.send("20\(self.endDateText)") | ||
guard let startDate = self.startDateText.toDefaultDate else { return } | ||
guard let endDate = self.endDateText.toDefaultDate else { return } | ||
self.setupCalendarRange(startDate: startDate, endDate: endDate) | ||
self.startDateTapPublisher.send(self.startDateText) | ||
self.endDateTapPublisher.send(self.endDateText) | ||
guard let startDate = self.startDateText.toDefaultDate, | ||
let endDate = self.endDateText.toDefaultDate else { return } | ||
self.setupInitialCalendarRange(startDate: startDate, endDate: endDate) | ||
} | ||
|
||
private func setupCalendarRange(startDate: Date, endDate: Date) { | ||
private func setupInitialCalendarRange(startDate: Date, endDate: Date) { | ||
self.calendar.select(startDate) | ||
self.calendar.select(endDate) | ||
self.setDateRange() | ||
} | ||
|
||
func setDateRange() { | ||
private func setDateRange() { | ||
guard self.countDateRange() <= 7 else { return } | ||
|
||
let isFirstClickPastDate = self.calendar.selectedDates[0] < self.calendar.selectedDates[1] | ||
if isFirstClickPastDate { | ||
self.setSelecteDate(startIndex: 0, | ||
self.setSelectedDate(startIndex: 0, | ||
endIndex: 1) | ||
} else { | ||
self.setSelecteDate(startIndex: 1, | ||
self.setSelectedDate(startIndex: 1, | ||
endIndex: 0) | ||
} | ||
} | ||
|
||
func setSelecteDate(startIndex: Int, endIndex: Int) { | ||
private func setSelectedDate(startIndex: Int, endIndex: Int) { | ||
var startDate = self.calendar.selectedDates[startIndex] | ||
while startDate < self.calendar.selectedDates[endIndex] { | ||
guard let addDate = Calendar.current.date(byAdding: .day, | ||
value: 1, | ||
to: startDate) else { return } | ||
self.calendar.select(addDate) | ||
startDate += self.oneDayInterval | ||
startDate += .oneDayInterval | ||
} | ||
self.tempStartDateText = self.calendar.selectedDates[startIndex].toDefaultString | ||
self.tempEndDateText = self.calendar.selectedDates[endIndex].toDefaultString | ||
self.startDateText = self.calendar.selectedDates[startIndex].toDefaultString | ||
self.endDateText = self.calendar.selectedDates[endIndex].toDefaultString | ||
} | ||
|
||
func countDateRange() -> Int { | ||
private func countDateRange() -> Int { | ||
let isFirstClickPastDate = self.calendar.selectedDates[0] < self.calendar.selectedDates[1] | ||
let selectdDate = isFirstClickPastDate | ||
? self.calendar.selectedDates[1].timeIntervalSince(self.calendar.selectedDates[0]) | ||
: self.calendar.selectedDates[0].timeIntervalSince(self.calendar.selectedDates[1]) | ||
let dateRangeCount = selectdDate / 86400 | ||
let dateRangeCount = selectdDate / .oneDayInterval | ||
|
||
return Int(dateRangeCount) + 1 | ||
} | ||
|
||
func getTempStartDate() -> String { | ||
return self.tempStartDateText | ||
func getStartDate() -> String { | ||
return self.startDateText | ||
} | ||
|
||
func getTempEndDate() -> String { | ||
return self.tempEndDateText | ||
func getEndDate() -> String { | ||
return self.endDateText | ||
} | ||
|
||
func setStartDateText(_ text: String) { | ||
self.startDateText = text | ||
} | ||
|
||
func setEndDateText(_ text: String) { | ||
self.endDateText = text | ||
} | ||
|
||
private func showAlertOverDateSelect() { | ||
self.viewController?.makeAlert(title: TextLiteral.Common.Calendar.maxAlertTitle.localized(), | ||
message: TextLiteral.Common.Calendar.maxDateContent.localized()) | ||
} | ||
} | ||
|
||
extension CalendarView: FSCalendarDelegate { | ||
/// 캘린더의 날짜를 선택했을 때 실행되는 함수. | ||
func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) { | ||
self.isFirstTap = true | ||
let isCreatedRoomOnlySelectedStartDate = calendar.selectedDates.count == 1 | ||
|
@@ -227,16 +221,15 @@ extension CalendarView: FSCalendarDelegate { | |
DispatchQueue.main.async { | ||
calendar.deselect(date) | ||
} | ||
self.viewController?.makeAlert(title: TextLiteral.Common.Calendar.maxAlertTitle.localized(), | ||
message: TextLiteral.Common.Calendar.maxDateContent.localized()) | ||
self.showAlertOverDateSelect() | ||
} else { | ||
self.tempEndDateText = date.toDefaultString | ||
self.endDateText = date.toDefaultString | ||
self.setDateRange() | ||
calendar.reloadData() | ||
} | ||
} else if isReclickedStartDate { | ||
self.tempStartDateText = date.toDefaultString | ||
self.tempEndDateText = "" | ||
self.startDateText = date.toDefaultString | ||
self.endDateText = "" | ||
(calendar.selectedDates).forEach { | ||
calendar.deselect($0) | ||
} | ||
Comment on lines
225
to
235
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p3; 해당 메서드 내부에 있는 각각의 로직을 메서드로 따로 뺄 순 없을까요? |
||
|
@@ -245,25 +238,26 @@ extension CalendarView: FSCalendarDelegate { | |
calendar.reloadData() | ||
} | ||
|
||
self.startDateTapPublisher.send(self.getTempStartDate()) | ||
self.endDateTapPublisher.send(self.getTempEndDate()) | ||
self.setupButtonState() | ||
self.startDateTapPublisher.send(self.getStartDate()) | ||
self.endDateTapPublisher.send(self.getEndDate()) | ||
} | ||
|
||
|
||
/// 이미 선택 되어있는 날짜를 클릭했을 때 실행되는 함수. | ||
func calendar(_ calendar: FSCalendar, didDeselect date: Date, at monthPosition: FSCalendarMonthPosition) { | ||
self.tempEndDateText = "" | ||
self.endDateText = "" | ||
self.endDateTapPublisher.send("") | ||
self.isFirstTap = true | ||
(calendar.selectedDates).forEach { | ||
calendar.deselect($0) | ||
} | ||
self.selectStartDate = date | ||
calendar.select(date) | ||
calendar.reloadData() | ||
self.setupButtonState() | ||
} | ||
|
||
/// 선택할 수 없는 날짜 지정 함수 | ||
func calendar(_ calendar: FSCalendar, shouldSelect date: Date, at monthPosition: FSCalendarMonthPosition) -> Bool { | ||
if date < Date() - self.oneDayInterval { | ||
if date < Date() - .oneDayInterval { | ||
self.viewController?.makeAlert(title: TextLiteral.Common.Calendar.pastAlertTitle.localized(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p1; 해당 로직에 사용중인 viewController를 삭제할 예정이라서 다른 방식으로 구현해주시면 감사드리겠습니다..🙇♂️ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ++) |
||
message: TextLiteral.Common.Calendar.pastAlertMessage.localized()) | ||
return false | ||
|
@@ -272,9 +266,10 @@ extension CalendarView: FSCalendarDelegate { | |
} | ||
} | ||
|
||
/// 캘린더의 숫자 색에 대한 함수 | ||
func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, titleDefaultColorFor date: Date) -> UIColor? { | ||
let isBeforeToday = date < Date() - self.oneDayInterval | ||
let isAWeekBeforeAfter = date < self.selectStartDate + self.sevenDaysInterval && date > self.selectStartDate - self.sevenDaysInterval | ||
let isBeforeToday = date < Date() - .oneDayInterval | ||
let isAWeekBeforeAfter = date < self.selectStartDate + .sevenDaysInterval && date > self.selectStartDate - .sevenDaysInterval | ||
let isDoneSelectedDate = calendar.selectedDates.count > 2 | ||
if isBeforeToday { | ||
return .grey004.withAlphaComponent(0.4) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// TimeInterval+Extension.swift | ||
// Manito | ||
// | ||
// Created by Mingwan Choi on 11/21/23. | ||
// | ||
|
||
import Foundation | ||
|
||
extension TimeInterval { | ||
static let oneDayInterval: TimeInterval = 86400 | ||
static let sevenDaysInterval: TimeInterval = 604800 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
p1;
하단에 보니깐 dataSource에서 제공해주는 API를 사용하지 않는데, 굳이
dataSource = self
를 할 필요가 있나요..?꼭 써줘야 하는 부분일까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
그러네용 없어도 될거같네요!