Skip to content

Commit

Permalink
Merge pull request #2 from KhubaibKhan4/notesdata
Browse files Browse the repository at this point in the history
-Notes with Images Init
  • Loading branch information
KhubaibKhan4 authored Jan 31, 2025
2 parents 46dd5eb + f1fff3e commit 3617881
Show file tree
Hide file tree
Showing 10 changed files with 715 additions and 189 deletions.
4 changes: 4 additions & 0 deletions Notes.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@
INFOPLIST_FILE = Notes/Info.plist;
INFOPLIST_KEY_NSCameraUsageDescription = "Provide us the Camera Permission to Take Images and Record Videos to Store in Notes App.";
INFOPLIST_KEY_NSLocationAlwaysAndWhenInUseUsageDescription = "Provide us the Location Permission to Provide the realtime location inside the app.";
INFOPLIST_KEY_NSLocationAlwaysUsageDescription = "Provide us the Location Permission to Provide the realtime location inside the app.";
INFOPLIST_KEY_NSLocationWhenInUseUsageDescription = "Provide us the Location Permission to Provide the realtime location inside the app.";
INFOPLIST_KEY_NSPhotoLibraryUsageDescription = "Provide us the Photo Library Access to get Image or Video.";
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
Expand Down Expand Up @@ -318,6 +320,8 @@
INFOPLIST_FILE = Notes/Info.plist;
INFOPLIST_KEY_NSCameraUsageDescription = "Provide us the Camera Permission to Take Images and Record Videos to Store in Notes App.";
INFOPLIST_KEY_NSLocationAlwaysAndWhenInUseUsageDescription = "Provide us the Location Permission to Provide the realtime location inside the app.";
INFOPLIST_KEY_NSLocationAlwaysUsageDescription = "Provide us the Location Permission to Provide the realtime location inside the app.";
INFOPLIST_KEY_NSLocationWhenInUseUsageDescription = "Provide us the Location Permission to Provide the realtime location inside the app.";
INFOPLIST_KEY_NSPhotoLibraryUsageDescription = "Provide us the Photo Library Access to get Image or Video.";
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
Expand Down
Binary file not shown.
78 changes: 78 additions & 0 deletions Notes.xcodeproj/xcshareddata/xcschemes/Notes.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1620"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES"
buildArchitectures = "Automatic">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "49BA1F852CBD8E700099CDCB"
BuildableName = "Notes.app"
BlueprintName = "Notes"
ReferencedContainer = "container:Notes.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "49BA1F852CBD8E700099CDCB"
BuildableName = "Notes.app"
BlueprintName = "Notes"
ReferencedContainer = "container:Notes.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "49BA1F852CBD8E700099CDCB"
BuildableName = "Notes.app"
BlueprintName = "Notes"
ReferencedContainer = "container:Notes.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,13 @@
<integer>0</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>49BA1F852CBD8E700099CDCB</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>
142 changes: 127 additions & 15 deletions Notes/AddNotes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@
import SwiftUI
import SwiftData
import MapKit
import PhotosUI
import CoreLocation

struct AddNotes: View {

@Environment(\.modelContext) private var context
@Environment(\.dismiss) private var dismiss

@State private var selectedItems = [PhotosPickerItem]()
@State private var selectedImages = [Image]()
@State private var selectedImagesData = [Data]()
@State private var selectedVideo: URL?

@Binding var title: String
@Binding var desc: String
@Binding var navTitle: String
Expand All @@ -24,23 +31,31 @@ struct AddNotes: View {
@State private var position: MapCameraPosition = .region(MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 48.8566, longitude: 2.3522), span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1)))

@Environment(\.presentationMode) private var presentationMode: Binding<PresentationMode>
@State var selectedLocation : CLLocationCoordinate2D?
@State private var selectedLocation: CLLocationCoordinate2D?
@State private var selectedLocationName: String = "No Location Selected"

@StateObject private var locationManager = LocationManager()
@State private var permissionDenied = false

var onSave: () -> Void

