damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 0f1390f4128ceb5cbb7135324080f5f6d2e685d4
parent 6bf52937014b238d17369bee2bc40ea1933c6d82
Author: Bryan Montz <bryanmontz@me.com>
Date:   Sun, 16 Jul 2023 08:00:54 -0500

Swift cleanup: remove duplicate or unnecessary initializers using default values

Signed-off-by: Bryan Montz <bryanmontz@me.com>
Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Mdamus/Components/EndBlock.swift | 6+-----
Mdamus/Models/ActionBarModel.swift | 14+-------------
Mdamus/Models/CreateAccountModel.swift | 8+-------
Mdamus/Models/DirectMessageModel.swift | 16+++-------------
Mdamus/Models/DraftsModel.swift | 7+------
Mdamus/Models/Notifications/EventGroup.swift | 6+-----
Mdamus/Models/Notifications/ZapGroup.swift | 12+++---------
Mdamus/Models/NotificationsModel.swift | 36+++++++++++-------------------------
Mdamus/Models/SignalModel.swift | 7+------
Mdamus/Util/EventHolder.swift | 26++++----------------------
Mdamus/Util/PreviewCache.swift | 6+-----
Mdamus/Views/Notifications/NotificationsView.swift | 7+------
Mdamus/Views/Profile/ProfileName.swift | 10+---------
Mdamus/Views/Relays/RecommendedRelayView.swift | 9+--------
14 files changed, 31 insertions(+), 139 deletions(-)

