Skip to content

Commit

Permalink
Support dropping avatar images
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentTreguier committed Oct 1, 2024
1 parent f73d9e2 commit 63e0d2e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 56 deletions.
4 changes: 4 additions & 0 deletions Fyreplace.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
4D54C96D2BF4E9BE001DE071 /* ArchiveScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D54C96C2BF4E9BE001DE071 /* ArchiveScreen.swift */; };
4D54C96F2BF4E9DF001DE071 /* DraftsScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D54C96E2BF4E9DF001DE071 /* DraftsScreen.swift */; };
4D54C9712BF4EA15001DE071 /* SettingsScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D54C9702BF4EA15001DE071 /* SettingsScreen.swift */; };
4D6A24DD2CAC193F001B4435 /* EditableAvatar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D6A24DC2CAC192F001B4435 /* EditableAvatar.swift */; };
4D7F9CC22C259C30007B1CC3 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 4D7F9CC12C259C00007B1CC3 /* PrivacyInfo.xcprivacy */; };
4D9B3B382C334B3A00A8F7AD /* LoginScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D9B3B372C334B3A00A8F7AD /* LoginScreen.swift */; };
4D9B3B3A2C334B6300A8F7AD /* RegisterScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D9B3B392C334B6300A8F7AD /* RegisterScreen.swift */; };
Expand Down Expand Up @@ -142,6 +143,7 @@
4D54C96E2BF4E9DF001DE071 /* DraftsScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DraftsScreen.swift; sourceTree = "<group>"; };
4D54C9702BF4EA15001DE071 /* SettingsScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsScreen.swift; sourceTree = "<group>"; };
4D6641DB2C5B963500BE3D07 /* .ios-test-version */ = {isa = PBXFileReference; lastKnownFileType = text; path = ".ios-test-version"; sourceTree = "<group>"; };
4D6A24DC2CAC192F001B4435 /* EditableAvatar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditableAvatar.swift; sourceTree = "<group>"; };
4D7F9CC12C259C00007B1CC3 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
4D9B3B372C334B3A00A8F7AD /* LoginScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginScreen.swift; sourceTree = "<group>"; };
4D9B3B392C334B6300A8F7AD /* RegisterScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RegisterScreen.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -222,6 +224,7 @@
4D30DA5C2C986B6000499450 /* Components */ = {
isa = PBXGroup;
children = (
4D6A24DC2CAC192F001B4435 /* EditableAvatar.swift */,
4D30DA5E2C986B6C00499450 /* Avatar.swift */,
);
path = Components;
Expand Down Expand Up @@ -655,6 +658,7 @@
4DE785952C8B17AE000EC4E5 /* SubmitOrCancel.swift in Sources */,
4DE785882C88F392000EC4E5 /* HTTPField.swift in Sources */,
4D060BCB2C9438E8008C32D1 /* View.swift in Sources */,
4D6A24DD2CAC193F001B4435 /* EditableAvatar.swift in Sources */,
4D51F2802C621ADB0018E76E /* ViewProtocol.swift in Sources */,
4DC5B1D92C720B9800B75A07 /* AuthenticationMiddleware.swift in Sources */,
4D9B3B472C36F50300A8F7AD /* UITextContentType.swift in Sources */,
Expand Down
67 changes: 67 additions & 0 deletions Fyreplace/Views/Components/EditableAvatar.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import PhotosUI
import SwiftUI

struct EditableAvatar: View {
let user: Components.Schemas.User?

let avatarSelected: (Data) async -> Void

@EnvironmentObject
private var eventBus: EventBus

@State
private var showEditOverlay = false

@State
private var avatarItem: PhotosPickerItem?

var body: some View {
let opacity = showEditOverlay ? 1.0 : 0.0
let blurred = showEditOverlay
PhotosPicker(selection: $avatarItem) {
Avatar(user: user, blurred: blurred)
.overlay {
Image(systemName: "pencil")
.scaleEffect(2)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.padding()
.background(.black.opacity(0.5))
.foregroundStyle(.white)
.clipShape(.circle)
.opacity(opacity)
}
}
.animation(.default.speed(3), value: showEditOverlay)
.buttonStyle(.borderless)
.onHover { showEditOverlay = $0 }
.dropDestination(for: Data.self) { items, _ in
guard let data = items.first else { return false }

Task {
await avatarSelected(data)
}

return true
}
.onChange(of: avatarItem) { item in
guard let item else { return }

Task {
await usePhotoItem(item)
}
}
}

private func usePhotoItem(_ item: PhotosPickerItem) async {
if let data = try? await item.loadTransferable(type: Data.self) {
await avatarSelected(data)
} else {
eventBus.send(
.failure(
title: "Settings.Error.Image.Title",
text: "Settings.Error.Image.Message"
)
)
}
}
}
44 changes: 1 addition & 43 deletions Fyreplace/Views/Screens/SettingsScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ struct SettingsScreen: View, SettingsScreenProtocol {
}
} header: {
LogoHeader(namespace: namespace) {
PickableAvatar(user: currentUser) { item in
Task {
await updateAvatar(with: item)
}
}
EditableAvatar(user: currentUser, avatarSelected: updateAvatar)
} textContent: {
Text("Settings.Header")
}
Expand Down Expand Up @@ -83,41 +79,3 @@ private struct DateText: View {
}
}
}

private struct PickableAvatar: View {
let user: Components.Schemas.User?

let avatarSelected: (PhotosPickerItem) -> Void

@State
private var showEditOverlay = false

@State
private var avatarItem: PhotosPickerItem?

var body: some View {
let opacity = showEditOverlay ? 1.0 : 0.0
let blurred = showEditOverlay
PhotosPicker(selection: $avatarItem) {
Avatar(user: user, blurred: blurred)
.overlay {
Image(systemName: "pencil")
.scaleEffect(2)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.padding()
.background(.black.opacity(0.5))
.foregroundStyle(.white)
.clipShape(.circle)
.opacity(opacity)
}
}
.animation(.default.speed(3), value: showEditOverlay)
.buttonStyle(.borderless)
.onHover { showEditOverlay = $0 }
.onChange(of: avatarItem) {
if let item = $0 {
avatarSelected(item)
}
}
}
}
13 changes: 0 additions & 13 deletions Fyreplace/Views/Screens/SettingsScreenProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,6 @@ extension SettingsScreenProtocol {
}
}

func updateAvatar(with item: PhotosPickerItem) async {
if let data = try? await item.loadTransferable(type: Data.self) {
await updateAvatar(with: data)
} else {
eventBus.send(
.failure(
title: "Settings.Error.Image.Title",
text: "Settings.Error.Image.Message"
)
)
}
}

func updateAvatar(with data: Data) async {
await call {
let response = try await api.setCurrentUserAvatar(body: .binary(.init(data)))
Expand Down

0 comments on commit 63e0d2e

Please sign in to comment.