From 232e031559b0030bdb72123670ba2dc016fdfe6e Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Wed, 13 Dec 2023 09:05:30 +0100 Subject: [PATCH] Add charts for tags --- .../DesignSystem/Views/TagChartView.swift | 24 +++++++++++++++++++ .../DesignSystem/Views/TagRowView.swift | 1 + Packages/Models/Sources/Models/Tag.swift | 10 +++++++- Packages/Status/Package.resolved | 4 ++-- .../Sources/Timeline/TimelineView.swift | 4 ++++ 5 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 Packages/DesignSystem/Sources/DesignSystem/Views/TagChartView.swift diff --git a/Packages/DesignSystem/Sources/DesignSystem/Views/TagChartView.swift b/Packages/DesignSystem/Sources/DesignSystem/Views/TagChartView.swift new file mode 100644 index 00000000..1d124ba9 --- /dev/null +++ b/Packages/DesignSystem/Sources/DesignSystem/Views/TagChartView.swift @@ -0,0 +1,24 @@ +import SwiftUI +import Charts +import Models + +public struct TagChartView: View { + let tag: Tag + + public init(tag: Tag) { + self.tag = tag + } + + public var body: some View { + Chart(tag.sortedHistory) { data in + AreaMark(x: .value("day", tag.sortedHistory.firstIndex(where: { $0.id == data.id }) ?? 0), + y: .value("uses", Int(data.uses) ?? 0)) + .interpolationMethod(.catmullRom) + } + .chartLegend(.hidden) + .chartXAxis(.hidden) + .chartYAxis(.hidden) + .frame(width: 70, height: 40) + .clipShape(RoundedRectangle(cornerRadius: 4)) + } +} diff --git a/Packages/DesignSystem/Sources/DesignSystem/Views/TagRowView.swift b/Packages/DesignSystem/Sources/DesignSystem/Views/TagRowView.swift index c3e0d9b0..de2f8d2c 100644 --- a/Packages/DesignSystem/Sources/DesignSystem/Views/TagRowView.swift +++ b/Packages/DesignSystem/Sources/DesignSystem/Views/TagRowView.swift @@ -21,6 +21,7 @@ public struct TagRowView: View { .foregroundStyle(.secondary) } Spacer() + TagChartView(tag: tag) } .contentShape(Rectangle()) .onTapGesture { diff --git a/Packages/Models/Sources/Models/Tag.swift b/Packages/Models/Sources/Models/Tag.swift index 54b2841c..987dc124 100644 --- a/Packages/Models/Sources/Models/Tag.swift +++ b/Packages/Models/Sources/Models/Tag.swift @@ -1,7 +1,10 @@ import Foundation public struct Tag: Codable, Identifiable, Equatable, Hashable { - public struct History: Codable { + public struct History: Codable, Identifiable { + public var id: String { + day + } public let day: String public let accounts: String public let uses: String @@ -24,6 +27,11 @@ public struct Tag: Codable, Identifiable, Equatable, Hashable { public let following: Bool public let history: [History] + public var sortedHistory: [History] { + history.sorted { + Int($0.day) ?? 0 < Int($1.day) ?? 0 + } + } public var totalUses: Int { history.compactMap { Int($0.uses) }.reduce(0, +) } diff --git a/Packages/Status/Package.resolved b/Packages/Status/Package.resolved index e1f1806b..1589e398 100644 --- a/Packages/Status/Package.resolved +++ b/Packages/Status/Package.resolved @@ -5,8 +5,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/divadretlaw/EmojiText", "state" : { - "revision" : "b5b0a30933a6dcb6601ad3625690a823fa3f6965", - "version" : "2.6.0" + "revision" : "bb1fd025fe6da0d98b57088e9b40a37277e95503", + "version" : "3.1.0" } }, { diff --git a/Packages/Timeline/Sources/Timeline/TimelineView.swift b/Packages/Timeline/Sources/Timeline/TimelineView.swift index 70097d8c..36778e58 100644 --- a/Packages/Timeline/Sources/Timeline/TimelineView.swift +++ b/Packages/Timeline/Sources/Timeline/TimelineView.swift @@ -7,6 +7,7 @@ import Status import SwiftData import SwiftUI import SwiftUIIntrospect +import Charts @MainActor public struct TimelineView: View { @@ -154,6 +155,9 @@ public struct TimelineView: View { if let tag = viewModel.tag { headerView { HStack { + TagChartView(tag: tag) + .padding(.top, 12) + VStack(alignment: .leading, spacing: 4) { Text("#\(tag.name)") .font(.scaledHeadline)