Skip to content

Commit

Permalink
Merge pull request #59 from AKORA-Studios/Exam-Widget
Browse files Browse the repository at this point in the history
Exam widget
  • Loading branch information
DragonCat4012 authored Nov 9, 2023
2 parents d5bfc88 + bf921bc commit 265d6b7
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 5 deletions.
6 changes: 6 additions & 0 deletions Calq.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
F1AAEF292A1BC1E5000CD044 /* Mapping Model 1.xcmappingmodel in Sources */ = {isa = PBXBuildFile; fileRef = F1AAEF282A1BC1E5000CD044 /* Mapping Model 1.xcmappingmodel */; };
F1AAEF302A1BDA01000CD044 /* LineChartBase.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1AAEF2F2A1BDA01000CD044 /* LineChartBase.swift */; };
F1AAEF312A1BDA01000CD044 /* LineChartBase.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1AAEF2F2A1BDA01000CD044 /* LineChartBase.swift */; };
F1ABCA362AE1AEF000AC58C2 /* ExamWidget.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1ABCA352AE1AEF000AC58C2 /* ExamWidget.swift */; };
F1ABCA372AE1B57D00AC58C2 /* ExamVM.swift in Sources */ = {isa = PBXBuildFile; fileRef = F10D66B3298D66C200993237 /* ExamVM.swift */; };
F1AF649329A3C7CC00BAE066 /* DoubleExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1AF649229A3C7CC00BAE066 /* DoubleExtension.swift */; };
F1B0B1682996489900F3D1FD /* EditGradeScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1B0B1672996489900F3D1FD /* EditGradeScreen.swift */; };
F1B0B16A299648A200F3D1FD /* EditSubjectScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = F1B0B169299648A200F3D1FD /* EditSubjectScreen.swift */; };
Expand Down Expand Up @@ -206,6 +208,7 @@
F1A772462A179617004B8DCC /* GradeType+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "GradeType+CoreDataProperties.swift"; sourceTree = SOURCE_ROOT; };
F1AAEF282A1BC1E5000CD044 /* Mapping Model 1.xcmappingmodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcmappingmodel; path = "Mapping Model 1.xcmappingmodel"; sourceTree = "<group>"; };
F1AAEF2F2A1BDA01000CD044 /* LineChartBase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LineChartBase.swift; sourceTree = "<group>"; };
F1ABCA352AE1AEF000AC58C2 /* ExamWidget.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExamWidget.swift; sourceTree = "<group>"; };
F1AF649229A3C7CC00BAE066 /* DoubleExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DoubleExtension.swift; sourceTree = "<group>"; };
F1B0B1672996489900F3D1FD /* EditGradeScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditGradeScreen.swift; sourceTree = "<group>"; };
F1B0B169299648A200F3D1FD /* EditSubjectScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditSubjectScreen.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -276,6 +279,7 @@
DD4F97A127515CAC00D3CC57 /* Info.plist */,
F1C04B5129BA376800DAC128 /* MediumBarChart.swift */,
F1C04B5329BA3E2D00DAC128 /* SmallCircleChart.swift */,
F1ABCA352AE1AEF000AC58C2 /* ExamWidget.swift */,
);
path = CalqWidget;
sourceTree = "<group>";
Expand Down Expand Up @@ -657,6 +661,7 @@
F1D29E7D2A17BCCB0001D469 /* LoadJSON.swift in Sources */,
F1A7724A2A179617004B8DCC /* GradeType+CoreDataProperties.swift in Sources */,
F1AAEF312A1BDA01000CD044 /* LineChartBase.swift in Sources */,
F1ABCA362AE1AEF000AC58C2 /* ExamWidget.swift in Sources */,
F1A7723C2A1795C6004B8DCC /* AppSettings+CoreDataProperties.swift in Sources */,
F1C04B5529BA3EAB00DAC128 /* LineChart.swift in Sources */,
F1A772402A1795C6004B8DCC /* UserTest+CoreDataProperties.swift in Sources */,
Expand All @@ -673,6 +678,7 @@
F1C04B5429BA3E2D00DAC128 /* SmallCircleChart.swift in Sources */,
F1A7723E2A1795C6004B8DCC /* UserTest+CoreDataClass.swift in Sources */,
DD4F979E27515CA800D3CC57 /* CalqWidget.swift in Sources */,
F1ABCA372AE1B57D00AC58C2 /* ExamVM.swift in Sources */,
F1A772422A1795C6004B8DCC /* UserSubject+CoreDataClass.swift in Sources */,
F1B292A02A3B6CD200AF2ACD /* GradeType+CoreDataClass.swift in Sources */,
DD4F97A827515CC600D3CC57 /* Util.swift in Sources */,
Expand Down
6 changes: 5 additions & 1 deletion Calq/de.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,9 @@
"LineChartWidget_Description"="Notenverlauf aller Fächer";
"BarChartWidget_DisplayName"="Balkendiagramm";
"BarChartWidget_Description"="Aktueller Durchschnitt aller Fächer";
"HalfyearBarChartWidgett_DisplayName"="Halbjahresdiagramm";
"HalfyearBarChartWidget_DisplayName"="Halbjahresdiagramm";
"HalfyearBarChartWidget_Description"="Durchshcnitt aller Halbjahre";
"ExamChartWidget_DisplayName"="Prüfungsansicht";
"ExamChartWidget_Description"="Zeigt alle Prüfungen und block 2";
"ExamChartWidget_DisplayTitle"="Prüfungsübersicht";
"ExamChartWidget_BlockTitle"="Blöcke";
6 changes: 5 additions & 1 deletion Calq/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,9 @@
"LineChartWidget_Description"="subject grades over time";
"BarChartWidget_DisplayName"="Bar Chart";
"BarChartWidget_Description"="current average for each subejct";
"HalfyearBarChartWidgett_DisplayName"="Halfyear Bar Chart";
"HalfyearBarChartWidget_DisplayName"="Halfyear Bar Chart";
"HalfyearBarChartWidget_Description"="Average of each subejct";
"ExamChartWidget_DisplayName"="Exam Chart";
"ExamChartWidget_Description"="Shows all exams + block 2";
"ExamChartWidget_DisplayTitle"="Exam Overview";
"ExamChartWidget_BlockTitle"="Blocks";
34 changes: 31 additions & 3 deletions CalqWidget/CalqWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ private struct Provider: TimelineProvider {
}

