From e04c8d800d275eaddf9f0dc0bcd83f54d0c1331b Mon Sep 17 00:00:00 2001 From: Jojo F Date: Sun, 24 Mar 2024 02:33:39 -0700 Subject: [PATCH] fix flag button. (#79) --- Shared/Models/Stores/ItemStore.swift | 2 -- Shared/Views/Components/CommentTile.swift | 21 +++++++++++---------- Shared/Views/ItemView.swift | 15 ++++++++++----- ZCombinator.xcodeproj/project.pbxproj | 4 ++-- 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/Shared/Models/Stores/ItemStore.swift b/Shared/Models/Stores/ItemStore.swift index 3734664..402f47d 100644 --- a/Shared/Models/Stores/ItemStore.swift +++ b/Shared/Models/Stores/ItemStore.swift @@ -11,8 +11,6 @@ extension ItemView { @Published var item: (any Item)? @Published var loadingItemId: Int? @Published var actionPerformed: Action = .none - @Published var showFlagDialog: Bool = .init() - /// Stores ids of loaded comments, including both root and child comments. @Published var loadedCommentIds: Set = .init() diff --git a/Shared/Views/Components/CommentTile.swift b/Shared/Views/Components/CommentTile.swift index 5a66247..4b1b8c9 100644 --- a/Shared/Views/Components/CommentTile.swift +++ b/Shared/Views/Components/CommentTile.swift @@ -13,17 +13,20 @@ extension ItemView { let onLoadMore: () -> Void let onShowHNSheet: () -> Void let onShowReplySheet: () -> Void - + let onFlag: () -> Void + init(comment: Comment, itemStore: ItemStore, onShowHNSheet: @escaping () -> Void, onShowReplySheet: @escaping () -> Void, - onLoadMore: @escaping () -> Void) { + onLoadMore: @escaping () -> Void, + onFlag: @escaping () -> Void) { self.level = comment.level ?? 0 self.comment = comment self.onShowHNSheet = onShowHNSheet self.onShowReplySheet = onShowReplySheet self.onLoadMore = onLoadMore + self.onFlag = onFlag self.itemStore = itemStore } @@ -50,13 +53,6 @@ extension ItemView { return AnyView(wrappedView) } - .confirmationDialog("Are you sure?", isPresented: $itemStore.showFlagDialog) { - Button("Flag", role: .destructive) { - onFlagTap() - } - } message: { - Text("Flag the post by \(comment.by.orEmpty)?") - } } } @@ -132,7 +128,12 @@ extension ItemView { } .disabled(!auth.loggedIn) Divider() - FlagButton(id: comment.id, showFlagDialog: $itemStore.showFlagDialog) + Button { + onFlag() + } label: { + Label("Flag", systemImage: "flag") + } + .disabled(!auth.loggedIn) Divider() ShareMenu(item: comment) CopyButton(text: comment.text.orEmpty, actionPerformed: $itemStore.actionPerformed) diff --git a/Shared/Views/ItemView.swift b/Shared/Views/ItemView.swift index 3ab7fa2..b656d4d 100644 --- a/Shared/Views/ItemView.swift +++ b/Shared/Views/ItemView.swift @@ -9,10 +9,11 @@ struct ItemView: View { @State private var showUrlSheet: Bool = .init() @State private var showReplySheet: Bool = .init() @State private var showFlagDialog: Bool = .init() + @State private var flaggingItem: (any Item)? static private var handledUrl: URL? = nil static private var hnSheetTarget: (any Item)? = nil static private var replySheetTarget: (any Item)? = nil - + let settings: Settings = .shared let level: Int @@ -67,10 +68,10 @@ struct ItemView: View { } .confirmationDialog("Are you sure?", isPresented: $showFlagDialog) { Button("Flag", role: .destructive) { - onFlagTap() + flag() } } message: { - Text("Flag the post by \(item.by.orEmpty)?") + Text("Flag the post by \(flaggingItem?.by.orEmpty ?? item.by.orEmpty)?") } .task { if itemStore.item == nil { @@ -168,6 +169,9 @@ struct ItemView: View { Task { await itemStore.loadKids(of: comment) } + } onFlag: { + flaggingItem = comment + showFlagDialog = true } .padding(.trailing, 4) } @@ -287,9 +291,10 @@ struct ItemView: View { } } - private func onFlagTap() { + private func flag() { + let id = flaggingItem?.id ?? item.id Task { - let res = await auth.flag(item.id) + let res = await auth.flag(id) if res { itemStore.actionPerformed = .flag diff --git a/ZCombinator.xcodeproj/project.pbxproj b/ZCombinator.xcodeproj/project.pbxproj index bd956dc..5cded0f 100644 --- a/ZCombinator.xcodeproj/project.pbxproj +++ b/ZCombinator.xcodeproj/project.pbxproj @@ -1087,7 +1087,7 @@ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_ENTITLEMENTS = "ZCombinator (iOS).entitlements"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 4; + CURRENT_PROJECT_VERSION = 5; DEVELOPMENT_TEAM = QMWX3X2NF7; ENABLE_PREVIEWS = YES; GENERATE_INFOPLIST_FILE = YES; @@ -1125,7 +1125,7 @@ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_ENTITLEMENTS = "ZCombinator (iOS).entitlements"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 4; + CURRENT_PROJECT_VERSION = 5; DEVELOPMENT_TEAM = QMWX3X2NF7; ENABLE_PREVIEWS = YES; GENERATE_INFOPLIST_FILE = YES;