Skip to content

Commit

Permalink
-Video Notes Impl
Browse files Browse the repository at this point in the history
  • Loading branch information
KhubaibKhan4 committed Jan 31, 2025
1 parent 322f2b0 commit 2dc3b03
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 5 deletions.
8 changes: 4 additions & 4 deletions Notes.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
/* Begin PBXFileSystemSynchronizedRootGroup section */
49B7C3BC2D00D8E900FE8DE5 /* Resources */ = {
isa = PBXFileSystemSynchronizedRootGroup;
exceptions = (
);
path = Resources;
sourceTree = "<group>";
};
Expand Down Expand Up @@ -282,6 +280,7 @@
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"Notes/Preview Content\"";
DEVELOPMENT_TEAM = X2MJP5MGGS;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Notes/Info.plist;
Expand All @@ -299,7 +298,7 @@
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.codespacepro.Notes;
PRODUCT_BUNDLE_IDENTIFIER = com.codespacepro.Notes.note;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
Expand All @@ -315,6 +314,7 @@
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"Notes/Preview Content\"";
DEVELOPMENT_TEAM = X2MJP5MGGS;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Notes/Info.plist;
Expand All @@ -332,7 +332,7 @@
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.codespacepro.Notes;
PRODUCT_BUNDLE_IDENTIFIER = com.codespacepro.Notes.note;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
Expand Down
Binary file not shown.
56 changes: 55 additions & 1 deletion Notes/AddNotes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import SwiftData
import MapKit
import PhotosUI
import CoreLocation
import AVKit

struct AddNotes: View {

Expand All @@ -20,6 +21,7 @@ struct AddNotes: View {
@State private var selectedImages = [Image]()
@State private var selectedImagesData = [Data]()
@State private var selectedVideo: URL?
@State private var selectedVideoItem: PhotosPickerItem?

@Binding var title: String
@Binding var desc: String
Expand Down Expand Up @@ -93,6 +95,36 @@ struct AddNotes: View {
.buttonStyle(.borderedProminent)
}

Section("Videos") {
PhotosPicker(
selection: $selectedVideoItem,
matching: .videos
) {
Label("Select Video", systemImage: "video.fill")
.padding(8)
.foregroundColor(.white)
.cornerRadius(8)
}
.buttonStyle(.borderedProminent)

if let videoURL = selectedVideo {
VideoPlayer(player: AVPlayer(url: videoURL))
.frame(height: 200)
.cornerRadius(8)
.overlay(alignment: .topTrailing) {
Button {
selectedVideo = nil
} label: {
Image(systemName: "xmark.circle.fill")
.padding(4)
.background(Color.black.opacity(0.6))
.clipShape(Circle())
}
.padding(6)
}
}
}

if !selectedImagesData.isEmpty {
withAnimation {
Section("Selected Content") {
Expand Down Expand Up @@ -120,7 +152,7 @@ struct AddNotes: View {
.onChange(of: selectedItems) { newItems in
Task {
selectedImages.removeAll()

for item in selectedItems {
if let image = try? await item.loadTransferable(type: Image.self) {
selectedImages.append(image)
Expand All @@ -137,6 +169,15 @@ struct AddNotes: View {

}
}
.onChange(of: selectedVideoItem) { _, newItem in
Task {
if let item = newItem {
if let url = try? await item.loadTransferable(type: URL.self) {
selectedVideo = await saveVideoToDocuments(url: url)
}
}
}
}
.onAppear {
locationManager.checkPermissionStatus()
}
Expand Down Expand Up @@ -254,6 +295,19 @@ struct AddNotes: View {
)
)
}
private func saveVideoToDocuments(url: URL) async -> URL? {
do {
let data = try Data(contentsOf: url)
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let fileName = "video-\(UUID().uuidString).\(url.pathExtension)"
let fileURL = documentsDirectory.appendingPathComponent(fileName)
try data.write(to: fileURL)
return fileURL
} catch {
print("Error saving video: \(error.localizedDescription)")
return nil
}
}
}

extension Image {
Expand Down
14 changes: 14 additions & 0 deletions Notes/NotesDetail.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,20 @@ struct NotesDetail: View {
}
.frame(height: 80)
}

func getVideoURL(from data: Data?) -> URL? {
guard let data = data else { return nil }

let tempURL = FileManager.default.temporaryDirectory.appendingPathComponent("tempVideo.mp4")

do {
try data.write(to: tempURL, options: .atomic)
return tempURL
} catch {
print("Error writing video data to file: \(error)")
return nil
}
}
}


Expand Down

0 comments on commit 2dc3b03

Please sign in to comment.