Skip to content

Commit

Permalink
Feature/bal 195 gp profile chart (#197)
Browse files Browse the repository at this point in the history
- NEW STATS VIEW FROM PROFILE
  • Loading branch information
gperissetcelteeka authored Jun 11, 2024
1 parent e2a7001 commit bf82388
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 8 deletions.
6 changes: 5 additions & 1 deletion Balance.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
6CB5181A29E45678007AADB4 /* ImageCollectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CB5181929E45678007AADB4 /* ImageCollectionView.swift */; };
6CB5181C29E45C0C007AADB4 /* BalanceExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CB5181B29E45C0B007AADB4 /* BalanceExtensions.swift */; };
6CB5181E29E6F395007AADB4 /* ImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CB5181D29E6F395007AADB4 /* ImageView.swift */; };
6CB845572BFD208200059FFA /* StatsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CB845562BFD208200059FFA /* StatsView.swift */; };
6CB8F7322AC61BCF0074FDD9 /* BalanceWatchApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CB8F72F2AC61BCE0074FDD9 /* BalanceWatchApp.swift */; };
6CB8F7332AC61BCF0074FDD9 /* ComplicationController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CB8F7302AC61BCE0074FDD9 /* ComplicationController.swift */; };
6CB8F73B2AC61F340074FDD9 /* WorkoutManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CB8F73A2AC61F340074FDD9 /* WorkoutManager.swift */; };
Expand Down Expand Up @@ -360,6 +361,7 @@
6CB5181929E45678007AADB4 /* ImageCollectionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageCollectionView.swift; sourceTree = "<group>"; };
6CB5181B29E45C0B007AADB4 /* BalanceExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BalanceExtensions.swift; sourceTree = "<group>"; };
6CB5181D29E6F395007AADB4 /* ImageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageView.swift; sourceTree = "<group>"; };
6CB845562BFD208200059FFA /* StatsView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatsView.swift; sourceTree = "<group>"; };
6CB8F72F2AC61BCE0074FDD9 /* BalanceWatchApp.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BalanceWatchApp.swift; sourceTree = "<group>"; };
6CB8F7302AC61BCE0074FDD9 /* ComplicationController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ComplicationController.swift; sourceTree = "<group>"; };
6CB8F73A2AC61F340074FDD9 /* WorkoutManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WorkoutManager.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -635,6 +637,7 @@
6C5523F629F80E8800F802D9 /* ShowHideSecureField.swift */,
6C5523F829F8126000F802D9 /* ProfileCellView.swift */,
6C47339029FC5C7D00DC72D0 /* PersonalDataView.swift */,
6CB845562BFD208200059FFA /* StatsView.swift */,
6CF978982A65EC970046E90A /* EmailHelper.swift */,
);
path = Profile;
Expand Down Expand Up @@ -1158,6 +1161,7 @@
6C55243129F86DD800F802D9 /* OnboardingFlow.swift in Sources */,
6CFB02412A438AFA000045E1 /* Photo.swift in Sources */,
6CFB02552A449C2A000045E1 /* ImageReviewRepository.swift in Sources */,
6CB845572BFD208200059FFA /* StatsView.swift in Sources */,
6C8868AD2A3A011A00000647 /* LoginViewLocal.swift in Sources */,
6C55243029F86DD800F802D9 /* InterestingModules.swift in Sources */,
6C47339129FC5C7D00DC72D0 /* PersonalDataView.swift in Sources */,
Expand Down Expand Up @@ -1768,7 +1772,7 @@
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/SvenTiigi/YouTubePlayerKit.git";
requirement = {
kind = upToNextMajorVersion;
kind = upToNextMinorVersion;
minimumVersion = 1.0.0;
};
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "9a2ec0153ac68a34b7437f77b1581088266f22c19125dfe889c09bd915b8fdc2",
"originHash" : "046d81b689bb9375e29ad15d6c5df7442a32fff88a2e2a791c2d3b5622b82ada",
"pins" : [
{
"identity" : "abseil-cpp-binary",
Expand Down Expand Up @@ -139,7 +139,7 @@
{
"identity" : "spezifirebase",
"kind" : "remoteSourceControl",
"location" : "https://github.com/StanfordSpezi/SpeziFirebase.git",
"location" : "https://github.com/StanfordSpezi/SpeziFirebase",
"state" : {
"revision" : "f05c859f75d317dca9b378b7f8a7cfa8e135df04",
"version" : "1.1.1"
Expand All @@ -157,7 +157,7 @@
{
"identity" : "spezionboarding",
"kind" : "remoteSourceControl",
"location" : "https://github.com/StanfordSpezi/SpeziOnboarding.git",
"location" : "https://github.com/StanfordSpezi/SpeziOnboarding",
"state" : {
"revision" : "8d6dda3501720a1952573439b21a503cbecd9e0f",
"version" : "1.2.0"
Expand Down Expand Up @@ -231,8 +231,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/SvenTiigi/YouTubePlayerKit.git",
"state" : {
"revision" : "5ade0dfbabaf7456069b4e8f604de261bf6d7c67",
"version" : "1.8.0"
"revision" : "7f5e23ef5948041bc2acba9f47b8f2128f93adca",
"version" : "1.0.0"
}
}
],
Expand Down
60 changes: 58 additions & 2 deletions Balance/Profile/ProfileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import SwiftUI

// swiftlint:disable type_body_length
// swiftlint:disable file_length
struct ProfileView: View {
@Environment(\.dismiss)
private var dismiss
Expand All @@ -34,7 +35,9 @@ struct ProfileView: View {
@State private var sliderValue: Float = 0.0
@State private var isEditing = false
@State private var sliderStringValue: String = ""

// @State private var postData = [PostCount]()
@State private var valuesData = [Double]()
@State private var namesData = [String]()
var body: some View {
ZStack {
VStack(alignment: .center, spacing: 0) {
Expand Down Expand Up @@ -121,11 +124,12 @@ struct ProfileView: View {
infoOption
updateOption
#else
resetOption
statsView
if logsIsEmpty == false {
shareOption
shareLink
}
resetOption
#endif
logoutOption
}
Expand All @@ -134,6 +138,28 @@ struct ProfileView: View {
}
}

var statsView: some View {
NavigationLink(
destination: ActivityLogBaseView(
viewName: "Stats View",
isDirectChildToContainer: true,
content: {
StatsView(valuesData: valuesData, namesData: namesData)
}
)
) {
ProfileCellView(image: "chart.pie", text: "How I used the App")
}.simultaneousGesture(TapGesture().onEnded {
// self.postData = topActions()
//
// for val in postData {
// self.valuesData.append(Double(val.count))
// self.namesData.append(val.eventName)
// }
topActions()
})
}

var avatarChangeView: some View {
Button(action: {
showingAvatarSheet.toggle()
Expand Down Expand Up @@ -473,4 +499,34 @@ struct ProfileView: View {
}
return noteAsCSV
}

func topActions() {
var byEvent: [PostCount] = []
var logActions = [String]()
var counts: [String: Int] = [:]

for log in logs {
for action in log.actions {
logActions.append(action.description)
}
}
for item in logActions {
counts[item] = (counts[item] ?? 0) + 1
}

for (key, value) in counts {
byEvent.append(PostCount(eventName: key, count: value))
}

let sortedEvents = byEvent.sorted {
$0.count > $1.count
}

for val in sortedEvents.first(elementCount: 5) {
self.valuesData.append(Double(val.count))
self.namesData.append(val.eventName)
}
return
// return sortedEvents.first(elementCount: 5)
}
}
122 changes: 122 additions & 0 deletions Balance/Profile/StatsView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
//
// PersonalDataView.swift
// Balance
//
// Created by Gonzalo Perisset on 28/04/2023.
//

import Charts
import SwiftUI

struct PostCount {
var eventName: String
var count: Int
}

struct StatsView: View {
@Environment(\.dismiss)
var dismiss
var valuesData: [Double]
var namesData: [String]

var body: some View {
ZStack {
backgroundColor.edgesIgnoringSafeArea(.all)
VStack {
HeaderMenu(title: "User Stats")
Spacer()
if #available(iOS 17.0, *) {
chartView
} else {
chartViewLib
}
Spacer()
}
}
}

var chartViewLib: some View {
Chart {
ForEach(namesData.indices, id: \.self) { index in
BarMark(
x: .value("Event", namesData[index]),
y: .value("Count", valuesData[index])
)
.foregroundStyle(
by: .value("Scheme", namesData[index])
)
}
}
.padding(20)
.chartXAxis(.hidden)
}
// VStack {
// GroupBox ( "Bar Chart - Step Count (x 1,000)") {
// Chart(currentWeek) {
// let stepThousands = Double($0.steps) / 1000.00
// BarMark(
// x: .value("Week Day", $0.weekday, unit: .day),
// y: .value("Step Count", $0.steps)
// )
// .foregroundStyle(Color.orange)
// .annotation(position: .overlay, alignment: .topLeading, spacing: 3) {
// Text("\(stepThousands, specifier: "%.1F")")
// .font(.footnote)
// .foregroundColor(.white)
// }
// }
// .chartYAxis(.hidden)
// .chartXAxis {
// AxisMarks (values: .stride (by: .day)) { value in
// AxisValueLabel(format: .dateTime.weekday(),
// centered: true)
// }
// }
// }
// .frame(height: 500)
//
// Spacer()
// }
// .padding()
//
// PieChartView(
// values: [1300, 500, 6, 3],
// names: ["Rent", "Transport", "Education", "2dsad"],
// formatter: {value in String(format: "$%.2f", value)})
// PieChartView(values: valuesData, names: namesData, formatter: { value in String(format: "$%.2f", value)})
// }

@available(iOS 17.0, *)
var chartView: some View {
Chart(namesData.indices, id: \.self) { index in
SectorMark(
angle: .value("Count", valuesData[index]),
innerRadius: .ratio(0.6),
angularInset: 2
)
.cornerRadius(5)
.foregroundStyle(by: .value("Event", namesData[index]))
.annotation(position: .overlay, alignment: .center) {
VStack {
// Text(item.category)
// .font(.caption)
Text("\(valuesData[index], format: .number.precision(.fractionLength(0)))")
.font(.caption)
.foregroundColor(.white)
}
}
}
.scaledToFit()
.chartLegend(alignment: .center, spacing: 16)
.chartBackground { chartProxy in
GeometryReader { geometry in
if let anchor = chartProxy.plotFrame {
let frame = geometry[anchor]
Text("Events")
.position(x: frame.midX, y: frame.midY)
}
}
}
.padding(30)
}
}

0 comments on commit bf82388

Please sign in to comment.