commit 18449c8c0da58021146b2ba73ee063d45346dac4
parent 044631b324764abf67699de8909155aab81ef810
Author: OlegAba <mail@olegaba.com>
Date: Sun, 5 Mar 2023 21:04:50 -0500
Fix repost button sometimes not working
Changelog-Fixed: Fix repost button sometimes not working
Closes: #738
Diffstat:
5 files changed, 76 insertions(+), 20 deletions(-)
diff --git a/damus.xcodeproj/project.pbxproj b/damus.xcodeproj/project.pbxproj
@@ -229,6 +229,7 @@
647D9A8D2968520300A295DE /* SideMenuView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 647D9A8C2968520300A295DE /* SideMenuView.swift */; };
64FBD06F296255C400D9D3B2 /* Theme.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64FBD06E296255C400D9D3B2 /* Theme.swift */; };
6C7DE41F2955169800E66263 /* Vault in Frameworks */ = {isa = PBXBuildFile; productRef = 6C7DE41E2955169800E66263 /* Vault */; };
+ 7C0F392F29B57CAF0039859C /* Binding+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C0F392E29B57CAF0039859C /* Binding+.swift */; };
7C60CAEF298471A1009C80D6 /* CoreSVG.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C60CAEE298471A1009C80D6 /* CoreSVG.swift */; };
7C902AE32981D55B002AB16E /* ZoomableScrollView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C902AE22981D55B002AB16E /* ZoomableScrollView.swift */; };
7C95CAEE299DCEF1009DCB67 /* KFOptionSetter+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C95CAED299DCEF1009DCB67 /* KFOptionSetter+.swift */; };
@@ -584,6 +585,7 @@
643EA5C7296B764E005081BB /* RelayFilterView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RelayFilterView.swift; sourceTree = "<group>"; };
647D9A8C2968520300A295DE /* SideMenuView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SideMenuView.swift; sourceTree = "<group>"; };
64FBD06E296255C400D9D3B2 /* Theme.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Theme.swift; sourceTree = "<group>"; };
+ 7C0F392E29B57CAF0039859C /* Binding+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Binding+.swift"; sourceTree = "<group>"; };
7C60CAEE298471A1009C80D6 /* CoreSVG.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreSVG.swift; sourceTree = "<group>"; };
7C902AE22981D55B002AB16E /* ZoomableScrollView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZoomableScrollView.swift; sourceTree = "<group>"; };
7C95CAED299DCEF1009DCB67 /* KFOptionSetter+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "KFOptionSetter+.swift"; sourceTree = "<group>"; };
@@ -863,6 +865,7 @@
4C7FF7D628233637009601DB /* Util */ = {
isa = PBXGroup;
children = (
+ 7C0F392D29B57C8F0039859C /* Extensions */,
4CE879492995B58700F758CC /* Relays */,
4CF0ABEA29844B2F00D66079 /* AnyCodable */,
4CC7AAE6297EFA7B00430951 /* Zap.swift */,
@@ -890,7 +893,6 @@
4CB883B5297730E400DC99E7 /* LNUrls.swift */,
3AB72AB8298ECF30004BB58C /* Translator.swift */,
4C2CDDF6299D4A5E00879FD5 /* Debouncer.swift */,
- 7C95CAED299DCEF1009DCB67 /* KFOptionSetter+.swift */,
4CE0E2AE29A2E82100DB4CA2 /* EventHolder.swift */,
3A3040F029A8FF97008A0F29 /* LocalizationUtil.swift */,
4C30AC7729A577AB00E2BD5A /* EventCache.swift */,
@@ -1151,6 +1153,15 @@
path = Posting;
sourceTree = "<group>";
};
+ 7C0F392D29B57C8F0039859C /* Extensions */ = {
+ isa = PBXGroup;
+ children = (
+ 7C95CAED299DCEF1009DCB67 /* KFOptionSetter+.swift */,
+ 7C0F392E29B57CAF0039859C /* Binding+.swift */,
+ );
+ path = Extensions;
+ sourceTree = "<group>";
+ };
F7F0BA23297892AE009531F3 /* Modifiers */ = {
isa = PBXGroup;
children = (
@@ -1535,6 +1546,7 @@
4CC7AAFA297F64AC00430951 /* EventMenu.swift in Sources */,
4C75EFBB2804A34C0006080F /* ProofOfWork.swift in Sources */,
4C3AC7A52836987600E1F516 /* MainTabView.swift in Sources */,
+ 7C0F392F29B57CAF0039859C /* Binding+.swift in Sources */,
3AA59D1D2999B0400061C48E /* DraftsModel.swift in Sources */,
3169CAED294FCCFC00EE4006 /* Constants.swift in Sources */,
4CB9D4A72992D02B00A9A7E4 /* ProfileNameView.swift in Sources */,
diff --git a/damus/ContentView.swift b/damus/ContentView.swift
@@ -89,6 +89,7 @@ struct ContentView: View {
@State var confirm_block: Bool = false
@State var user_blocked_confirm: Bool = false
@State var confirm_overwrite_mutelist: Bool = false
+ @State var current_boost: NostrEvent? = nil
@State var filter_state : FilterState = .posts_and_replies
@State private var isSideBarOpened = false
@StateObject var home: HomeModel = HomeModel()
@@ -369,12 +370,7 @@ struct ContentView: View {
}
.onReceive(handle_notify(.boost)) { notif in
- guard let privkey = self.privkey else {
- return
- }
- let ev = notif.object as! NostrEvent
- let boost = make_boost_event(pubkey: pubkey, privkey: privkey, boosted: ev)
- self.damus_state?.pool.send(.event(boost))
+ current_boost = (notif.object as? NostrEvent)
}
.onReceive(handle_notify(.open_thread)) { obj in
//let ev = obj.object as! NostrEvent
@@ -565,6 +561,16 @@ struct ContentView: View {
Text("Could not find user to block...", comment: "Alert message to indicate that the blocked user could not be found.")
}
})
+ .alert(NSLocalizedString("Repost", comment: "Title of alert for confirming to repost a post."), isPresented: $current_boost.mappedToBool()) {
+ Button(NSLocalizedString("Cancel", comment: "Button to cancel out of reposting a post.")) {
+ current_boost = nil
+ }
+ Button(NSLocalizedString("Repost", comment: "Button to confirm reposting a post.")) {
+ self.damus_state?.pool.send(.event(current_boost!))
+ }
+ } message: {
+ Text("Are you sure you want to repost this?", comment: "Alert message to ask if user wants to repost a post.")
+ }
}
func switch_timeline(_ timeline: Timeline) {
diff --git a/damus/Util/Extensions/Binding+.swift b/damus/Util/Extensions/Binding+.swift
@@ -0,0 +1,49 @@
+//
+// Binding+.swift
+// damus
+//
+// Created by Oleg Abalonski on 3/5/23.
+// Ref: https://josephduffy.co.uk/posts/mapping-optional-binding-to-bool
+
+import os.log
+import SwiftUI
+
+extension Binding where Value == Bool {
+ /// Creates a binding by mapping an optional value to a `Bool` that is
+ /// `true` when the value is non-`nil` and `false` when the value is `nil`.
+ ///
+ /// When the value of the produced binding is set to `false` the value
+ /// of `bindingToOptional`'s `wrappedValue` is set to `nil`.
+ ///
+ /// Setting the value of the produce binding to `true` does nothing and
+ /// will log an error.
+ ///
+ /// - parameter bindingToOptional: A `Binding` to an optional value, used to calculate the `wrappedValue`.
+ public init<Wrapped>(mappedTo bindingToOptional: Binding<Wrapped?>) {
+ self.init(
+ get: { bindingToOptional.wrappedValue != nil },
+ set: { newValue in
+ if !newValue {
+ bindingToOptional.wrappedValue = nil
+ } else {
+ os_log(
+ .error,
+ "Optional binding mapped to optional has been set to `true`, which will have no effect. Current value: %@",
+ String(describing: bindingToOptional.wrappedValue)
+ )
+ }
+ }
+ )
+ }
+}
+
+extension Binding {
+ /// Returns a binding by mapping this binding's value to a `Bool` that is
+ /// `true` when the value is non-`nil` and `false` when the value is `nil`.
+ ///
+ /// When the value of the produced binding is set to `false` this binding's value
+ /// is set to `nil`.
+ public func mappedToBool<Wrapped>() -> Binding<Bool> where Value == Wrapped? {
+ return Binding<Bool>(mappedTo: self)
+ }
+}
diff --git a/damus/Util/KFOptionSetter+.swift b/damus/Util/Extensions/KFOptionSetter+.swift
diff --git a/damus/Views/ActionBar/EventActionBar.swift b/damus/Views/ActionBar/EventActionBar.swift
@@ -26,7 +26,6 @@ struct EventActionBar: View {
// just used for previews
@State var sheet: ActionBarSheet? = nil
- @State var confirm_boost: Bool = false
@State var show_share_sheet: Bool = false
@State var show_share_action: Bool = false
@@ -60,7 +59,7 @@ struct EventActionBar: View {
if bar.boosted {
notify(.delete, bar.our_boost)
} else {
- self.confirm_boost = true
+ send_boost()
}
}
.accessibilityLabel(NSLocalizedString("Boosts", comment: "Accessibility label for boosts button"))
@@ -115,16 +114,6 @@ struct EventActionBar: View {
}
}
}
- .alert(NSLocalizedString("Repost", comment: "Title of alert for confirming to repost a post."), isPresented: $confirm_boost) {
- Button(NSLocalizedString("Cancel", comment: "Button to cancel out of reposting a post.")) {
- confirm_boost = false
- }
- Button(NSLocalizedString("Repost", comment: "Button to confirm reposting a post.")) {
- send_boost()
- }
- } message: {
- Text("Are you sure you want to repost this?", comment: "Alert message to ask if user wants to repost a post.")
- }
.onReceive(handle_notify(.update_stats)) { n in
let target = n.object as! String
guard target == self.event.id else { return }
@@ -151,7 +140,7 @@ struct EventActionBar: View {
self.bar.our_boost = boost
- damus_state.pool.send(.event(boost))
+ notify(.boost, boost)
}
func send_like() {