Skip to content
This repository has been archived by the owner on Nov 13, 2020. It is now read-only.

Commit

Permalink
Make dateModified optional in outline view
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmeuli committed May 20, 2020
1 parent 9da6001 commit 0c8dca8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
4 changes: 2 additions & 2 deletions QLPlugin/Utils/FileTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class FileTree {
/// Parses the provided file/directory's path and creates a new `FileTreeNode` at the correct
/// position in the tree. If a file/directory's parent directory doesn't exist yet, it will
/// be created (with `dateModified` set to `nil`).
func addNode(path: String, isDirectory: Bool, size: Int, dateModified: Date) throws {
func addNode(path: String, isDirectory: Bool, size: Int, dateModified: Date?) throws {
try addNode(
parentNode: root,
pathParts: path.split(separator: "/", omittingEmptySubsequences: true),
Expand All @@ -75,7 +75,7 @@ class FileTree {
pathPartIndex: Int,
isDirectory: Bool,
size: Int,
dateModified: Date
dateModified: Date?
) throws {
let isLastPathPart = pathPartIndex == pathParts.count - 1
let name = String(pathParts[pathPartIndex])
Expand Down
29 changes: 29 additions & 0 deletions QLPlugin/Views/PreviewVCs/OutlinePreviewVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class OutlinePreviewVC: NSViewController, PreviewVC {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)

// Register required value transformers
ValueTransformer.setValueTransformer(DateTransformer(), forName: .dateTransformerName)
ValueTransformer.setValueTransformer(IconTransformer(), forName: .iconTransformerName)
ValueTransformer.setValueTransformer(SizeTransformer(), forName: .sizeTransformerName)
}
Expand Down Expand Up @@ -59,6 +60,33 @@ class OutlinePreviewVC: NSViewController, PreviewVC {
}
}

/// `ValueTransformer` which formats the provided date.
class DateTransformer: ValueTransformer {
let dateFormatter = DateFormatter()
let fallbackValue = "--"

override init() {
// Use same date format as Finder
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .short
dateFormatter.doesRelativeDateFormatting = true
}

override class func transformedValueClass() -> AnyClass { NSString.self }

override class func allowsReverseTransformation() -> Bool { false }

override func transformedValue(_ value: Any?) -> Any? {
guard let date = value as? Date else {
return nil
}

// Dates which are `nil` are passed to this function as epoch dates (default value). If
// this is the case, return "--" instead (same behavior as Finder)
return date.timeIntervalSince1970 == 0 ? fallbackValue : dateFormatter.string(from: date)
}
}

/// `ValueTransformer` which returns the correct icon depending on whether the current row
/// represents a file or directory.
class IconTransformer: ValueTransformer {
Expand Down Expand Up @@ -104,6 +132,7 @@ class SizeTransformer: ValueTransformer {
}

extension NSValueTransformerName {
static let dateTransformerName = NSValueTransformerName(rawValue: "DateTransformer")
static let iconTransformerName = NSValueTransformerName(rawValue: "IconTransformer")
static let sizeTransformerName = NSValueTransformerName(rawValue: "SizeTransformer")
}
8 changes: 5 additions & 3 deletions QLPlugin/Views/PreviewVCs/OutlinePreviewVC.xib
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="16096" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="16096"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand Down Expand Up @@ -107,13 +106,16 @@
<rect key="frame" x="0.0" y="0.0" width="200" height="17"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" lineBreakMode="truncatingTail" sendsActionOnEndEditing="YES" title="Table View Cell" id="EnR-V3-kK2">
<dateFormatter key="formatter" dateStyle="medium" timeStyle="short" doesRelativeDateFormatting="YES" id="dg9-ex-Uou"/>
<font key="font" metaFont="system"/>
<color key="textColor" name="secondaryLabelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<connections>
<binding destination="bGz-mP-JvV" name="value" keyPath="objectValue.dateModified" id="HTZ-fQ-kiV"/>
<binding destination="bGz-mP-JvV" name="value" keyPath="objectValue.dateModified" id="yWK-4r-mhv">
<dictionary key="options">
<string key="NSValueTransformerName">DateTransformer</string>
</dictionary>
</binding>
</connections>
</textField>
</subviews>
Expand Down
2 changes: 1 addition & 1 deletion QLPlugin/Views/Previews/TARPreview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class TARPreview: Preview {
for fileMatch in fileMatches {
let permissions = fileMatch[1]
let size = Int(fileMatch[2]) ?? 0
let dateModified = parseDate(dateString: fileMatch[3]) ?? Date()
let dateModified = parseDate(dateString: fileMatch[3])
let path = fileMatch[4]
do {
// Add file/directory node to tree
Expand Down
2 changes: 1 addition & 1 deletion QLPlugin/Views/Previews/ZIPPreview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ZIPPreview: Preview {
for fileMatch in fileMatches {
let permissions = fileMatch[1]
let size = Int(fileMatch[2]) ?? 0
let dateModified = dateFormatter.date(from: fileMatch[3]) ?? Date()
let dateModified = dateFormatter.date(from: fileMatch[3])
let path = fileMatch[4]
// Ignore "__MACOSX" subdirectory (ZIP resource fork created by macOS)
if !path.hasPrefix("__MACOSX") {
Expand Down

0 comments on commit 0c8dca8

Please sign in to comment.