Skip to content

Commit

Permalink
Refact[#205] : DateExtension과 Date 파일이 합쳐질 수 있어서 합쳤습니다.
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentino1994 committed Jun 30, 2022
1 parent 0fc47b7 commit dd324fa
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 68 deletions.
Binary file not shown.
64 changes: 64 additions & 0 deletions kko_okk/Models/Date.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,68 @@ extension Date {
var daySixDayBefore: Date {
return Calendar.current.date(byAdding: .day, value: -6, to: self)!
}

var dayAfter: Date {
return Calendar.current.date(byAdding: .day, value: 1, to: self)!
}

var dayBefore: Date {
return Calendar.current.date(byAdding: .day, value: -1, to: self)!
}

func startOftheMonth(now: Date) -> Date {
let now: Date = now
let dateFormatter: DateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "ko_kr")
dateFormatter.timeZone = TimeZone(abbreviation: "KST")
dateFormatter.dateFormat = "yyyy-MM-dd"

let kr: String = dateFormatter.string(from: now)
var temp: [Substring] = kr.split(separator: "-")
temp[temp.count - 1] = "01"
let result: String = temp.joined(separator: "-")

return dateFormatter.date(from: result) ?? Date()
}

func endOftheMonth(now: Date) -> Date {
let now: Date = now
let dateFormatter: DateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "ko_kr")
dateFormatter.timeZone = TimeZone(abbreviation: "KST")
dateFormatter.dateFormat = "yyyy-MM-dd"

let kr: String = dateFormatter.string(from: now)
var temp: [Substring] = kr.split(separator: "-")
temp[temp.count - 2] = Substring("0") + Substring(String(Int(temp[temp.count - 2])! == 12 ? 1 : Int(temp[temp.count - 2])! + 1))
temp[temp.count - 1] = "01"
let result: String = temp.joined(separator: "-")

return dateFormatter.date(from: result) ?? Date()
}

func calcParentProgress(dailyPromises: FetchedResults<Promise>, now: Date) -> CGFloat {
let dailyPromises = dailyPromises.filter{ Calendar.current.startOfDay(for: now) <= $0.madeTime && $0.madeTime <= Calendar.current.startOfDay(for: now).dayAfter }

let parentPromises: [Promise] = dailyPromises.filter { $0.subject == "parent"}

let parentDoneCount: Int = parentPromises.filter { $0.isDone == true }.count

let parentProgress: CGFloat = parentPromises.count != 0 ? CGFloat(Double(parentDoneCount) / Double(parentPromises.count) * 100) : 0


return parentProgress
}

func calcChildProgress(dailyPromises: FetchedResults<Promise>, now: Date) -> CGFloat {
let dailyPromises = dailyPromises.filter{ Calendar.current.startOfDay(for: now) <= $0.madeTime && $0.madeTime <= Calendar.current.startOfDay(for: now).dayAfter }

let childPromises: [Promise] = dailyPromises.filter { $0.subject == "child"}

let childDoneCount: Int = childPromises.filter { $0.isDone == true }.count

let childProgress: CGFloat = childPromises.count != 0 ? CGFloat(Double(childDoneCount) / Double(childPromises.count) * 100) : 0

return childProgress
}
}
136 changes: 68 additions & 68 deletions kko_okk/Models/DateExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,71 +5,71 @@
// Created by Geunil Park on 2022/06/18.
//

import Foundation
import SwiftUI

extension Date {
var dayAfter: Date {
return Calendar.current.date(byAdding: .day, value: 1, to: self)!
}

var dayBefore: Date {
return Calendar.current.date(byAdding: .day, value: -1, to: self)!
}

func startOftheMonth(now: Date) -> Date {
let now: Date = now
let dateFormatter: DateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "ko_kr")
dateFormatter.timeZone = TimeZone(abbreviation: "KST")
dateFormatter.dateFormat = "yyyy-MM-dd"

let kr: String = dateFormatter.string(from: now)
var temp: [Substring] = kr.split(separator: "-")
temp[temp.count - 1] = "01"
let result: String = temp.joined(separator: "-")

return dateFormatter.date(from: result) ?? Date()
}

func endOftheMonth(now: Date) -> Date {
let now: Date = now
let dateFormatter: DateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "ko_kr")
dateFormatter.timeZone = TimeZone(abbreviation: "KST")
dateFormatter.dateFormat = "yyyy-MM-dd"

let kr: String = dateFormatter.string(from: now)
var temp: [Substring] = kr.split(separator: "-")
temp[temp.count - 2] = Substring("0") + Substring(String(Int(temp[temp.count - 2])! == 12 ? 1 : Int(temp[temp.count - 2])! + 1))
temp[temp.count - 1] = "01"
let result: String = temp.joined(separator: "-")

