Skip to content

Commit c305ee0

Browse files
authored
Merge pull request #6 from alfredcc/main
public Initializes of `ACData`
2 parents 684b8e3 + 6a4d2e0 commit c305ee0

File tree

7 files changed

+53
-61
lines changed

7 files changed

+53
-61
lines changed

AxisContributionExample/AxisContributionExample.xcodeproj/project.pbxproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@
361361
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
362362
CODE_SIGN_STYLE = Automatic;
363363
CURRENT_PROJECT_VERSION = 1;
364-
DEVELOPMENT_TEAM = SM6445X39C;
364+
DEVELOPMENT_TEAM = 5FRV5G4F5S;
365365
ENABLE_PREVIEWS = YES;
366366
GENERATE_INFOPLIST_FILE = YES;
367367
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
@@ -391,7 +391,7 @@
391391
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
392392
CODE_SIGN_STYLE = Automatic;
393393
CURRENT_PROJECT_VERSION = 1;
394-
DEVELOPMENT_TEAM = SM6445X39C;
394+
DEVELOPMENT_TEAM = 5FRV5G4F5S;
395395
ENABLE_PREVIEWS = YES;
396396
GENERATE_INFOPLIST_FILE = YES;
397397
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
@@ -424,7 +424,7 @@
424424
CODE_SIGN_STYLE = Automatic;
425425
COMBINE_HIDPI_IMAGES = YES;
426426
CURRENT_PROJECT_VERSION = 1;
427-
DEVELOPMENT_TEAM = SM6445X39C;
427+
DEVELOPMENT_TEAM = 5FRV5G4F5S;
428428
ENABLE_HARDENED_RUNTIME = YES;
429429
ENABLE_PREVIEWS = YES;
430430
GENERATE_INFOPLIST_FILE = YES;

