diff options
| author | Fuwn <[email protected]> | 2025-09-05 19:48:59 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-09-05 19:48:59 -0700 |
| commit | d5b81fcbc1a8c009204b2482d490e8d8b96c6c26 (patch) | |
| tree | 3cf3593d4aa9facf028d4fe40d952f848f5b2b3f /Sora/Views | |
| parent | feat: Development commit (diff) | |
| download | sora-testing-d5b81fcbc1a8c009204b2482d490e8d8b96c6c26.tar.xz sora-testing-d5b81fcbc1a8c009204b2482d490e8d8b96c6c26.zip | |
feat: Development commit
Diffstat (limited to 'Sora/Views')
| -rw-r--r-- | Sora/Views/FavoriteMenuButtonView.swift | 112 | ||||
| -rw-r--r-- | Sora/Views/FavoritePostThumbnailView.swift | 91 | ||||
| -rw-r--r-- | Sora/Views/FavoritesView.swift | 567 | ||||
| -rw-r--r-- | Sora/Views/MainView.swift | 20 | ||||
| -rw-r--r-- | Sora/Views/Post/Details/PostDetailsView.swift | 26 | ||||
| -rw-r--r-- | Sora/Views/Post/Grid/PostGridFavoriteButtonView.swift | 45 |
6 files changed, 848 insertions, 13 deletions
diff --git a/Sora/Views/FavoriteMenuButtonView.swift b/Sora/Views/FavoriteMenuButtonView.swift new file mode 100644 index 0000000..6fee55a --- /dev/null +++ b/Sora/Views/FavoriteMenuButtonView.swift @@ -0,0 +1,112 @@ +import SwiftUI + +struct FavoriteMenuButtonView: View { + @EnvironmentObject var settings: SettingsManager + @EnvironmentObject var manager: BooruManager + let post: BooruPost + var disableNewCollection = false + @State private var isNewCollectionAlertPresented = false + @State private var newCollectionName = "" + @State private var itemPendingCollectionAssignment: UUID? + @State private var isCollectionErrorAlertPresented = false + + var body: some View { + let isFavorited = settings.isFavorite(postId: post.id, provider: manager.provider) + + Menu { + Button(action: { + if isFavorited { + settings.removeFavorite(withPostId: post.id, provider: manager.provider) + } else { + settings.addFavorite(post: post, provider: manager.provider) + } + }) { + if isFavorited { + Label("Remove from Favorites", systemImage: "heart.fill") + } else { + Label("Add to Favorites", systemImage: "heart") + } + } + + Menu { + ForEach(settings.folders.filter { $0.topLevelName == nil }, id: \.id) { folder in + Button(action: { + let newFavorite = SettingsFavoritePost( + post: post, provider: manager.provider, folder: folder.id + ) + + settings.favorites.append(newFavorite) + }) { + Label(folder.name, systemImage: "folder") + } + .disabled(isFavoritedInFolder(folderId: folder.id)) + } + + let topLevelFolders = settings.folders + .reduce(into: [String: [SettingsFolder]]()) { result, folder in + guard let topLevelName = folder.topLevelName else { return } + + result[topLevelName, default: []].append(folder) + } + + ForEach(topLevelFolders.keys.sorted(), id: \.self) { topLevelName in + Menu { + ForEach(topLevelFolders[topLevelName] ?? [], id: \.id) { folder in + Button(action: { + let newFavorite = SettingsFavoritePost( + post: post, + provider: manager.provider, + folder: folder.id + ) + + settings.favorites.append(newFavorite) + }) { + Text(folder.shortName) + } + .disabled(isFavoritedInFolder(folderId: folder.id)) + } + } label: { + Text(topLevelName) + } + } + + Button(action: { + isNewCollectionAlertPresented = true + }) { + Label("New Collection", systemImage: "plus") + } + .disabled(disableNewCollection) + } label: { + Label("Favorite to Collection", systemImage: "folder.badge.plus") + } + } label: { + if isFavorited { + Label("Favorited", systemImage: "heart.fill") + } else { + Label("Favorite", systemImage: "heart") + } + } + .collectionAlerts( + isNewCollectionAlertPresented: $isNewCollectionAlertPresented, + newCollectionName: $newCollectionName, + isCollectionErrorAlertPresented: $isCollectionErrorAlertPresented + ) { newCollectionName in + let newFolder = SettingsFolder(name: newCollectionName) + + settings.folders.append(newFolder) + + let newFavorite = SettingsFavoritePost( + post: post, provider: manager.provider, folder: newFolder.id + ) + + settings.favorites.append(newFavorite) + } + } + + private func isFavoritedInFolder(folderId: UUID) -> Bool { + settings.favorites.contains { favorite in + favorite.folder == folderId && favorite.postId == post.id + && favorite.provider == manager.provider + } + } +} diff --git a/Sora/Views/FavoritePostThumbnailView.swift b/Sora/Views/FavoritePostThumbnailView.swift new file mode 100644 index 0000000..661f34a --- /dev/null +++ b/Sora/Views/FavoritePostThumbnailView.swift @@ -0,0 +1,91 @@ +import NetworkImage +import SwiftUI + +struct FavoritePostThumbnailView: View { + @EnvironmentObject var settings: SettingsManager + let favorite: SettingsFavoritePost + let onRemove: () -> Void + + private var thumbnailURL: URL? { + switch settings.thumbnailQuality { + case .preview: + favorite.previewUrl.flatMap(URL.init) + + case .sample: + favorite.previewUrl.flatMap(URL.init) + + case .original: + favorite.fileUrl.flatMap(URL.init) + } + } + + @ViewBuilder + private func primaryImageContent(image: Image) -> some View { + let isFiltered = settings.blurRatings.contains(favorite.rating) + + image + .resizable() + .aspectRatio(contentMode: .fit) + .blur(radius: isFiltered ? 8 : 0) + .clipped() + .animation(.default, value: isFiltered) + .clipShape(RoundedRectangle(cornerRadius: 8)) + } + + @ViewBuilder + private func imageContent(image: Image) -> some View { + if settings.uniformThumbnailGrid { + GeometryReader { proxy in + primaryImageContent(image: image) + .frame(width: proxy.size.width, height: proxy.size.width) + } + .clipped() + .aspectRatio(1, contentMode: .fit) + } else { + primaryImageContent(image: image) + } + } + + var body: some View { + NetworkImage( + url: thumbnailURL, + transaction: Transaction(animation: .default) + ) { image in + imageContent(image: image) + } placeholder: { + PostGridThumbnailPlaceholderView() + } + } +} + +#Preview { + let sampleFavorite = SettingsFavoritePost( + post: BooruPost( + id: "123", + height: 100, + score: "10", + fileURL: URL(string: "https://example.com/file.jpg")!, + parentID: "0", + sampleURL: URL(string: "https://example.com/sample.jpg")!, + sampleWidth: 100, + sampleHeight: 100, + previewURL: URL(string: "https://example.com/preview.jpg")!, + rating: .safe, + tags: ["sample", "test"], + width: 100, + change: nil, + md5: "abc123", + creatorID: "1", + authorID: nil, + createdAt: Date(), + status: "active", + source: "", + previewWidth: 100, + previewHeight: 100 + ), + provider: .yandere + ) + + FavoritePostThumbnailView(favorite: sampleFavorite) { () } + .environmentObject(SettingsManager()) +} diff --git a/Sora/Views/FavoritesView.swift b/Sora/Views/FavoritesView.swift new file mode 100644 index 0000000..27726a7 --- /dev/null +++ b/Sora/Views/FavoritesView.swift @@ -0,0 +1,567 @@ +// swiftlint:disable file_length + +import SwiftUI +import WaterfallGrid + +struct FavoritesView: View { // swiftlint:disable:this type_body_length + @EnvironmentObject var settings: SettingsManager + @EnvironmentObject var manager: BooruManager + @Binding var selectedTab: Int + @State private var searchText: String = "" + @State private var isShowingRemoveAllConfirmation = false + @Binding var isPresented: Bool + @State private var isNewCollectionAlertPresented = false + @State private var newCollectionName = "" + @State private var itemPendingCollectionAssignment: UUID? + @State private var isCollectionErrorAlertPresented = false + @State private var selectedCollectionOption: CollectionPickerOption = .all + @State private var sort: SettingsBookmarkSort = .dateAdded + @State private var isCollectionPickerPresented = false + @State private var isProviderPickerPresented = false + @State private var selectedProvider: BooruProvider? + @State private var navigationPath = NavigationPath() + @State private var isDetailsSheetPresented = false + @State private var selectedFavoriteForDetails: SettingsFavoritePost? + + var filteredFavorites: [SettingsFavoritePost] { + settings.favorites.filter { favorite in + let matchesFolder: Bool = { + switch selectedCollectionOption { + case .all: + return true + + case .uncategorized: + return favorite.folder == nil + + case .folder(let folderId): + return favorite.folder == folderId + } + }() + let matchesSearch = + searchText.isEmpty + || favorite.tags + .joined(separator: " ") + .lowercased() + .contains(searchText.lowercased()) + let matchesProvider = + selectedProvider == nil + || favorite.provider == selectedProvider + + return matchesFolder && matchesSearch && matchesProvider + } + } + + var sortedFilteredFavorites: [SettingsFavoritePost] { + filteredFavorites.sorted { (lhs: SettingsFavoritePost, rhs: SettingsFavoritePost) in + switch sort { + case .dateAdded: + return lhs.date > rhs.date + + case .lastVisited: + return lhs.lastVisit > rhs.lastVisit + + case .visitCount: + return lhs.visitedCount > rhs.visitedCount + } + } + } + + private func isProviderFavorited(_ provider: BooruProvider) -> Bool { + settings.favorites.contains { $0.provider == provider } + } + + private func isCollectionPopulated(_ folder: UUID?) -> Bool { + settings.favorites.contains { $0.folder == folder } + } + + var body: some View { + NavigationStack(path: $navigationPath) { + VStack(spacing: 0) { + if settings.favorites.isEmpty { + ContentUnavailableView( + "No Favorites", + systemImage: "heart", + description: Text("Tap the heart button on a post to add it to favorites.") + ) + } else { + #if os(macOS) + if !settings.folders.isEmpty { + Picker("Sort", selection: $sort) { + ForEach(SettingsBookmarkSort.allCases, id: \.rawValue) { sortMode in + Text(sortMode.rawValue).tag(sortMode) + } + } + .padding() + .pickerStyle(.menu) + + Picker("Collection", selection: $selectedCollectionOption) { + Text(CollectionPickerOption.all.name(settings: settings)) + .tag(CollectionPickerOption.all) + + if settings.favorites.contains(where: { $0.folder == nil }) { + Text(CollectionPickerOption.uncategorized.name(settings: settings)) + .tag(CollectionPickerOption.uncategorized) + } + + ForEach(settings.folders.filter { $0.topLevelName == nil }, id: \.id) { folder in + Text(folder.name).tag(CollectionPickerOption.folder(folder.id)) + } + + let topLevelFolders = settings.folders + .reduce(into: [String: [SettingsFolder]]()) { result, folder in + guard let topLevelName = folder.topLevelName else { return } + + result[topLevelName, default: []].append(folder) + } + + ForEach(topLevelFolders.keys.sorted(), id: \.self) { topLevelName in + Menu { + ForEach(topLevelFolders[topLevelName] ?? [], id: \.id) { folder in + Button(action: { + selectedCollectionOption = .folder(folder.id) + }) { + Text(folder.shortName) + } + } + } label: { + Text(topLevelName) + } + } + } + .padding(.bottom) + .padding(.horizontal) + .pickerStyle(.menu) + + Picker("Provider", selection: $selectedProvider) { + Text("All").tag(nil as BooruProvider?) + + ForEach(BooruProvider.allCases, id: \.rawValue) { provider in + if isProviderFavorited(provider) { + Text(provider.rawValue).tag(provider) + } + } + } + .padding(.bottom) + .padding(.horizontal) + .pickerStyle(.menu) + } + #endif + + ScrollView { + if filteredFavorites.isEmpty + && (!searchText.isEmpty || !(selectedCollectionOption == .all)) + { + ContentUnavailableView( + "No matching favorites found", + systemImage: "heart.slash", + description: Text("Try adjusting your search or collection filter.") + ) + } else { + gridView(columnCount: settings.thumbnailGridColumns) + } + } + .searchable(text: $searchText) + } + } + } + .navigationTitle("Favorites") + .refreshable { settings.syncFromCloud() } + .toolbar { + #if os(macOS) + ToolbarItem { + Button(action: { + settings.syncFromCloud() + }) { + Label("Refresh", systemImage: "arrow.clockwise") + } + } + #endif + + #if os(macOS) + ToolbarItem { + Button( + role: .destructive, + action: { + isShowingRemoveAllConfirmation = true + } + ) { + Label("Remove All", systemImage: "trash") + } + } + #endif + + #if os(iOS) + ToolbarItemGroup(placement: .secondaryAction) { + Menu { + Picker("Sort By", selection: $sort) { + ForEach(SettingsBookmarkSort.allCases, id: \.rawValue) { sortMode in + Text(sortMode.rawValue).tag(sortMode) + } + } + } label: { + Label("Sort By", systemImage: "arrow.up.arrow.down") + } + + Menu { + Picker("Collection", selection: $selectedCollectionOption) { + Text(CollectionPickerOption.all.name(settings: settings)) + .tag(CollectionPickerOption.all) + + if settings.favorites.contains(where: { $0.folder == nil }) { + Text(CollectionPickerOption.uncategorized.name(settings: settings)) + .tag(CollectionPickerOption.uncategorized) + } + + ForEach(settings.folders.filter { $0.topLevelName == nil }, id: \.id) { folder in + Text(folder.name).tag(CollectionPickerOption.folder(folder.id)) + } + + let topLevelFolders = settings.folders + .reduce(into: [String: [SettingsFolder]]()) { result, folder in + guard let topLevelName = folder.topLevelName else { return } + + result[topLevelName, default: []].append(folder) + } + + ForEach(topLevelFolders.keys.sorted(), id: \.self) { topLevelName in + Menu { + ForEach(topLevelFolders[topLevelName] ?? [], id: \.id) { folder in + Button(action: { + selectedCollectionOption = .folder(folder.id) + }) { + Text(folder.shortName) + } + } + } label: { + Text(topLevelName) + } + } + } + } label: { + Label("Collection", systemImage: "folder") + } + + Menu { + Picker("Provider", selection: $selectedProvider) { + Text("All").tag(nil as BooruProvider?) + + ForEach(BooruProvider.allCases, id: \.rawValue) { provider in + Text(provider.rawValue) + .tag(provider) + .selectionDisabled(!isProviderFavorited(provider)) + } + } + } label: { + Label("Provider", systemImage: "globe") + } + + Button( + role: .destructive, + action: { + isShowingRemoveAllConfirmation = true + } + ) { + Label("Delete All", systemImage: "trash") + } + } + #endif + } + .alert( + "Are you sure you want to remove all favorites? This action cannot be undone.", + isPresented: $isShowingRemoveAllConfirmation + ) { + Button("Remove All Favorites") { + settings.favorites.removeAll() + } + + Button("Cancel", role: .cancel) { () } + } + .collectionAlerts( + isNewCollectionAlertPresented: $isNewCollectionAlertPresented, + newCollectionName: $newCollectionName, + isCollectionErrorAlertPresented: $isCollectionErrorAlertPresented + ) { newCollectionName in + let newFolder = SettingsFolder(name: newCollectionName) + + settings.folders.append(newFolder) + + if let id = itemPendingCollectionAssignment { + settings.updateFavoriteFolder(withID: id, folder: newFolder.id) + } + + itemPendingCollectionAssignment = nil + } + .navigationDestination(for: PostWithContext.self) { context in + PostDetailsView( + post: context.post, + navigationPath: $navigationPath, + posts: context.posts, + baseSearchText: context.baseSearchText + ) + } + .sheet(isPresented: $isDetailsSheetPresented) { + if let favorite = selectedFavoriteForDetails { + NavigationView { + VStack(alignment: .leading, spacing: 16) { + Text("Favorite Details") + .font(.title2) + .fontWeight(.bold) + + VStack(alignment: .leading, spacing: 12) { + VStack(alignment: .leading, spacing: 4) { + Text("Tags") + .font(.headline) + Text(favorite.tags.joined(separator: ", ").lowercased()) + .font(.body) + .foregroundStyle(Color.secondary) + } + + VStack(alignment: .leading, spacing: 4) { + Text("Favorited") + .font(.headline) + Text(favorite.createdAt.formatted()) + .font(.body) + .foregroundStyle(Color.secondary) + } + + VStack(alignment: .leading, spacing: 4) { + Text("Provider") + .font(.headline) + Text(favorite.provider.rawValue) + .font(.body) + .foregroundStyle(Color.secondary) + } + + if let folder = favorite.folder, let folderName = settings.folderName(forID: folder) { + VStack(alignment: .leading, spacing: 4) { + Text("Collection") + .font(.headline) + Text(folderName) + .font(.body) + .foregroundStyle(Color.secondary) + } + } + + VStack(alignment: .leading, spacing: 4) { + Text("Visit Count") + .font(.headline) + Text("\(favorite.visitedCount) times") + .font(.body) + .foregroundStyle(Color.secondary) + } + + if favorite.lastVisit != favorite.createdAt { + VStack(alignment: .leading, spacing: 4) { + Text("Last Visited") + .font(.headline) + Text(favorite.lastVisit.formatted()) + .font(.body) + .foregroundStyle(Color.secondary) + } + } + } + + Spacer() + } + .padding() + .navigationTitle("Details") + #if os(iOS) + .navigationBarTitleDisplayMode(.inline) + #endif + .toolbar { + #if os(iOS) + ToolbarItem(placement: .navigationBarTrailing) { + Button("Done") { + isDetailsSheetPresented = false + } + } + #else + ToolbarItem(placement: .cancellationAction) { + Button("Done") { + isDetailsSheetPresented = false + } + } + #endif + } + } + } + } + } + + @ViewBuilder + private func gridView(columnCount: Int) -> some View { + if settings.alternativeThumbnailGrid { + let columnsData = getColumnsData(columnCount: columnCount) + + HStack(alignment: .top) { + ForEach(0..<columnCount, id: \.self) { columnIndex in + LazyVStack { + ForEach(columnsData[columnIndex], id: \.id) { favorite in + favoriteGridContent(favorite: favorite) + .id(favorite.id) + } + } + .transaction { $0.animation = nil } + } + } + #if os(macOS) + .padding(8) + #else + .padding(.horizontal) + #endif + .transition(.opacity) + } else { + WaterfallGrid(sortedFilteredFavorites, id: \.id) { favorite in + favoriteGridContent(favorite: favorite) + .id(favorite.id) + } + .gridStyle(columns: columnCount) + .transaction { $0.animation = nil } + #if os(macOS) + .padding(8) + #else + .padding(.horizontal) + #endif + .transition(.opacity) + } + } + + private func getColumnsData(columnCount: Int) -> [[SettingsFavoritePost]] { + (0..<columnCount).map { columnIndex in + sortedFilteredFavorites.enumerated().compactMap { index, favorite in + index % columnCount == columnIndex ? favorite : nil + } + } + } + + private func favoriteGridContent(favorite: SettingsFavoritePost) -> some View { + NavigationLink( + value: PostWithContext( + post: favorite.toBooruPost(), + posts: sortedFilteredFavorites.map { $0.toBooruPost() }, + baseSearchText: nil + ) + ) { + FavoritePostThumbnailView( + favorite: favorite, + ) { + settings.removeFavorite(withID: favorite.id) + } + } + .buttonStyle(PlainButtonStyle()) + .contextMenu { + Button(action: { + selectedFavoriteForDetails = favorite + isDetailsSheetPresented = true + }) { + Label("Show Details", systemImage: "info.circle") + } + + Button(action: { + manager.searchText += " \(favorite.tags.joined(separator: " "))" + manager.selectedPost = nil + + let localManager = manager + let localSettings = settings + + isPresented.toggle() + + Task(priority: .userInitiated) { + await localManager.performSearch(settings: localSettings) + } + }) { + Label("Add to Search", systemImage: "plus") + } + + Menu { + ForEach(settings.folders.filter { $0.topLevelName == nil }, id: \.id) { folder in + Button(action: { + settings.updateFavoriteFolder(withID: favorite.id, folder: folder.id) + }) { + Label(folder.name, systemImage: "folder") + } + .disabled(favorite.folder == folder.id) + } + + let topLevelFolders = settings.folders + .reduce(into: [String: [SettingsFolder]]()) { result, folder in + guard let topLevelName = folder.topLevelName else { return } + + result[topLevelName, default: []].append(folder) + } + + ForEach(topLevelFolders.keys.sorted(), id: \.self) { topLevelName in + Menu { + ForEach(topLevelFolders[topLevelName] ?? [], id: \.id) { folder in + Button(action: { + settings.updateFavoriteFolder(withID: favorite.id, folder: folder.id) + }) { + Text(folder.shortName) + } + .disabled(favorite.folder == folder.id) + } + } label: { + Text(topLevelName) + } + } + + Button(action: { + itemPendingCollectionAssignment = favorite.id + isNewCollectionAlertPresented = true + }) { + Label("New Collection", systemImage: "plus") + } + } label: { + Label("\(favorite.folder != nil ? "Move" : "Add") to Collection", systemImage: "folder") + } + + if favorite.folder != nil { + Button(action: { + settings.updateFavoriteFolder(withID: favorite.id, folder: nil) + }) { + Label("Remove from Collection", systemImage: "folder.badge.minus") + } + } + + Button { + settings.removeFavorite(withID: favorite.id) + } label: { + Label("Delete", systemImage: "trash") + } + } + } +} + +extension SettingsFavoritePost { + func toBooruPost() -> BooruPost { + BooruPost( + id: postId, + height: height, + score: "0", + fileURL: fileUrl.flatMap(URL.init) ?? URL(string: "https://example.com")!, + parentID: "0", + sampleURL: previewUrl.flatMap(URL.init) ?? URL(string: "https://example.com")!, + sampleWidth: width, + sampleHeight: height, + previewURL: thumbnailUrl.flatMap(URL.init) ?? URL(string: "https://example.com")!, + rating: rating, + tags: tags, + width: width, + change: nil, + md5: "", + creatorID: "0", + authorID: nil, + createdAt: createdAt, + status: "active", + source: "", + previewWidth: width, + previewHeight: height + ) + } +} + +#Preview { + FavoritesView(selectedTab: .constant(1), isPresented: .constant(false)) + .environmentObject(SettingsManager()) + .environmentObject(BooruManager(.yandere)) +} diff --git a/Sora/Views/MainView.swift b/Sora/Views/MainView.swift index dd73c49..f01a1e6 100644 --- a/Sora/Views/MainView.swift +++ b/Sora/Views/MainView.swift @@ -41,8 +41,14 @@ struct MainView: View { } } + Tab("Favorites", systemImage: "heart", value: 2) { + NavigationStack { + FavoritesView(selectedTab: $selectedTab, isPresented: .constant(false)) + } + } + #if os(macOS) - Tab("Search History", systemImage: "clock.arrow.circlepath", value: 3) { + Tab("Search History", systemImage: "clock.arrow.circlepath", value: 4) { PostGridSearchHistoryView( selectedTab: $selectedTab, isPresented: .constant(false) @@ -51,7 +57,7 @@ struct MainView: View { #endif #if DEBUG || !os(macOS) - Tab("Settings", systemImage: "gear", value: 2) { + Tab("Settings", systemImage: "gear", value: 3) { SettingsView() } #endif @@ -68,6 +74,12 @@ struct MainView: View { .tabItem { Label("Bookmarks", systemImage: "bookmark") } .tag(1) + NavigationStack { + FavoritesView(selectedTab: $selectedTab, isPresented: .constant(false)) + } + .tabItem { Label("Favorites", systemImage: "heart") } + .tag(2) + #if os(macOS) NavigationStack { PostGridSearchHistoryView( @@ -76,13 +88,13 @@ struct MainView: View { ) } .tabItem { Label("Search History", systemImage: "clock.arrow.circlepath") } - .tag(3) + .tag(4) #endif #if DEBUG || !os(macOS) SettingsView() .tabItem { Label("Settings", systemImage: "gear") } - .tag(2) + .tag(3) #endif } } diff --git a/Sora/Views/Post/Details/PostDetailsView.swift b/Sora/Views/Post/Details/PostDetailsView.swift index 4a7daf3..bca956a 100644 --- a/Sora/Views/Post/Details/PostDetailsView.swift +++ b/Sora/Views/Post/Details/PostDetailsView.swift @@ -14,13 +14,13 @@ struct PostDetailsView: View { private var imageURL: URL? { switch settings.detailViewQuality { case .preview: - post.previewURL + currentPost.previewURL case .sample: - post.sampleURL + currentPost.sampleURL case .original: - post.fileURL + currentPost.fileURL } } @@ -43,6 +43,10 @@ struct PostDetailsView: View { return sourcePosts.filter { settings.displayRatings.contains($0.rating) } } + private var currentPost: BooruPost { + manager.selectedPost ?? post + } + var body: some View { VStack(spacing: 0) { #if os(macOS) @@ -50,20 +54,20 @@ struct PostDetailsView: View { url: imageURL, loadingStage: $loadingStage, finalLoadingState: .loaded, - post: post + post: currentPost ) { PostDetailsImageView( - url: post.previewURL, + url: currentPost.previewURL, loadingStage: $loadingStage ) - .id(post.previewURL) + .id(currentPost.previewURL) } .id(imageURL) #else PostDetailsCarouselView( posts: filteredPosts, loadingStage: $loadingStage, - focusedPost: post + focusedPost: currentPost ) .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center) #endif @@ -71,7 +75,7 @@ struct PostDetailsView: View { if settings.displayDetailsInformationBar { VStack(spacing: 5) { HStack { - Text(post.createdAt.formatted()) + Text(currentPost.createdAt.formatted()) .frame(maxWidth: .infinity, alignment: .leading) Group { @@ -119,6 +123,10 @@ struct PostDetailsView: View { } } + ToolbarItem { + PostGridFavoriteButtonView(post: currentPost) + } + #if os(macOS) if settings.enableShareShortcut { ToolbarItem { @@ -146,7 +154,7 @@ struct PostDetailsView: View { PostDetailsTagsView( isPresented: $isTagsSheetPresented, navigationPath: $navigationPath, - tags: post.tags, + tags: currentPost.tags, isNestedContext: posts != nil, baseSearchText: baseSearchText ) diff --git a/Sora/Views/Post/Grid/PostGridFavoriteButtonView.swift b/Sora/Views/Post/Grid/PostGridFavoriteButtonView.swift new file mode 100644 index 0000000..bd6b6f9 --- /dev/null +++ b/Sora/Views/Post/Grid/PostGridFavoriteButtonView.swift @@ -0,0 +1,45 @@ +import SwiftUI + +struct PostGridFavoriteButtonView: View { + @EnvironmentObject private var manager: BooruManager + @EnvironmentObject private var settings: SettingsManager + let post: BooruPost + + var isFavorited: Bool { + settings.isFavorite(postId: post.id, provider: manager.provider) + } + + var body: some View { + FavoriteMenuButtonView(post: post) + } +} + +#Preview { + let samplePost = BooruPost( + id: "123", + height: 100, + score: "10", + fileURL: URL(string: "https://example.com/file.jpg")!, + parentID: "0", + sampleURL: URL(string: "https://example.com/sample.jpg")!, + sampleWidth: 100, + sampleHeight: 100, + previewURL: URL(string: "https://example.com/preview.jpg")!, + rating: .safe, + tags: ["sample", "test"], + width: 100, + change: nil, + md5: "abc123", + creatorID: "1", + authorID: nil, + createdAt: Date(), + status: "active", + source: "", + previewWidth: 100, + previewHeight: 100 + ) + + PostGridFavoriteButtonView(post: samplePost) + .environmentObject(SettingsManager()) + .environmentObject(BooruManager(.yandere)) +} |