2022-12-03 06:56:38 +01:00
|
|
|
//
|
|
|
|
// DocumentItem.swift
|
|
|
|
// Browse
|
|
|
|
//
|
|
|
|
// Created by Javier Cicchelli on 03/12/2022.
|
|
|
|
// Copyright © 2022 Röck+Cöde. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2022-12-14 23:57:20 +01:00
|
|
|
import DataModels
|
2022-12-03 06:56:38 +01:00
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct DocumentItem: View {
|
|
|
|
|
|
|
|
// MARK: Properties
|
|
|
|
|
2022-12-16 17:20:19 +01:00
|
|
|
let item: any FileSystemItem
|
2022-12-14 23:57:20 +01:00
|
|
|
let select: ActionClosure
|
|
|
|
let download: ActionClosure
|
|
|
|
let delete: ActionClosure
|
2022-12-14 01:43:31 +01:00
|
|
|
|
2022-12-16 00:10:42 +01:00
|
|
|
private let dateAdapter = DateAdapter()
|
|
|
|
private let sizeAdapter = SizeAdapter()
|
|
|
|
|
2022-12-03 06:56:38 +01:00
|
|
|
// MARK: Body
|
|
|
|
|
|
|
|
var body: some View {
|
2022-12-14 01:43:31 +01:00
|
|
|
Button {
|
2022-12-14 23:57:20 +01:00
|
|
|
select()
|
2022-12-14 01:43:31 +01:00
|
|
|
} label: {
|
|
|
|
HStack(spacing: 16) {
|
|
|
|
Image.document
|
|
|
|
.icon(size: 32)
|
|
|
|
.foregroundColor(.red)
|
2022-12-03 06:56:38 +01:00
|
|
|
|
2022-12-14 01:43:31 +01:00
|
|
|
VStack(spacing: 8) {
|
|
|
|
Text(item.name)
|
|
|
|
.itemName()
|
2022-12-03 06:56:38 +01:00
|
|
|
|
2022-12-14 01:43:31 +01:00
|
|
|
HStack {
|
2022-12-16 00:10:42 +01:00
|
|
|
Text(dateAdapter(value: document?.lastModifiedAt))
|
2022-12-14 01:43:31 +01:00
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
2022-12-16 00:10:42 +01:00
|
|
|
Text(sizeAdapter(value: document?.size))
|
2022-12-14 01:43:31 +01:00
|
|
|
}
|
|
|
|
.font(.subheadline)
|
|
|
|
.foregroundColor(.secondary)
|
2022-12-03 06:56:38 +01:00
|
|
|
}
|
|
|
|
}
|
2022-12-14 01:43:31 +01:00
|
|
|
.padding(.vertical, 4)
|
2022-12-03 06:56:38 +01:00
|
|
|
}
|
2022-12-14 01:17:26 +01:00
|
|
|
.swipeActions(
|
|
|
|
edge: .trailing,
|
|
|
|
allowsFullSwipe: true
|
|
|
|
) {
|
|
|
|
Button {
|
2022-12-14 23:57:20 +01:00
|
|
|
delete()
|
2022-12-14 01:17:26 +01:00
|
|
|
} label: {
|
|
|
|
Label {
|
|
|
|
Text(
|
|
|
|
"browse.swipe_action.delete_item.text",
|
|
|
|
bundle: .module
|
|
|
|
)
|
|
|
|
} icon: {
|
|
|
|
Image.trash
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.tint(.red)
|
2022-12-14 01:43:31 +01:00
|
|
|
|
2022-12-14 01:17:26 +01:00
|
|
|
Button {
|
2022-12-14 23:57:20 +01:00
|
|
|
download()
|
2022-12-14 01:17:26 +01:00
|
|
|
} label: {
|
|
|
|
Label {
|
|
|
|
Text(
|
|
|
|
"browse.swipe_action.download_item.text",
|
|
|
|
bundle: .module
|
|
|
|
)
|
|
|
|
} icon: {
|
|
|
|
Image.download
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.tint(.orange)
|
|
|
|
}
|
2022-12-03 06:56:38 +01:00
|
|
|
}
|
2022-12-14 01:43:31 +01:00
|
|
|
|
2022-12-03 06:56:38 +01:00
|
|
|
}
|
|
|
|
|
2022-12-14 01:17:26 +01:00
|
|
|
// MARK: - Helpers
|
|
|
|
|
|
|
|
private extension DocumentItem {
|
|
|
|
var document: Document? { item as? Document }
|
|
|
|
}
|
|
|
|
|
2022-12-03 06:56:38 +01:00
|
|
|
// MARK: - Image+Constants
|
|
|
|
|
|
|
|
private extension Image {
|
|
|
|
static let document = Image(systemName: "doc.fill")
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Previews
|
|
|
|
|
|
|
|
struct DocumentItem_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2022-12-14 01:17:26 +01:00
|
|
|
DocumentItem(item: Document(
|
|
|
|
id: "1234567890",
|
2022-12-03 06:56:38 +01:00
|
|
|
name: "Some document name goes in here...",
|
2022-12-14 01:17:26 +01:00
|
|
|
contentType: "some content type",
|
|
|
|
size: .random(in: 1 ... 100),
|
|
|
|
lastModifiedAt: .now
|
2022-12-14 23:57:20 +01:00
|
|
|
)) {
|
|
|
|
// select closure.
|
|
|
|
} download: {
|
|
|
|
// download closure.
|
|
|
|
} delete: {
|
|
|
|
// delete closure.
|
2022-12-14 01:17:26 +01:00
|
|
|
}
|
2022-12-03 06:56:38 +01:00
|
|
|
.previewDisplayName("Document item")
|
|
|
|
|
2022-12-14 01:17:26 +01:00
|
|
|
DocumentItem(item: Document(
|
|
|
|
id: "1234567890",
|
2022-12-03 06:56:38 +01:00
|
|
|
name: "Some very, extremely long document name goes in here...",
|
2022-12-14 01:17:26 +01:00
|
|
|
contentType: "some content type",
|
|
|
|
size: .random(in: 1 ... 100),
|
|
|
|
lastModifiedAt: .now
|
2022-12-14 23:57:20 +01:00
|
|
|
)) {
|
|
|
|
// select closure.
|
|
|
|
} download: {
|
|
|
|
// download closure.
|
|
|
|
} delete: {
|
|
|
|
// delete closure.
|
2022-12-14 01:17:26 +01:00
|
|
|
}
|
2022-12-03 06:56:38 +01:00
|
|
|
.previewDisplayName("Document item with long name")
|
|
|
|
}
|
|
|
|
}
|