-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
camera: add ability to preview media taken with camera
- Loading branch information
1 parent
4413ec0
commit 49860d7
Showing
5 changed files
with
338 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// | ||
// MediaViewer.swift | ||
// damus | ||
// | ||
// Created by Suhail Saqan on 12/22/23. | ||
// | ||
|
||
import SwiftUI | ||
import Kingfisher | ||
|
||
// MARK: - Camera Media Viewer | ||
struct CameraMediaView: View { | ||
let video_controller: VideoController | ||
let urls: [MediaUrl] | ||
|
||
@Environment(\.presentationMode) var presentationMode | ||
|
||
@State private var selectedIndex = 0 | ||
@State var showMenu = true | ||
|
||
let settings: UserSettingsStore | ||
|
||
var tabViewIndicator: some View { | ||
HStack(spacing: 10) { | ||
ForEach(urls.indices, id: \.self) { index in | ||
Capsule() | ||
.fill(index == selectedIndex ? Color(UIColor.label) : Color.secondary) | ||
.frame(width: 7, height: 7) | ||
.onTapGesture { | ||
selectedIndex = index | ||
} | ||
} | ||
} | ||
.padding() | ||
.background(.regularMaterial) | ||
.clipShape(Capsule()) | ||
} | ||
|
||
var body: some View { | ||
ZStack { | ||
Color(.systemBackground) | ||
.ignoresSafeArea() | ||
|
||
TabView(selection: $selectedIndex) { | ||
ForEach(urls.indices, id: \.self) { index in | ||
ZoomableScrollView { | ||
ImageContainerView(video_controller: video_controller, url: urls[index], settings: settings) | ||
.aspectRatio(contentMode: .fit) | ||
.padding(.top, Theme.safeAreaInsets?.top) | ||
.padding(.bottom, Theme.safeAreaInsets?.bottom) | ||
} | ||
.ignoresSafeArea() | ||
.tag(index) | ||
} | ||
} | ||
.ignoresSafeArea() | ||
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never)) | ||
.gesture(TapGesture(count: 2).onEnded { | ||
// Prevents menu from hiding on double tap | ||
}) | ||
.gesture(TapGesture(count: 1).onEnded { | ||
showMenu.toggle() | ||
}) | ||
.overlay( | ||
GeometryReader { geo in | ||
VStack { | ||
if showMenu { | ||
NavDismissBarView() | ||
Spacer() | ||
|
||
if (urls.count > 1) { | ||
tabViewIndicator | ||
} | ||
} | ||
} | ||
.animation(.easeInOut, value: showMenu) | ||
.padding(.bottom, geo.safeAreaInsets.bottom == 0 ? 12 : 0) | ||
} | ||
) | ||
} | ||
} | ||
} | ||
|
||
struct CameraMediaView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
let url: MediaUrl = .image(URL(string: "https://jb55.com/red-me.jpg")!) | ||
CameraMediaView(video_controller: test_damus_state.video, urls: [url], settings: test_damus_state.settings) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
// | ||
// 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: CameraMediaView(video_controller: damus_state.video, urls: model.mediaItems.map { mediaItem in | ||
switch mediaItem.type { | ||
case .image: | ||
return .image(mediaItem.url) | ||
case .video: | ||
return .video(mediaItem.url) | ||
} | ||
}, settings: damus_state.settings) | ||
.navigationBarBackButtonHidden(true) | ||
) { | ||
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) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.