Skip to content

Commit 91fcc5b

Browse files
committed
change the camera view to the new custom camera
1 parent 889e870 commit 91fcc5b

File tree

3 files changed

+249
-23
lines changed

3 files changed

+249
-23
lines changed

damus.xcodeproj/project.pbxproj

+15-3
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,8 @@
426426
BA4AB0AE2A63B9270070A32A /* AddEmojiView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA4AB0AD2A63B9270070A32A /* AddEmojiView.swift */; };
427427
BA4AB0B02A63B94D0070A32A /* EmojiListItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA4AB0AF2A63B94D0070A32A /* EmojiListItemView.swift */; };
428428
BA693074295D649800ADDB87 /* UserSettingsStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA693073295D649800ADDB87 /* UserSettingsStore.swift */; };
429+
BA8A4F0F2A2D95F70045C48C /* CameraView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA8A4F0E2A2D95F70045C48C /* CameraView.swift */; };
430+
BA8A4F132A2D96AD0045C48C /* CameraPreview.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA8A4F122A2D96AD0045C48C /* CameraPreview.swift */; };
429431
BAB68BED29543FA3007BA466 /* SelectWalletView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAB68BEC29543FA3007BA466 /* SelectWalletView.swift */; };
430432
D2277EEA2A089BD5006C3807 /* Router.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2277EE92A089BD5006C3807 /* Router.swift */; };
431433
D71DC1EC2A9129C3006E207C /* PostViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D71DC1EB2A9129C3006E207C /* PostViewTests.swift */; };
@@ -1120,6 +1122,8 @@
11201122
BA4AB0AD2A63B9270070A32A /* AddEmojiView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddEmojiView.swift; sourceTree = "<group>"; };
11211123
BA4AB0AF2A63B94D0070A32A /* EmojiListItemView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmojiListItemView.swift; sourceTree = "<group>"; };
11221124
BA693073295D649800ADDB87 /* UserSettingsStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserSettingsStore.swift; sourceTree = "<group>"; };
1125+
BA8A4F0E2A2D95F70045C48C /* CameraView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CameraView.swift; sourceTree = "<group>"; };
1126+
BA8A4F122A2D96AD0045C48C /* CameraPreview.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CameraPreview.swift; sourceTree = "<group>"; };
11231127
BAB68BEC29543FA3007BA466 /* SelectWalletView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectWalletView.swift; sourceTree = "<group>"; };
11241128
D2277EE92A089BD5006C3807 /* Router.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Router.swift; sourceTree = "<group>"; };
11251129
D71DC1EB2A9129C3006E207C /* PostViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostViewTests.swift; sourceTree = "<group>"; };
@@ -1648,6 +1652,7 @@
16481652
BA3759952ABCCF360018D73B /* Camera */,
16491653
F71694E82A66221E001F4053 /* Onboarding */,
16501654
4C190F232A547D1700027FD5 /* NostrScript */,
1655+
BA8A4F0D2A2D95880045C48C /* Camera */,
16511656
4C7D09692A0AEA0400943473 /* CodeScanner */,
16521657
4C7D095A2A098C5C00943473 /* Wallet */,
16531658
4C8D1A6D29F31E4100ACDF75 /* Buttons */,
@@ -2576,6 +2581,7 @@
25762581
4C190F252A547D2000027FD5 /* LoadScript.swift in Sources */,
25772582
4C59B98C2A76C2550032FFEB /* ProfileUpdatedNotify.swift in Sources */,
25782583
4C363A8C28236B92006E126D /* PubkeyView.swift in Sources */,
2584+
BA3BF28A2A7F156B00600232 /* ImageResizer.swift in Sources */,
25792585
4CDA128A29E9D10C0006FA5A /* SignalView.swift in Sources */,
25802586
4C12535C2A76CA540004F4B8 /* LoginNotify.swift in Sources */,
25812587
4C5C7E68284ED36500A22DF5 /* SearchHomeModel.swift in Sources */,
@@ -2660,6 +2666,7 @@
26602666
4C30AC7629A5770900E2BD5A /* NotificationItemView.swift in Sources */,
26612667
BA3759972ABCCF360018D73B /* CameraPreview.swift in Sources */,
26622668
4C86F7C42A76C44C00EC0817 /* ZappingNotify.swift in Sources */,
2669+
BA27222E2A806E39004CDF52 /* VideoCaptureProcessor.swift in Sources */,
26632670
4C363A8428233689006E126D /* Parser.swift in Sources */,
26642671
3AAA95CA298DF87B00F3D526 /* TranslationService.swift in Sources */,
26652672
4CE4F9E328528C5200C00DD9 /* AddRelayView.swift in Sources */,
@@ -2760,6 +2767,7 @@
27602767
4C4E137D2A76D63600BDD832 /* UnmuteThreadNotify.swift in Sources */,
27612768
4CE4F0F829DB7399005914DB /* ThiccDivider.swift in Sources */,
27622769
4CE0E2B629A3ED5500DB4CA2 /* InnerTimelineView.swift in Sources */,
2770+
BA3BF28C2A7F156B00600232 /* CameraService.swift in Sources */,
27632771
4C363A8828236948006E126D /* BlocksView.swift in Sources */,
27642772
4C06670628FCB08600038D2A /* ImageCarousel.swift in Sources */,
27652773
3A23838E2A297DD200E5AA2E /* ZapButtonModel.swift in Sources */,
@@ -2769,6 +2777,7 @@
27692777
4C64305C2A945AFF00B0C0E9 /* MusicController.swift in Sources */,
27702778
5053ACA72A56DF3B00851AE3 /* DeveloperSettingsView.swift in Sources */,
27712779
F79C7FAD29D5E9620000F946 /* EditPictureControl.swift in Sources */,
2780+
BA3BF2892A7F156B00600232 /* PhotoCaptureProcessor.swift in Sources */,
27722781
4C9F18E229AA9B6C008C55EC /* CustomizeZapView.swift in Sources */,
27732782
4C2859602A12A2BE004746F7 /* SupporterBadge.swift in Sources */,
27742783
4C1A9A2A29DDF54400516EAC /* DamusVideoPlayer.swift in Sources */,
@@ -2901,6 +2910,7 @@
29012910
5C513FBA297F72980072348F /* CustomPicker.swift in Sources */,
29022911
4C1253622A76D00B0004F4B8 /* PostNotify.swift in Sources */,
29032912
4CACA9D5280C31E100D9BBE8 /* ReplyView.swift in Sources */,
2913+
BA3BF28F2A7F1B2D00600232 /* CameraModel.swift in Sources */,
29042914
F7908E92298B0F0700AB113A /* RelayDetailView.swift in Sources */,
29052915
4C9147002A2A891E00DDEA40 /* error.c in Sources */,
29062916
4CE879552996BAB900F758CC /* RelayPaidDetail.swift in Sources */,
@@ -2909,6 +2919,7 @@
29092919
4CF0ABD42980996B00D66079 /* Report.swift in Sources */,
29102920
4C06670B28FDE64700038D2A /* damus.c in Sources */,
29112921
4C1253642A76D08F0004F4B8 /* ReportNotify.swift in Sources */,
2922+
BA8A4F0F2A2D95F70045C48C /* CameraView.swift in Sources */,
29122923
4C1A9A2529DDDF2600516EAC /* ZapSettingsView.swift in Sources */,
29132924
4C2CDDF7299D4A5E00879FD5 /* Debouncer.swift in Sources */,
29142925
3AAA95CC298E07E900F3D526 /* DeepLPlan.swift in Sources */,
@@ -2937,6 +2948,7 @@
29372948
3AA59D1D2999B0400061C48E /* DraftsModel.swift in Sources */,
29382949
3169CAED294FCCFC00EE4006 /* Constants.swift in Sources */,
29392950
4C9AA14A2A4587A6003F49FD /* NotificationStatusModel.swift in Sources */,
2951+
BA8A4F132A2D96AD0045C48C /* CameraPreview.swift in Sources */,
29402952
4CB9D4A72992D02B00A9A7E4 /* ProfileNameView.swift in Sources */,
29412953
4CE4F0F429D779B5005914DB /* PostBox.swift in Sources */,
29422954
BA37598E2ABCCE500018D73B /* VideoCaptureProcessor.swift in Sources */,
@@ -3253,7 +3265,7 @@
32533265
CODE_SIGN_STYLE = Automatic;
32543266
CURRENT_PROJECT_VERSION = 23;
32553267
DEVELOPMENT_ASSET_PATHS = "\"damus/Preview Content\"";
3256-
DEVELOPMENT_TEAM = XK7H4JAB3D;
3268+
DEVELOPMENT_TEAM = XL4476DR2X;
32573269
ENABLE_PREVIEWS = YES;
32583270
GENERATE_INFOPLIST_FILE = YES;
32593271
INFOPLIST_FILE = damus/Info.plist;
@@ -3279,7 +3291,7 @@
32793291
"$(PROJECT_DIR)",
32803292
);
32813293
MARKETING_VERSION = 1.6;
3282-
PRODUCT_BUNDLE_IDENTIFIER = com.jb55.damus2;
3294+
PRODUCT_BUNDLE_IDENTIFIER = com.suhail.damus2;
32833295
PRODUCT_NAME = "$(TARGET_NAME)";
32843296
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator";
32853297
SUPPORTS_MACCATALYST = YES;
@@ -3302,7 +3314,7 @@
33023314
CODE_SIGN_STYLE = Automatic;
33033315
CURRENT_PROJECT_VERSION = 23;
33043316
DEVELOPMENT_ASSET_PATHS = "\"damus/Preview Content\"";
3305-
DEVELOPMENT_TEAM = XK7H4JAB3D;
3317+
DEVELOPMENT_TEAM = XL4476DR2X;
33063318
ENABLE_PREVIEWS = YES;
33073319
GENERATE_INFOPLIST_FILE = YES;
33083320
INFOPLIST_FILE = damus/Info.plist;

