Skip to content

Commit

Permalink
fix flag button. (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
Livinglist committed Mar 24, 2024
1 parent 8d92439 commit e04c8d8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
2 changes: 0 additions & 2 deletions Shared/Models/Stores/ItemStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int> = .init()
Expand Down
21 changes: 11 additions & 10 deletions Shared/Views/Components/CommentTile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)?")
}
}
}

Expand Down Expand Up @@ -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)
Expand Down
15 changes: 10 additions & 5 deletions Shared/Views/ItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -168,6 +169,9 @@ struct ItemView: View {
Task {
await itemStore.loadKids(of: comment)
}
} onFlag: {
flaggingItem = comment
showFlagDialog = true
}
.padding(.trailing, 4)
}
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions ZCombinator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e04c8d8

Please sign in to comment.