From 24b1602972421ddc32d78281752a6b67503e0fb0 Mon Sep 17 00:00:00 2001 From: Nathan Mattes Date: Mon, 27 Mar 2023 22:41:36 +0200 Subject: [PATCH] Search online for hashtags (IOS-37) --- Mastodon.xcodeproj/project.pbxproj | 4 +++ .../Handler/HashtagIntentHandler.swift | 34 +++++++++++++++++++ MastodonIntent/IntentHandler.swift | 2 ++ .../WidgetExtension.intentdefinition | 22 ++---------- 4 files changed, 42 insertions(+), 20 deletions(-) create mode 100644 MastodonIntent/Handler/HashtagIntentHandler.swift diff --git a/Mastodon.xcodeproj/project.pbxproj b/Mastodon.xcodeproj/project.pbxproj index e27e02499..8e68c405a 100644 --- a/Mastodon.xcodeproj/project.pbxproj +++ b/Mastodon.xcodeproj/project.pbxproj @@ -150,6 +150,7 @@ D8A6AB6C291C5136003AB663 /* MastodonLoginViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8A6AB6B291C5136003AB663 /* MastodonLoginViewController.swift */; }; D8E5C346296DAB84007E76A7 /* DataSourceFacade+Status+History.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8E5C345296DAB84007E76A7 /* DataSourceFacade+Status+History.swift */; }; D8E5C349296DB8A3007E76A7 /* StatusEditHistoryViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8E5C348296DB8A3007E76A7 /* StatusEditHistoryViewController.swift */; }; + D8F0372C29D232730027DE2E /* HashtagIntentHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8F0372B29D232730027DE2E /* HashtagIntentHandler.swift */; }; D8F8A03A29CA5C15000195DD /* HashtagWidgetView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8F8A03929CA5C15000195DD /* HashtagWidgetView.swift */; }; D8F8A03C29CA5CB6000195DD /* HashtagWidget.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8F8A03B29CA5CB6000195DD /* HashtagWidget.swift */; }; DB0009A626AEE5DC009B9D2D /* Intents.intentdefinition in Sources */ = {isa = PBXBuildFile; fileRef = DB0009A926AEE5DC009B9D2D /* Intents.intentdefinition */; settings = {ATTRIBUTES = (codegen, ); }; }; @@ -796,6 +797,7 @@ D8A6FE6629325F5900666A47 /* Localizable.stringsdict */ = {isa = PBXFileReference; lastKnownFileType = text.plist.stringsdict; path = Localizable.stringsdict; sourceTree = ""; }; D8E5C345296DAB84007E76A7 /* DataSourceFacade+Status+History.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DataSourceFacade+Status+History.swift"; sourceTree = ""; }; D8E5C348296DB8A3007E76A7 /* StatusEditHistoryViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusEditHistoryViewController.swift; sourceTree = ""; }; + D8F0372B29D232730027DE2E /* HashtagIntentHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HashtagIntentHandler.swift; sourceTree = ""; }; D8F8A03929CA5C15000195DD /* HashtagWidgetView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HashtagWidgetView.swift; sourceTree = ""; }; D8F8A03B29CA5CB6000195DD /* HashtagWidget.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HashtagWidget.swift; sourceTree = ""; }; DB0009A826AEE5DC009B9D2D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.intentdefinition; name = Base; path = Base.lproj/Intents.intentdefinition; sourceTree = ""; }; @@ -2341,6 +2343,7 @@ DBB8AB4526AECDE200F6D281 /* SendPostIntentHandler.swift */, 2AE202AB297FE19100F66E55 /* FollowersCountIntentHandler.swift */, 2A86A14529892944007F1062 /* MultiFollowersCountIntentHandler.swift */, + D8F0372B29D232730027DE2E /* HashtagIntentHandler.swift */, ); path = Handler; sourceTree = ""; @@ -3979,6 +3982,7 @@ 2AE202AC297FE19100F66E55 /* FollowersCountIntentHandler.swift in Sources */, DB64BA452851F23000ADF1B7 /* MastodonAuthentication+Fetch.swift in Sources */, DB64BA482851F29300ADF1B7 /* Account+Fetch.swift in Sources */, + D8F0372C29D232730027DE2E /* HashtagIntentHandler.swift in Sources */, 2A86A14629892944007F1062 /* MultiFollowersCountIntentHandler.swift in Sources */, DB8FABCA26AEC7B2008E5AF4 /* IntentHandler.swift in Sources */, ); diff --git a/MastodonIntent/Handler/HashtagIntentHandler.swift b/MastodonIntent/Handler/HashtagIntentHandler.swift new file mode 100644 index 000000000..eaaf6c349 --- /dev/null +++ b/MastodonIntent/Handler/HashtagIntentHandler.swift @@ -0,0 +1,34 @@ +// Copyright © 2023 Mastodon gGmbH. All rights reserved. + +import Foundation +import Intents + +class HashtagIntentHandler: INExtension, HashtagIntentHandling { + func provideHashtagOptionsCollection(for intent: HashtagIntent, searchTerm: String?) async throws -> INObjectCollection { + + guard let authenticationBox = WidgetExtension.appContext + .authenticationService + .mastodonAuthenticationBoxes + .first + else { + return INObjectCollection(items: []) + } + + var results: [NSString] = [] + + if let searchTerm, searchTerm.isEmpty == false { + let searchResults = try await WidgetExtension.appContext + .apiService + .search(query: .init(q: searchTerm, type: .hashtags), authenticationBox: authenticationBox) + .value + .hashtags.compactMap { $0.name as NSString } + results = searchResults + + } else { + //TODO: Show hashtags I follow + } + + return INObjectCollection(items: results) + + } +} diff --git a/MastodonIntent/IntentHandler.swift b/MastodonIntent/IntentHandler.swift index c8fb67d4c..692af7ee3 100644 --- a/MastodonIntent/IntentHandler.swift +++ b/MastodonIntent/IntentHandler.swift @@ -19,6 +19,8 @@ class IntentHandler: INExtension { return FollowersCountIntentHandler() case is MultiFollowersCountIntent: return MultiFollowersCountIntentHandler() + case is HashtagIntent: + return HashtagIntentHandler() default: return self } diff --git a/WidgetExtension/Base.lproj/WidgetExtension.intentdefinition b/WidgetExtension/Base.lproj/WidgetExtension.intentdefinition index aca5db658..667efed07 100644 --- a/WidgetExtension/Base.lproj/WidgetExtension.intentdefinition +++ b/WidgetExtension/Base.lproj/WidgetExtension.intentdefinition @@ -430,29 +430,11 @@ INIntentParameterPromptDialogType Primary - - INIntentParameterPromptDialogCustom - - INIntentParameterPromptDialogFormatString - There are ${count} options matching ‘${hashtag}’. - INIntentParameterPromptDialogFormatStringID - oQ0WOE - INIntentParameterPromptDialogType - DisambiguationIntroduction - - - INIntentParameterPromptDialogCustom - - INIntentParameterPromptDialogFormatString - Just to confirm, you wanted ‘${hashtag}’? - INIntentParameterPromptDialogFormatStringID - REztzm - INIntentParameterPromptDialogType - Confirmation - INIntentParameterSupportsDynamicEnumeration + INIntentParameterSupportsSearch + INIntentParameterTag 5 INIntentParameterType