var body: some View {
VStack{
VStack {
Form {
Section("Note") {
TextField("Title", text: $title)
TextField("Description", text: $desc)
}
Section("Map") {
Button("Add Map", systemImage: "map.circle") {
isMapSheet = !isMapSheet
if locationManager.permissionGranted {
isMapSheet = true
if let userLocation = locationManager.userLocation {
zoomToUserLocation(userLocation)
}
} else {
locationManager.requestLocationPermission()
}
}
.foregroundColor(.white)
.buttonStyle(.borderedProminent)
Expand All @@ -63,14 +78,76 @@ struct AddNotes: View {
)
}

Section("Media") {
Button("Add Media", systemImage: "photo.on.rectangle.angled.fill") {
locationManager.checkLocationAuthorization()
Section("Photos") {
PhotosPicker(
selection: $selectedItems,
matching: .images
) {
Label("Select Images", systemImage: "photo.on.rectangle.angled.fill")
.padding(8)
.foregroundColor(.white)
.background(.blue)
.cornerRadius(8)

}
.foregroundColor(.white)
.buttonStyle(.borderedProminent)
}

if !selectedImagesData.isEmpty {
withAnimation {
Section("Selected Content") {
if selectedImages.isEmpty {
Text("No images selected")
.foregroundColor(.gray)
} else {
ScrollView(.horizontal, showsIndicators: false) {
HStack {
ForEach(selectedImages.indices, id: \..self) { index in
selectedImages[index]
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 100, height: 100)
.clipShape(RoundedRectangle(cornerRadius: 8))
.padding(4)
}
}
}
}
}
}
}
}
.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)
}
}

var imageDataArray = [Data]()
for item in newItems {
if let data = try? await item.loadTransferable(type: Data.self) {
imageDataArray.append(data)
}
}
selectedImagesData = imageDataArray

}
}
.onAppear {
locationManager.checkPermissionStatus()
}
.onChange(of: locationManager.userLocation) { location in
if let userLocation = location {
zoomToUserLocation(userLocation)
}
}
.onChange(of: locationManager.permissionDenied) { denied in
permissionDenied = denied
}
}.navigationTitle("Add Notes")
.navigationBarBackButtonHidden(true)
Expand All @@ -83,17 +160,13 @@ struct AddNotes: View {
Label(selectedLocationName, image: "mappin")
}
}.mapControls({
/// Shows up when you pitch to zoom
MapScaleView()
/// Shows up when you rotate the map
MapCompass()
/// 3D and 2D button on the top right
MapPitchToggle()
})
.frame(width: .infinity, height: .infinity)
.onTapGesture {position in
.onTapGesture { position in
if let mapLocation = proxy.convert(position, from: .local) {
print("Location: \(mapLocation)")
selectedLocation = mapLocation
locationList.append(mapLocation)

Expand Down Expand Up @@ -134,12 +207,18 @@ struct AddNotes: View {
}
ToolbarItem(placement: .topBarTrailing) {
Button {
var item = NotesItem(title: title, desc: desc, isPinned: false, location: selectedLocation)
context.insert(item)
try? context.save()
let noteItem = NotesItem(
title: title,
desc: desc,
isPinned: false,
location: selectedLocation,
images: selectedImagesData,
videoURL: selectedVideo
)

context.insert(noteItem)
onSave()
presentationMode.wrappedValue.dismiss()

} label: {
Text("Save")
}
Expand All @@ -166,4 +245,37 @@ struct AddNotes: View {
}
}
}

private func zoomToUserLocation(_ userLocation: CLLocation) {
let coordinate = userLocation.coordinate
self.position = .region(
MKCoordinateRegion(
center: coordinate,
span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
)
)
}
}

extension Image {
func asData() -> Data? {
guard let uiImage = self.asUIImage() else { return nil }
return uiImage.jpegData(compressionQuality: 1.0)
}

func asUIImage() -> UIImage? {
let hostingController = UIHostingController(rootView: self)
let view = hostingController.view
let size = hostingController.sizeThatFits(in: CGSize(width: 1000, height: 1000))
view?.bounds = CGRect(origin: .zero, size: size)
view?.setNeedsLayout()
view?.layoutIfNeeded()

UIGraphicsBeginImageContextWithOptions(view?.bounds.size ?? .zero, false, 0)
view?.drawHierarchy(in: view?.bounds ?? .zero, afterScreenUpdates: true)
let uiImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return uiImage
}
}
26 changes: 13 additions & 13 deletions Notes/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ struct ContentView: View {
@State private var isSelected : Int = 0
@AppStorage("isPermissionGranted") var isPermissionGranted = false
var body: some View {
TabView(selection:$isSelected) {
Tab("Home", systemImage: "house",value: 0) {
HomeScreen()
}
TabView(selection:$isSelected) {
Tab("Home", systemImage: "house",value: 0) {
HomeScreen()
}

Tab("Todo", systemImage: "checklist",value: 1) {
TodoScreen()

Tab("Todo", systemImage: "checklist",value: 1) {
TodoScreen()

}

Tab("Setting", systemImage: "gear",value:2) {
SettingScreen()
}

}

Tab("Setting", systemImage: "gear",value:2) {
SettingScreen()
}

}
}
}

Expand Down
Loading

0 comments on commit 3617881

Please sign in to comment.