Skip to main content

Overview

UploadHistoryStore manages the upload history by persisting completed uploads to UserDefaults. It provides an observable list of upload items that automatically syncs to disk, allowing users to track their upload activity across app launches.
Located at: Fiaxe/Services/UploadHistoryStore.swift:6

Type Definition

An observable class that automatically notifies SwiftUI views when the history changes.

Properties

items

The current list of upload history items.
[UploadItem]
Array of completed uploads, sorted with newest first. Automatically persisted to UserDefaults.

Methods

add()

Adds a new upload item to the history.
UploadItem
required
The upload item to add to history. Contains file name, size, R2 key, upload date, and public URL.

Implementation Details

The method:
  1. Inserts the new item at index 0 (beginning of array)
  2. Maintains reverse chronological order (newest first)
  3. Automatically persists to UserDefaults

remove()

Removes upload items at the specified indices.
IndexSet
required
Indices of items to remove. Typically provided by SwiftUI’s onDelete modifier.

Implementation Details

Used with SwiftUI’s List deletion:

clearAll()

Removes all items from the history.
Useful for implementing a “Clear History” button.

Implementation Details

Persistence Implementation

The store uses UserDefaults with JSON encoding:
String
UserDefaults key: "fiaxe.uploadHistory"

Initialization

The store automatically loads saved history on initialization:
History is restored when the app launches.

Data Format

History is stored as JSON-encoded array of UploadItem objects:

Usage Example

Adding Items After Upload

Automatic Persistence

Zero-Effort PersistenceThe store automatically saves to UserDefaults after every modification:
  • add() saves immediately
  • remove() saves immediately
  • clearAll() saves immediately
No manual save calls are needed.

Observable Behavior

The @Observable macro enables automatic SwiftUI updates:
Views automatically refresh when:
  • Items are added
  • Items are removed
  • All items are cleared

Integration with r2Vault

The upload history is integrated into the menu bar popover:
Users can:
  • View recent uploads
  • Copy public URLs
  • Delete individual items
  • Clear entire history

Data Retention

Unlimited HistoryThe current implementation stores all uploads indefinitely. For production use, consider:
  • Limiting to the most recent N uploads
  • Auto-deleting items older than X days
  • Implementing search/filter functionality
  • Adding pagination for large histories

Privacy Considerations

Stored in Plain TextUpload history is stored in UserDefaults without encryption:
  • File names are visible
  • Public URLs are visible
  • Upload dates are visible
  • R2 keys are visible
For sensitive uploads, consider:
  • Adding encryption for stored data
  • Providing a “Clear History on Quit” option
  • Implementing automatic cleanup
  • Storing less identifying information

Error Handling

The store gracefully handles encoding/decoding errors:
Failures result in:
  • Empty history on load failure
  • No persistence on save failure
  • No crashes or error messages

Testing Considerations

For testing, use a separate UserDefaults suite: