Skip to content

Commit

Permalink
refactor: #90 Move Date-related methods and relocate UserBook methods
Browse files Browse the repository at this point in the history
- Date ๊ด€๋ จ ๋ฉ”์„œ๋“œ๋ฅผ Date ํƒ€์ž…์˜ extension์œผ๋กœ ์ด๋™
- UserBook ๋‚ด๋ถ€ ํ”„๋กœํผํ‹ฐ์— ์ ‘๊ทผํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ ์ ˆํ•œ ๋ชจ๋ธ๋กœ ์žฌ๋ฐฐ์น˜
  • Loading branch information
zaehorang committed Nov 22, 2024
1 parent 0d79e30 commit 98a02e0
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 53 deletions.
17 changes: 17 additions & 0 deletions FiveGuyes/FiveGuyes/Resources/Extensions/Date+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,22 @@ extension Date {
formatter.dateFormat = "MM์›” dd์ผ"
return formatter.string(from: self)
}
}

// Date ํ™•์žฅ์œผ๋กœ ๋‚ ์งœ ๋ฌธ์ž์—ด ํฌ๋งท ์ถ”๊ฐ€
extension Date {
func toYearMonthDayString() -> String {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
return formatter.string(from: self)
}

/// 04:00 AM์„ ๊ธฐ์ค€์œผ๋กœ ๋‚ ์งœ๋ฅผ ์กฐ์ •ํ•˜์—ฌ "yyyy-MM-dd" ํ˜•์‹์œผ๋กœ ๋ฐ˜ํ™˜
func toAdjustedYearMonthDayString() -> String {
let calendar = Calendar.current
let adjustedDate = calendar.date(byAdding: .hour, value: -4, to: self) ?? self
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
return formatter.string(from: adjustedDate)
}
}
68 changes: 28 additions & 40 deletions FiveGuyes/FiveGuyes/Sources/Models/ReadingScheduleCalculator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ import Foundation

struct ReadingScheduleCalculator {

// TODO: Date ํƒ€์ž…์˜ extension ๋ฉ”์„œ๋“œ๋กœ ์˜ฎ๊ธฐ๊ธฐ
// ๋ฐ์ดํ„ฐ์˜ ํ‚ค ๊ฐ’์„ ํŒŒ์‹ฑํ•ด์„œ ๊ฐ€์ ธ์˜ค๋Š” ๋ฉ”์„œ๋“œ
private func toYearMonthDayString(_ date: Date) -> String {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
return formatter.string(from: date)
}

// MARK: ์ฒซ๋‚ ์„ ๊ธฐ์ค€์œผ๋กœ ์ฝ์–ด์•ผํ•˜๋Š” ํŽ˜์ด์ง€๋ฅผ ํ• ๋‹นํ•˜๋Š” ๋ฉ”์„œ๋“œ (์ดˆ๊ธฐ ํŽ˜์ด์ง€ ๊ณ„์‚ฐ)
func calculateInitialDailyTargets(for currentReadingBook: UserBook) {
let (pagesPerDay, remainderPages) = firstCalculatePagesPerDay(for: currentReadingBook)
Expand All @@ -30,9 +22,9 @@ struct ReadingScheduleCalculator {
var remainderOffset = remainderPages
var cumulativePages = 0

while toYearMonthDayString(targetDate) <= toYearMonthDayString(currentReadingBook.book.targetEndDate) {
let dateKey = toYearMonthDayString(targetDate)
if !currentReadingBook.book.nonReadingDays.map({ toYearMonthDayString($0) }).contains(dateKey) {
while currentReadingBook.getReadingRecordsKey(targetDate) <= currentReadingBook.getReadingRecordsKey(currentReadingBook.book.targetEndDate) {
let dateKey = currentReadingBook.getReadingRecordsKey(targetDate)
if !currentReadingBook.book.nonReadingDays.map({ currentReadingBook.getReadingRecordsKey($0) }).contains(dateKey) {
cumulativePages += pagesPerDay
print("๐Ÿฒ๐Ÿฒ๐Ÿฒ: \(dateKey), \(cumulativePages)")
currentReadingBook.readingRecords[dateKey] = ReadingRecord(targetPages: cumulativePages, pagesRead: 0)
Expand All @@ -43,7 +35,7 @@ struct ReadingScheduleCalculator {
// ๋‚จ์€ ์ฑ… ๋ถ„๋Ÿ‰์€ ๋’ค์—์„œ๋ถ€ํ„ฐ 1ํŽ˜์ด์ง€์”ฉ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
var remainderTargetDate = currentReadingBook.book.targetEndDate
while remainderOffset > 0 {
let dateKey = toYearMonthDayString(remainderTargetDate)
let dateKey = currentReadingBook.getReadingRecordsKey(remainderTargetDate)
guard var record = currentReadingBook.readingRecords[dateKey] else { return }
record.targetPages += remainderOffset
currentReadingBook.readingRecords[dateKey] = record
Expand All @@ -57,14 +49,15 @@ struct ReadingScheduleCalculator {
}

// MARK: ์ฝ์€ ํŽ˜์ด์ง€ ์ž…๋ ฅ ๋ฉ”์„œ๋“œ (์˜ค๋Š˜ ๋‚ ์งœ์—๋งŒ ๊ฐ’์„ ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ)
// TODO: 04์‹œ ๊ธฐ์ค€์œผ๋กœ ๋“ฑ๋กํ•˜๊ธฐ โฐ
func updateReadingProgress(for currentReadingBook: UserBook, pagesRead: Int, from today: Date) {
let dateKey = toYearMonthDayString(today)
let dateKey = currentReadingBook.getReadingRecordsKey(today)

// ๊ธฐ๋ก์ด ์—†์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’ ์ถ”๊ฐ€
var record = currentReadingBook.readingRecords[dateKey, default: ReadingRecord(targetPages: 0, pagesRead: 0)]

// nonReadingDays์—์„œ today ์ œ๊ฑฐ (dateKey๋กœ ๋น„๊ต)
if let index = currentReadingBook.book.nonReadingDays.firstIndex(where: { toYearMonthDayString($0) == dateKey }) {
if let index = currentReadingBook.book.nonReadingDays.firstIndex(where: { currentReadingBook.getReadingRecordsKey($0) == dateKey }) {
currentReadingBook.book.nonReadingDays.remove(at: index)
} else {
print("์ง€์šธ ๋‚ ์งœ ์—†์Œ == ์ด๋ฏธ ํ• ๋‹น๋˜์–ด ์žˆ๋Š” ๋‚ ์ž…๋‹ˆ๋‹ค.")
Expand All @@ -86,7 +79,7 @@ struct ReadingScheduleCalculator {
}
}

// MARK: ๋” ์ฝ๊ฑฐ๋‚˜, ๋œ ์ฝ์œผ๋ฉด ์ดํ›„ ๋‚ ์งœ์˜ ํ• ๋‹น๋Ÿ‰์„ ๋‹ค์‹œ ๊ณ„์‚ฐํ•œ๋‹ค.
/// ํ•˜๋ฃจ ํ• ๋‹น๋Ÿ‰๋ณด๋‹ค ๋” ์ฝ๊ฑฐ๋‚˜, ๋œ ์ฝ์œผ๋ฉด ์ดํ›„ ๋‚ ์งœ์˜ ํ• ๋‹น๋Ÿ‰์„ ๋‹ค์‹œ ๊ณ„์‚ฐํ•œ๋‹ค.
func adjustFutureTargets(for currentReadingBook: UserBook, from date: Date) {
let totalRemainingPages = calculateRemainingPages(for: currentReadingBook)
print("โŒ: \(totalRemainingPages)")
Expand All @@ -101,10 +94,10 @@ struct ReadingScheduleCalculator {

var nextDate = Calendar.current.date(byAdding: .day, value: 1, to: date)!

while toYearMonthDayString(nextDate) <= toYearMonthDayString(currentReadingBook.book.targetEndDate) {
let dateKey = toYearMonthDayString(nextDate)
while currentReadingBook.getReadingRecordsKey(nextDate) <= currentReadingBook.getReadingRecordsKey(currentReadingBook.book.targetEndDate) {
let dateKey = currentReadingBook.getReadingRecordsKey(nextDate)

if !currentReadingBook.book.nonReadingDays.map({ toYearMonthDayString($0) }).contains(dateKey) {
if !currentReadingBook.book.nonReadingDays.map({ currentReadingBook.getReadingRecordsKey($0) }).contains(dateKey) {
guard var record = currentReadingBook.readingRecords[dateKey] else { return }
print("๐Ÿฆถ: \(dateKey) / \(record)")
cumulativePages += pagesPerDay
Expand All @@ -117,7 +110,7 @@ struct ReadingScheduleCalculator {

var remainingTargetDate = currentReadingBook.book.targetEndDate
while remainderOffset > 0 {
let dateKey = toYearMonthDayString(remainingTargetDate)
let dateKey = currentReadingBook.getReadingRecordsKey(remainingTargetDate)

guard var record = currentReadingBook.readingRecords[dateKey] else { return }

Expand All @@ -129,8 +122,8 @@ struct ReadingScheduleCalculator {
}
}

// TODO: ์—ฌ๊ธฐ๊ฐ€ ๋ฌธ์ œ!!!!!!!!!!!!!!!!!
// ์ด์ „ ํ• ๋‹น๋Ÿ‰์„ ์ฝ์ง€ ์•Š๊ณ  ์ƒˆ๋กญ๊ฒŒ ๋“ค์–ด์™”์„ ๋•Œ ์žฌํ• ๋‹น์„ ์œ„ํ•œ ๋ฉ”์„œ๋“œ
// TODO: 04์‹œ ๊ธฐ์ค€์œผ๋กœ ๋“ฑ๋กํ•˜๊ธฐ โฐ
/// ์ง€๋‚œ ๋‚ ์˜ ํ• ๋‹น๋Ÿ‰์„ ์ฝ์ง€ ์•Š๊ณ , ์•ฑ์— ์ƒˆ๋กญ๊ฒŒ ์ ‘์†ํ•  ๋•Œ ํŽ˜์ด์ง€๋ฅผ ์žฌํ• ๋‹นํ•ด์ฃผ๋Š” ๋ฉ”์„œ๋“œ
func reassignPagesFromLastReadDate(for currentReadingBook: UserBook) {
// ์ด๋ฏธ ์ฝ์—ˆ์œผ๋ฉด ์žฌ๋ถ„๋ฐฐ x
if hasReadPagesToday(for: currentReadingBook) { return }
Expand All @@ -142,11 +135,11 @@ struct ReadingScheduleCalculator {

var targetDate = Date() // ์˜ค๋Š˜ ๋‚ ์งœ๋ถ€ํ„ฐ ์ƒˆ๋กœ ํ• ๋‹น ์‹œ์ž‘

while toYearMonthDayString(targetDate) <= toYearMonthDayString(currentReadingBook.book.targetEndDate) {
let dateKey = toYearMonthDayString(targetDate)
while currentReadingBook.getReadingRecordsKey(targetDate) <= currentReadingBook.getReadingRecordsKey(currentReadingBook.book.targetEndDate) {
let dateKey = currentReadingBook.getReadingRecordsKey(targetDate)

// ๋น„๋…์„œ์ผ์ด ์•„๋‹ˆ๋ฉด ํ• ๋‹น๋Ÿ‰์„ ์ƒˆ๋กœ ์„ค์ •
if !currentReadingBook.book.nonReadingDays.map({ toYearMonthDayString($0) }).contains(dateKey) {
if !currentReadingBook.book.nonReadingDays.map({ currentReadingBook.getReadingRecordsKey($0) }).contains(dateKey) {
cumulativePages += pagesPerDay
currentReadingBook.readingRecords[dateKey] = ReadingRecord(targetPages: cumulativePages, pagesRead: 0)
}
Expand All @@ -157,7 +150,7 @@ struct ReadingScheduleCalculator {
// ๋‚˜๋จธ์ง€ ํŽ˜์ด์ง€๋ฅผ ๋งˆ์ง€๋ง‰ ๋ฉฐ์น ์— ๋ฐฐ๋ถ„
var remainingTargetDate = currentReadingBook.book.targetEndDate
while remainderOffset > 0 {
let dateKey = toYearMonthDayString(remainingTargetDate)
let dateKey = currentReadingBook.getReadingRecordsKey(remainingTargetDate)
guard var record = currentReadingBook.readingRecords[dateKey] else { return }

record.targetPages += remainderOffset
Expand All @@ -167,9 +160,10 @@ struct ReadingScheduleCalculator {
}
}

// ์˜ค๋Š˜ ํ• ๋‹น๋Ÿ‰์ด ์ฝํ˜”๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฉ”์„œ๋“œ
// TODO: 04์‹œ ๊ธฐ์ค€์œผ๋กœ ๋“ฑ๋กํ•˜๊ธฐ โฐ
/// ์˜ค๋Š˜ ํ• ๋‹น๋Ÿ‰์ด ์ฝํ˜”๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฉ”์„œ๋“œ
private func hasReadPagesToday(for currentReadingBook: UserBook) -> Bool {
let todayKey = toYearMonthDayString(Date())
let todayKey = currentReadingBook.getReadingRecordsKey(Date())
return currentReadingBook.readingRecords[todayKey]?.pagesRead != 0
}

Expand All @@ -178,9 +172,9 @@ struct ReadingScheduleCalculator {
func firstCalculateTotalReadingDays(for currentReadingBook: UserBook) -> Int {
var totalDays = 0
var targetDate = currentReadingBook.book.startDate
while toYearMonthDayString(targetDate) <= toYearMonthDayString(currentReadingBook.book.targetEndDate) {
let dateKey = toYearMonthDayString(targetDate)
if !currentReadingBook.book.nonReadingDays.map({ toYearMonthDayString($0) }).contains(dateKey) {
while currentReadingBook.getReadingRecordsKey(targetDate) <= currentReadingBook.getReadingRecordsKey(currentReadingBook.book.targetEndDate) {
let dateKey = currentReadingBook.getReadingRecordsKey(targetDate)
if !currentReadingBook.book.nonReadingDays.map({ currentReadingBook.getReadingRecordsKey($0) }).contains(dateKey) {
totalDays += 1
}
targetDate = Calendar.current.date(byAdding: .day, value: 1, to: targetDate)!
Expand All @@ -203,14 +197,15 @@ struct ReadingScheduleCalculator {
return currentReadingBook.book.totalPages - currentReadingBook.lastPagesRead
}

// TODO: 04์‹œ ๊ธฐ์ค€์œผ๋กœ ๋“ฑ๋กํ•˜๊ธฐ โฐ
// ์™„๋…๊นŒ์ง€ ๋‚จ์€ ๊ธฐ๊ฐ„์„ ๊ตฌํ•˜๋Š” ๋ฉ”์„œ๋“œ (์˜ค๋Š˜๋ถ€ํ„ฐ)
func calculateRemainingReadingDays(for currentReadingBook: UserBook) -> Int {
var remainingDays = 0
var targetDate = Date()

while toYearMonthDayString(targetDate) <= toYearMonthDayString(currentReadingBook.book.targetEndDate) {
let dateKey = toYearMonthDayString(targetDate)
if !currentReadingBook.book.nonReadingDays.map({ toYearMonthDayString($0) }).contains(dateKey) {
while currentReadingBook.getReadingRecordsKey(targetDate) <= currentReadingBook.getReadingRecordsKey(currentReadingBook.book.targetEndDate) {
let dateKey = currentReadingBook.getReadingRecordsKey(targetDate)
if !currentReadingBook.book.nonReadingDays.map({ currentReadingBook.getReadingRecordsKey($0) }).contains(dateKey) {
remainingDays += 1
}
targetDate = Calendar.current.date(byAdding: .day, value: 1, to: targetDate)!
Expand All @@ -231,11 +226,4 @@ struct ReadingScheduleCalculator {

return (pagesPerDay, remainder)
}

// ํŠน์ • ๋‚ ์˜ ๋ฌ™ํ‘œ๋Ÿ‰๊ณผ ์‹ค์ œ ์ฝ์€ ํŽ˜์ด์ง€์˜ ์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฉ”์„œ๋“œ
func getReadingRecord(for currentReadingBook: UserBook, for date: Date) -> ReadingRecord? {
let dateKey = toYearMonthDayString(date)
print("๐Ÿ’ต๐Ÿ’ต๐Ÿ’ต๐Ÿ’ต: \(dateKey)")
return currentReadingBook.readingRecords[dateKey]
}
}
13 changes: 12 additions & 1 deletion FiveGuyes/FiveGuyes/Sources/Models/UserBook.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ final class UserBook {
}

extension UserBook {
/// Date ํƒ€์ž…์˜ ๊ฐ’์„ readingRecords์˜ ํ‚ค ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ๋ฉ”์„œ๋“œ
func getReadingRecordsKey(_ date: Date) -> String { date.toYearMonthDayString() }

func markAsCompleted(review: String) {
// ์ฑ…์„ ์™„๋… ์ƒํƒœ๋กœ ์„ค์ •
book.targetEndDate = Date()
Expand Down Expand Up @@ -75,7 +78,15 @@ extension UserBook {

func findNextReadingPagesPerDay() -> Int {
let readingScheduleCalculator = ReadingScheduleCalculator()

return readingScheduleCalculator.calculatePagesPerDay(for: self).pagesPerDay
}

// TODO: 04์‹œ ๊ธฐ์ค€์œผ๋กœ ๋“ฑ๋กํ•˜๊ธฐ โฐ
/// ํŠน์ • ๋‚ ์˜ ๋ฌ™ํ‘œ๋Ÿ‰๊ณผ ์‹ค์ œ ์ฝ์€ ํŽ˜์ด์ง€์˜ ์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฉ”์„œ๋“œ
func getReadingRecord(for date: Date) -> ReadingRecord? {
let dateKey = self.getReadingRecordsKey(date)
print("๐Ÿ’ต๐Ÿ’ต๐Ÿ’ต๐Ÿ’ต: \(dateKey)")
return self.readingRecords[dateKey]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,3 @@ struct WeeklyPageCalendarView_Previews: PreviewProvider {
return userBook
}
}

// Date ํ™•์žฅ์œผ๋กœ ๋‚ ์งœ ๋ฌธ์ž์—ด ํฌ๋งท ์ถ”๊ฐ€
extension Date {
func toYearMonthDayString() -> String {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
formatter.timeZone = TimeZone.current
return formatter.string(from: self)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ struct DailyProgressView: View {
private let notificationManager = NotificationManager()

private var today: Date {
// TODO: today๊ฐ€ ์ „๋‚ ๋กœ ๋‚˜์™€์„œ ์ผ๋‹จ ํ•˜๋ฃจ ๋”ํ•จ
Date()
}
let readingScheduleCalculator = ReadingScheduleCalculator()
Expand Down Expand Up @@ -137,7 +136,7 @@ struct DailyProgressView: View {
.onAppear {
print("๐Ÿฏ๐Ÿฏ๐Ÿฏ๐Ÿฏ๐Ÿฏ: \(today)")

if let readingRecord = readingScheduleCalculator.getReadingRecord(for: userBook, for: today) {
if let readingRecord = userBook.getReadingRecord(for: today) {
pagesToReadToday = readingRecord.targetPages
}

Expand Down

0 comments on commit 98a02e0

Please sign in to comment.