From 1064cecb1de2ea7a4b5222447c868bb30a4369c7 Mon Sep 17 00:00:00 2001 From: Terry Yiu <963907+tyiu@users.noreply.github.com> Date: Tue, 20 Aug 2024 19:34:39 +0300 Subject: [PATCH] Add support for search by calendar list events --- .swiftlint.yml | 1 + .../Assets/Localization/Localizable.xcstrings | 4 +- Comingle/Views/CalendarListEventView.swift | 2 +- Comingle/Views/CalendarsView.swift | 2 +- Comingle/Views/EventListView.swift | 101 +++++++++++++----- 5 files changed, 79 insertions(+), 31 deletions(-) diff --git a/.swiftlint.yml b/.swiftlint.yml index 9b46a1f..40891a3 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -1,5 +1,6 @@ disabled_rules: - cyclomatic_complexity - file_length + - function_body_length - line_length - type_body_length diff --git a/Comingle/Assets/Localization/Localizable.xcstrings b/Comingle/Assets/Localization/Localizable.xcstrings index f071bbb..503318c 100644 --- a/Comingle/Assets/Localization/Localizable.xcstrings +++ b/Comingle/Assets/Localization/Localizable.xcstrings @@ -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" } } } diff --git a/Comingle/Views/CalendarListEventView.swift b/Comingle/Views/CalendarListEventView.swift index 775b270..1dcdeb7 100644 --- a/Comingle/Views/CalendarListEventView.swift +++ b/Comingle/Views/CalendarListEventView.swift @@ -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) } ) diff --git a/Comingle/Views/CalendarsView.swift b/Comingle/Views/CalendarsView.swift index e27af7f..f81c831 100644 --- a/Comingle/Views/CalendarsView.swift +++ b/Comingle/Views/CalendarsView.swift @@ -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() diff --git a/Comingle/Views/EventListView.swift b/Comingle/Views/EventListView.swift index 5529fd8..312dca0 100644 --- a/Comingle/Views/EventListView.swift +++ b/Comingle/Views/EventListView.swift @@ -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) @@ -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) } } } @@ -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 @@ -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) } } }