diff --git a/damus/Components/EndBlock.swift b/damus/Components/EndBlock.swift @@ -10,11 +10,7 @@ import SwiftUI struct EndBlock: View { let height: CGFloat - init() { - self.height = 10.0 - } - - init(height: Float) { + init(height: Float = 10) { self.height = CGFloat(height) } diff --git a/damus/Models/ActionBarModel.swift b/damus/Models/ActionBarModel.swift @@ -28,19 +28,7 @@ class ActionBarModel: ObservableObject { return ActionBarModel(likes: 0, boosts: 0, zaps: 0, zap_total: 0, replies: 0, our_like: nil, our_boost: nil, our_zap: nil, our_reply: nil) } - init() { - self.our_like = nil - self.our_boost = nil - self.our_reply = nil - self.our_zap = nil - self.likes = 0 - self.boosts = 0 - self.zaps = 0 - self.zap_total = 0 - self.replies = 0 - } - - init(likes: Int, boosts: Int, zaps: Int, zap_total: Int64, replies: Int, our_like: NostrEvent?, our_boost: NostrEvent?, our_zap: Zapping?, our_reply: NostrEvent?) { + init(likes: Int = 0, boosts: Int = 0, zaps: Int = 0, zap_total: Int64 = 0, replies: Int = 0, our_like: NostrEvent? = nil, our_boost: NostrEvent? = nil, our_zap: Zapping? = nil, our_reply: NostrEvent? = nil) { self.likes = likes self.boosts = boosts self.zaps = zaps diff --git a/damus/Models/CreateAccountModel.swift b/damus/Models/CreateAccountModel.swift @@ -35,13 +35,7 @@ class CreateAccountModel: ObservableObject { return Keypair(pubkey: self.pubkey, privkey: self.privkey) } - init() { - let keypair = generate_new_keypair() - self.pubkey = keypair.pubkey - self.privkey = keypair.privkey! - } - - init(real: String, nick: String, about: String) { + init(real: String = "", nick: String = "", about: String = "") { let keypair = generate_new_keypair() self.pubkey = keypair.pubkey self.privkey = keypair.privkey! diff --git a/damus/Models/DirectMessageModel.swift b/damus/Models/DirectMessageModel.swift @@ -14,11 +14,11 @@ class DirectMessageModel: ObservableObject { } } - @Published var draft: String + @Published var draft: String = "" let pubkey: String - var is_request: Bool + var is_request = false var our_pubkey: String func determine_is_request() -> Bool { @@ -31,19 +31,9 @@ class DirectMessageModel: ObservableObject { return true } - init(events: [NostrEvent], our_pubkey: String, pubkey: String) { + init(events: [NostrEvent] = [], our_pubkey: String, pubkey: String) { self.events = events - self.is_request = false self.our_pubkey = our_pubkey - self.draft = "" - self.pubkey = pubkey - } - - init(our_pubkey: String, pubkey: String) { - self.events = [] - self.is_request = false - self.our_pubkey = our_pubkey - self.draft = "" self.pubkey = pubkey } } diff --git a/damus/Models/DraftsModel.swift b/damus/Models/DraftsModel.swift @@ -11,12 +11,7 @@ class DraftArtifacts { var content: NSMutableAttributedString var media: [UploadedMedia] - init() { - self.content = NSMutableAttributedString(string: "") - self.media = [] - } - - init(content: NSMutableAttributedString, media: [UploadedMedia]) { + init(content: NSMutableAttributedString = NSMutableAttributedString(string: ""), media: [UploadedMedia] = []) { self.content = content self.media = media } diff --git a/damus/Models/Notifications/EventGroup.swift b/damus/Models/Notifications/EventGroup.swift @@ -18,11 +18,7 @@ class EventGroup { return first.created_at } - init() { - self.events = [] - } - - init(events: [NostrEvent]) { + init(events: [NostrEvent] = []) { self.events = events } diff --git a/damus/Models/Notifications/ZapGroup.swift b/damus/Models/Notifications/ZapGroup.swift @@ -8,9 +8,9 @@ import Foundation class ZapGroup { - var zaps: [Zapping] - var msat_total: Int64 - var zappers: Set<String> + var zaps: [Zapping] = [] + var msat_total: Int64 = 0 + var zappers = Set<String>() var last_event_at: Int64 { guard let first = zaps.first else { @@ -46,12 +46,6 @@ class ZapGroup { return grp } - init() { - self.zaps = [] - self.msat_total = 0 - self.zappers = Set() - } - @discardableResult func insert(_ zap: Zapping) -> Bool { if !insert_uniq_sorted_zap_by_created(zaps: &zaps, new_zap: zap) { diff --git a/damus/Models/NotificationsModel.swift b/damus/Models/NotificationsModel.swift @@ -99,34 +99,20 @@ enum NotificationItem { } class NotificationsModel: ObservableObject, ScrollQueue { - var incoming_zaps: [Zapping] - var incoming_events: [NostrEvent] - var should_queue: Bool + var incoming_zaps: [Zapping] = [] + var incoming_events: [NostrEvent] = [] + var should_queue: Bool = true // mappings from events to - var zaps: [String: ZapGroup] - var profile_zaps: ZapGroup - var reactions: [String: EventGroup] - var reposts: [String: EventGroup] - var replies: [NostrEvent] - var has_reply: Set<String> - var has_ev: Set<String> + var zaps: [String: ZapGroup] = [:] + var profile_zaps = ZapGroup() + var reactions: [String: EventGroup] = [:] + var reposts: [String: EventGroup] = [:] + var replies: [NostrEvent] = [] + var has_reply = Set<String>() + var has_ev = Set<String>() - @Published var notifications: [NotificationItem] - - init() { - self.zaps = [:] - self.reactions = [:] - self.reposts = [:] - self.replies = [] - self.has_reply = Set() - self.should_queue = true - self.incoming_zaps = [] - self.incoming_events = [] - self.profile_zaps = ZapGroup() - self.notifications = [] - self.has_ev = Set() - } + @Published var notifications: [NotificationItem] = [] func set_should_queue(_ val: Bool) { self.should_queue = val diff --git a/damus/Models/SignalModel.swift b/damus/Models/SignalModel.swift @@ -12,12 +12,7 @@ class SignalModel: ObservableObject { @Published var signal: Int @Published var max_signal: Int - init() { - self.signal = 0 - self.max_signal = 0 - } - - init(signal: Int, max_signal: Int) { + init(signal: Int = 0, max_signal: Int = 0) { self.signal = signal self.max_signal = max_signal } diff --git a/damus/Util/EventHolder.swift b/damus/Util/EventHolder.swift @@ -9,10 +9,10 @@ import Foundation /// Used for holding back events until they're ready to be displayed class EventHolder: ObservableObject, ScrollQueue { - private var has_event: Set<String> + private var has_event = Set<String>() @Published var events: [NostrEvent] var incoming: [NostrEvent] - var should_queue: Bool + var should_queue = false var on_queue: ((NostrEvent) -> Void)? func set_should_queue(_ val: Bool) { @@ -27,28 +27,10 @@ class EventHolder: ObservableObject, ScrollQueue { events + incoming } - init() { - self.should_queue = false - self.events = [] - self.incoming = [] - self.has_event = Set() - self.on_queue = nil - } - - init(on_queue: @escaping (NostrEvent) -> ()) { - self.should_queue = false - self.events = [] - self.incoming = [] - self.has_event = Set() - self.on_queue = on_queue - } - - init(events: [NostrEvent], incoming: [NostrEvent]) { - self.should_queue = false + init(events: [NostrEvent] = [], incoming: [NostrEvent] = [], on_queue: ((NostrEvent) -> ())? = nil) { self.events = events self.incoming = incoming - self.has_event = Set() - self.on_queue = nil + self.on_queue = on_queue } func filter(_ isIncluded: (NostrEvent) -> Bool) { diff --git a/damus/Util/PreviewCache.swift b/damus/Util/PreviewCache.swift @@ -65,13 +65,9 @@ enum PreviewState { } class PreviewCache { - private var previews: [String: Preview] + private var previews: [String: Preview] = [:] func lookup(_ evid: String) -> Preview? { return previews[evid] } - - init() { - self.previews = [:] - } } diff --git a/damus/Views/Notifications/NotificationsView.swift b/damus/Views/Notifications/NotificationsView.swift @@ -41,12 +41,7 @@ class NotificationFilter: ObservableObject, Equatable { return lhs.state == rhs.state && lhs.fine_filter == rhs.fine_filter } - init() { - self.state = .all - self.fine_filter = .all - } - - init(state: NotificationFilterState, fine_filter: FriendFilter) { + init(state: NotificationFilterState = .all, fine_filter: FriendFilter = .all) { self.state = state self.fine_filter = fine_filter } diff --git a/damus/Views/Profile/ProfileName.swift b/damus/Views/Profile/ProfileName.swift @@ -35,16 +35,8 @@ struct ProfileName: View { @State var display_name: DisplayName? @State var nip05: NIP05? @State var donation: Int? - - init(pubkey: String, profile: Profile?, damus: DamusState, show_nip5_domain: Bool = true) { - self.pubkey = pubkey - self.profile = profile - self.prefix = "" - self.show_nip5_domain = show_nip5_domain - self.damus_state = damus - } - init(pubkey: String, profile: Profile?, prefix: String, damus: DamusState, show_nip5_domain: Bool = true) { + init(pubkey: String, profile: Profile?, prefix: String = "", damus: DamusState, show_nip5_domain: Bool = true) { self.pubkey = pubkey self.profile = profile self.prefix = prefix diff --git a/damus/Views/Relays/RecommendedRelayView.swift b/damus/Views/Relays/RecommendedRelayView.swift @@ -14,14 +14,7 @@ struct RecommendedRelayView: View { @Binding var showActionButtons: Bool - init(damus: DamusState, relay: String, showActionButtons: Binding<Bool>) { - self.damus = damus - self.relay = relay - self.add_button = true - self._showActionButtons = showActionButtons - } - - init(damus: DamusState, relay: String, add_button: Bool, showActionButtons: Binding<Bool>) { + init(damus: DamusState, relay: String, add_button: Bool = true, showActionButtons: Binding<Bool>) { self.damus = damus self.relay = relay self.add_button = add_button