Skip to content

Commit

Permalink
fix: cannot preview course page on watchOS
Browse files Browse the repository at this point in the history
  • Loading branch information
fsy2001 committed Dec 3, 2024
1 parent 0bda2e6 commit 9c80868
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions FudanUI/Utils/Preview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ func setupPreview() async {
await CampusModel.shared.forceLogin(username: "123456", password: "")

let courseCacheURL = Bundle.module.url(forResource: "course", withExtension: "json", subdirectory: "Preview")!
#if os(watchOS)
try! Disk.save(data: try! Data(contentsOf: courseCacheURL), to: .applicationSupport, as: "preview/fdutools/course-model.json")
#else
try! Disk.save(try! Data(contentsOf: courseCacheURL), to: .applicationSupport, as: "preview/fdutools/course-model.json")
#endif

let walletLogs: [WalletLog] = decodePreviewData(filename: "wallet-logs", directory: "my")
let electricityLogs: [ElectricityLog] = decodePreviewData(filename: "electricity-logs", directory: "my")
Expand Down
12 changes: 12 additions & 0 deletions Utils/DiskForWatch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ public class Disk {
}
}

public static func save(data: Data, to directory: Directory, as path: String) throws {
if path.hasSuffix("/") {
throw NSError()
}
do {
let url = try createURL(for: path, in: directory)
try data.write(to: url, options: .atomic)
} catch {
throw error
}
}

public static func retrieve<T: Decodable>(_ path: String, from directory: Directory, as type: T.Type, decoder: JSONDecoder = JSONDecoder()) throws -> T {
if path.hasSuffix("/") {
throw NSError()
Expand Down

0 comments on commit 9c80868

Please sign in to comment.