From 6a814ab12843565c0f13d97be4ec351136a044d4 Mon Sep 17 00:00:00 2001 From: shilangyu Date: Sun, 16 Jan 2022 14:16:24 +0100 Subject: [PATCH] Add searching to CreatePostStore --- assets/l10n/intl_en.arb | 6 +- lib/l10n/l10n_from_string.dart | 47 ++++++++++++++ lib/pages/create_post/create_post_store.dart | 64 ++++++++++++++++++++ lib/util/async_store.dart | 7 ++- lib/widgets/editor.dart | 4 +- 5 files changed, 123 insertions(+), 5 deletions(-) diff --git a/assets/l10n/intl_en.arb b/assets/l10n/intl_en.arb index 6f51827..989d925 100644 --- a/assets/l10n/intl_en.arb +++ b/assets/l10n/intl_en.arb @@ -347,5 +347,9 @@ "instance_added": "Instance successfully added", "@instance_added": {}, "required_field": "required field", - "@required_field": {} + "@required_field": {}, + "no_communities_found": "No communities found", + "@no_communities_found": {}, + "network_error": "Network error", + "@network_error": {} } diff --git a/lib/l10n/l10n_from_string.dart b/lib/l10n/l10n_from_string.dart index d982ee0..13920ee 100644 --- a/lib/l10n/l10n_from_string.dart +++ b/lib/l10n/l10n_from_string.dart @@ -123,6 +123,7 @@ abstract class L10nStrings { static const number_of_posts = 'number_of_posts'; static const number_of_subscribers = 'number_of_subscribers'; static const number_of_users = 'number_of_users'; + static const number_of_communities = 'number_of_communities'; static const unsubscribe = 'unsubscribe'; static const subscribe = 'subscribe'; static const messages = 'messages'; @@ -145,6 +146,22 @@ abstract class L10nStrings { static const bot_account = 'bot_account'; static const show_bot_accounts = 'show_bot_accounts'; static const show_read_posts = 'show_read_posts'; + static const site_not_set_up = 'site_not_set_up'; + static const nerd_stuff = 'nerd_stuff'; + static const open_in_browser = 'open_in_browser'; + static const cannot_open_in_browser = 'cannot_open_in_browser'; + static const about = 'about'; + static const see_all = 'see_all'; + static const admins = 'admins'; + static const trending_communities = 'trending_communities'; + static const communities_of_instance = 'communities_of_instance'; + static const day = 'day'; + static const week = 'week'; + static const month = 'month'; + static const six_months = 'six_months'; + static const required_field = 'required_field'; + static const no_communities_found = 'no_communities_found'; + static const network_error = 'network_error'; } extension L10nFromString on String { @@ -406,6 +423,36 @@ extension L10nFromString on String { return L10n.of(context).show_bot_accounts; case L10nStrings.show_read_posts: return L10n.of(context).show_read_posts; + case L10nStrings.site_not_set_up: + return L10n.of(context).site_not_set_up; + case L10nStrings.nerd_stuff: + return L10n.of(context).nerd_stuff; + case L10nStrings.open_in_browser: + return L10n.of(context).open_in_browser; + case L10nStrings.cannot_open_in_browser: + return L10n.of(context).cannot_open_in_browser; + case L10nStrings.about: + return L10n.of(context).about; + case L10nStrings.see_all: + return L10n.of(context).see_all; + case L10nStrings.admins: + return L10n.of(context).admins; + case L10nStrings.trending_communities: + return L10n.of(context).trending_communities; + case L10nStrings.day: + return L10n.of(context).day; + case L10nStrings.week: + return L10n.of(context).week; + case L10nStrings.month: + return L10n.of(context).month; + case L10nStrings.six_months: + return L10n.of(context).six_months; + case L10nStrings.required_field: + return L10n.of(context).required_field; + case L10nStrings.no_communities_found: + return L10n.of(context).no_communities_found; + case L10nStrings.network_error: + return L10n.of(context).network_error; default: return this; diff --git a/lib/pages/create_post/create_post_store.dart b/lib/pages/create_post/create_post_store.dart index a386d18..93e7e50 100644 --- a/lib/pages/create_post/create_post_store.dart +++ b/lib/pages/create_post/create_post_store.dart @@ -36,6 +36,36 @@ abstract class _CreatePostStore with Store { bool nsfw; final submitState = AsyncStore(); + final searchCommunitiesState = AsyncStore>(); + + @action + Future?> searchCommunities( + String searchTerm, + Jwt? token, + ) { + if (searchTerm.isEmpty) { + return searchCommunitiesState.runLemmy( + instanceHost, + ListCommunities( + type: PostListingType.all, + sort: SortType.topAll, + limit: 20, + auth: token?.raw, + ), + ); + } else { + return searchCommunitiesState.runLemmy( + instanceHost, + SearchCommunities( + q: searchTerm, + sort: SortType.topAll, + listingType: PostListingType.all, + limit: 20, + auth: token?.raw, + ), + ); + } + } @action Future submit(Jwt token) async { @@ -61,3 +91,37 @@ abstract class _CreatePostStore with Store { ); } } + +class SearchCommunities implements LemmyApiQuery> { + final Search base; + + SearchCommunities({ + required String q, + PostListingType? listingType, + SortType? sort, + int? page, + int? limit, + String? auth, + }) : base = Search( + q: q, + type: SearchType.communities, + listingType: listingType, + sort: sort, + page: page, + limit: limit, + auth: auth, + ); + + @override + String get path => base.path; + + @override + HttpMethod get httpMethod => base.httpMethod; + + @override + List responseFactory(Map json) => + base.responseFactory(json).communities; + + @override + Map toJson() => base.toJson(); +} diff --git a/lib/util/async_store.dart b/lib/util/async_store.dart index 8c59d4d..01b2a54 100644 --- a/lib/util/async_store.dart +++ b/lib/util/async_store.dart @@ -5,6 +5,8 @@ import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:lemmy_api_client/v3.dart'; import 'package:mobx/mobx.dart'; +import '../l10n/l10n_from_string.dart'; + part 'async_store.freezed.dart'; part 'async_store.g.dart'; @@ -51,11 +53,10 @@ abstract class _AsyncStore with Store { return result; } on SocketException { - // TODO: use an existing l10n key if (data != null) { - asyncState = data.copyWith(errorTerm: 'network_error'); + asyncState = data.copyWith(errorTerm: L10nStrings.network_error); } else { - asyncState = const AsyncState.error('network_error'); + asyncState = const AsyncState.error(L10nStrings.network_error); } } catch (err) { if (data != null) { diff --git a/lib/widgets/editor.dart b/lib/widgets/editor.dart index 401161e..f4d8a1f 100644 --- a/lib/widgets/editor.dart +++ b/lib/widgets/editor.dart @@ -12,6 +12,7 @@ class Editor extends HookWidget { final int? minLines; final int? maxLines; final String? labelText; + final String? initialValue; final bool autofocus; /// Whether the editor should be preview the contents @@ -27,6 +28,7 @@ class Editor extends HookWidget { this.minLines = 5, this.maxLines, this.labelText, + this.initialValue, this.fancy = false, required this.instanceHost, this.autofocus = false, @@ -34,7 +36,7 @@ class Editor extends HookWidget { @override Widget build(BuildContext context) { - final defaultController = useTextEditingController(); + final defaultController = useTextEditingController(text: initialValue); final actualController = controller ?? defaultController; if (fancy) {