Skip to content

Commit

Permalink
Fix SwiftData memory access issues and fix unlocalized strings
Browse files Browse the repository at this point in the history
  • Loading branch information
tyiu committed Aug 4, 2024
1 parent dea7a0e commit a1467c8
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 33 deletions.
36 changes: 36 additions & 0 deletions Comingle/Assets/Localization/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,18 @@
}
}
},
"reportIssue" : {
"comment" : "Button to report an issue about the app.",
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Report an Issue"
}
}
}
},
"resetAllFields" : {
"comment" : "Button to reset all fields to the starting point of a fresh event creation.",
"extractionState" : "manual",
Expand Down Expand Up @@ -1129,6 +1141,18 @@
}
}
},
"settingsAbout" : {
"comment" : "Settings about section title.",
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "About"
}
}
}
},
"settingsAppearance" : {
"comment" : "Settings section for appearance of the app.",
"extractionState" : "manual",
Expand Down Expand Up @@ -1465,6 +1489,18 @@
}
}
},
"version" : {
"comment" : "Label for the app version in the settings view.",
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Version"
}
}
}
},
"viewProfile" : {
"comment" : "Button to view the active profile.",
"extractionState" : "manual",
Expand Down
52 changes: 23 additions & 29 deletions Comingle/Controllers/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ class AppState: ObservableObject, Hashable {

@Published var activeTab: HomeTabs = .following

@Published var persistentNostrEvents: [String: PersistentNostrEvent] = [:]

@Published var followListEvents: [String: FollowListEvent] = [:]
@Published var metadataEvents: [String: MetadataEvent] = [:]
@Published var timeBasedCalendarEvents: [String: TimeBasedCalendarEvent] = [:]
Expand Down Expand Up @@ -217,6 +215,13 @@ class AppState: ObservableObject, Hashable {
relayWritePool.add(relay: $0)
}
}

func persistentNostrEvent(_ eventId: String) -> PersistentNostrEvent? {
let descriptor = FetchDescriptor<PersistentNostrEvent>(
predicate: #Predicate { $0.eventId == eventId }
)
return try? modelContext.fetch(descriptor).first
}
}

