Skip to content

Commit

Permalink
Working Assignment Groups
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulon12 committed Dec 19, 2024
1 parent 51d5ada commit cbb0ee0
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 34 deletions.
4 changes: 4 additions & 0 deletions CanvasPlusPlayground.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
B7F950392D1279A1004BB470 /* User.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7F950382D1279A1004BB470 /* User.swift */; };
B7F9503B2D127AD0004BB470 /* Profile.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7F9503A2D127AD0004BB470 /* Profile.swift */; };
B7F9503D2D12ADAE004BB470 /* Submission.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7F9503C2D12ADAE004BB470 /* Submission.swift */; };
B7F9503F2D133435004BB470 /* AssignmentGroup.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7F9503E2D133435004BB470 /* AssignmentGroup.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -164,6 +165,7 @@
B7F950382D1279A1004BB470 /* User.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = User.swift; sourceTree = "<group>"; };
B7F9503A2D127AD0004BB470 /* Profile.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Profile.swift; sourceTree = "<group>"; };
B7F9503C2D12ADAE004BB470 /* Submission.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Submission.swift; sourceTree = "<group>"; };
B7F9503E2D133435004BB470 /* AssignmentGroup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AssignmentGroup.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -393,6 +395,7 @@
A324BA602D079927005F53FA /* Plain */ = {
isa = PBXGroup;
children = (
B7F9503E2D133435004BB470 /* AssignmentGroup.swift */,
B7F9503A2D127AD0004BB470 /* Profile.swift */,
B7F950382D1279A1004BB470 /* User.swift */,
192EC0492C963B9000AF8528 /* Assignment.swift */,
Expand Down Expand Up @@ -587,6 +590,7 @@
A3049B5B2D0E5C18002F3166 /* QuizPermissions.swift in Sources */,
A3FFD03E2CE0065A006BAB51 /* NetworkError.swift in Sources */,
B53D95A22CA0A22A00647EE9 /* PeopleView.swift in Sources */,
B7F9503F2D133435004BB470 /* AssignmentGroup.swift in Sources */,
B76455042C8DF61B002DF00E /* CourseView.swift in Sources */,
A3049B682D0F3ECA002F3166 /* QuizzesViewModel.swift in Sources */,
9B92A4252C93856100C21CFC /* CourseAnnouncementManager.swift in Sources */,
Expand Down
19 changes: 15 additions & 4 deletions CanvasPlusPlayground/Common/Network/CanvasRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ enum CanvasRequest: Hashable {
case getAnnouncements(courseId: String, startDate: Date = .distantPast, endDate: Date = .now, perPage: String = "100")

case getAssignments(courseId: String)

case getAssignmentGroups(courseId: String)

case getEnrollments(courseId: String, perPage: String = "100")

case getQuizzes(courseId: String, searchTerm: String? = nil)
Expand Down Expand Up @@ -61,7 +62,10 @@ enum CanvasRequest: Hashable {

case let .getAssignments(courseId):
"courses/\(courseId)/assignments"


case let .getAssignmentGroups(courseId):
"courses/\(courseId)/assignment_groups"

case let .getEnrollments(courseId, _):
"courses/\(courseId)/enrollments"

Expand Down Expand Up @@ -99,8 +103,13 @@ enum CanvasRequest: Hashable {
[
("search_term", searchTerm)
]
case let .getAssignments(courseId):
case .getAssignments:
[
("include[]", "submission")
]
case .getAssignmentGroups:
[
("include[]", "assignments"),
("include[]", "submission")
]
default:
Expand All @@ -117,7 +126,7 @@ enum CanvasRequest: Hashable {
switch self {
case let .getCourse(id):
return id
case let .getTabs(courseId), let .getAnnouncements(courseId, _, _, _), let .getAssignments(courseId), let .getEnrollments(courseId, _), let .getAllCourseFiles(courseId), let .getAllCourseFolders(courseId), let .getQuizzes(courseId, _):
case let .getTabs(courseId), let .getAnnouncements(courseId, _, _, _), let .getAssignments(courseId), let .getAssignmentGroups(courseId), let .getEnrollments(courseId, _), let .getAllCourseFiles(courseId), let .getAllCourseFolders(courseId), let .getQuizzes(courseId,_):
return courseId
case let.getCourseRootFolder(courseId):
return "\(courseId)_root"
Expand Down Expand Up @@ -169,6 +178,8 @@ enum CanvasRequest: Hashable {
[Announcement].self
case .getAssignments:
[Assignment].self
case .getAssignmentGroups:
[AssignmentGroup].self
case .getEnrollments:
[Enrollment].self
case .getQuizzes:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,37 @@ import SwiftUI
@Observable
class CourseAssignmentManager {
private let courseID: String?
var assignments = [Assignment]()
var assignmentGroups: [AssignmentGroup] = []

init(courseID: String?) {
self.courseID = courseID
}

func fetchAssignments() async {
guard let courseID = courseID, let (data, _) = try? await CanvasService.shared.fetchResponse(.getAssignments(courseId: courseID)) else {
print("Failed to fetch assignments.")
func fetchAssignmentGroups() async {
guard let courseID = courseID, let (data, _) = try? await CanvasService.shared.fetchResponse(.getAssignmentGroups(courseId: courseID)) else {
print("Failed to fetch assignment groups.")
return
}

self.assignmentGroups = (try? JSONDecoder().decode([AssignmentGroup].self, from: data)) ?? []
}

static func getAssignmentsForCourse(courseID: String) async -> [Assignment] {
await CourseAssignmentManager.fetchAssignments(courseID: courseID)
}

private static func fetchAssignments(courseID: String) async -> [Assignment] {
guard let (data, _) = try? await CanvasService.shared.fetchResponse(.getAssignments(courseId: courseID)) else {
print("Failed to fetch assignments.")
return []
}

do {
self.assignments = try JSONDecoder().decode([Assignment].self, from: data)
return try JSONDecoder().decode([Assignment].self, from: data)
} catch {
print(error)
}
}

static func getAssignmentsForCourse(courseID: String) async -> [Assignment] {
let manager = CourseAssignmentManager(courseID: courseID)
await manager.fetchAssignments()
return manager.assignments

return []
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,20 @@ struct CourseAssignmentsView: View {
}

var body: some View {
List(assignmentManager.assignments, id: \.id) { assignment in
HStack {
VStack(alignment: .leading) {
Text(assignment.name ?? "")
.font(.headline)
Group {
if let submission = assignment.submission {
Text(
submission.workflowState?.rawValue.capitalized ?? "Unknown Status"
)
}
}
.font(.subheadline)
List(assignmentManager.assignmentGroups) { assignmentGroup in
Section {
ForEach(assignmentGroup.assignments ?? []) { assignment in
AssignmentRow(assignment: assignment, showGrades: showGrades)
}
} header: {
HStack {
Text(assignmentGroup.name ?? "")

if showGrades, let submission = assignment.submission {
Spacer()

Text(submission.score?.truncatingTrailingZeros ?? "--") +
Text("/") +
Text(assignment.pointsPossible?.truncatingTrailingZeros ?? "--")
if let groupWeight = assignmentGroup.groupWeight {
Text("\(groupWeight)%")
}
}
}
}
Expand All @@ -54,7 +47,37 @@ struct CourseAssignmentsView: View {

private func loadAssignments() async {
isLoadingAssignments = true
await assignmentManager.fetchAssignments()
await assignmentManager.fetchAssignmentGroups()
isLoadingAssignments = false
}
}

private struct AssignmentRow: View {
let assignment: Assignment
let showGrades: Bool

var body: some View {
HStack {
VStack(alignment: .leading) {
Text(assignment.name ?? "")
.font(.headline)
Group {
if let submission = assignment.submission {
Text(
submission.workflowState?.rawValue.capitalized ?? "Unknown Status"
)
}
}
.font(.subheadline)
}

if showGrades, let submission = assignment.submission {
Spacer()

Text(submission.score?.truncatingTrailingZeros ?? "--") +
Text("/") +
Text(assignment.pointsPossible?.truncatingTrailingZeros ?? "--")
}
}
}
}
2 changes: 1 addition & 1 deletion CanvasPlusPlayground/Schema/Plain/Assignment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ import Foundation
"restrict_quantitative_data":false}
*/

struct Assignment: Codable {
struct Assignment: Codable, Identifiable {
let id: Int
let description: String?
let dueAt: String?
Expand Down
42 changes: 42 additions & 0 deletions CanvasPlusPlayground/Schema/Plain/AssignmentGroup.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// AssignmentGroup.swift
// CanvasPlusPlayground
//
// Created by Rahul on 12/18/24.
//

import Foundation

struct AssignmentGroup: Codable, Identifiable {
let id: Int?
let name: String?
let position: Int?
let groupWeight: Int?
let sisSourceID: String?
let integrationData: [String: String]?
let assignments: [Assignment]?
let rules: GradingRules?

enum CodingKeys: String, CodingKey {
case id
case name
case position
case groupWeight = "group_weight"
case sisSourceID = "sis_source_id"
case integrationData = "integration_data"
case assignments
case rules
}
}

struct GradingRules: Codable {
let dropLowest: Int?
let dropHighest: Int?
let neverDrop: [Int]?

enum CodingKeys: String, CodingKey {
case dropLowest = "drop_lowest"
case dropHighest = "drop_highest"
case neverDrop = "never_drop"
}
}

0 comments on commit cbb0ee0

Please sign in to comment.