Skip to content
This repository has been archived by the owner on Aug 23, 2022. It is now read-only.

Commit

Permalink
Adding Asset Photos into Documents Folder Working
Browse files Browse the repository at this point in the history
  • Loading branch information
josiahdowdy committed Jan 7, 2022
1 parent 8e3f849 commit c9fc432
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 69 deletions.
150 changes: 140 additions & 10 deletions Art_Athlete/Views/MainViews/MultipleSelectRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@ import SwiftUI
import Files
import CoreLocation

@available(macCatalyst 15.0, *)
public class ImageProvider {
// convenient for specific image
public static func picture() -> UIImage {
return UIImage(named: "picture", in: Bundle(for: self), with: nil) ?? UIImage()
}

// for any image located in bundle where this class has built
public static func image(named: String) -> UIImage? {
return UIImage(named: named, in: Bundle(for: self), with: nil)
}
}

@available(macCatalyst 15, *)
struct MultipleSelectRow: View {
@Environment(\.refresh) var refreshAction: RefreshAction?

Expand All @@ -17,6 +29,7 @@ struct MultipleSelectRow: View {
@EnvironmentObject var homeData: HomeViewModel

@State var trimVal : CGFloat = 0
@State var imageProvider = ImageProvider()

@State var needRefresh: Bool = false
@State private var alertShowing = false
Expand All @@ -34,8 +47,6 @@ struct MultipleSelectRow: View {
@State var imageSize: CGSize = .zero // << or initial from NSImage
@State private var animationAmount = 1.0

private let locationManager = CLLocationManager()


//@State var selected = 0 // 1

Expand Down Expand Up @@ -68,12 +79,10 @@ struct MultipleSelectRow: View {
@ViewBuilder
func ShowAllFolders()->some View{
HStack {
Text("Photos").font(.title3)
.foregroundColor(currentDarkLightMode == .dark ? Color.white : Color.black)
Text("Photos").font(.title3).foregroundColor(currentDarkLightMode == .dark ? Color.white : Color.black)

ZStack {


LoadFoldersButtoniPad(needRefresh: $needRefresh)
.environmentObject(homeData)
.foregroundColor(currentDarkLightMode == .dark ? Color.white : Color.black)
Expand All @@ -97,10 +106,8 @@ struct MultipleSelectRow: View {
}

Button(action: {
self.locationManager.requestAlwaysAuthorization()
self.locationManager.requestWhenInUseAuthorization()


createFolder()
saveImage()
}) {
Text("AUTH")
}
Expand All @@ -125,6 +132,129 @@ struct MultipleSelectRow: View {
}
} //End func.

func getAssetImages() {
let imageName = ["jump", "dance", "couple", "dance2", "standing"]
let img1 = ImageProvider.image(named: "jump")
let img2 = ImageProvider.image(named: "dance")
let img3 = ImageProvider.image(named: "couple")
let img4 = ImageProvider.image(named: "dance2")
let img5 = ImageProvider.image(named: "standing")

let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
let docURL = URL(fileURLWithPath: documentsDirectory)//URL(string: documentsDirectory)!
let dataPath = docURL.appendingPathComponent("jump")

do {
let data1 = img1!.jpegData(compressionQuality: 1.0)


} catch let error {
print("xx. JD00: error saving file with error", error.localizedDescription)
}
}

func createFolder() {
//create directory
let documentsPath = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])

let logsPath = documentsPath.appendingPathComponent("Poses")

print(logsPath!)
do{
try FileManager.default.createDirectory(atPath: logsPath!.path, withIntermediateDirectories: true, attributes: nil)
}catch let error as NSError{
print(error)
// print(" Unable to create directory \(error)”)
}
}

//Start funcs.
func saveImage() { //imageName: String, image: Data
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
let docURL = URL(fileURLWithPath: documentsDirectory)//URL(string: documentsDirectory)!
let dataPath = docURL.appendingPathComponent("Poses")


print("1. JD00 → dataPath → \(dataPath.path)")

guard let documentsDirectory2 = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }

// let bundlePath = Bundle.main.bundlePath
// print("4. JD00 → bundlePath \(bundlePath)")

if let fileURL = Bundle.main.url(forResource: "jump", withExtension: "png") {
// if let fileURL = Bundle.main.url(forResource: "PhotoPack", withExtension: "") {
print("2. JD00 → fileURL \(fileURL)")
let image = UIImage(contentsOfFile: fileURL.path)
let fileName = "Poop"//imageName
let folderURL = documentsDirectory2.appendingPathComponent(fileName)
let writePath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("Poses")
let imageName = fileURL.lastPathComponent
// if !FileManager.default.fileExists(atPath: fileURL.path) {
let folderAndFileName = "Poses/".appending(imageName)

let dataPath2 = docURL.appendingPathComponent(folderAndFileName)


print("4. JD00 → writePath \(writePath.path)")

//if fileURL.startAccessingSecurityScopedResource() {
// if folderURL.startAccessingSecurityScopedResource() {
// print("4. JD00 → Security allowed.")

do {
let data = image!.jpegData(compressionQuality: 1.0)
//let data = image2!.pngData()//.pngData()
// try FileManager.default.createDirectory(atPath: dataPath.absoluteString, withIntermediateDirectories: true, attributes: nil)
//try data!.write(to: folderURL)
//try data!.write(to: folderURL)
try data!.write(to: dataPath2) //writePath)

print("\n5. JD00 → ACCESS GRANTED ******************\n")
} catch let error {
print("5. JD00: error saving file with error", error.localizedDescription)
}
// }
// folderURL.stopAccessingSecurityScopedResource()

//Move File
// if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first{
// let oldPath = dir.appendingPathComponent(“jump.png”)
// let newPath = dir.appendingPathComponent(“data/fileMove.txt”)
// let fileManager = FileManager.default
// do{
// try fileManager.moveItem(at: oldPath, to: newPath)
// }catch{
// print(“cant move the file…”)
// }
// }

do {
let data = image!.jpegData(compressionQuality: 1.0)
try data!.write(to: writePath)
// let originFolder = try Folder(path: dataPath.path)
// let targetFolder = try Folder(path: folderURL.path)
// try originFolder.files.move(to: targetFolder)
} catch let error {
print("6. JD00: error saving file:", error.localizedDescription)
}

// do {
// try FileManager.default.createDirectory(atPath: folderURL.path, withIntermediateDirectories: true, attributes: nil)
// //URL(string: fileURL.path)!
// } catch {
// print("JD00:", error.localizedDescription)
// }
//}


} else {
print("2. JD00: NOT FOUND")
}
}

private func rectReader() -> some View {
return GeometryReader { (geometry) -> AnyView in
let imageSize = geometry.size
Expand Down
80 changes: 40 additions & 40 deletions Art_Athlete/Views/MainViews/StartButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,46 +101,46 @@ struct StartButton: View {
} //End Func.

//IPHONE and iPad load files.
func loadFolderFilesiPad() {
///1. Print the files in the directory.
guard
let path = FileManager
.default
.urls(for: .documentDirectory, in: .userDomainMask)
.first
else { //.appendingPathComponent("\(name).jpg")
print("error getting path.")
return
}

let loadFiles = Folder.documents?.files
let subFolders = Folder.documents?.subfolders

print("JD451: loadfiles ••••••••• \(String(describing: loadFiles.debugDescription))")
print("JD451: subfolders ••••••••• \(String(describing: subFolders))")

for file in loadFiles! { //
prefs.arrayOfURLStrings.append(file.url.absoluteString)
}

print("JD451: path DIRECTORY is --> \(path)")
// print("JD451: path DIRECTORY is --> \(path)")



print(prefs.arrayOfFolderNames)
do {
for i in prefs.arrayOfFolderNames {
let loadFolderURL = try Folder.documents!.subfolder(named: i)

for file in try Folder(path: loadFolderURL.path).files {
prefs.arrayOfURLStrings.append(file.url.absoluteString)
}
}
} catch {
print("JD452: error loading files from download folder.", error)
}
} //End Func.
// func loadFolderFilesiPad() {
// ///1. Print the files in the directory.
// guard
// let path = FileManager
// .default
// .urls(for: .documentDirectory, in: .userDomainMask)
// .first
// else { //.appendingPathComponent("\(name).jpg")
// print("error getting path.")
// return
// }
//
// let loadFiles = Folder.documents?.files
// let subFolders = Folder.documents?.subfolders
//
// print("JD451: loadfiles ••••••••• \(String(describing: loadFiles.debugDescription))")
// print("JD451: subfolders ••••••••• \(String(describing: subFolders))")
//
// for file in loadFiles! { //
// prefs.arrayOfURLStrings.append(file.url.absoluteString)
// }
//
// print("JD451: path DIRECTORY is --> \(path)")
// // print("JD451: path DIRECTORY is --> \(path)")
//
//
//
// print(prefs.arrayOfFolderNames)
// do {
// for i in prefs.arrayOfFolderNames {
// let loadFolderURL = try Folder.documents!.subfolder(named: i)
//
// for file in try Folder(path: loadFolderURL.path).files {
// prefs.arrayOfURLStrings.append(file.url.absoluteString)
// }
// }
// } catch {
// print("JD452: error loading files from download folder.", error)
// }
// } //End Func.

func getImageFromFileManager() {
// image = manager.getImage(name: imageName)
Expand Down
14 changes: 7 additions & 7 deletions Art_Athlete/Views/PhotosAssets/PhotoAssets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ struct PhotoAssets: View {
} // ForEach
} //List

List() {
ForEach(posePhotos) { photo in
//LandmarkRow(photo:photo)
// Image(photo.thumbnailName).cornerRadius(8)
Image(photo.photoName)
} // ForEach
} // List
// List() {
// ForEach(posePhotos) { photo in
// //LandmarkRow(photo:photo)
// // Image(photo.thumbnailName).cornerRadius(8)
// Image(photo.photoName)
// } // ForEach
// } // List
// } // NavView
} // Body
}
Expand Down
20 changes: 8 additions & 12 deletions Art_Athlete/pose_referenceApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ struct pose_referenceApp: App {

@AppStorage("isFirstLaunch") var isFirstLaunch = true

init() {
isFirstLaunch = true //For debugging.
print("JD00 → isFirstLaunch \(isFirstLaunch)")
if (isFirstLaunch) {
saveImage() //imageName: <#T##String#>, image: <#T##Data#>
// isFirstLaunch = false
}
}
// init() {
// isFirstLaunch = true //For debugging.
// print("JD00 → isFirstLaunch \(isFirstLaunch)")
// if (isFirstLaunch) {
// saveImage() //imageName: <#T##String#>, image: <#T##Data#>
// // isFirstLaunch = false
// }
// }


/*.~"~._.~"~._.~"~._.~"~._.~"~._.~"~._.~"~._.~"~._.~"~._.~"~._.~"~._.~"~._.~"~._.~"~.*/
Expand Down Expand Up @@ -118,10 +118,6 @@ struct pose_referenceApp: App {
} else {
print("2. JD00: NOT FOUND")
}




}

}
Expand Down

0 comments on commit c9fc432

Please sign in to comment.