damus/Views/Camera/CameraView.swift

+212
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
//
2+
// CameraView.swift
3+
// damus
4+
//
5+
// Created by Suhail Saqan on 8/5/23.
6+
//
7+
8+
import SwiftUI
9+
import Combine
10+
import AVFoundation
11+
12+
struct CameraView: View {
13+
let damus_state: DamusState
14+
let action: (([MediaItem]) -> Void)
15+
16+
@Environment(\.presentationMode) var presentationMode
17+
18+
@StateObject var model: CameraModel
19+
20+
@State var currentZoomFactor: CGFloat = 1.0
21+
22+
public init(damus_state: DamusState, action: @escaping (([MediaItem]) -> Void)) {
23+
self.damus_state = damus_state
24+
self.action = action
25+
_model = StateObject(wrappedValue: CameraModel())
26+
}
27+
28+
var captureButton: some View {
29+
Button {
30+
if model.isRecording {
31+
withAnimation {
32+
model.stopRecording()
33+
}
34+
} else {
35+
withAnimation {
36+
model.capturePhoto()
37+
}
38+
}
39+
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
40+
} label: {
41+
ZStack {
42+
Circle()
43+
.fill( model.isRecording ? .red : DamusColors.black)
44+
.frame(width: model.isRecording ? 85 : 65, height: model.isRecording ? 85 : 65, alignment: .center)
45+
46+
Circle()
47+
.stroke( model.isRecording ? .red : DamusColors.white, lineWidth: 4)
48+
.frame(width: model.isRecording ? 95 : 75, height: model.isRecording ? 95 : 75, alignment: .center)
49+
}
50+
.frame(alignment: .center)
51+
}
52+
.simultaneousGesture(
53+
LongPressGesture(minimumDuration: 0.5).onEnded({ value in
54+
if (!model.isCameraButtonDisabled) {
55+
withAnimation {
56+
model.startRecording()
57+
model.captureMode = .video
58+
}
59+
}
60+
})
61+
)
62+
.buttonStyle(.plain)
63+
}
64+
65+
var capturedPhotoThumbnail: some View {
66+
ZStack {
67+
if model.thumbnail != nil {
68+
Image(uiImage: model.thumbnail.thumbnailImage!)
69+
.resizable()
70+
.aspectRatio(contentMode: .fill)
71+
.frame(width: 60, height: 60)
72+
.clipShape(RoundedRectangle(cornerRadius: 10, style: .continuous))
73+
}
74+
if model.isPhotoProcessing {
75+
ProgressView()
76+
.progressViewStyle(CircularProgressViewStyle(tint: DamusColors.white))
77+
}
78+
}
79+
}
80+
81+
var closeButton: some View {
82+
Button {
83+
presentationMode.wrappedValue.dismiss()
84+
model.stop()
85+
} label: {
86+
HStack {
87+
Image(systemName: "xmark")
88+
.font(.system(size: 24))
89+
}
90+
.frame(minWidth: 40, minHeight: 40)
91+
}
92+
.accentColor(DamusColors.white)
93+
}
94+
95+
var flipCameraButton: some View {
96+
Button(action: {
97+
model.flipCamera()
98+
}, label: {
99+
HStack {
100+
Image(systemName: "camera.rotate.fill")
101+
.font(.system(size: 20))
102+
}
103+
.frame(minWidth: 40, minHeight: 40)
104+
})
105+
.accentColor(DamusColors.white)
106+
}
107+
108+
var toggleFlashButton: some View {
109+
Button(action: {
110+
model.switchFlash()
111+
}, label: {
112+
HStack {
113+
Image(systemName: model.isFlashOn ? "bolt.fill" : "bolt.slash.fill")
114+
.font(.system(size: 20))
115+
}
116+
.frame(minWidth: 40, minHeight: 40)
117+
})
118+
.accentColor(model.isFlashOn ? .yellow : DamusColors.white)
119+
}
120+
121+
var body: some View {
122+
NavigationView {
123+
GeometryReader { reader in
124+
ZStack {
125+
DamusColors.black.edgesIgnoringSafeArea(.all)
126+
127+
CameraPreview(session: model.session)
128+
.padding(.bottom, 175)
129+
.edgesIgnoringSafeArea(.all)
130+
.gesture(
131+
DragGesture().onChanged({ (val) in
132+
if abs(val.translation.height) > abs(val.translation.width) {
133+
let percentage: CGFloat = -(val.translation.height / reader.size.height)
134+
let calc = currentZoomFactor + percentage
135+
let zoomFactor: CGFloat = min(max(calc, 1), 5)
136+
137+
currentZoomFactor = zoomFactor
138+
model.zoom(with: zoomFactor)
139+
}
140+
})
141+
)
142+
.onAppear {
143+
model.configure()
144+
}
145+
.alert(isPresented: $model.showAlertError, content: {
146+
Alert(title: Text(model.alertError.title), message: Text(model.alertError.message), dismissButton: .default(Text(model.alertError.primaryButtonTitle), action: {
147+
model.alertError.primaryAction?()
148+
}))
149+
})
150+
.overlay(
151+
Group {
152+
if model.willCapturePhoto {
153+
Color.black
154+
}
155+
}
156+
)
157+
158+
VStack {
159+
if !model.isRecording {
160+
HStack {
161+
closeButton
162+
163+
Spacer()
164+
165+
HStack {
166+
flipCameraButton
167+
toggleFlashButton
168+
}
169+
}
170+
.padding(.horizontal, 20)
171+
}
172+
173+
Spacer()
174+
175+
HStack(alignment: .center) {
176+
if !model.mediaItems.isEmpty {
177+
NavigationLink(destination: Text(model.mediaItems.map { $0.url.absoluteString }.joined(separator: ", "))) {
178+
capturedPhotoThumbnail
179+
}
180+
.frame(width: 100, alignment: .leading)
181+
}
182+
183+
Spacer()
184+
185+
captureButton
186+
187+
Spacer()
188+
189+
if !model.mediaItems.isEmpty {
190+
Button(action: {
191+
action(model.mediaItems)
192+
presentationMode.wrappedValue.dismiss()
193+
model.stop()
194+
}) {
195+
Text("Upload")
196+
.frame(width: 100, height: 40, alignment: .center)
197+
.foregroundColor(DamusColors.white)
198+
.overlay {
199+
RoundedRectangle(cornerRadius: 24)
200+
.stroke(DamusColors.white, lineWidth: 2)
201+
}
202+
}
203+
}
204+
}
205+
.frame(height: 100)
206+
.padding([.horizontal, .vertical], 20)
207+
}
208+
}
209+
}
210+
}
211+
}
212+
}

