From c490a9acb0cf490b84e0c6116c49e9d124fe96aa Mon Sep 17 00:00:00 2001 From: Suhail Saqan Date: Tue, 19 Sep 2023 13:38:41 -0700 Subject: [PATCH] change the camera view to the new custom camera change the camera view to the new custom camera --- damus.xcodeproj/project.pbxproj | 2 +- .../xcshareddata/swiftpm/Package.resolved | 39 ++++ damus/Views/Camera/CameraView.swift | 212 ++++++++++++++++++ damus/Views/PostView.swift | 42 ++-- 4 files changed, 274 insertions(+), 21 deletions(-) create mode 100644 damus.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved create mode 100644 damus/Views/Camera/CameraView.swift diff --git a/damus.xcodeproj/project.pbxproj b/damus.xcodeproj/project.pbxproj index 3d922da936..8544211c65 100644 --- a/damus.xcodeproj/project.pbxproj +++ b/damus.xcodeproj/project.pbxproj @@ -2938,8 +2938,8 @@ 3169CAED294FCCFC00EE4006 /* Constants.swift in Sources */, 4C9AA14A2A4587A6003F49FD /* NotificationStatusModel.swift in Sources */, 4CB9D4A72992D02B00A9A7E4 /* ProfileNameView.swift in Sources */, - 4CE4F0F429D779B5005914DB /* PostBox.swift in Sources */, BA37598E2ABCCE500018D73B /* VideoCaptureProcessor.swift in Sources */, + 4CE4F0F429D779B5005914DB /* PostBox.swift in Sources */, 4C9B0DF32A65C46800CBDA21 /* ProfileEditButton.swift in Sources */, 4C32B95F2A9AD44700DC3548 /* Enum.swift in Sources */, 4C2859622A12A7F0004746F7 /* GoldSupportGradient.swift in Sources */, diff --git a/damus.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/damus.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved new file mode 100644 index 0000000000..c8409240c6 --- /dev/null +++ b/damus.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -0,0 +1,39 @@ +{ + "pins" : [ + { + "identity" : "gsplayer", + "kind" : "remoteSourceControl", + "location" : "https://github.com/wxxsw/GSPlayer", + "state" : { + "revision" : "aa6dad7943d52f5207f7fcc2ad3e4274583443b8", + "version" : "0.2.26" + } + }, + { + "identity" : "kingfisher", + "kind" : "remoteSourceControl", + "location" : "https://github.com/onevcat/Kingfisher", + "state" : { + "revision" : "415b1d97fb38bda1e5a6b2dde63354720832110b", + "version" : "7.6.1" + } + }, + { + "identity" : "secp256k1.swift", + "kind" : "remoteSourceControl", + "location" : "https://github.com/jb55/secp256k1.swift", + "state" : { + "revision" : "40b4b38b3b1c83f7088c76189a742870e0ca06a9" + } + }, + { + "identity" : "swift-markdown-ui", + "kind" : "remoteSourceControl", + "location" : "https://github.com/damus-io/swift-markdown-ui", + "state" : { + "revision" : "76bb7971da7fbf429de1c84f1244adf657242fee" + } + } + ], + "version" : 2 +} diff --git a/damus/Views/Camera/CameraView.swift b/damus/Views/Camera/CameraView.swift new file mode 100644 index 0000000000..c021bb710c --- /dev/null +++ b/damus/Views/Camera/CameraView.swift @@ -0,0 +1,212 @@ +// +// CameraView.swift +// damus +// +// Created by Suhail Saqan on 8/5/23. +// + +import SwiftUI +import Combine +import AVFoundation + +struct CameraView: View { + let damus_state: DamusState + let action: (([MediaItem]) -> Void) + + @Environment(\.presentationMode) var presentationMode + + @StateObject var model: CameraModel + + @State var currentZoomFactor: CGFloat = 1.0 + + public init(damus_state: DamusState, action: @escaping (([MediaItem]) -> Void)) { + self.damus_state = damus_state + self.action = action + _model = StateObject(wrappedValue: CameraModel()) + } + + var captureButton: some View { + Button { + if model.isRecording { + withAnimation { + model.stopRecording() + } + } else { + withAnimation { + model.capturePhoto() + } + } + UIImpactFeedbackGenerator(style: .medium).impactOccurred() + } label: { + ZStack { + Circle() + .fill( model.isRecording ? .red : DamusColors.black) + .frame(width: model.isRecording ? 85 : 65, height: model.isRecording ? 85 : 65, alignment: .center) + + Circle() + .stroke( model.isRecording ? .red : DamusColors.white, lineWidth: 4) + .frame(width: model.isRecording ? 95 : 75, height: model.isRecording ? 95 : 75, alignment: .center) + } + .frame(alignment: .center) + } + .simultaneousGesture( + LongPressGesture(minimumDuration: 0.5).onEnded({ value in + if (!model.isCameraButtonDisabled) { + withAnimation { + model.startRecording() + model.captureMode = .video + } + } + }) + ) + .buttonStyle(.plain) + } + + var capturedPhotoThumbnail: some View { + ZStack { + if model.thumbnail != nil { + Image(uiImage: model.thumbnail.thumbnailImage!) + .resizable() + .aspectRatio(contentMode: .fill) + .frame(width: 60, height: 60) + .clipShape(RoundedRectangle(cornerRadius: 10, style: .continuous)) + } + if model.isPhotoProcessing { + ProgressView() + .progressViewStyle(CircularProgressViewStyle(tint: DamusColors.white)) + } + } + } + + var closeButton: some View { + Button { + presentationMode.wrappedValue.dismiss() + model.stop() + } label: { + HStack { + Image(systemName: "xmark") + .font(.system(size: 24)) + } + .frame(minWidth: 40, minHeight: 40) + } + .accentColor(DamusColors.white) + } + + var flipCameraButton: some View { + Button(action: { + model.flipCamera() + }, label: { + HStack { + Image(systemName: "camera.rotate.fill") + .font(.system(size: 20)) + } + .frame(minWidth: 40, minHeight: 40) + }) + .accentColor(DamusColors.white) + } + + var toggleFlashButton: some View { + Button(action: { + model.switchFlash() + }, label: { + HStack { + Image(systemName: model.isFlashOn ? "bolt.fill" : "bolt.slash.fill") + .font(.system(size: 20)) + } + .frame(minWidth: 40, minHeight: 40) + }) + .accentColor(model.isFlashOn ? .yellow : DamusColors.white) + } + + var body: some View { + NavigationView { + GeometryReader { reader in + ZStack { + DamusColors.black.edgesIgnoringSafeArea(.all) + + CameraPreview(session: model.session) + .padding(.bottom, 175) + .edgesIgnoringSafeArea(.all) + .gesture( + DragGesture().onChanged({ (val) in + if abs(val.translation.height) > abs(val.translation.width) { + let percentage: CGFloat = -(val.translation.height / reader.size.height) + let calc = currentZoomFactor + percentage + let zoomFactor: CGFloat = min(max(calc, 1), 5) + + currentZoomFactor = zoomFactor + model.zoom(with: zoomFactor) + } + }) + ) + .onAppear { + model.configure() + } + .alert(isPresented: $model.showAlertError, content: { + Alert(title: Text(model.alertError.title), message: Text(model.alertError.message), dismissButton: .default(Text(model.alertError.primaryButtonTitle), action: { + model.alertError.primaryAction?() + })) + }) + .overlay( + Group { + if model.willCapturePhoto { + Color.black + } + } + ) + + VStack { + if !model.isRecording { + HStack { + closeButton + + Spacer() + + HStack { + flipCameraButton + toggleFlashButton + } + } + .padding(.horizontal, 20) + } + + Spacer() + + HStack(alignment: .center) { + if !model.mediaItems.isEmpty { + NavigationLink(destination: Text(model.mediaItems.map { $0.url.absoluteString }.joined(separator: ", "))) { + capturedPhotoThumbnail + } + .frame(width: 100, alignment: .leading) + } + + Spacer() + + captureButton + + Spacer() + + if !model.mediaItems.isEmpty { + Button(action: { + action(model.mediaItems) + presentationMode.wrappedValue.dismiss() + model.stop() + }) { + Text("Upload") + .frame(width: 100, height: 40, alignment: .center) + .foregroundColor(DamusColors.white) + .overlay { + RoundedRectangle(cornerRadius: 24) + .stroke(DamusColors.white, lineWidth: 2) + } + } + } + } + .frame(height: 100) + .padding([.horizontal, .vertical], 20) + } + } + } + } + } +} diff --git a/damus/Views/PostView.swift b/damus/Views/PostView.swift index ffcc300307..e07e0c9324 100644 --- a/damus/Views/PostView.swift +++ b/damus/Views/PostView.swift @@ -58,7 +58,7 @@ struct PostView: View { @State var newCursorIndex: Int? @State var textHeight: CGFloat? = nil - @State var mediaToUpload: MediaUpload? = nil + @State var mediaToUpload: [MediaUpload] = [] @StateObject var image_upload: ImageUploadModel = ImageUploadModel() @StateObject var tagModel: TagModel = TagModel() @@ -343,6 +343,15 @@ struct PostView: View { pks.append(pk) } } + + func addToMediaToUpload(mediaItem: MediaItem) { + switch mediaItem.type { + case .image: + mediaToUpload.append(.image(mediaItem.url)) + case .video: + mediaToUpload.append(.video(mediaItem.url)) + } + } var body: some View { GeometryReader { (deviceSize: GeometryProxy) in @@ -384,36 +393,29 @@ struct PostView: View { } .sheet(isPresented: $attach_media) { ImagePicker(uploader: damus_state.settings.default_media_uploader, sourceType: .photoLibrary, pubkey: damus_state.pubkey, image_upload_confirm: $image_upload_confirm) { img in - self.mediaToUpload = .image(img) + self.mediaToUpload.append(.image(img)) } onVideoPicked: { url in - self.mediaToUpload = .video(url) + self.mediaToUpload.append(.video(url)) } .alert(NSLocalizedString("Are you sure you want to upload this media?", comment: "Alert message asking if the user wants to upload media."), isPresented: $image_upload_confirm) { Button(NSLocalizedString("Upload", comment: "Button to proceed with uploading."), role: .none) { - if let mediaToUpload { - self.handle_upload(media: mediaToUpload) + if !mediaToUpload.isEmpty { + self.handle_upload(media: mediaToUpload[0]) self.attach_media = false } } Button(NSLocalizedString("Cancel", comment: "Button to cancel the upload."), role: .cancel) {} } } - .sheet(isPresented: $attach_camera) { - - ImagePicker(uploader: damus_state.settings.default_media_uploader, sourceType: .camera, pubkey: damus_state.pubkey, image_upload_confirm: $image_upload_confirm) { img in - self.mediaToUpload = .image(img) - } onVideoPicked: { url in - self.mediaToUpload = .video(url) - } - .alert(NSLocalizedString("Are you sure you want to upload this media?", comment: "Alert message asking if the user wants to upload media."), isPresented: $image_upload_confirm) { - Button(NSLocalizedString("Upload", comment: "Button to proceed with uploading."), role: .none) { - if let mediaToUpload { - self.handle_upload(media: mediaToUpload) - self.attach_camera = false - } + .fullScreenCover(isPresented: $attach_camera) { + CameraView(damus_state: damus_state, action: { items in + for item in items { + addToMediaToUpload(mediaItem: item) } - Button(NSLocalizedString("Cancel", comment: "Button to cancel the upload."), role: .cancel) {} - } + for media in mediaToUpload { + self.handle_upload(media: media) + } + }) } .onAppear() { let loaded_draft = load_draft()