Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Network Layer Revamped #147

Merged
merged 31 commits into from
Dec 24, 2024
Merged
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
3f8fc00
fixed: enrollments was set off main thread
azooz2003-bit Dec 13, 2024
2cff85c
new gradesVM
azooz2003-bit Dec 13, 2024
50939b4
make sync discardable
azooz2003-bit Dec 13, 2024
ed93daa
move grades logic to different VM
azooz2003-bit Dec 13, 2024
1ff1aad
announcement and course sets moved to main
azooz2003-bit Dec 13, 2024
4d6f2ab
wrong query made
azooz2003-bit Dec 14, 2024
c01452f
rename getPeople -> getEnrollments to resolve ambiguities
azooz2003-bit Dec 14, 2024
436a23e
quiz schema half done
azooz2003-bit Dec 15, 2024
5a89e3b
quiz model complete
azooz2003-bit Dec 15, 2024
689aa1f
added getQuizzes to service layer
azooz2003-bit Dec 15, 2024
5beecbc
add quiz to repository
azooz2003-bit Dec 15, 2024
b83a47b
optionalized some properties, quizzes working
azooz2003-bit Dec 15, 2024
75deec6
fixed array storage failure
azooz2003-bit Dec 15, 2024
e6c258f
created temporary list view
azooz2003-bit Dec 15, 2024
e5b909c
use all_quizzes instead of quizzes for request
azooz2003-bit Dec 16, 2024
a1f0952
title as fallback for quizzes sort
azooz2003-bit Dec 16, 2024
4654551
remove print
azooz2003-bit Dec 16, 2024
46170df
attempt 1 to change parent
azooz2003-bit Dec 17, 2024
1e92ef6
protocol made
azooz2003-bit Dec 18, 2024
7f0b7a1
handle nil values
azooz2003-bit Dec 20, 2024
6b4827d
all api requests recreated
azooz2003-bit Dec 20, 2024
0b72b64
fix quiz predicate compile error, hide enrollment predicate for now
azooz2003-bit Dec 20, 2024
53e3167
predicates complete
azooz2003-bit Dec 21, 2024
b22518f
inits for all reqs
azooz2003-bit Dec 21, 2024
d35eb29
revamped network layer complete! more type safe + models can accomoda…
azooz2003-bit Dec 21, 2024
1393ce2
remove redundancies
azooz2003-bit Dec 21, 2024
d6cff4e
canvas request renamed
azooz2003-bit Dec 21, 2024
4785f35
pull
azooz2003-bit Dec 21, 2024
bcdeffb
fixed profile fetch
azooz2003-bit Dec 22, 2024
f6e5f2b
used userId for commonPeople, removed redundant coalescing, repositor…
azooz2003-bit Dec 22, 2024
f6d09bc
made tabs class oops
azooz2003-bit Dec 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
used userId for commonPeople, removed redundant coalescing, repositor…
…y setup done via init
  • Loading branch information
azooz2003-bit committed Dec 22, 2024
commit f6e5f2b160e508cc8f0c43966159336d7ed599e6
1 change: 1 addition & 0 deletions CanvasPlusPlayground/CanvasPlusPlaygroundApp.swift
Original file line number Diff line number Diff line change
@@ -26,5 +26,6 @@ struct CanvasPlusPlaygroundApp: App {

init() {
print(URL.applicationSupportDirectory.path(percentEncoded: false))
CanvasService.shared.activate()
}
}
4 changes: 2 additions & 2 deletions CanvasPlusPlayground/Common/Network/CanvasRequest.swift
Original file line number Diff line number Diff line change
@@ -40,8 +40,8 @@ struct CanvasRequest {
GetAssignmentsRequest(courseId: courseId)
}

