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
48 changes: 48 additions & 0 deletions Piperly.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

29 changes: 28 additions & 1 deletion Piperly/App/PiperlyApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,38 @@ import SwiftUI

@main
struct PiperlyApp: App {
@StateObject private var bookStore = BookStore()
@StateObject private var bookStore: BookStore
private let ttsEngine = TTSEngine()
@State private var showVoiceSetup = false
@Environment(\.scenePhase) private var scenePhase

@MainActor
init() {
let router = LibrarySyncRouter()
let store = BookStore(librarySync: router)
_bookStore = StateObject(wrappedValue: store)

guard CloudKitLibrarySync.isEnabled() else { return }
Task {
guard let cloudSync = try? CloudKitLibrarySync(
enabled: true,
localSnapshotProvider: { [weak store] in
store?.librarySnapshotRecords() ?? []
},
remoteChangeHandler: { [weak store] changes, scope in
guard let store else { return .complete }
if scope.isCurrentAccount {
return router.applyingCurrentAccountChanges(scope: scope) {
store.applyRemoteChanges(changes)
}
}
return store.applyRemoteChanges(changes)
}
) else { return }
await router.use(cloudSync)
}
}

var body: some Scene {
WindowGroup {
ContentView(ttsEngine: ttsEngine)
Expand Down
30 changes: 19 additions & 11 deletions Piperly/Models/Book.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,41 @@ struct Book: Identifiable, Codable {
let author: String
let fileName: String
var coverImageName: String?
var modifiedAt: Date

init(
id: UUID = UUID(),
contentIdentity: String,
title: String,
author: String,
fileName: String,
coverImageName: String? = nil
coverImageName: String? = nil,
modifiedAt: Date = .now
) {
self.id = id
self.contentIdentity = contentIdentity
self.title = title
self.author = author
self.fileName = fileName
self.coverImageName = coverImageName
self.modifiedAt = modifiedAt
}

private enum CodingKeys: String, CodingKey {
case id, contentIdentity, title, author, fileName, coverImageName, modifiedAt
}

/// Legacy blobs predate `contentIdentity`; default it to "" so the book
/// survives decode. BookStore backfills the real hash (and migrates the
/// filename) on load. See BookStore.backfillContentIdentities().
init(from decoder: Decoder) throws {
let c = try decoder.container(keyedBy: CodingKeys.self)
id = try c.decode(UUID.self, forKey: .id)
contentIdentity = try c.decodeIfPresent(String.self, forKey: .contentIdentity) ?? ""
title = try c.decode(String.self, forKey: .title)
author = try c.decode(String.self, forKey: .author)
fileName = try c.decode(String.self, forKey: .fileName)
coverImageName = try c.decodeIfPresent(String.self, forKey: .coverImageName)
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(UUID.self, forKey: .id)
// Legacy blobs predate contentIdentity; default to "" so the book
// survives decode. BookStore.backfillContentIdentities() hashes the
// on-disk file and migrates the filename on load.
contentIdentity = try container.decodeIfPresent(String.self, forKey: .contentIdentity) ?? ""
title = try container.decode(String.self, forKey: .title)
author = try container.decode(String.self, forKey: .author)
fileName = try container.decode(String.self, forKey: .fileName)
coverImageName = try container.decodeIfPresent(String.self, forKey: .coverImageName)
modifiedAt = try container.decodeIfPresent(Date.self, forKey: .modifiedAt) ?? .distantPast
}
}
34 changes: 21 additions & 13 deletions Piperly/Models/Bookmark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct Bookmark: Identifiable, Codable, Hashable, ProfileScoped {
let progression: Double
let sticker: BookmarkSticker
let createdAt: Date
var modifiedAt: Date

init(
id: UUID = UUID(),
Expand All @@ -48,7 +49,8 @@ struct Bookmark: Identifiable, Codable, Hashable, ProfileScoped {
title: String?,
progression: Double,
sticker: BookmarkSticker,
createdAt: Date = .now
createdAt: Date = .now,
modifiedAt: Date = .now
) {
self.id = id
self.profileID = profileID
Expand All @@ -58,24 +60,30 @@ struct Bookmark: Identifiable, Codable, Hashable, ProfileScoped {
self.progression = progression
self.sticker = sticker
self.createdAt = createdAt
self.modifiedAt = modifiedAt
}

private enum CodingKeys: String, CodingKey {
case id, profileID, bookID, locatorJSON, title, progression, sticker, createdAt, modifiedAt
}

/// Legacy blobs predate `profileID`; default it to a sentinel so the record
/// survives decode. BookStore rehomes the sentinel onto the active profile.
init(from decoder: Decoder) throws {
let c = try decoder.container(keyedBy: CodingKeys.self)
id = try c.decode(UUID.self, forKey: .id)
profileID = try c.decodeIfPresent(UUID.self, forKey: .profileID) ?? ProfileScopedDefaults.legacyProfileID
bookID = try c.decode(UUID.self, forKey: .bookID)
locatorJSON = try c.decode(String.self, forKey: .locatorJSON)
title = try c.decodeIfPresent(String.self, forKey: .title)
progression = try c.decode(Double.self, forKey: .progression)
sticker = try c.decode(BookmarkSticker.self, forKey: .sticker)
createdAt = try c.decode(Date.self, forKey: .createdAt)
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(UUID.self, forKey: .id)
// Legacy blobs predate profileID; default to a sentinel so the record
// survives decode. BookStore rehomes it onto the active profile.
profileID = try container.decodeIfPresent(UUID.self, forKey: .profileID) ?? ProfileScopedDefaults.legacyProfileID
bookID = try container.decode(UUID.self, forKey: .bookID)
locatorJSON = try container.decode(String.self, forKey: .locatorJSON)
title = try container.decodeIfPresent(String.self, forKey: .title)
progression = try container.decode(Double.self, forKey: .progression)
sticker = try container.decode(BookmarkSticker.self, forKey: .sticker)
createdAt = try container.decode(Date.self, forKey: .createdAt)
modifiedAt = try container.decodeIfPresent(Date.self, forKey: .modifiedAt) ?? createdAt
}

func withProfileID(_ id: UUID) -> Bookmark {
Bookmark(id: self.id, profileID: id, bookID: bookID, locatorJSON: locatorJSON, title: title, progression: progression, sticker: sticker, createdAt: createdAt)
Bookmark(id: self.id, profileID: id, bookID: bookID, locatorJSON: locatorJSON, title: title, progression: progression, sticker: sticker, createdAt: createdAt, modifiedAt: modifiedAt)
}
}

Expand Down
34 changes: 33 additions & 1 deletion Piperly/Models/ReaderProfile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ struct ReaderProfile: Identifiable, Codable, Hashable, Sendable {
var fontSize: Double
var readerTheme: String
var hasCompletedVoiceSetup: Bool
var metadataModifiedAt: Date
var preferencesModifiedAt: Date

init(
id: UUID = UUID(),
Expand All @@ -69,7 +71,9 @@ struct ReaderProfile: Identifiable, Codable, Hashable, Sendable {
speechRate: Double = Self.defaultSpeechRate,
fontSize: Double = Self.defaultFontSize,
readerTheme: String = Self.defaultReaderTheme,
hasCompletedVoiceSetup: Bool = false
hasCompletedVoiceSetup: Bool = false,
metadataModifiedAt: Date = .now,
preferencesModifiedAt: Date = .now
) throws {
self.id = id
self.name = try Self.validatedNickname(name)
Expand All @@ -81,6 +85,8 @@ struct ReaderProfile: Identifiable, Codable, Hashable, Sendable {
self.fontSize = fontSize
self.readerTheme = readerTheme
self.hasCompletedVoiceSetup = hasCompletedVoiceSetup
self.metadataModifiedAt = metadataModifiedAt
self.preferencesModifiedAt = preferencesModifiedAt
}

init() {
Expand All @@ -94,6 +100,8 @@ struct ReaderProfile: Identifiable, Codable, Hashable, Sendable {
fontSize = Self.defaultFontSize
readerTheme = Self.defaultReaderTheme
hasCompletedVoiceSetup = false
metadataModifiedAt = .now
preferencesModifiedAt = .now
}

mutating func updateNickname(_ name: String) throws {
Expand Down Expand Up @@ -154,6 +162,9 @@ struct ReaderProfile: Identifiable, Codable, Hashable, Sendable {
case fontSize
case readerTheme
case hasCompletedVoiceSetup
case metadataModifiedAt
case preferencesModifiedAt
case modifiedAt
}

init(from decoder: Decoder) throws {
Expand All @@ -173,5 +184,26 @@ struct ReaderProfile: Identifiable, Codable, Hashable, Sendable {
?? Self.defaultReaderTheme
hasCompletedVoiceSetup = try container.decodeIfPresent(Bool.self, forKey: .hasCompletedVoiceSetup)
?? false
let legacyModifiedAt = try container.decodeIfPresent(Date.self, forKey: .modifiedAt) ?? createdAt
metadataModifiedAt = try container.decodeIfPresent(Date.self, forKey: .metadataModifiedAt)
?? legacyModifiedAt
preferencesModifiedAt = try container.decodeIfPresent(Date.self, forKey: .preferencesModifiedAt)
?? legacyModifiedAt
}

func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id)
try container.encode(name, forKey: .name)
try container.encode(avatarSymbol, forKey: .avatarSymbol)
try container.encode(colorName, forKey: .colorName)
try container.encode(createdAt, forKey: .createdAt)
try container.encode(voiceIdentifier, forKey: .voiceIdentifier)
try container.encode(speechRate, forKey: .speechRate)
try container.encode(fontSize, forKey: .fontSize)
try container.encode(readerTheme, forKey: .readerTheme)
try container.encode(hasCompletedVoiceSetup, forKey: .hasCompletedVoiceSetup)
try container.encode(metadataModifiedAt, forKey: .metadataModifiedAt)
try container.encode(preferencesModifiedAt, forKey: .preferencesModifiedAt)
}
}
42 changes: 25 additions & 17 deletions Piperly/Models/SavedWord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,10 @@ struct SavedWord: Identifiable, Codable, Hashable, Sendable, ProfileScoped {
var tapCount: Int
let savedAt: Date
var lastTappedAt: Date

/// Legacy blobs predate `profileID`; default it to a sentinel so the record
/// survives decode. BookStore rehomes the sentinel onto the active profile.
init(from decoder: Decoder) throws {
let c = try decoder.container(keyedBy: CodingKeys.self)
id = try c.decode(UUID.self, forKey: .id)
profileID = try c.decodeIfPresent(UUID.self, forKey: .profileID) ?? ProfileScopedDefaults.legacyProfileID
word = try c.decode(String.self, forKey: .word)
displayWord = try c.decode(String.self, forKey: .displayWord)
bookID = try c.decode(UUID.self, forKey: .bookID)
bookTitle = try c.decode(String.self, forKey: .bookTitle)
tapCount = try c.decode(Int.self, forKey: .tapCount)
savedAt = try c.decode(Date.self, forKey: .savedAt)
lastTappedAt = try c.decode(Date.self, forKey: .lastTappedAt)
}
var modifiedAt: Date

func withProfileID(_ id: UUID) -> SavedWord {
SavedWord(id: self.id, profileID: id, word: word, displayWord: displayWord, bookID: bookID, bookTitle: bookTitle, tapCount: tapCount, savedAt: savedAt, lastTappedAt: lastTappedAt)
SavedWord(id: self.id, profileID: id, word: word, displayWord: displayWord, bookID: bookID, bookTitle: bookTitle, tapCount: tapCount, savedAt: savedAt, lastTappedAt: lastTappedAt, modifiedAt: modifiedAt)
}

init(
Expand All @@ -55,7 +41,8 @@ struct SavedWord: Identifiable, Codable, Hashable, Sendable, ProfileScoped {
bookTitle: String,
tapCount: Int = 1,
savedAt: Date = .now,
lastTappedAt: Date = .now
lastTappedAt: Date = .now,
modifiedAt: Date = .now
) {
self.id = id
self.profileID = profileID
Expand All @@ -66,5 +53,26 @@ struct SavedWord: Identifiable, Codable, Hashable, Sendable, ProfileScoped {
self.tapCount = tapCount
self.savedAt = savedAt
self.lastTappedAt = lastTappedAt
self.modifiedAt = modifiedAt
}

private enum CodingKeys: String, CodingKey {
case id, profileID, word, displayWord, bookID, bookTitle, tapCount, savedAt, lastTappedAt, modifiedAt
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(UUID.self, forKey: .id)
// Legacy blobs predate profileID; default to a sentinel so the record
// survives decode. BookStore rehomes it onto the active profile.
profileID = try container.decodeIfPresent(UUID.self, forKey: .profileID) ?? ProfileScopedDefaults.legacyProfileID
word = try container.decode(String.self, forKey: .word)
displayWord = try container.decode(String.self, forKey: .displayWord)
bookID = try container.decode(UUID.self, forKey: .bookID)
bookTitle = try container.decode(String.self, forKey: .bookTitle)
tapCount = try container.decode(Int.self, forKey: .tapCount)
savedAt = try container.decode(Date.self, forKey: .savedAt)
lastTappedAt = try container.decode(Date.self, forKey: .lastTappedAt)
modifiedAt = try container.decodeIfPresent(Date.self, forKey: .modifiedAt) ?? lastTappedAt
}
}
18 changes: 18 additions & 0 deletions Piperly/Piperly.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>aps-environment</key>
<string>development</string>
<key>com.apple.developer.icloud-container-identifiers</key>
<array>
<string>iCloud.com.piperly.app</string>
</array>
<key>com.apple.developer.icloud-services</key>
<array>
<string>CloudKit</string>
</array>
<key>com.apple.developer.ubiquity-kvstore-identifier</key>
<string>$(TeamIdentifierPrefix)com.piperly.app</string>
</dict>
</plist>
Loading
Loading