Skip to content

Commit

Permalink
fix: FileManager as source of truth
Browse files Browse the repository at this point in the history
  • Loading branch information
Ambrdctr committed Oct 23, 2024
1 parent 6e376b2 commit cb7aa88
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 207 deletions.
64 changes: 18 additions & 46 deletions SwissTransferCore/DisplayableFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,64 +19,36 @@
import Foundation

public class DisplayableFile: Identifiable, Hashable {
public let id: String
public var id: String {
url.path()
}

public let name: String
public let isFolder: Bool

public var children = [DisplayableFile]()
public var parent: DisplayableFile?

// Real Files property
public var url: URL?
private var size: Int64 = 0
public var url: URL
public var size: Int64 = 0
public var mimeType = ""

/// Fake folder init
public init(folderName: String) {
id = UUID().uuidString
name = folderName
isFolder = true
}

public init(uploadFile: UploadFile) {
id = uploadFile.id
name = uploadFile.url.lastPathComponent
url = uploadFile.url
size = uploadFile.size
mimeType = uploadFile.mimeType
isFolder = false
}

public var computedSize: Int64 {
if isFolder {
return children.map { $0.computedSize }.reduce(0, +)
}
return size
}

/// Return all file children in the tree (no folder)
public func computedChildren() async -> [DisplayableFile] {
var array = [DisplayableFile]()
if isFolder {
for element in children {
await array.append(contentsOf: element.computedChildren())
}
} else {
array.append(self)
}
return array
public init?(url: URL) {
guard let resources = try? url.resourceValues(forKeys: [
.isDirectoryKey,
.nameKey
]) else { return nil }

self.url = url
name = url.lastPathComponent
isFolder = resources.isDirectory ?? false
size = Int64(url.size())
mimeType = url.typeIdentifier ?? ""
}

public func hash(into hasher: inout Hasher) {
hasher.combine(id)

_ = children.map {
hasher.combine($0.hashValue)
}
}

public static func == (lhs: DisplayableFile, rhs: DisplayableFile) -> Bool {
lhs.id == rhs.id && lhs.children == rhs.children
lhs.id == rhs.id
}
}

Expand Down
10 changes: 9 additions & 1 deletion SwissTransferCore/UploadFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

import Foundation
import OSLog

public class UploadFile: Identifiable {
public var id: String {
Expand All @@ -36,8 +37,15 @@ public class UploadFile: Identifiable {
]), resources.isDirectory == false else { return nil }

self.url = url
path = resources.name ?? url.lastPathComponent
size = Int64(resources.fileSize ?? 0)
mimeType = url.typeIdentifier ?? ""

do {
let baseURL = try URL.tmpUploadDirectory()
path = String(url.path().trimmingPrefix(baseURL.path()))
} catch {
Logger.general.error("Error while constructing file path: \(url) \(error.localizedDescription)")
return nil
}
}
}
24 changes: 11 additions & 13 deletions SwissTransferFeatures/NewTransferView/FileListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ struct FileListView: View {
@Environment(\.dismiss) private var dismiss
@EnvironmentObject private var newTransferManager: NewTransferManager

private var files: [DisplayableFile] {
if let folder {
return folder.children
}
return newTransferManager.displayableFiles
}
@State private var files = [DisplayableFile]()

private let folder: DisplayableFile?

Expand All @@ -40,7 +35,7 @@ struct FileListView: View {
}

private var filesSize: Int64 {
files.map { $0.computedSize }.reduce(0, +)
files.map { $0.size }.reduce(0, +)
}

private let columns = [
Expand All @@ -64,21 +59,21 @@ struct FileListView: View {
ForEach(files) { file in
if file.isFolder {
NavigationLink(value: file) {
LargeFileCell(folderName: file.name, folderSize: file.computedSize) {
Task {
await newTransferManager.remove(file: file)
LargeFileCell(folderName: file.name, folderSize: file.size) {
newTransferManager.remove(file: file) {
files = newTransferManager.filesAt(folderURL: folder?.url)
}
}
}
} else {
LargeFileCell(
fileName: file.name,
fileSize: file.computedSize,
fileSize: file.size,
url: file.url,
mimeType: file.mimeType
) {
Task {
await newTransferManager.remove(file: file)
newTransferManager.remove(file: file) {
files = newTransferManager.filesAt(folderURL: folder?.url)
}
}
}
Expand All @@ -88,6 +83,9 @@ struct FileListView: View {
}
.padding(value: .medium)
}
.onAppear {
files = newTransferManager.filesAt(folderURL: folder?.url)
}
.onChange(of: files) { _ in
if files.isEmpty {
dismiss()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ struct NewTransferFilesCellView: View {
@EnvironmentObject private var newTransferManager: NewTransferManager

@State private var isShowingFileList = false
@State private var files = [DisplayableFile]()

private var filesSize: Int64 {
newTransferManager.displayableFiles.map { $0.computedSize }.reduce(0, +)
files.map { $0.size }.reduce(0, +)
}

var body: some View {
Expand All @@ -41,7 +42,7 @@ struct NewTransferFilesCellView: View {
NavigationLink(value: DisplayableRootFolder()) {
HStack {
Text(
"\(STResourcesStrings.Localizable.filesCount(newTransferManager.displayableFiles.count)) · \(filesSize.formatted(.defaultByteCount))"
"\(STResourcesStrings.Localizable.filesCount(files.count)) · \(filesSize.formatted(.defaultByteCount))"
)
.font(.ST.callout)
.frame(maxWidth: .infinity, alignment: .leading)
Expand All @@ -56,7 +57,7 @@ struct NewTransferFilesCellView: View {
ScrollView(.horizontal) {
HStack(spacing: IKPadding.medium) {
AddFilesMenuView { urls in
newTransferManager.addFiles(urls: urls)
files = newTransferManager.addFiles(urls: urls)
} label: {
STResourcesAsset.Images.plus.swiftUIImage
.iconSize(.large)
Expand All @@ -65,19 +66,19 @@ struct NewTransferFilesCellView: View {
.background(Color.ST.background, in: .rect(cornerRadius: IKRadius.large))
}

ForEach(newTransferManager.displayableFiles) { file in
ForEach(files) { file in
if file.isFolder {
NavigationLink(value: file) {
SmallThumbnailView {
Task {
await newTransferManager.remove(file: file)
newTransferManager.remove(file: file) {
files = newTransferManager.filesAt(folderURL: nil)
}
}
}
} else {
SmallThumbnailView(url: file.url, mimeType: file.mimeType) {
Task {
await newTransferManager.remove(file: file)
newTransferManager.remove(file: file) {
files = newTransferManager.filesAt(folderURL: nil)
}
}
}
Expand All @@ -93,6 +94,9 @@ struct NewTransferFilesCellView: View {
.background(Color.ST.cardBackground, in: .rect(cornerRadius: IKRadius.large))
}
.padding(.horizontal, value: .medium)
.onAppear {
files = newTransferManager.filesAt(folderURL: nil)
}
}
}

Expand Down
Loading

0 comments on commit cb7aa88

Please sign in to comment.