func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> Void) {
let entry = SimpleEntry(date: Date(), barChartData: BarChartEntry.example, halfyearbarChartData: BarChartEntry.exampleHalfyear, lineChartData: LineChartEntry.example, circleChartData: CircleChartData.example)
let entry = SimpleEntry(date: Date(), barChartData: BarChartEntry.example, halfyearbarChartData: BarChartEntry.exampleHalfyear, lineChartData: LineChartEntry.example, circleChartData: CircleChartData.example, examChartData: WidgetExamEntrys.example)
completion(entry)
}

func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
let timeline = Timeline(entries: [SimpleEntry(date: Date(), barChartData: BarChartEntry.getData(), halfyearbarChartData: BarChartEntry.getDataHalfyear(), lineChartData: LineChartEntry.getData(), circleChartData: circleChartData())], policy: .atEnd)
let timeline = Timeline(entries: [SimpleEntry(date: Date(), barChartData: BarChartEntry.getData(), halfyearbarChartData: BarChartEntry.getDataHalfyear(), lineChartData: LineChartEntry.getData(), circleChartData: circleChartData(), examChartData: WidgetExamEntrys.createWidgetEntrys())], policy: .atEnd)
completion(timeline)
}
}
Expand All @@ -32,6 +32,7 @@ private struct SimpleEntry: TimelineEntry {
var halfyearbarChartData: [BarChartEntry] = []
var lineChartData: [[LineChartEntry]] = []
var circleChartData: CircleChartData = CircleChartData(percent: 0, upperText: "?", lowerText: "?")
var examChartData: WidgetExamEntrys = WidgetExamEntrys(entrycount: 5, blockpoints2: 0, blockpoints1: 12, entries: [])
}

private struct CalqWidgetEntryView: View {
Expand Down Expand Up @@ -66,6 +67,14 @@ private struct CalqWidgetEntryView3: View {
}
}

private struct CalqWidgetEntryView4: View {
var entry: SimpleEntry

var body: some View {
ExamWidget(value: entry.examChartData)
}
}

struct CalqWidget: Widget {
let kind: String = "AverageWidget"

Expand Down Expand Up @@ -114,12 +123,26 @@ struct HalfyearBarChartWidget: Widget {
StaticConfiguration(kind: kind, provider: Provider()) { entry in
CalqWidgetEntryView3(entry: entry)
}
.configurationDisplayName("HalfyearBarChartWidgett_DisplayName")
.configurationDisplayName("HalfyearBarChartWidget_DisplayName")
.description("HalfyearBarChartWidget_Description")
.supportedFamilies([.systemMedium])
}
}

// MARK: Medium HalfyearBarChart Widget
struct ExamChartWidget: Widget {
let kind: String = "ExamChartWidget"

var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider()) { entry in
CalqWidgetEntryView4(entry: entry)
}
.configurationDisplayName("ExamChartWidget_DisplayName")
.description("ExamChartWidget_Description")
.supportedFamilies([.systemMedium])
}
}