damus/Views/PostView.swift

+22-20
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ struct PostView: View {
5858
@State var newCursorIndex: Int?
5959
@State var textHeight: CGFloat? = nil
6060

61-
@State var mediaToUpload: MediaUpload? = nil
61+
@State var mediaToUpload: [MediaUpload] = []
6262

6363
@StateObject var image_upload: ImageUploadModel = ImageUploadModel()
6464
@StateObject var tagModel: TagModel = TagModel()
@@ -343,6 +343,15 @@ struct PostView: View {
343343
pks.append(pk)
344344
}
345345
}
346+
347+
func addToMediaToUpload(mediaItem: MediaItem) {
348+
switch mediaItem.type {
349+
case .image:
350+
mediaToUpload.append(.image(mediaItem.url))
351+
case .video:
352+
mediaToUpload.append(.video(mediaItem.url))
353+
}
354+
}
346355

347356
var body: some View {
348357
GeometryReader { (deviceSize: GeometryProxy) in
@@ -384,36 +393,29 @@ struct PostView: View {
384393
}
385394
.sheet(isPresented: $attach_media) {
386395
ImagePicker(uploader: damus_state.settings.default_media_uploader, sourceType: .photoLibrary, pubkey: damus_state.pubkey, image_upload_confirm: $image_upload_confirm) { img in
387-
self.mediaToUpload = .image(img)
396+
self.mediaToUpload.append(.image(img))
388397
} onVideoPicked: { url in
389-
self.mediaToUpload = .video(url)
398+
self.mediaToUpload.append(.video(url))
390399
}
391400
.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) {
392401
Button(NSLocalizedString("Upload", comment: "Button to proceed with uploading."), role: .none) {
393-
if let mediaToUpload {
394-
self.handle_upload(media: mediaToUpload)
402+
if !mediaToUpload.isEmpty {
403+
self.handle_upload(media: mediaToUpload[0])
395404
self.attach_media = false
396405
}
397406
}
398407
Button(NSLocalizedString("Cancel", comment: "Button to cancel the upload."), role: .cancel) {}
399408
}
400409
}
401-
.sheet(isPresented: $attach_camera) {
402-
403-
ImagePicker(uploader: damus_state.settings.default_media_uploader, sourceType: .camera, pubkey: damus_state.pubkey, image_upload_confirm: $image_upload_confirm) { img in
404-
self.mediaToUpload = .image(img)
405-
} onVideoPicked: { url in
406-
self.mediaToUpload = .video(url)
407-
}
408-
.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) {
409-
Button(NSLocalizedString("Upload", comment: "Button to proceed with uploading."), role: .none) {
410-
if let mediaToUpload {
411-
self.handle_upload(media: mediaToUpload)
412-
self.attach_camera = false
413-
}
410+
.fullScreenCover(isPresented: $attach_camera) {
411+
CameraView(damus_state: damus_state, action: { items in
412+
for item in items {
413+
addToMediaToUpload(mediaItem: item)
414414
}
415-
Button(NSLocalizedString("Cancel", comment: "Button to cancel the upload."), role: .cancel) {}
416-
}
415+
for media in mediaToUpload {
416+
self.handle_upload(media: media)
417+
}
418+
})
417419
}
418420
.onAppear() {
419421
let loaded_draft = load_draft()

0 commit comments

Comments
 (0)