From 9b276f55d2ea9b68cfa925a03a9c95201b3058e2 Mon Sep 17 00:00:00 2001 From: Gerrit Linnemann Date: Sat, 15 Aug 2020 20:25:38 +0200 Subject: [PATCH] Markieren der aktuellen Auswahl --- YAPS/YAPS/ContentView.swift | 98 ++++++++++++++++------------- YAPS/YAPS/FinderHelper.swift | 8 ++- YAPS/YAPS/Model/YapsFile.swift | 3 +- YAPS/YAPS/Shared.swift | 2 +- YAPS/YAPS/Struct/YapsFileCell.swift | 4 +- 5 files changed, 66 insertions(+), 49 deletions(-) diff --git a/YAPS/YAPS/ContentView.swift b/YAPS/YAPS/ContentView.swift index d57fac4..a7b15a6 100644 --- a/YAPS/YAPS/ContentView.swift +++ b/YAPS/YAPS/ContentView.swift @@ -15,56 +15,56 @@ struct ContentView: View { var body: some View { VStack { HStack { - VStack(alignment: .leading) { - HStack { - Button(action: { - let files = FinderHelper.shared.askForFolderAndGetFiles() - - if files.count > 0 { - self.fileList.removeAll() - self.fileList.append(contentsOf: files) + NavigationView { + VStack(alignment: .leading) { + HStack { + Button(action: { + let files = FinderHelper.shared.askForFolderAndGetFiles() + + if files.count > 0 { + self.fileList.removeAll() + + 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: { - if !Shared.shared.destinationDefined { - Shared.shared.destination = FinderHelper.shared.selectFolder(modalTitle: "Choose destination folder.") - Shared.shared.destinationDefined = true + List { + ForEach(self.fileList, id: \.self) { yapsFile in + YapsFileCell(focused: self.checkCurrentFile(yapsFile: yapsFile), yapsFile: yapsFile) + .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()) - .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 self.previewImg.self @@ -79,6 +79,20 @@ struct ContentView: View { .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 { diff --git a/YAPS/YAPS/FinderHelper.swift b/YAPS/YAPS/FinderHelper.swift index afe9348..57bfb06 100644 --- a/YAPS/YAPS/FinderHelper.swift +++ b/YAPS/YAPS/FinderHelper.swift @@ -41,19 +41,21 @@ class FinderHelper { func askForFolderAndGetFiles() -> [YapsFile] { 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 var fileList = [YapsFile]() do { 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 { - 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) + index += 1 } } catch { // failed to read directory – bad permissions, perhaps? diff --git a/YAPS/YAPS/Model/YapsFile.swift b/YAPS/YAPS/Model/YapsFile.swift index 2576171..5c19b31 100644 --- a/YAPS/YAPS/Model/YapsFile.swift +++ b/YAPS/YAPS/Model/YapsFile.swift @@ -10,7 +10,8 @@ import Foundation struct YapsFile: Identifiable, Hashable { let id = UUID() - + let index: Int let name: String let file: URL + var current: Bool } diff --git a/YAPS/YAPS/Shared.swift b/YAPS/YAPS/Shared.swift index 31e4e2a..28b4993 100644 --- a/YAPS/YAPS/Shared.swift +++ b/YAPS/YAPS/Shared.swift @@ -11,7 +11,7 @@ import Foundation class Shared: ObservableObject { 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 destinationDefined: Bool = false diff --git a/YAPS/YAPS/Struct/YapsFileCell.swift b/YAPS/YAPS/Struct/YapsFileCell.swift index 21fd316..b66dbae 100644 --- a/YAPS/YAPS/Struct/YapsFileCell.swift +++ b/YAPS/YAPS/Struct/YapsFileCell.swift @@ -10,7 +10,7 @@ import Foundation import SwiftUI struct YapsFileCell: View { - @State var focused: Bool = false + @State var focused: Bool var yapsFile : YapsFile @@ -21,7 +21,7 @@ struct YapsFileCell: View { FinderHelper.shared.getImageByFiletype(file: yapsFile.file).resizable().frame(width: 20, height: 20) // Text - Text(yapsFile.name) + Text("#\(yapsFile.index) \(yapsFile.name)") .foregroundColor(self.focused ? .blue : .gray) } }