AxisContributionExample/Shared/ContentView.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ struct ContentView: View {
1616
@State private var constant: ACConstant = .init(axisMode: .horizontal, levelLabel: .number)
1717
@State private var rowSize: CGFloat = 11
1818
@State private var rowImageName: String = ""
19-
@State private var dates: [Date] = []
19+
@State private var dataSet: [Date: ACData] = [:]
2020

2121
var body: some View {
2222
VStack {
2323
Spacer()
2424
// AxisContribution(constant: constant, source: getDates())
25-
AxisContribution(constant: constant, source: dates) { indexSet, data in
25+
AxisContribution(constant: constant, source: dataSet) { indexSet, data in
2626
if rowImageName.isEmpty {
2727
defaultBackground
2828
}else {
@@ -64,13 +64,13 @@ struct ContentView: View {
6464
}
6565
.pickerStyle(.segmented)
6666
Button("Refresh Dates") {
67-
dates = getDates()
67+
dataSet = getDates()
6868
}
6969
.padding()
7070
}
7171
.padding()
7272
.onAppear {
73-
dates = getDates()
73+
dataSet = getDates()
7474
}
7575
}
7676

@@ -103,11 +103,11 @@ struct ContentView: View {
103103
.frame(width: rowSize, height: rowSize)
104104
}
105105

106-
private func getDates() -> [Date] {
107-
var sequenceDatas = [Date]()
106+
private func getDates() -> [Date: ACData] {
107+
var sequenceDatas = [Date: ACData]()
108108
for _ in 0..<300 {
109109
let date = Date.randomBetween(start: Date().dateHalfAyear, end: Date())
110-
sequenceDatas.append(date)
110+
sequenceDatas[date.startOfDay] = .init(date: date.startOfDay, count: Int.random(in: 0...10))
111111
}
112112
return sequenceDatas
113113
}

Sources/AxisContribution/AxisContribution.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public struct AxisContribution<B, F>: View where B: View, F: View {
4747
@StateObject private var store = ACDataStore()
4848

4949
private var constant: ACConstant = .init()
50-
private var sourceDatas: [Date]? = nil
50+
private var sourceDatas: [Date: ACData] = [:]
5151
private var externalDatas: [[ACData]]? = nil
5252

5353
public var background: ((ACIndexSet?, ACData?) -> B)? = nil
@@ -190,7 +190,7 @@ public extension AxisContribution where B == EmptyView, F == EmptyView {
190190
/// - Parameters:
191191
/// - constant: Settings that define the contribution view.
192192
/// - sourceDates: An array of contributed dates.
193-
init(constant: ACConstant = .init(), source sourceDates: [Date] = []) {
193+
init(constant: ACConstant = .init(), source sourceDates: [Date: ACData] = [:]) {
194194
self.constant = constant
195195
self.sourceDatas = sourceDates
196196
}
@@ -214,7 +214,7 @@ public extension AxisContribution where B : View, F : View {
214214
/// - background: The view that is the background of the row view.
215215
/// - foreground: The view that is the foreground of the row view.
216216
init(constant: ACConstant = .init(),
217-
source sourceDates: [Date] = [],
217+
source sourceDates: [Date: ACData] = [:],
218218
@ViewBuilder background: @escaping (ACIndexSet?, ACData?) -> B,
219219
@ViewBuilder foreground: @escaping (ACIndexSet?, ACData?) -> F) {
220220
self.constant = constant
@@ -241,6 +241,6 @@ public extension AxisContribution where B : View, F : View {
241241

242242
struct AxisContribution_Previews: PreviewProvider {
243243
static var previews: some View {
244-
AxisContribution(constant: .init(), source: [])
244+
AxisContribution(constant: .init(), source: [:])
245245
}
246246
}

Sources/AxisContribution/Model/ACData.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class ACData: Equatable {
3838
/// - Parameters:
3939
/// - date: Current date.
4040
/// - count: The number contributed to the current date. The default value is `0`.
41-
init(date: Date, count: Int = 0) {
41+
public init(date: Date, count: Int = 0) {
4242
self.date = date
4343
self.count = count
4444
}

Sources/AxisContribution/View/ACGridStack.swift

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,11 @@ import SwiftUI
2727

2828
/// A view that represents a grid view.
2929
struct ACGridStack<B, F>: View where B: View, F: View {
30-
3130
@EnvironmentObject private var store: ACDataStore
3231

3332
let constant: ACConstant
34-
var background: ((ACIndexSet?, ACData?) -> B)? = nil
35-
var foreground: ((ACIndexSet?, ACData?) -> F)? = nil
33+
var background: ((ACIndexSet?, ACData?) -> B)?
34+
var foreground: ((ACIndexSet?, ACData?) -> F)?
3635

3736
@State private var rowSize: CGSize = .zero
3837
@State private var titleWidth: CGFloat = .zero
@@ -43,7 +42,8 @@ struct ACGridStack<B, F>: View where B: View, F: View {
4342
.font(store.constant.font)
4443
}
4544

46-
//MARK: - Properties
45+
// MARK: - Properties
46+
4747
/// Property that displays the grid view.
4848
private var content: some View {
4949
let spacing = store.constant.spacing
@@ -66,14 +66,14 @@ struct ACGridStack<B, F>: View where B: View, F: View {
6666
Rectangle()
6767
.fill(Color.clear)
6868
.frame(width: rowSize.height, height: rowSize.height)
69-
.overlay(getMonthTitle(column) ,alignment: .leading)
69+
.overlay(getMonthTitle(column), alignment: .leading)
7070
ForEach(Array(datas.enumerated()), id: \.offset) { row, data in
7171
getRowView(column: column, row: row, data: data)
7272
}
7373
}
7474
}
7575
}
76-
}else {
76+
} else {
7777
VStack(alignment: .leading, spacing: spacing) {
7878
ZStack(alignment: .bottom) {
7979
let size = titleWidth
@@ -100,7 +100,7 @@ struct ACGridStack<B, F>: View where B: View, F: View {
100100
}
101101
}
102102

103-
//MARK: - Methods
103+
// MARK: - Methods
104104

105105
/// A method that returns a row view.
106106
/// - Parameters:
@@ -110,15 +110,10 @@ struct ACGridStack<B, F>: View where B: View, F: View {
110110
/// - Returns: -
111111
private func getRowView(column: Int, row: Int, data: ACData) -> some View {
112112
ZStack {
113-
if data.date.startOfDay > Date().startOfDay {
114-
background?(ACIndexSet(column: column, row: row), data)
115-
.hidden()
116-
}else {
117-
background?(ACIndexSet(column: column, row: row), data)
118-
foreground?(ACIndexSet(column: column, row: row), data)
119-
.opacity(getOpacity(count: data.count))
120-
.takeSize($rowSize)
121-
}
113+
background?(ACIndexSet(column: column, row: row), data)
114+
foreground?(ACIndexSet(column: column, row: row), data)
115+
.opacity(getOpacity(count: data.count))
116+
.takeSize($rowSize)
122117
}
123118
}
124119

@@ -135,15 +130,18 @@ struct ACGridStack<B, F>: View where B: View, F: View {
135130
.fixedSize(horizontal: true, vertical: false)
136131
.takeSize($_titleSize)
137132
}
138-
}else {
139-
Text(store.datas[column][0].date.monthTitle)
140-
.lineLimit(1)
141-
.fixedSize(horizontal: true, vertical: false)
142-
.takeSize($_titleSize)
133+
} else {
134+
let date = store.datas[column][0].date
135+
if date > constant.fromDate && date < constant.toDate {
136+
Text(date.monthTitle)
137+
.lineLimit(1)
138+
.fixedSize(horizontal: true, vertical: false)
139+
.takeSize($_titleSize)
140+
}
143141
}
144142
}
145143
}
146-
.onChange(of: _titleSize) { newValue in
144+
.onChange(of: _titleSize) { _ in
147145
titleWidth = max(titleWidth, _titleSize.width)
148146
}
149147
}
@@ -154,29 +152,29 @@ struct ACGridStack<B, F>: View where B: View, F: View {
154152
private func getOpacity(count: Int) -> CGFloat {
155153
if count == 0 {
156154
return ACLevel.zero.opacity
157-
}else if ACLevel.first.rawValue * store.constant.levelSpacing >= count {
155+
} else if ACLevel.first.rawValue * store.constant.levelSpacing >= count {
158156
return ACLevel.first.opacity
159-
}else if ACLevel.second.rawValue * store.constant.levelSpacing >= count {
157+
} else if ACLevel.second.rawValue * store.constant.levelSpacing >= count {
160158
return ACLevel.second.opacity
161-
}else if ACLevel.third.rawValue * store.constant.levelSpacing >= count {
159+
} else if ACLevel.third.rawValue * store.constant.levelSpacing >= count {
162160
return ACLevel.third.opacity
163-
}else if ACLevel.fourth.rawValue * store.constant.levelSpacing >= count {
161+
} else if ACLevel.fourth.rawValue * store.constant.levelSpacing >= count {
164162
return ACLevel.fourth.opacity
165163
}
166164
return 1.0
167165
}
168166
}
169167

170-
extension ACGridStack where B : View, F : View {
171-
168+
extension ACGridStack where B: View, F: View {
172169
/// Initializes `ACGridStack`
173170
/// - Parameters:
174171
/// - constant: Settings that define the contribution view.
175172
/// - background: The view that is the background of the row view.
176173
/// - foreground: The view that is the foreground of the row view.
177174
init(constant: ACConstant,
178175
@ViewBuilder background: @escaping (ACIndexSet?, ACData?) -> B,
179-
@ViewBuilder foreground: @escaping (ACIndexSet?, ACData?) -> F) {
176+
@ViewBuilder foreground: @escaping (ACIndexSet?, ACData?) -> F)
177+
{
180178
self.constant = constant
181179
self.background = background
182180
self.foreground = foreground
@@ -185,6 +183,9 @@ extension ACGridStack where B : View, F : View {
185183

186184
struct ACGridStack_Previews: PreviewProvider {
187185
static var previews: some View {
188-
AxisContribution(constant: .init(), source: [])
186+
AxisContribution(
187+
constant: .init(),
188+
source: [:]
189+
)
189190
}
190191
}

Sources/AxisContribution/ViewModel/ACDataProvider.swift

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,22 @@ public class ACDataProvider {
3535
/// - constant: Settings that define the contribution view.
3636
/// - sourceDates: An array of contributed dates.
3737
/// - Returns: mapped data
38-
public func mappedData(constant: ACConstant, source sourceDates: [Date]) -> [[ACData]] {
38+
public func mappedData(constant: ACConstant, source sourceDates: [Date: ACData]) -> [[ACData]] {
3939
var newDatas = [[ACData]]()
4040
var dateWeekly = Date.datesWeekly(from: constant.fromDate, to: constant.toDate)
4141
if constant.axisMode == .vertical {
4242
dateWeekly = dateWeekly.reversed()
4343
}
4444
dateWeekly.forEach { date in
4545
let datas = date.datesInWeek.map { date -> ACData in
46-
let data = ACData(date: date, count: getDateCount(sourceDates: sourceDates, date: date))
47-
return data
46+
if let data = sourceDates[date] {
47+
return data
48+
} else {
49+
return ACData(date: date, count: 0)
50+
}
4851
}
4952
newDatas.append(datas)
5053
}
5154
return newDatas
5255
}
53-
54-
/// Returns data corresponding to the date you pass in.
55-
/// - Parameters:
56-
/// - sourceDates: An array of contributed dates.
57-
/// - date: The date required to return the data.
58-
/// - Returns: -
59-
private func getDateCount(sourceDates: [Date], date: Date) -> Int {
60-
let dates = sourceDates.filter { d in
61-
Calendar.current.isDate(d, inSameDayAs: date)
62-
}
63-
return dates.count
64-
}
6556
}

Sources/AxisContribution/ViewModel/ACDataStore.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class ACDataStore: ObservableObject {
4444
/// - Parameters:
4545
/// - constant: Settings that define the contribution view.
4646
/// - sourceDates: An array of contributed dates.
47-
func setup(constant: ACConstant, source sourceDates: [Date]? = nil) {
47+
func setup(constant: ACConstant, source sourceDates: [Date: ACData]? = nil) {
4848
self.constant = constant
4949
if let sourceDates = sourceDates {
5050
self.datas = ACDataProvider.shared.mappedData(constant: constant, source: sourceDates)

0 commit comments

Comments
 (0)