-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from naneun/eddy-20
[iOS] 특정 시점을 입력하면 DayCreator가 해당 달의 �달력 날짜 정보를 반환함
- Loading branch information
Showing
4 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// Day.swift | ||
// Cherrybnb | ||
// | ||
// Created by Bumgeun Song on 2022/05/27. | ||
// Copyright © 2022 Codesquad. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct Day { | ||
// Day가 나타내는 시점(일) | ||
let date: Date | ||
|
||
// Calendar에 보여지는 숫자 | ||
var number: String { | ||
dateFormatter.string(from: date) | ||
} | ||
|
||
// Calendar에서 선택되었는지 여부 | ||
let isSelected: Bool | ||
|
||
// 현재 이전 날짜인지 여부 | ||
let isPast: Bool | ||
|
||
let dateFormatter: DateFormatter = { | ||
let dateFormatter = DateFormatter() | ||
dateFormatter.dateFormat = "d" | ||
return dateFormatter | ||
}() | ||
} | ||
|
||
struct Month { | ||
let date: Date | ||
|
||
var title: String { | ||
dateFormatter.string(from: date) | ||
} | ||
|
||
let dateFormatter: DateFormatter = { | ||
let dateFormatter = DateFormatter() | ||
dateFormatter.dateFormat = "yyyy년 MM월" | ||
dateFormatter.locale = Locale(identifier: "ko_KR") | ||
return dateFormatter | ||
}() | ||
} |
71 changes: 71 additions & 0 deletions
71
iOS/Targets/Cherrybnb/Sources/Search/Model/DayCreator.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// | ||
// DayCreator.swift | ||
// Cherrybnb | ||
// | ||
// Created by Bumgeun Song on 2022/05/27. | ||
// Copyright © 2022 Codesquad. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
enum CalendarError: Error { | ||
case metadataGeneration | ||
case offsetMonthGeneration | ||
} | ||
|
||
struct DayCreator { | ||
let calendar = Calendar(identifier: .gregorian) | ||
|
||
var selectedDate: Date? | ||
|
||
// 기준일로부터 offset만큼 떨어진 달의 Day를 return. | ||
func daysInMonth(for basedate: Date, monthOffset: Int) -> [Day] { | ||
guard let firstDayOfMonth = calendar.date(from: calendar.dateComponents([.year, .month], from: basedate)), | ||
let firstDayOfOffsetMonth = calendar.date(byAdding: .month, value: monthOffset, to: firstDayOfMonth) else { | ||
print(CalendarError.offsetMonthGeneration) | ||
return [] | ||
} | ||
|
||
return daysInMonth(for: firstDayOfOffsetMonth) | ||
} | ||
|
||
// 기준일이 속한 달의 Day를 return. | ||
func daysInMonth(for basedate: Date) -> [Day] { | ||
guard let metadata = try? monthMetadata(for: basedate) else { | ||
print(CalendarError.metadataGeneration) | ||
return [] | ||
} | ||
|
||
return (0..<metadata.numberOfDays).map { offset in | ||
let date = calendar.date(byAdding: .day, value: offset, to: metadata.firstDay) ?? basedate | ||
let isSelected = date == selectedDate | ||
let isPast = date < basedate | ||
|
||
return Day(date: date, isSelected: isSelected, isPast: isPast) | ||
} | ||
} | ||
|
||
private func monthMetadata(for basedate: Date) throws -> MonthMetaData { | ||
guard let numberOfDaysInMonth = calendar.range(of: .day, in: .month, for: basedate)?.count, | ||
let firstDayOfMonth = calendar.date(from: calendar.dateComponents([.year, .month], from: basedate)) else { | ||
throw CalendarError.metadataGeneration | ||
} | ||
|
||
let weekDayOfFirstDay = calendar.component(.weekday, from: firstDayOfMonth) | ||
|
||
return MonthMetaData(numberOfDays: numberOfDaysInMonth, firstDay: firstDayOfMonth, firstDayWeekday: weekDayOfFirstDay) | ||
} | ||
} | ||
|
||
struct MonthCreator { | ||
let calendar = Calendar(identifier: .gregorian) | ||
|
||
func createMonth(for basedate: Date, monthOffset: Int) -> Month? { | ||
guard let firstDayOfMonth = calendar.date(from: calendar.dateComponents([.year, .month], from: basedate)), | ||
let firstDayOfOffsetMonth = calendar.date(byAdding: .month, value: monthOffset, to: firstDayOfMonth) else { | ||
print(CalendarError.offsetMonthGeneration) | ||
return nil | ||
} | ||
return Month(date: firstDayOfOffsetMonth) | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
iOS/Targets/Cherrybnb/Sources/Search/Model/MonthMetaData.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// MonthMetaData.swift | ||
// Cherrybnb | ||
// | ||
// Created by Bumgeun Song on 2022/05/27. | ||
// Copyright © 2022 Codesquad. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct MonthMetaData { | ||
let numberOfDays: Int | ||
let firstDay: Date | ||
let firstDayWeekday: Int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters