Markieren der aktuellen Auswahl

This commit is contained in:
Gerrit Linnemann 2020-08-15 20:25:38 +02:00
parent 8ff01612a8
commit 9b276f55d2
5 changed files with 66 additions and 49 deletions

View File

@ -15,56 +15,56 @@ struct ContentView: View {
var body: some View { var body: some View {
VStack { VStack {
HStack { HStack {
VStack(alignment: .leading) { NavigationView {
HStack { VStack(alignment: .leading) {
Button(action: { HStack {
let files = FinderHelper.shared.askForFolderAndGetFiles() Button(action: {
let files = FinderHelper.shared.askForFolderAndGetFiles()
if files.count > 0 { if files.count > 0 {
self.fileList.removeAll() self.fileList.removeAll()
self.fileList.append(contentsOf: files)
self.fileList.append(contentsOf: files)
}
}) {
Text("Select Folder")
}
Button(action: {
if !Shared.shared.destinationDefined {
Shared.shared.destination = FinderHelper.shared.selectFolder(modalTitle: "Choose destination folder.")
Shared.shared.destinationDefined = true
}
FinderHelper.shared.iLikeThisImage(yapsFile: Shared.shared.currentFile, destination: Shared.shared.destination)
}) {
Text("Get It!")
} }
}) {
Text("Select Folder")
} }
Button(action: { List {
if !Shared.shared.destinationDefined { ForEach(self.fileList, id: \.self) { yapsFile in
Shared.shared.destination = FinderHelper.shared.selectFolder(modalTitle: "Choose destination folder.") YapsFileCell(focused: self.checkCurrentFile(yapsFile: yapsFile), yapsFile: yapsFile)
Shared.shared.destinationDefined = true .onTapGesture(perform: {
print("pressed \(yapsFile.name)")
self.previewImg = FinderHelper.shared.getImageByURL(source: yapsFile.file)
.resizable()
Shared.shared.currentFile = yapsFile
self.resetCurrentFile()
self.fileList[yapsFile.index].current = true
})
} }
FinderHelper.shared.iLikeThisImage(yapsFile: Shared.shared.currentFile, destination: Shared.shared.destination)
}) {
Text("Get It!")
}
}
List {
ForEach(self.fileList, id: \.self) { yapsFile in
YapsFileCell(yapsFile: yapsFile)
.focusable(true, onFocusChange: { (focusChanged) in
//self.focused = focusChanged
print("focus changed \(focusChanged)")
})
.onTapGesture(perform: {
let selectedImageURL: URL = yapsFile.file
print("pressed \(yapsFile.name)")
self.previewImg = FinderHelper.shared.getImageByURL(source: selectedImageURL)
.resizable()
Shared.shared.currentFile = yapsFile
})
} }
} }
.listStyle(SidebarListStyle()) .listStyle(SidebarListStyle())
.focusable()
.onMoveCommand { (direction) in
print("direction: \(direction)")
}
} }
.frame(width: 200.0) .focusable()
.onMoveCommand { (direction) in
print("direction: \(direction)")
print("#\(Shared.shared.currentFile.index) :: \(Shared.shared.currentFile.name)")
}
GeometryReader { geo in GeometryReader { geo in
self.previewImg.self self.previewImg.self
@ -79,6 +79,20 @@ struct ContentView: View {
.lineLimit(1) .lineLimit(1)
} }
} }
func checkCurrentFile(yapsFile: YapsFile) -> Bool {
let result: Bool = (Shared.shared.currentFile.index == yapsFile.index)
if result {
print("current file \(yapsFile.name)")
}
return result
}
func resetCurrentFile() {
for yapsFile in self.fileList {
self.fileList[yapsFile.index].current = false
}
}
} }
struct ContentView_Previews: PreviewProvider { struct ContentView_Previews: PreviewProvider {

View File

@ -41,19 +41,21 @@ class FinderHelper {
func askForFolderAndGetFiles() -> [YapsFile] { func askForFolderAndGetFiles() -> [YapsFile] {
let sourceFolder = selectFolder(modalTitle: "Select image folder") let sourceFolder = selectFolder(modalTitle: "Select image folder")
return getFilesList(path: sourceFolder) return getFileList(path: sourceFolder)
} }
func getFilesList(path: URL) -> [YapsFile] { func getFileList(path: URL) -> [YapsFile] {
let fm = FileManager.default let fm = FileManager.default
var fileList = [YapsFile]() var fileList = [YapsFile]()
do { do {
let items = try fm.contentsOfDirectory(at: path, includingPropertiesForKeys: nil, options: .skipsHiddenFiles).filter{ $0.pathExtension == "jpg" || $0.pathExtension == "jpeg" } let items = try fm.contentsOfDirectory(at: path, includingPropertiesForKeys: nil, options: .skipsHiddenFiles).filter{ $0.pathExtension == "jpg" || $0.pathExtension == "jpeg" }
var index = 0
for item in items { for item in items {
let yapsFile: YapsFile = YapsFile(name: item.lastPathComponent, file: item) let yapsFile: YapsFile = YapsFile(index: index, name: item.lastPathComponent, file: item, current: false)
fileList.append(yapsFile) fileList.append(yapsFile)
index += 1
} }
} catch { } catch {
// failed to read directory bad permissions, perhaps? // failed to read directory bad permissions, perhaps?

View File

@ -10,7 +10,8 @@ import Foundation
struct YapsFile: Identifiable, Hashable { struct YapsFile: Identifiable, Hashable {
let id = UUID() let id = UUID()
let index: Int
let name: String let name: String
let file: URL let file: URL
var current: Bool
} }

View File

@ -11,7 +11,7 @@ import Foundation
class Shared: ObservableObject { class Shared: ObservableObject {
static let shared = Shared() static let shared = Shared()
var currentFile: YapsFile = YapsFile(name: "EMPTY", file: .init(fileURLWithPath: "Y")) var currentFile: YapsFile = YapsFile(index: -1, name: "EMPTY", file: .init(fileURLWithPath: "Y"), current: false)
var destination: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] var destination: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
var destinationDefined: Bool = false var destinationDefined: Bool = false

View File

@ -10,7 +10,7 @@ import Foundation
import SwiftUI import SwiftUI
struct YapsFileCell: View { struct YapsFileCell: View {
@State var focused: Bool = false @State var focused: Bool
var yapsFile : YapsFile var yapsFile : YapsFile
@ -21,7 +21,7 @@ struct YapsFileCell: View {
FinderHelper.shared.getImageByFiletype(file: yapsFile.file).resizable().frame(width: 20, height: 20) FinderHelper.shared.getImageByFiletype(file: yapsFile.file).resizable().frame(width: 20, height: 20)
// Text // Text
Text(yapsFile.name) Text("#\(yapsFile.index) \(yapsFile.name)")
.foregroundColor(self.focused ? .blue : .gray) .foregroundColor(self.focused ? .blue : .gray)
} }
} }