feat: show loading indicator when photo is being loaded

This commit is contained in:
dscyrescotti
2024-06-24 21:23:42 +07:00
parent cfa177f3f3
commit a2ed7c43bb
3 changed files with 9 additions and 1 deletions

View File

@@ -20,6 +20,7 @@ public class Tool: NSObject, ObservableObject {
@Published var draggedPen: Pen? @Published var draggedPen: Pen?
// MARK: - Photo // MARK: - Photo
@Published var selectedPhotoItem: PhotoItem? @Published var selectedPhotoItem: PhotoItem?
@Published var isLoadingPhoto: Bool = false
@Published var selection: ToolSelection = .hand @Published var selection: ToolSelection = .hand
@@ -120,6 +121,7 @@ public class Tool: NSObject, ObservableObject {
func selectPhoto(_ image: UIImage, for canvasID: NSManagedObjectID) { func selectPhoto(_ image: UIImage, for canvasID: NSManagedObjectID) {
guard let (resizedImage, dimension) = resizePhoto(of: image) else { return } guard let (resizedImage, dimension) = resizePhoto(of: image) else { return }
let photoItem = bookmarkPhoto(of: resizedImage, in: dimension, with: canvasID) let photoItem = bookmarkPhoto(of: resizedImage, in: dimension, with: canvasID)
isLoadingPhoto = false
withAnimation { withAnimation {
selectedPhotoItem = photoItem selectedPhotoItem = photoItem
} }

View File

@@ -46,7 +46,7 @@ struct MemoView: View {
.overlay(alignment: .bottomLeading) { .overlay(alignment: .bottomLeading) {
zoomControl zoomControl
} }
.disabled(textFieldState) .disabled(textFieldState || tool.isLoadingPhoto)
.overlay(alignment: .top) { .overlay(alignment: .top) {
Toolbar(size: size, memo: memo, tool: tool, canvas: canvas, history: history) Toolbar(size: size, memo: memo, tool: tool, canvas: canvas, history: history)
} }
@@ -61,6 +61,11 @@ struct MemoView: View {
EmptyView() EmptyView()
} }
} }
.overlay {
if tool.isLoadingPhoto {
loadingIndicator("Loading photo...")
}
}
} }
@ViewBuilder @ViewBuilder

View File

@@ -62,6 +62,7 @@ struct Toolbar: View {
.onChange(of: photosPickerItem) { oldValue, newValue in .onChange(of: photosPickerItem) { oldValue, newValue in
if newValue != nil { if newValue != nil {
Task { Task {
tool.isLoadingPhoto = true
let data = try? await newValue?.loadTransferable(type: Data.self) let data = try? await newValue?.loadTransferable(type: Data.self)
if let data, let image = UIImage(data: data) { if let data, let image = UIImage(data: data) {
tool.selectPhoto(image, for: canvas.canvasID) tool.selectPhoto(image, for: canvas.canvasID)