-
-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1246 from TortugaPower/watch-bookmarks
Add bookmarks to the Apple Watch
- Loading branch information
Showing
12 changed files
with
476 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// | ||
// BookmarksView.swift | ||
// BookPlayerWatch | ||
// | ||
// Created by GC on 1/10/25. | ||
// Copyright © 2025 BookPlayer LLC. All rights reserved. | ||
// | ||
|
||
import BookPlayerWatchKit | ||
import SwiftUI | ||
|
||
struct BookmarksView: View { | ||
@StateObject var model: BookmarksViewModel | ||
|
||
@State var error: Error? | ||
|
||
@Environment(\.dismiss) var dismiss | ||
|
||
var body: some View { | ||
List { | ||
HStack { | ||
Spacer() | ||
Button { | ||
do { | ||
try model.createBookmark() | ||
} catch { | ||
self.error = error | ||
} | ||
} label: { | ||
Image(systemName: "plus.circle.fill") | ||
.resizable() | ||
.scaledToFit() | ||
.frame(width: 24, height: 24) | ||
} | ||
.buttonStyle(PlainButtonStyle()) | ||
Spacer() | ||
} | ||
.frame(height: 24) | ||
.listRowBackground(Color.clear) | ||
|
||
Section { | ||
ForEach(model.userBookmarks) { bookmark in | ||
Button { | ||
model.playerManager.jumpTo(bookmark.time + 0.01, recordBookmark: false) | ||
dismiss() | ||
} label: { | ||
VStack(alignment: .leading) { | ||
Text(TimeParser.formatTime(bookmark.time)) | ||
.foregroundColor(Color.secondary) | ||
.font(.footnote) | ||
if let note = bookmark.note { | ||
Text(note) | ||
} | ||
} | ||
.frame(minHeight: 24) | ||
.padding(.vertical, Spacing.S4) | ||
} | ||
.swipeActions { | ||
Button( | ||
role: .destructive, | ||
action: { model.deleteBookmark(bookmark) }, | ||
label: { | ||
Image(systemName: "trash") | ||
.imageScale(.large) | ||
} | ||
) | ||
.accessibilityLabel("delete_button".localized) | ||
} | ||
} | ||
} header: { | ||
Text("bookmark_type_user_title".localized) | ||
.foregroundStyle(Color.accentColor) | ||
} | ||
|
||
Section { | ||
ForEach(model.automaticBookmarks) { bookmark in | ||
Button { | ||
model.playerManager.jumpTo(bookmark.time + 0.01, recordBookmark: false) | ||
dismiss() | ||
} label: { | ||
VStack(alignment: .leading) { | ||
HStack { | ||
Text(TimeParser.formatTime(bookmark.time)) | ||
.foregroundColor(Color.secondary) | ||
.font(.footnote) | ||
Spacer() | ||
if let imageName = bookmark.getImageNameForType() { | ||
Image(systemName: imageName) | ||
.foregroundColor(Color.secondary) | ||
} | ||
} | ||
if let note = bookmark.note { | ||
Text(note) | ||
} | ||
} | ||
} | ||
} | ||
} header: { | ||
Text("bookmark_type_automatic_title".localized) | ||
.foregroundStyle(Color.accentColor) | ||
.padding(.top, 10) | ||
} | ||
} | ||
.environment(\.defaultMinListRowHeight, 1) | ||
.customListSectionSpacing(0) | ||
.errorAlert(error: $error) | ||
.navigationTitle("bookmarks_title") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// | ||
// BookmarksViewModel.swift | ||
// BookPlayerWatch | ||
// | ||
// Created by GC on 1/10/25. | ||
// Copyright © 2025 BookPlayer LLC. All rights reserved. | ||
// | ||
|
||
import BookPlayerWatchKit | ||
import Combine | ||
import Foundation | ||
|
||
@MainActor | ||
class BookmarksViewModel: ObservableObject { | ||
@Published var automaticBookmarks = [SimpleBookmark]() | ||
@Published var userBookmarks = [SimpleBookmark]() | ||
@Published var selectedBookmarkToDelete: SimpleBookmark? | ||
private var disposeBag = Set<AnyCancellable>() | ||
|
||
let playerManager: PlayerManager | ||
let libraryService: LibraryServiceProtocol | ||
let syncService: SyncServiceProtocol | ||
|
||
init(coreServices: CoreServices) { | ||
self.playerManager = coreServices.playerManager | ||
self.libraryService = coreServices.libraryService | ||
self.syncService = coreServices.syncService | ||
|
||
self.bindCurrentItemObserver() | ||
} | ||
|
||
func bindCurrentItemObserver() { | ||
playerManager.currentItemPublisher() | ||
.receive(on: DispatchQueue.main) | ||
.sink { [weak self] currentItem in | ||
guard let self else { return } | ||
|
||
if let currentItem { | ||
self.automaticBookmarks = self.getAutomaticBookmarks(for: currentItem.relativePath) | ||
self.userBookmarks = self.getUserBookmarks(for: currentItem.relativePath) | ||
self.syncBookmarks(for: currentItem.relativePath) | ||
} else { | ||
self.automaticBookmarks = [] | ||
self.userBookmarks = [] | ||
} | ||
} | ||
.store(in: &disposeBag) | ||
} | ||
|
||
func syncBookmarks(for relativePath: String) { | ||
Task { @MainActor [weak self] in | ||
guard | ||
let self = self, | ||
let bookmarks = try await self.syncService.syncBookmarksList(relativePath: relativePath) | ||
else { return } | ||
|
||
self.userBookmarks = bookmarks | ||
} | ||
} | ||
|
||
func getAutomaticBookmarks(for relativePath: String) -> [SimpleBookmark] { | ||
let playBookmarks = self.libraryService.getBookmarks(of: .play, relativePath: relativePath) ?? [] | ||
let skipBookmarks = self.libraryService.getBookmarks(of: .skip, relativePath: relativePath) ?? [] | ||
|
||
let bookmarks = playBookmarks + skipBookmarks | ||
|
||
return bookmarks.sorted(by: { $0.time < $1.time }) | ||
} | ||
|
||
func getUserBookmarks(for relativePath: String) -> [SimpleBookmark] { | ||
return self.libraryService.getBookmarks(of: .user, relativePath: relativePath) ?? [] | ||
} | ||
|
||
func createBookmark() throws { | ||
guard let currentItem = playerManager.currentItem else { return } | ||
|
||
let currentTime = currentItem.currentTime | ||
|
||
if let bookmark = libraryService.getBookmark( | ||
at: currentTime, | ||
relativePath: currentItem.relativePath, | ||
type: .user | ||
) { | ||
throw BookmarksAlerts.bookmarkExists(bookmark: bookmark) | ||
} | ||
|
||
if let bookmark = libraryService.createBookmark( | ||
at: floor(currentTime), | ||
relativePath: currentItem.relativePath, | ||
type: .user | ||
) { | ||
syncService.scheduleSetBookmark( | ||
relativePath: currentItem.relativePath, | ||
time: floor(currentTime), | ||
note: nil | ||
) | ||
userBookmarks = getUserBookmarks(for: currentItem.relativePath) | ||
throw BookmarksAlerts.bookmarkCreated(bookmark: bookmark) | ||
} else { | ||
throw BookmarksAlerts.fileMissing | ||
} | ||
} | ||
|
||
func deleteBookmark(_ bookmark: SimpleBookmark) { | ||
libraryService.deleteBookmark(bookmark) | ||
userBookmarks = getUserBookmarks(for: bookmark.relativePath) | ||
syncService.scheduleDeleteBookmark(bookmark) | ||
} | ||
} | ||
|
||
enum BookmarksAlerts: LocalizedError { | ||
case bookmarkExists(bookmark: SimpleBookmark) | ||
case bookmarkCreated(bookmark: SimpleBookmark) | ||
case fileMissing | ||
|
||
public var errorDescription: String? { | ||
switch self { | ||
case .bookmarkExists(let bookmark): | ||
let formattedTime = TimeParser.formatTime(bookmark.time) | ||
return String.localizedStringWithFormat("bookmark_exists_title".localized, formattedTime) | ||
case .bookmarkCreated(let bookmark): | ||
let formattedTime = TimeParser.formatTime(bookmark.time) | ||
return String.localizedStringWithFormat("bookmark_created_title".localized, formattedTime) | ||
case .fileMissing: | ||
return "file_missing_title".localized | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.