Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
31 changes: 13 additions & 18 deletions PennMobile/Home/HomeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,30 +196,25 @@ extension Optional {

_ = await announcementsTask
async let wrappedTask = Task {
let url = URL(string: "https://pennmobile.org/api/wrapped/semester/2025S-public/")!
guard let req = try? await URLRequest(url: url, mode: .accessToken) else {
let url = URL(string: "https://pennmobile.org/api/wrapped/semester/current/")!
guard let req = try? await URLRequest(url: url, mode: .accessToken),
let (data, response) = try? await URLSession.shared.data(for: req),
let httpResponse = response as? HTTPURLResponse,
httpResponse.statusCode == 200 else {
DispatchQueue.main.async {
self.data.wrapped = .success(WrappedModel(semester: "", pages: []))
}
return
}
let task = URLSession.shared.dataTask(with: url) { data, response, _ in
guard let httpResponse = response as? HTTPURLResponse, let data, httpResponse.statusCode == 200 else {
DispatchQueue.main.async {
self.data.wrapped = .success(WrappedModel(semester: "", pages: []))
}
return
}
DispatchQueue.main.async {
do {
let wrapped = try JSONDecoder().decode(WrappedModel.self, from: data)
self.data.wrapped = .success(wrapped)
} catch {
self.data.wrapped = .failure(error)
}
}

DispatchQueue.main.async {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

await MainActor.run

Or just mark the entire Task as @MainActor

do {
let wrapped = try JSONDecoder().decode(WrappedModel.self, from: data)
self.data.wrapped = .success(wrapped)
} catch {
self.data.wrapped = .failure(error)
}
task.resume()
}
}

_ = await wrappedTask
Expand Down
93 changes: 0 additions & 93 deletions PennMobile/Wrapped/Fonts/Poppins/OFL.txt

This file was deleted.

Binary file removed PennMobile/Wrapped/Fonts/Poppins/Poppins-Black.ttf
Binary file not shown.
Binary file not shown.
Binary file removed PennMobile/Wrapped/Fonts/Poppins/Poppins-Bold.ttf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed PennMobile/Wrapped/Fonts/Poppins/Poppins-Italic.ttf
Binary file not shown.
Binary file removed PennMobile/Wrapped/Fonts/Poppins/Poppins-Light.ttf
Binary file not shown.
Binary file not shown.
Binary file removed PennMobile/Wrapped/Fonts/Poppins/Poppins-Medium.ttf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed PennMobile/Wrapped/Fonts/Poppins/Poppins-Thin.ttf
Binary file not shown.
Binary file not shown.
73 changes: 67 additions & 6 deletions PennMobile/Wrapped/WrappedModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,50 @@
//

import Foundation
import Lottie
import CoreText

public struct WrappedModel: Decodable {
let semester: String
// Designed to be optional for forwards compatability
// (making pages an optional field was a design discussion for disabling wrapped between semesters)
var pages: [WrappedUnit]
let fonts: [String: URL]

var fontProvider: WrappedFontProvider?

public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
self.semester = try values.decode(String.self, forKey: .semester)
self.pages = try values.decodeIfPresent([WrappedUnit].self, forKey: .pages) ?? []
self.fonts = try values.decodeIfPresent([String: URL].self, forKey: .fonts) ?? [:]
}

public init(semester: String, pages: [WrappedUnit]) {
public init(semester: String, pages: [WrappedUnit], fonts: [String: URL] = [:]) {
self.pages = pages
self.semester = semester
self.fonts = fonts
}

enum CodingKeys: String, CodingKey {
case pages, semester
case pages, semester, fonts
}

mutating func loadModel() async {
var newPages: [WrappedUnit] = await self.pages.asyncMap { page in
var newPage = page
await newPage.loadAnimation()
return newPage
let newPages = await withTaskGroup(of: WrappedUnit.self, returning: [WrappedUnit].self) { group in
for page in self.pages {
var newPage = page
group.addTask {
await newPage.loadAnimation()
return newPage
}
}

var results: [WrappedUnit] = []
for await result in group {
results.append(result)
}
return results
}

// Fail if duplicate ID (note: this silently fails)
Expand All @@ -47,5 +64,49 @@ public struct WrappedModel: Decodable {
}

self.pages = newPages.filter({ $0.lottie != nil }).sorted(by: { $0.id < $1.id })
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anli5005 This isn't what you mean but WrappedUnitView cannot display before we have a loaded model


let fontFiles: [String: Data] = await withTaskGroup(of: (String, Data?).self, returning: [String: Data].self) { group in
for (name, downloadURL) in self.fonts {
group.addTask {
let request = URLRequest(url: downloadURL)
guard let (localURL, response) = try? await URLSession.shared.download(for: request) else {
return (name, nil)
}
return (name, try? Data(contentsOf: localURL))
}
}

var results: [String: Data] = [:]
for await result in group {
guard result.1 != nil else { continue }
results[result.0] = result.1
}
return results
}

self.fontProvider = WrappedFontProvider(from: fontFiles)
}
}

class WrappedFontProvider: AnimationFontProvider, Equatable {
static func == (lhs: WrappedFontProvider, rhs: WrappedFontProvider) -> Bool {
lhs === rhs
}

let fonts: [String: CGFont]
init(from files: [String: Data]) {
var fonts: [String: CGFont] = [:]
for (name, data) in files {
if let provider = CGDataProvider(data: data as CFData) {
fonts[name] = CGFont(provider)
}
}
self.fonts = fonts
}

func fontFor(family: String, size: CGFloat) -> CTFont? {
guard let font = fonts[family] else { return nil }
let ctFont = CTFontCreateWithGraphicsFont(font, size, nil, nil)
return ctFont
}
}
5 changes: 2 additions & 3 deletions PennMobile/Wrapped/WrappedUnitView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ struct WrappedUnitView: View {

var body: some View {
// https://github.com/pennlabs/penn-mobile-ios/pull/602#discussion_r2070443421
// Note, if we get to this point, unit.lottie really should not be nil
// Note, if we get to this point, unit.lottie really should not be nil, neither should the fontProvider
let progressFraction = (CGFloat(vm.activeUnitProgress) * unit.time!) / unit.lottie!.duration
let normalizedProgress = progressFraction - floor(progressFraction)
GeometryReader { proxy in
LottieView(animation: unit.lottie!)
.textProvider(DictionaryTextProvider(unit.values))
// TODO: Define a custom font provider conforming class that fetches fonts dynamically from backend.
.fontProvider(DefaultFontProvider())
.fontProvider(experienceVM.model.fontProvider!)
Comment on lines +19 to +25
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of trusting that neither is nil here. Can we engineer this in a more type-safe way to eliminate the possibility of crashes like the one seen in #630?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the "trust me bro" guarantee, but yes I (or someone on iOS) can look into.

The thought here is that WrappedUnitView shouldn't even be showing for a given model if that model doesn't have a lottie animation loaded, same thing with the font provider. But there does exist a mechanism to guarantee, so you're correct in that regard.

.playbackMode(vm.activeUnit == unit ? vm.activeUnitPlaybackMode : .paused(at:.currentFrame))
.currentProgress(vm.activeUnit == unit ? normalizedProgress : 0)
.rotation3DEffect(
Expand Down