return dateFormatter.date(from: result) ?? Date()
}

func calcParentProgress(dailyPromises: FetchedResults<Promise>, now: Date) -> CGFloat {
let dailyPromises = dailyPromises.filter{ Calendar.current.startOfDay(for: now) <= $0.madeTime && $0.madeTime <= Calendar.current.startOfDay(for: now).dayAfter }

let parentPromises: [Promise] = dailyPromises.filter { $0.subject == "parent"}

let parentDoneCount: Int = parentPromises.filter { $0.isDone == true }.count

let parentProgress: CGFloat = parentPromises.count != 0 ? CGFloat(Double(parentDoneCount) / Double(parentPromises.count) * 100) : 0


return parentProgress
}

func calcChildProgress(dailyPromises: FetchedResults<Promise>, now: Date) -> CGFloat {
let dailyPromises = dailyPromises.filter{ Calendar.current.startOfDay(for: now) <= $0.madeTime && $0.madeTime <= Calendar.current.startOfDay(for: now).dayAfter }

let childPromises: [Promise] = dailyPromises.filter { $0.subject == "child"}

let childDoneCount: Int = childPromises.filter { $0.isDone == true }.count

let childProgress: CGFloat = childPromises.count != 0 ? CGFloat(Double(childDoneCount) / Double(childPromises.count) * 100) : 0

return childProgress
}
}
//import Foundation
//import SwiftUI
//
//extension Date {
// var dayAfter: Date {
// return Calendar.current.date(byAdding: .day, value: 1, to: self)!
// }
//
// var dayBefore: Date {
// return Calendar.current.date(byAdding: .day, value: -1, to: self)!
// }
//
// func startOftheMonth(now: Date) -> Date {
// let now: Date = now
// let dateFormatter: DateFormatter = DateFormatter()
// dateFormatter.locale = Locale(identifier: "ko_kr")
// dateFormatter.timeZone = TimeZone(abbreviation: "KST")
// dateFormatter.dateFormat = "yyyy-MM-dd"
//
// let kr: String = dateFormatter.string(from: now)
// var temp: [Substring] = kr.split(separator: "-")
// temp[temp.count - 1] = "01"
// let result: String = temp.joined(separator: "-")
//
// return dateFormatter.date(from: result) ?? Date()
// }
//
// func endOftheMonth(now: Date) -> Date {
// let now: Date = now
// let dateFormatter: DateFormatter = DateFormatter()
// dateFormatter.locale = Locale(identifier: "ko_kr")
// dateFormatter.timeZone = TimeZone(abbreviation: "KST")
// dateFormatter.dateFormat = "yyyy-MM-dd"
//
// let kr: String = dateFormatter.string(from: now)
// var temp: [Substring] = kr.split(separator: "-")
// temp[temp.count - 2] = Substring("0") + Substring(String(Int(temp[temp.count - 2])! == 12 ? 1 : Int(temp[temp.count - 2])! + 1))
// temp[temp.count - 1] = "01"
// let result: String = temp.joined(separator: "-")
//
// return dateFormatter.date(from: result) ?? Date()
// }
//
// func calcParentProgress(dailyPromises: FetchedResults<Promise>, now: Date) -> CGFloat {
// let dailyPromises = dailyPromises.filter{ Calendar.current.startOfDay(for: now) <= $0.madeTime && $0.madeTime <= Calendar.current.startOfDay(for: now).dayAfter }
//
// let parentPromises: [Promise] = dailyPromises.filter { $0.subject == "parent"}
//
// let parentDoneCount: Int = parentPromises.filter { $0.isDone == true }.count
//
// let parentProgress: CGFloat = parentPromises.count != 0 ? CGFloat(Double(parentDoneCount) / Double(parentPromises.count) * 100) : 0
//
//
// return parentProgress
// }
//
// func calcChildProgress(dailyPromises: FetchedResults<Promise>, now: Date) -> CGFloat {
// let dailyPromises = dailyPromises.filter{ Calendar.current.startOfDay(for: now) <= $0.madeTime && $0.madeTime <= Calendar.current.startOfDay(for: now).dayAfter }
//
// let childPromises: [Promise] = dailyPromises.filter { $0.subject == "child"}
//
// let childDoneCount: Int = childPromises.filter { $0.isDone == true }.count
//
// let childProgress: CGFloat = childPromises.count != 0 ? CGFloat(Double(childDoneCount) / Double(childPromises.count) * 100) : 0
//
// return childProgress
// }
//}

0 comments on commit dd324fa

Please sign in to comment.