Skip to content

Commit

Permalink
Add support for search by calendar list events
Browse files Browse the repository at this point in the history
  • Loading branch information
tyiu committed Aug 20, 2024
1 parent 266a53f commit 1064cec
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 31 deletions.
1 change: 1 addition & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
disabled_rules:
- cyclomatic_complexity
- file_length
- function_body_length
- line_length
- type_body_length
4 changes: 2 additions & 2 deletions Comingle/Assets/Localization/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -661,14 +661,14 @@
}
}
},
"noCalendarTitle" : {
"noCalendarName" : {
"comment" : "Text to indicate that there is no title for the calendar.",
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "No Title"
"value" : "No Name"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Comingle/Views/CalendarListEventView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct CalendarListEventView: View {
}
},
label: {
Text(calendarListEvent.title ?? calendarListEvent.firstValueForRawTagName("name") ?? "No Name")
Text(calendarListEvent.title ?? calendarListEvent.firstValueForRawTagName("name") ?? String(localized: .localizable.noCalendarName))
.font(.headline)
}
)
Expand Down
2 changes: 1 addition & 1 deletion Comingle/Views/CalendarsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct CalendarsView: View {

func titleAndProfileView(_ calendarListEvent: CalendarListEvent) -> some View {
VStack(alignment: .leading) {
Text(calendarListEvent.title?.trimmedOrNilIfEmpty ?? calendarListEvent.firstValueForRawTagName("name")?.trimmedOrNilIfEmpty ?? String(localized: .localizable.noCalendarTitle))
Text(calendarListEvent.title?.trimmedOrNilIfEmpty ?? calendarListEvent.firstValueForRawTagName("name")?.trimmedOrNilIfEmpty ?? String(localized: .localizable.noCalendarName))
.font(.headline)

Divider()
Expand Down
101 changes: 74 additions & 27 deletions Comingle/Views/EventListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,33 @@ struct EventListView: View, MetadataCoding {
List {
// Search by npub.
if eventListType == .all,
let searchText = searchViewModel.debouncedSearchText.trimmedOrNilIfEmpty,
let authorPublicKey = PublicKey(npub: searchText) {
Section(
content: {
ProfilePictureAndNameView(publicKeyHex: authorPublicKey.hex)
let searchText = searchViewModel.debouncedSearchText.trimmedOrNilIfEmpty {
if let authorPublicKey = PublicKey(npub: searchText) {
Section(
content: {
ProfilePictureAndNameView(publicKeyHex: authorPublicKey.hex)
}
)
} else {
if let metadata = try? decodedMetadata(from: searchText), let kind = metadata.kind, kind == EventKind.calendar.rawValue, let pubkey = metadata.pubkey, let publicKey = PublicKey(hex: pubkey) {
// Search by naddr.
if let identifier = metadata.identifier,
let eventCoordinates = try? EventCoordinates(kind: EventKind(rawValue: Int(kind)), pubkey: publicKey, identifier: identifier),
let calendarListEvent = appState.calendarListEvents[eventCoordinates.tag.value] {
Section(
content: {
HStack {
calendarTitleAndProfileView(calendarListEvent)

if let imageURL = calendarListEvent.imageURL {
imageView(imageURL)
}
}
}
)
}
}
)
}
}

let filteredEvents = events(timeTabFilter)
Expand Down Expand Up @@ -112,11 +132,7 @@ struct EventListView: View, MetadataCoding {
}

if let calendarEventImageURL = event.imageURL {
KFImage.url(calendarEventImageURL)
.resizable()
.placeholder { ProgressView() }
.scaledToFit()
.frame(maxWidth: 100, maxHeight: 200)
imageView(calendarEventImageURL)
}
}
}
Expand All @@ -136,6 +152,25 @@ struct EventListView: View, MetadataCoding {
}
}

func calendarTitleAndProfileView(_ calendarListEvent: CalendarListEvent) -> some View {
VStack(alignment: .leading) {
Text(calendarListEvent.title?.trimmedOrNilIfEmpty ?? calendarListEvent.firstValueForRawTagName("name")?.trimmedOrNilIfEmpty ?? String(localized: .localizable.noCalendarName))
.font(.headline)

Divider()

ProfilePictureAndNameView(publicKeyHex: calendarListEvent.pubkey)
}
}

func imageView(_ imageURL: URL) -> some View {
KFImage.url(imageURL)
.resizable()
.placeholder { ProgressView() }
.scaledToFit()
.frame(maxWidth: 100, maxHeight: 200)
}

private func resolveTimeZone(_ timeZone: TimeZone?) -> TimeZone {
guard let timeZone else {
return Calendar.autoupdatingCurrent.timeZone
Expand Down Expand Up @@ -173,25 +208,37 @@ struct EventListView: View, MetadataCoding {
return appState.pastProfileEvents(authorPublicKey.hex)
}
}
if let metadata = try? decodedMetadata(from: searchText), let kind = metadata.kind, kind == EventKind.timeBasedCalendarEvent.rawValue, let pubkey = metadata.pubkey, let publicKey = PublicKey(hex: pubkey) {
// Search by naddr.
if let identifier = metadata.identifier,
let eventCoordinates = try? EventCoordinates(kind: EventKind(rawValue: Int(kind)), pubkey: publicKey, identifier: identifier),
let timeBasedCalendarEvent = appState.timeBasedCalendarEvents[eventCoordinates.tag.value] {
if timeTabFilter == .upcoming && !timeBasedCalendarEvent.isUpcoming {
self.timeTabFilter = .past
} else if timeTabFilter == .past && !timeBasedCalendarEvent.isPast {
self.timeTabFilter = .upcoming
}
return [timeBasedCalendarEvent]
// Search by nevent.
} else if let eventId = metadata.eventId {
let results = appState.searchTrie.find(key: eventId)
if let metadata = try? decodedMetadata(from: searchText), let kind = metadata.kind, let pubkey = metadata.pubkey, let publicKey = PublicKey(hex: pubkey) {
if kind == EventKind.timeBasedCalendarEvent.rawValue {
// Search by naddr.
if let identifier = metadata.identifier,
let eventCoordinates = try? EventCoordinates(kind: EventKind(rawValue: Int(kind)), pubkey: publicKey, identifier: identifier),
let timeBasedCalendarEvent = appState.timeBasedCalendarEvents[eventCoordinates.tag.value] {
if timeTabFilter == .upcoming && !timeBasedCalendarEvent.isUpcoming {
self.timeTabFilter = .past
} else if timeTabFilter == .past && !timeBasedCalendarEvent.isPast {
self.timeTabFilter = .upcoming
}
return [timeBasedCalendarEvent]
// Search by nevent.
} else if let eventId = metadata.eventId {
let results = appState.searchTrie.find(key: eventId)
switch timeTabFilter {
case .upcoming:
return appState.upcomingEvents(results)
case .past:
return appState.pastEvents(results)
}
}
} else if kind == EventKind.calendar.rawValue,
let identifier = metadata.identifier,
let coordinates = try? EventCoordinates(kind: EventKind(rawValue: Int(kind)), pubkey: publicKey, identifier: identifier) {
let coordinatesString = coordinates.tag.value
switch timeTabFilter {
case .upcoming:
return appState.upcomingEvents(results)
return appState.upcomingEventsOnCalendarList(coordinatesString)
case .past:
return appState.pastEvents(results)
return appState.pastEventsOnCalendarList(coordinatesString)
}
}
}
Expand Down

0 comments on commit 1064cec

Please sign in to comment.