Skip to content
Merged
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
4 changes: 2 additions & 2 deletions DataModel/Sources/DataModel/Model/DocumentModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,14 @@ public struct ProtoDocument: DocumentProtocol, Equatable, Sendable {
public var documentType: UInt?
public var correspondent: UInt?
public var tags: [UInt]
public var created: Date
public var created: Date?
public var storagePath: UInt?

public var customFields = CustomFieldRawEntryList()

public init(
title: String = "", asn: UInt? = nil, documentType: UInt? = nil, correspondent: UInt? = nil,
tags: [UInt] = [], created: Date = .now, storagePath: UInt? = nil
tags: [UInt] = [], created: Date? = .now, storagePath: UInt? = nil
) {
self.title = title
self.asn = asn
Expand Down
10 changes: 6 additions & 4 deletions Networking/Sources/Networking/Api/ApiRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,12 @@ extension ApiRepository: Repository {
mp.add(name: "correspondent", string: String(corr))
}

let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let createdStr = formatter.string(from: document.created)
mp.add(name: "created", string: createdStr)
if let created = document.created {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let createdStr = formatter.string(from: created)
mp.add(name: "created", string: createdStr)
}

if let dt = document.documentType {
mp.add(name: "document_type", string: String(dt))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ extension TransientRepository: Repository {
asn: document.asn,
documentType: document.documentType,
correspondent: document.correspondent,
created: document.created,
created: document.created ?? .now,
tags: document.tags,
added: .now,
modified: .now,
Expand Down
60 changes: 50 additions & 10 deletions swift-paperless/Views/Filter/ClearableDatePickerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,26 @@ import SwiftUI

struct ClearableDatePickerView: View {
@Binding var value: Date?
let title: LocalizedStringResource?

init(value: Binding<Date?>, title: LocalizedStringResource? = nil) {
self._value = value
self.title = title
}

var body: some View {
HStack {
Text(title ?? "")
.frame(maxWidth: .infinity, alignment: .leading)
if let unwrapped = Binding(unwrapping: $value) {
DatePicker(selection: unwrapped, displayedComponents: .date) {
Image(systemName: "xmark.circle.fill")
.foregroundColor(.secondary)
.accessibilityLabel(String(localized: .localizable(.dateClear)))
.contentShape(Rectangle())
.frame(maxWidth: .infinity, alignment: .trailing)
.onTapGesture {
value = nil
}
}
Image(systemName: "xmark.circle.fill")
.foregroundColor(.secondary)
.accessibilityLabel(String(localized: .localizable(.dateClear)))
.contentShape(Rectangle())
.onTapGesture {
value = nil
}
DatePicker(selection: unwrapped, displayedComponents: .date) {}
} else {
HStack {
Image(systemName: "plus.circle.fill")
Expand All @@ -37,3 +43,37 @@ struct ClearableDatePickerView: View {
.animation(.spring, value: value)
}
}

private struct ClearableDatePickerPreview: View {
@State private var emptyValue: Date?
@State private var setValue: Date?
@State private var noTitleValue: Date?

init() {
_emptyValue = State(initialValue: nil)
_setValue = State(initialValue: Date(timeIntervalSince1970: 0))
_noTitleValue = State(initialValue: nil)
}

var body: some View {
Form {
Section("With Title") {
ClearableDatePickerView(
value: $emptyValue,
title: "No Date Set"
)
ClearableDatePickerView(
value: $setValue,
title: "Date Set"
)
}
Section("No Title") {
ClearableDatePickerView(value: $noTitleValue)
}
}
}
}

#Preview("ClearableDatePickerView") {
ClearableDatePickerPreview()
}
56 changes: 50 additions & 6 deletions swift-paperless/Views/Import/CreateDocumentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ private struct DetailView: View {
}
}

extension ProtoDocument {
fileprivate var createdDate: Date {
get {
created ?? .now
}
set {
created = newValue
}
}
}

struct CreateDocumentView: View {
private enum Status {
case none
Expand All @@ -124,7 +135,10 @@ struct CreateDocumentView: View {
@State private var status = Status.none
@State private var isAsnValid = true

@AppStorage("CreateDocumentUseOriginalTitle", store: .group, )
@AppStorage("IncludeDocumentCreatedDate", store: .group)
private var includeCreatedDate = true

@AppStorage("CreateDocumentUseOriginalTitle", store: .group)
private var useOriginalTitle = false

private var isDocumentValid: Bool {
Expand All @@ -142,12 +156,14 @@ struct CreateDocumentView: View {
sourceUrl = url
let initialTitle = url.deletingPathExtension().lastPathComponent
.precomposedStringWithCanonicalMapping
_document = State(
initialValue: ProtoDocument(title: initialTitle))
self.callback = callback
self.share = share
self.title = title ?? String(localized: .localizable(.documentAdd))
thumbnailView = ThumbnailView(sourceUrl: sourceUrl)

let initialDocument = ProtoDocument(
title: initialTitle, created: includeCreatedDate ? .now : nil)
_document = State(initialValue: initialDocument)
}

func upload() async {
Expand Down Expand Up @@ -183,6 +199,7 @@ struct CreateDocumentView: View {
document.correspondent = nil
document.tags = []
document.storagePath = nil
document.created = includeCreatedDate ? .now : nil
}

private var filename: String {
Expand Down Expand Up @@ -246,9 +263,31 @@ struct CreateDocumentView: View {
DocumentAsnEditingView(document: $document, isValid: $isAsnValid)
.alignmentGuide(.listRowSeparatorLeading) { _ in 0 }

DatePicker(
String(localized: .localizable(.documentEditCreatedDateLabel)),
selection: $document.created, displayedComponents: .date)
// Toggle(
// .localizable(.documentEditCreatedDateLabel),
// isOn: $includeCreatedDate
// )

// .onChange(of: includeCreatedDate) { _, include in
// if include {
// if document.created == nil {
// document.created = .now
// }
// } else {
// document.created = nil
// }
// }

ClearableDatePickerView(
value: $document.created, title: .localizable(.documentEditCreatedDateLabel))

// if includeCreatedDate {
// DatePicker(
// String(localized: .localizable(.documentEditCreatedDateLabel)),
// selection: $document.createdDate,
// displayedComponents: .date
// )
// }

Toggle(.localizable(.documentUploadUseOriginalFilename), isOn: $useOriginalTitle)
}
Expand Down Expand Up @@ -413,6 +452,11 @@ struct CreateDocumentView: View {
default: break
}
}

// When the user adds or removes the date, we remember that for the future
.onChange(of: document.created) {
includeCreatedDate = document.created != nil
}
}

.errorOverlay(errorController: errorController, offset: 20)
Expand Down
Loading