// MARK: Bundle
@main
struct CalqWidgetBundle: WidgetBundle {
Expand All @@ -129,6 +152,7 @@ struct CalqWidgetBundle: WidgetBundle {
BarChartWidget()
LineChartWidget()
HalfyearBarChartWidget()
ExamChartWidget()
}
}

Expand All @@ -155,5 +179,9 @@ struct Widgets_Previews: PreviewProvider {
CalqWidgetEntryView(entry: SimpleEntry(date: Date()))
.previewContext(WidgetPreviewContext(family: .systemMedium))
.previewDisplayName("NoData")

CalqWidgetEntryView4(entry: SimpleEntry(date: Date(), examChartData: WidgetExamEntrys.example))
.previewContext(WidgetPreviewContext(family: .systemMedium))
.previewDisplayName("ExamChart")
}
}
140 changes: 140 additions & 0 deletions CalqWidget/ExamWidget.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
//
// ExamWidget.swift
// CalqWidgetExtension
//
// Created by Kiara on 16.19.23.
//

import SwiftUI

struct WidgetExamEntrys {
let entrycount: Int
let blockpoints2: Int
let blockpoints1: Int
let entries: [WidgetExamEntry]

static let example = WidgetExamEntrys(entrycount: 5, blockpoints2: 100, blockpoints1: 300, entries: WidgetExamEntry.example)

static func createWidgetEntrys() -> WidgetExamEntrys {
var arr: [WidgetExamEntry] = []
let blockpoints = generateBlockTwo()
let blockpoints1 = generateBlockOne()
let entrycount = Util.getSettings().hasFiveExams ? 5 : 4

for num in (1...entrycount) {
let subject = getExam(num)
guard let sub = subject else {
arr.append(WidgetExamEntry(points: 0, name: ""))
continue
}
arr.append(WidgetExamEntry(points: Int(sub.exampoints), color: getSubjectColor(sub), name: sub.name))
}
return WidgetExamEntrys(entrycount: entrycount, blockpoints2: blockpoints, blockpoints1: blockpoints1, entries: arr)
}
}

struct WidgetExamEntry: Identifiable {
var points: Int
var color: Color = .blue
var name: String
let id = UUID()

static let example = [
WidgetExamEntry(points: 12, name: "a"),
WidgetExamEntry(points: 1, name: "b"),
WidgetExamEntry(points: 6, name: "c"),
WidgetExamEntry(points: 3, name: "d"),
WidgetExamEntry(points: 0, name: "e")
]
}

struct ExamWidget: View {
var value: WidgetExamEntrys
private let gray = Color(.systemGray4)

var body: some View {
VStack {
if value.entries.isEmpty {
EmptyMediumView()
} else {
HStack {
Text("ExamChartWidget_DisplayTitle")
Spacer()
Text("ExamChartWidget_BlockTitle")
}.frame(height: 10)

HStack {
VStack {
ForEach(value.entries.indices) { index in
examView(index)
}
}
blockView().frame(width: 20)
blockView("II").frame(width: 20)
}
}
}.padding()
}

func blockView(_ name: String = "I") -> some View {
GeometryReader { geo in
let points = name == "I" ? value.blockpoints1 : value.blockpoints2
let maxPoints = name == "I" ? 600.0 : 300.0

let fullHeight = geo.size.height - 12
let percent = CGFloat(Double(points)/maxPoints)

VStack(spacing: 2) {
ZStack(alignment: .bottom) {
RoundedRectangle(cornerRadius: 8)
.fill(gray)
.frame(height: fullHeight)
RoundedRectangle(cornerRadius: 8)
.fill(Color.accentColor)
.frame(height: fullHeight * percent)
VStack(spacing: 0) {
Text("\(points)")
.rotationEffect(.degrees(-90))
.font(.footnote)
.frame(height: fullHeight, alignment: .bottomTrailing)
.fixedSize(horizontal: true, vertical: false)
Spacer()
}
}

Text(name)
.font(.footnote)
.foregroundColor(gray)
.frame(height: 10)
}
}
}

func examView(_ index: Int) -> some View {
GeometryReader { geo in
let exam = value.entries[index]
let fullWidth = geo.size.width - 12
let percent = CGFloat(Double(exam.points)/15.0)

HStack(spacing: 2) {
Text("\(index+1)")
.font(.footnote)
.foregroundColor(gray)
.frame(width: 10)
ZStack(alignment: .leading) {
RoundedRectangle(cornerRadius: 8)
.fill(gray)
.frame(width: fullWidth)
RoundedRectangle(cornerRadius: 8)
.fill(exam.color)
.frame(width: fullWidth * percent)
HStack {
Text(exam.name)
Spacer()
Text(String(exam.points))
}.padding(.horizontal)
}
}
}
}
}

0 comments on commit 265d6b7

Please sign in to comment.