-
Notifications
You must be signed in to change notification settings - Fork 4
/
Project.swift
77 lines (67 loc) · 1.8 KB
/
Project.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import Foundation
public struct Project: Equatable, Sendable, Decodable {
public init(
date: Date,
type: String,
name: String,
tags: [String],
url: URL?
) {
self.date = date
self.type = type
self.name = name
self.tags = tags
self.url = url
}
@ProjectDate public var date: Date
public var type: String
public var name: String
public var tags: [String]
public var url: URL?
}
extension Project: Identifiable {
public struct ID: Hashable, Sendable {
public init(date: Date, name: String) {
self.date = date
self.name = name
}
public var date: Date
public var name: String
}
public var id: ID { ID(date: date, name: name) }
}
extension Project {
@propertyWrapper
public struct ProjectDate: Equatable, Sendable {
public init(wrappedValue: Date) {
self.wrappedValue = wrappedValue
}
public var wrappedValue: Foundation.Date
}
}
extension Project.ProjectDate: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let string = try container.decode(String.self)
guard let date = Self.dateFormatter.date(from: string) else {
throw DecodingError.dataCorruptedError(
in: container,
debugDescription: #"Invalid date format: "\#(string)""#
)
}
self.wrappedValue = date
}
static let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy.MM.dd"
return formatter
}()
}
extension Array<Project> {
public static let preview: [Project] = {
let url = Bundle.module.url(forResource: "projects_preview", withExtension: "json")!
let data = try! Data(contentsOf: url)
let decoder = JSONDecoder()
return try! decoder.decode([Project].self, from: data)
}()
}