Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Open transferDetail in a sheet #90

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions SwissTransferCore/Array+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Infomaniak SwissTransfer - iOS App
Copyright (C) 2024 Infomaniak Network SA

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import Foundation

public extension Array where Element: DisplayableFile {
func filesSize() -> Int64 {
Ambrdctr marked this conversation as resolved.
Show resolved Hide resolved
return map { $0.fileSize }.reduce(0, +)
}
}
46 changes: 10 additions & 36 deletions SwissTransferCore/DisplayableFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,16 @@

import Foundation

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

public let name: String
public let isFolder: Bool

public let url: URL
public let size: Int64
public let mimeType: String

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)
}

public static func == (lhs: DisplayableFile, rhs: DisplayableFile) -> Bool {
lhs.id == rhs.id
}
public protocol DisplayableFile: Identifiable, Hashable {
var uid: String { get }
var isFolder: Bool { get }
var fileName: String { get }
var fileSize: Int64 { get }
var mimeType: String? { get }

func localURL(in container: String) -> URL?
}

public struct DisplayableRootFolder: Identifiable, Hashable {
public let id = UUID()

public init() {}
public extension DisplayableFile {
var id: String { uid }
}
10 changes: 8 additions & 2 deletions SwissTransferCore/File+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@
import OSLog
import STCore

extension FileUi: DisplayableFile {}

Check warning on line 23 in SwissTransferCore/File+Extension.swift

View workflow job for this annotation

GitHub Actions / Build and Test project

extension declares a conformance of imported type 'FileUi' to imported protocol 'Identifiable'; this will not behave correctly if the owners of 'STCore' introduce this conformance in the future

Check warning on line 23 in SwissTransferCore/File+Extension.swift

View workflow job for this annotation

GitHub Actions / Build and Test project

extension declares a conformance of imported type 'FileUi' to imported protocol 'Identifiable'; this will not behave correctly if the owners of 'STCore' introduce this conformance in the future

public extension FileUi {
func localURL(in transfer: TransferUi) -> URL? {
func localURL(in container: String) -> URL? {
return try? URL.tmpDownloadsDirectory()
.appendingPathComponent("\(transfer.uuid)/")
.appendingPathComponent("\(container)/")
.appendingPathComponent("\(uid)/")
.appendingPathComponent(fileName)
}

func localURL(in transfer: TransferUi) -> URL? {
localURL(in: transfer.uuid)
}
}
6 changes: 6 additions & 0 deletions SwissTransferCore/Transfer+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
import Foundation
import STCore

extension TransferUi: Identifiable {

Check warning on line 22 in SwissTransferCore/Transfer+Extension.swift

View workflow job for this annotation

GitHub Actions / Build and Test project

extension declares a conformance of imported type 'TransferUi' to imported protocol 'Identifiable'; this will not behave correctly if the owners of 'STCore' introduce this conformance in the future

Check warning on line 22 in SwissTransferCore/Transfer+Extension.swift

View workflow job for this annotation

GitHub Actions / Build and Test project

extension declares a conformance of imported type 'TransferUi' to imported protocol 'Identifiable'; this will not behave correctly if the owners of 'STCore' introduce this conformance in the future
public var id: String {
uuid
}
}

public extension TransferUi {
var name: String {
return date.formatted(.prettyDate)
Expand Down
75 changes: 75 additions & 0 deletions SwissTransferCore/TransferableFile.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Infomaniak SwissTransfer - iOS App
Copyright (C) 2024 Infomaniak Network SA

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import Foundation
import STCore

public class TransferableFile: Hashable, DisplayableFile {
public var uid: String {
localURL.path()
}

public var fileName: String
public var fileSize: Int64
private var localURL: URL
public var mimeType: String?
public var isFolder: Bool

public func localURL(in container: String) -> URL? {
localURL
}

public init?(url: URL) {
guard let resources = try? url.resourceValues(forKeys: [
.isDirectoryKey,
.nameKey
]) else { return nil }

localURL = url
fileName = url.lastPathComponent
isFolder = resources.isDirectory ?? false
fileSize = Int64(url.size())
mimeType = url.typeIdentifier ?? ""
}

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

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

public struct TransferableRootFolder: Identifiable, Hashable {
public let id = UUID()

public init() {}
}

public struct RemoveFileAction {
private let action: (TransferableFile) -> Void

public init(action: @escaping (TransferableFile) -> Void) {
self.action = action
}

public func callAsFunction(file: TransferableFile) {
action(file)
}
}
10 changes: 10 additions & 0 deletions SwissTransferCore/Utils/PreviewHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ import Foundation
import STCore

public enum PreviewHelper {
public static let sampleFolder = FileUi(
uid: "folderUUID",
fileName: "Folder name",
path: nil,
isFolder: true,
fileSize: 8561,
mimeType: nil,
localPath: nil
)

public static let sampleFile = FileUi(
uid: "fileUUID",
fileName: "File name",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,18 @@ struct IdentifiableURL: Identifiable {
let url: URL
}

struct DownloadButton: View {
public struct DownloadButton: View {
@EnvironmentObject private var downloadManager: DownloadManager

@State private var downloadedTransferURL: IdentifiableURL?

let transfer: TransferUi

var body: some View {
public init(transfer: TransferUi) {
self.transfer = transfer
}

public var body: some View {
Button(action: startOrCancelDownloadIfNeeded) {
Label(
title: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import OSLog
import STCore
import SwiftUI
import SwissTransferCore
import SwissTransferCoreUI

struct DownloadableFileCellView: View {
@EnvironmentObject private var downloadManager: DownloadManager
Expand All @@ -34,12 +33,7 @@ struct DownloadableFileCellView: View {

var body: some View {
Button(action: startOrCancelDownloadIfNeeded) {
LargeFileCell(
fileName: file.fileName,
fileSize: file.fileSize,
url: file.localURL(in: transfer),
mimeType: file.mimeType ?? ""
)
LargeFileCell(file: file, container: transfer.uuid)
}
.buttonStyle(.plain)
.downloadProgressAlertFor(transfer: transfer, file: file) { downloadedFileURL in
Expand Down
74 changes: 74 additions & 0 deletions SwissTransferCoreUI/Components/FileGridView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Infomaniak SwissTransfer - iOS App
Copyright (C) 2024 Infomaniak Network SA

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import InfomaniakCoreSwiftUI
import STCore
import SwiftUI
import SwissTransferCore

public struct FileGridView: View {
private let columns = [
GridItem(.flexible(), spacing: IKPadding.medium),
GridItem(.flexible(), spacing: IKPadding.medium)
]

private let files: [any DisplayableFile]
private let transfer: TransferUi?
private let removeAction: RemoveFileAction?

public init(files: [any DisplayableFile], transfer: TransferUi?, removeAction: RemoveFileAction? = nil) {
self.files = files
self.transfer = transfer
self.removeAction = removeAction
}

public var body: some View {
LazyVGrid(
columns: columns,
alignment: .center,
spacing: IKPadding.medium,
pinnedViews: []
) {
ForEach(files, id: \.id) { file in
if file.isFolder {
NavigationLink(value: file) {
LargeFileCell(
file: file,
container: transfer?.uuid,
removeAction: removeAction
)
}
} else {
if let transfer, let fileUi = file as? FileUi {
DownloadableFileCellView(transfer: transfer, file: fileUi)
} else {
LargeFileCell(
file: file,
container: transfer?.uuid,
removeAction: removeAction
)
}
}
}
}
}
}

#Preview {
FileGridView(files: [], transfer: nil)
}
Loading
Loading