extension AppState: EventVerifying, RelayDelegate {
Expand Down Expand Up @@ -416,15 +421,13 @@ extension AppState: EventVerifying, RelayDelegate {
if let timeBasedCalendarEvent = timeBasedCalendarEvents[deletedEventCoordinate.tag.value], timeBasedCalendarEvent.createdAt <= deletionEvent.createdAt {
timeBasedCalendarEvents.removeValue(forKey: deletedEventCoordinate.tag.value)
calendarEventsToRsvps.removeValue(forKey: deletedEventCoordinate.tag.value)
persistentNostrEvents.removeValue(forKey: timeBasedCalendarEvent.id)
}
case .calendarEventRSVP:
if let rsvp = rsvps[deletedEventCoordinate.tag.value], rsvp.createdAt <= deletionEvent.createdAt {
rsvps.removeValue(forKey: deletedEventCoordinate.tag.value)
if let calendarEventCoordinates = rsvp.calendarEventCoordinates?.tag.value {
calendarEventsToRsvps[calendarEventCoordinates]?.removeAll(where: { $0 == rsvp })
}
persistentNostrEvents.removeValue(forKey: rsvp.id)
}
default:
continue
Expand All @@ -434,7 +437,7 @@ extension AppState: EventVerifying, RelayDelegate {

private func deleteFromEventIds(_ deletionEvent: DeletionEvent) {
for deletedEventId in deletionEvent.deletedEventIds {
if let persistentNostrEvent = persistentNostrEvents[deletedEventId] {
if let persistentNostrEvent = persistentNostrEvent(deletedEventId) {
let nostrEvent = persistentNostrEvent.nostrEvent

guard nostrEvent.pubkey == deletionEvent.pubkey else {
Expand Down Expand Up @@ -465,7 +468,6 @@ extension AppState: EventVerifying, RelayDelegate {
continue
}

persistentNostrEvents.removeValue(forKey: deletedEventId)
modelContext.delete(persistentNostrEvent)
do {
try modelContext.save()
Expand All @@ -489,13 +491,12 @@ extension AppState: EventVerifying, RelayDelegate {
// If the verification throws an error, that means they are invalid and we should ignore the event.
try? self.verifyEvent(nostrEvent)

if let existingEvent = self.persistentNostrEvents[nostrEvent.id] {
if let existingEvent = self.persistentNostrEvent(nostrEvent.id) {
if !existingEvent.relays.contains(where: { $0 == relay.url }) {
existingEvent.relays.append(relay.url)
}
} else {
let persistentNostrEvent = PersistentNostrEvent(nostrEvent: nostrEvent, relays: [relay.url])
self.persistentNostrEvents[persistentNostrEvent.nostrEvent.id] = persistentNostrEvent
self.modelContext.insert(persistentNostrEvent)
do {
try self.modelContext.save()
Expand Down Expand Up @@ -523,26 +524,19 @@ extension AppState: EventVerifying, RelayDelegate {

func loadPersistentNostrEvents(_ persistentNostrEvents: [PersistentNostrEvent]) {
for persistentNostrEvent in persistentNostrEvents {
if let existingEvent = self.persistentNostrEvents[persistentNostrEvent.nostrEvent.id] {
let missingRelays = Set(persistentNostrEvent.relays).subtracting(Set(existingEvent.relays))
existingEvent.relays.append(contentsOf: missingRelays)
} else {
self.persistentNostrEvents[persistentNostrEvent.nostrEvent.id] = persistentNostrEvent

switch persistentNostrEvent.nostrEvent {
case let followListEvent as FollowListEvent:
self.didReceiveFollowListEvent(followListEvent)
case let metadataEvent as MetadataEvent:
self.didReceiveMetadataEvent(metadataEvent)
case let timeBasedCalendarEvent as TimeBasedCalendarEvent:
self.didReceiveTimeBasedCalendarEvent(timeBasedCalendarEvent)
case let rsvpEvent as CalendarEventRSVP:
self.didReceiveCalendarEventRSVP(rsvpEvent)
case let deletionEvent as DeletionEvent:
self.didReceiveDeletionEvent(deletionEvent)
default:
break
}
switch persistentNostrEvent.nostrEvent {
case let followListEvent as FollowListEvent:
self.didReceiveFollowListEvent(followListEvent)
case let metadataEvent as MetadataEvent:
self.didReceiveMetadataEvent(metadataEvent)
case let timeBasedCalendarEvent as TimeBasedCalendarEvent:
self.didReceiveTimeBasedCalendarEvent(timeBasedCalendarEvent)
case let rsvpEvent as CalendarEventRSVP:
self.didReceiveCalendarEventRSVP(rsvpEvent)
case let deletionEvent as DeletionEvent:
self.didReceiveDeletionEvent(deletionEvent)
default:
break
}
}

Expand All @@ -559,7 +553,7 @@ extension AppState: EventVerifying, RelayDelegate {
try? relay.closeSubscription(with: subscriptionId)
case let .ok(eventId, success, message):
if success {
if let persistentNostrEvent = persistentNostrEvents[eventId], !persistentNostrEvent.relays.contains(relay.url) {
if let persistentNostrEvent = persistentNostrEvent(eventId), !persistentNostrEvent.relays.contains(relay.url) {
persistentNostrEvent.relays.append(relay.url)
}
} else if message.prefix == .rateLimited {
Expand Down
4 changes: 2 additions & 2 deletions Comingle/Views/EventView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ struct EventView: View, EventCreating {
}
}

if let persistentNostrEvent = appState.persistentNostrEvents[event.id] {
if let persistentNostrEvent = appState.persistentNostrEvent(event.id) {
Divider()

VStack {
Expand Down Expand Up @@ -584,7 +584,7 @@ struct EventView: View, EventCreating {
ToolbarItem {
Menu {
if let event = event {
let relays = appState.persistentNostrEvents[event.id]?.relays ?? []
let relays = appState.persistentNostrEvent(event.id)?.relays ?? []
let shareableEventCoordinates = try? event.shareableEventCoordinates(relayURLStrings: relays.map { $0.absoluteString })

if appState.keypair != nil && appState.publicKey?.hex == event.pubkey {
Expand Down
12 changes: 10 additions & 2 deletions Comingle/Views/Settings/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,22 @@ struct SettingsView: View {
var aboutSection: some View {
Section(
content: {
LabeledContent("Version", value: viewModel.appVersion)
LabeledContent(String(localized: .localizable.version), value: viewModel.appVersion)

NavigationLink(destination: AcknowledgementsView()) {
Text(.localizable.acknowledgements)
}

if let url = URL(string: "https://github.com/comingle-co/comingle-ios/issues") {
Button(action: {
UIApplication.shared.open(url)
}, label: {
Text(.localizable.reportIssue)
})
}
},
header: {
Text("About")
Text(.localizable.settingsAbout)
}
)
}
Expand Down

0 comments on commit a1467c8

Please sign in to comment.