static func getEnrollments(courseId: String, perPage: Int = 50) -> GetEnrollmentsRequest {
GetEnrollmentsRequest(courseId: courseId, perPage: perPage)
static func getEnrollments(courseId: String, userId: Int? = nil, perPage: Int = 50) -> GetEnrollmentsRequest {
GetEnrollmentsRequest(courseId: courseId, userId: userId?.asString, perPage: perPage)
}

static func getQuizzes(courseId: String, searchTerm: String? = nil) -> GetQuizzesRequest {
33 changes: 14 additions & 19 deletions CanvasPlusPlayground/Common/Network/CanvasService.swift
Original file line number Diff line number Diff line change
@@ -8,31 +8,21 @@
import Foundation
import SwiftData

struct CanvasService {
class CanvasService {
static var shared = CanvasService()

let repository: CanvasRepository
var repository: CanvasRepository?

init() {
var repo: CanvasRepository?
let semaphore = DispatchSemaphore(value: 0)

Task.detached {
let initializedRepo = CanvasRepository()
repo = initializedRepo
semaphore.signal()
}

semaphore.wait()

guard let repo else {
preconditionFailure("Error initializing CanvasRepository.")
func activate() {
Task.detached { [weak self] in
let repo = CanvasRepository()
self?.repository = repo
}
self.repository = repo
}

/// Only loads from storage, doesn't make a network call
func load<Request: CacheableAPIRequest>(_ request: Request) async throws -> [Request.Subject]? {
guard let repository else { return nil }

// Get cached data for this type then filter to only get models related to `request`
let cached: [Request.Subject]? = try await request.load(from: repository)
@@ -42,6 +32,8 @@ struct CanvasService {

/// Number of occurences of models related to request
func loadCount<Request: CacheableAPIRequest>(_ request: Request) async throws -> Int {
guard let repository else { return 0 }

return try await request.loadCount(from: repository)
}

@@ -50,7 +42,9 @@ struct CanvasService {
_ request: Request,
onNewBatch: ([Request.Subject]) -> Void = { _ in }
) async throws -> [Request.Subject] {
// Call syncWithAPI for cacheable requests
guard let repository else { return [] }

// Call for cacheable requests
let result = try await request.syncWithAPI(to: repository, onNewBatch: onNewBatch)
print("Synced: \(result)")
return result
@@ -70,6 +64,7 @@ struct CanvasService {
onCacheReceive: ([Request.Subject]?) -> Void = { _ in },
onNewBatch: ([Request.Subject]) -> Void = { _ in }
) async throws -> [Request.Subject] {
guard let repository else { return [] }

let cached: [Request.Subject]? = try await request.load(from: repository)
onCacheReceive(cached) // Share cached version with caller.
@@ -92,7 +87,7 @@ struct CanvasService {
// MARK: Repository actions

func clearStorage() {
repository.modelContainer.deleteAllData()
repository?.modelContainer.deleteAllData()
}

private func filterByDescriptor<T>(
2 changes: 1 addition & 1 deletion CanvasPlusPlayground/Common/Storage/Cacheable.swift
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ protocol Cacheable: Codable, PersistentModel {
extension Cacheable {

func update<V>(keypath: ReferenceWritableKeyPath<Self, V>, value: V) async {
await CanvasService.shared.repository.update(model: self, keypath: keypath, value: value)
await CanvasService.shared.repository?.update(model: self, keypath: keypath, value: value)
}

func hash(into hasher: inout Hasher) {
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ struct AggregatedAssignmentsListCell: View {

var body: some View {
VStack(alignment: .leading) {
Text(assignment.name ?? "Missing name")
Text(assignment.name)

Text("\(course.displayName)")
.font(.subheadline)
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ struct CourseAssignmentsView: View {
List(assignmentManager.assignments, id: \.id) { assignment in
HStack {
VStack(alignment: .leading) {
Text(assignment.name ?? "")
Text(assignment.name)
.font(.headline)
Group {
if let submission = assignment.submission {
2 changes: 1 addition & 1 deletion CanvasPlusPlayground/Features/Files/FileType.swift
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ enum FileType {

/// Checks the filename for file format, returns nil if File format isn't supported or format was uninferable.
static func fromFile(_ file: File) -> FileType? {
let fileExtension = URL(fileURLWithPath: file.filename ?? "").pathExtension.lowercased()
let fileExtension = URL(fileURLWithPath: file.filename).pathExtension.lowercased()

switch fileExtension {
case "latex": return .latex
2 changes: 1 addition & 1 deletion CanvasPlusPlayground/Features/Files/FoldersPageView.swift
Original file line number Diff line number Diff line change
@@ -53,7 +53,7 @@ struct FoldersPageView: View {
func FileRow(for file: File) -> some View {
if file.url != nil {
NavigationLink(destination: destination(for: file)) {
Label(file.displayName ?? "Couldn't find file name.", systemImage: "document")
Label(file.displayName, systemImage: "document")
}
} else {
Label("File not available.", systemImage: "document")
2 changes: 1 addition & 1 deletion CanvasPlusPlayground/Features/People/PeopleManager.swift
Original file line number Diff line number Diff line change
@@ -83,7 +83,7 @@ class PeopleManager {
for course in courses {
var didAlreadyAddCourse = false
let courseID = course.id
let request = CanvasRequest.getEnrollments(courseId: courseID)
let request = CanvasRequest.getEnrollments(courseId: courseID, userId: userID)

func processEnrollments(_ enrollments: [Enrollment]) {
guard !didAlreadyAddCourse else { return }
1 change: 0 additions & 1 deletion CanvasPlusPlayground/Features/People/PeopleView.swift
Original file line number Diff line number Diff line change
@@ -92,7 +92,6 @@ struct PeopleView: View {
}

private var displayedUsers: [User] {
var result = peopleManager.users

return peopleManager.users.filter { user in
let matchesSearchText = searchText.isEmpty || user.name?.localizedCaseInsensitiveContains(searchText) ?? true
2 changes: 1 addition & 1 deletion CanvasPlusPlayground/Features/Quiz/CourseQuizzesView.swift
Original file line number Diff line number Diff line change
@@ -44,7 +44,7 @@ struct CourseQuizzesView: View {
func quizCell(for quiz: Quiz) -> some View {
HStack {
VStack {
Text(quiz.title ?? "No Title")
Text(quiz.title)
.bold()
.frame(maxWidth: .infinity, alignment: .leading)

2 changes: 1 addition & 1 deletion CanvasPlusPlayground/Features/Quiz/QuizzesViewModel.swift
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ class QuizzesViewModel {
return unsorted.mapValues {
$0.sorted {
if $0.dueAt == $1.dueAt {
$0.title ?? "" < $1.title ?? ""
$0.title < $1.title
} else { ($0.dueAt) < $1.dueAt }
}
}