Skip to content

Open source CapCut alternative.

TypeScript

feat(ios): profile photo in the avatar menu

David Le78adfbaf3ae6
diff --git a/apps/ios/Donkey/Controllers/AuthController.swift b/apps/ios/Donkey/Controllers/AuthController.swift
index f6ee0013..98d2a743 100644
--- a/apps/ios/Donkey/Controllers/AuthController.swift
+++ b/apps/ios/Donkey/Controllers/AuthController.swift
@@ -142,13 +142,20 @@ final class AuthController: NSObject, AuthServicing {
var id: String
var name: String?
var email: String
+ var image: String?
}
var user: User?
}
guard let user = try? JSONDecoder().decode(SessionResponse.self, from: data).user else {
throw AuthError.server("Your session has expired. Sign in again.")
}
- return UserProfile(id: user.id, name: user.name ?? "", email: user.email, superUser: await superUser)
+ return UserProfile(
+ id: user.id,
+ name: user.name ?? "",
+ email: user.email,
+ superUser: await superUser,
+ imageURL: user.image.flatMap(URL.init(string:))
+ )
}
/// The session payload carries identity only; /api/account/me carries the
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/AuthModel.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/AuthModel.swift
index aa959bdd..dbb8c0e4 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/AuthModel.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/AuthModel.swift
@@ -6,21 +6,27 @@ nonisolated public struct UserProfile: Equatable, Codable, Sendable {
public var email: String
/// Accounts with the operator role; gates the analytics dashboard.
public var superUser: Bool
+ /// The provider's profile photo (Google's picture claim); nil for Apple
+ /// accounts and pre-photo caches.
+ public var imageURL: URL?
- public init(id: String, name: String, email: String, superUser: Bool = false) {
+ public init(id: String, name: String, email: String, superUser: Bool = false, imageURL: URL? = nil) {
self.id = id
self.name = name
self.email = email
self.superUser = superUser
+ self.imageURL = imageURL
}
- /// Profiles cached before the role field existed decode with it off.
+ /// Profiles cached before the role and photo fields existed decode with
+ /// them off.
public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(String.self, forKey: .id)
name = try container.decode(String.self, forKey: .name)
email = try container.decode(String.self, forKey: .email)
superUser = try container.decodeIfPresent(Bool.self, forKey: .superUser) ?? false
+ imageURL = try container.decodeIfPresent(URL.self, forKey: .imageURL)
}
public var initial: String {
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/Components.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/Components.swift
index e6f59f6e..a7355137 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/Components.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/Components.swift
@@ -27,39 +27,60 @@ struct AvatarMenu: View {
var body: some View {
Menu {
- if auth.user?.superUser == true {
- Button("Analytics", systemImage: "chart.xyaxis.line") {
- app.showsAnalytics = true
- }
- Divider()
- }
- Picker("Appearance", selection: $app.appearance) {
+ Picker(selection: $app.appearance) {
Text("System").tag(AppearancePreference.system)
Text("Light").tag(AppearancePreference.light)
Text("Dark").tag(AppearancePreference.dark)
+ } label: {
+ Label("Appearance", systemImage: "circle.lefthalf.filled")
}
.pickerStyle(.menu)
+ if auth.user?.superUser == true {
+ Button("Analytics", systemImage: "chart.xyaxis.line") {
+ app.showsAnalytics = true
+ }
+ }
Divider()
Button("Log Out", systemImage: "rectangle.portrait.and.arrow.right", role: .destructive) {
Task { await auth.signOut() }
}
.tint(.red)
} label: {
- Circle()
- .fill(LinearGradient(
- colors: [Color(hex: "#7b8cff"), Color(hex: "#d86bd1")],
- startPoint: .topLeading,
- endPoint: .bottomTrailing
- ))
+ avatar
.frame(width: 34, height: 34)
- .overlay {
- Text(auth.user?.initial ?? "?")
- .font(.footnote.weight(.bold))
- .foregroundStyle(.white)
- }
+ .clipShape(Circle())
}
.accessibilityLabel("Profile")
}
+
+ /// The account's profile photo when the provider supplied one, with the
+ /// initial monogram standing in while it loads or when there is none.
+ @ViewBuilder
+ private var avatar: some View {
+ if let url = auth.user?.imageURL {
+ AsyncImage(url: url) { image in
+ image.resizable().scaledToFill()
+ } placeholder: {
+ monogram
+ }
+ } else {
+ monogram
+ }
+ }
+
+ private var monogram: some View {
+ Circle()
+ .fill(LinearGradient(
+ colors: [Color(hex: "#7b8cff"), Color(hex: "#d86bd1")],
+ startPoint: .topLeading,
+ endPoint: .bottomTrailing
+ ))
+ .overlay {
+ Text(auth.user?.initial ?? "?")
+ .font(.footnote.weight(.bold))
+ .foregroundStyle(.white)
+ }
+ }
}
struct EmptyState: View {
diff --git a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift
index f14c2231..9a3dc3a1 100644
--- a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift
+++ b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift
@@ -596,4 +596,17 @@ import Testing
let decoded = try JSONDecoder().decode(UserProfile.self, from: JSONEncoder().encode(profile))
#expect(decoded.superUser)
}
+
+ @Test func profileCachedBeforePhotoFieldDecodesWithoutOne() throws {
+ let data = Data(#"{"id":"u1","name":"Dana","email":"[email protected]"}"#.utf8)
+ let profile = try JSONDecoder().decode(UserProfile.self, from: data)
+ #expect(profile.imageURL == nil)
+ }
+
+ @Test func photoRoundTrips() throws {
+ let url = URL(string: "https://lh3.googleusercontent.com/a/photo")
+ let profile = UserProfile(id: "u1", name: "Dana", email: "[email protected]", imageURL: url)
+ let decoded = try JSONDecoder().decode(UserProfile.self, from: JSONEncoder().encode(profile))
+ #expect(decoded.imageURL == url)
+ }
}