Skip to content

Commit

Permalink
[#229] ImageViewer 이미지 저장 버튼 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
00yhsp committed Nov 28, 2024
1 parent 0d2aeca commit 68a05f0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
46 changes: 38 additions & 8 deletions AGAMI/Sources/Presentation/View/Components/ImageViewerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,26 @@
//

import SwiftUI
import PhotosUI

struct ImageViewerView: View {
@Environment(PlakeCoordinator.self) private var coordinator
@State private var image: UIImage?

@State private var isDownloading = false
let urlString: String

var body: some View {
VStack {
Spacer()
if let image = image {
PinchableImageView(image: image)
} else {
ProgressView()
ZStack {
VStack {
Spacer()
if let image = image {
PinchableImageView(image: image)
} else {
ProgressView("다운로드 중...")
}
Spacer()
}
Spacer()
if isDownloading { ProgressView("다운로드 중...") }
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color(.sBlack))
Expand All @@ -35,6 +39,16 @@ struct ImageViewerView: View {
.foregroundStyle(Color(.sMain))
}
}
ToolbarItem(placement: .topBarTrailing) {
if let image = image {
Button {
Task { await savePhotoToAlbum() }
} label: {
Image(systemName: "square.and.arrow.down")
.foregroundStyle(Color(.sMain))
}
}
}
}
.onAppear(perform: loadImage)
}
Expand All @@ -48,6 +62,22 @@ struct ImageViewerView: View {
await MainActor.run { self.image = image }
}
}

private func savePhotoToAlbum() async {
isDownloading = true
defer { isDownloading = false }

let status = await PHPhotoLibrary.requestAuthorization(for: .addOnly)
guard let image = image,
status == .authorized || status == .limited
else { return }

await MainActor.run {
PHPhotoLibrary.shared().performChanges {
PHAssetChangeRequest.creationRequestForAsset(from: image)
}
}
}
}

private struct PinchableImageView: UIViewRepresentable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct PlakePlaylistView: View {
} message: {
Text("삭제한 플레이크는 되돌릴 수 없습니다.")
}
.alert("게정 상태 문제", isPresented: $viewModel.presentationState.isShowingExportingAppleMusicFailedAlert) {
.alert("계정 상태 문제", isPresented: $viewModel.presentationState.isShowingExportingAppleMusicFailedAlert) {
ExportingFailedAlertActions(viewModel: viewModel)
} message: {
Text("플레이크를 내보낼 수 없습니다.\n Apple Music의 계정 상태를 확인해주세요.")
Expand Down

0 comments on commit 68a05f0

Please sign in to comment.