Add searching to CreatePostStore

This commit is contained in:
shilangyu 2022-01-16 14:16:24 +01:00
parent 8c54e38e99
commit c320585810
5 changed files with 123 additions and 5 deletions

View File

@ -347,5 +347,9 @@
"instance_added": "Instance successfully added", "instance_added": "Instance successfully added",
"@instance_added": {}, "@instance_added": {},
"required_field": "required field", "required_field": "required field",
"@required_field": {} "@required_field": {},
"no_communities_found": "No communities found",
"@no_communities_found": {},
"network_error": "Network error",
"@network_error": {}
} }

View File

@ -123,6 +123,7 @@ abstract class L10nStrings {
static const number_of_posts = 'number_of_posts'; static const number_of_posts = 'number_of_posts';
static const number_of_subscribers = 'number_of_subscribers'; static const number_of_subscribers = 'number_of_subscribers';
static const number_of_users = 'number_of_users'; static const number_of_users = 'number_of_users';
static const number_of_communities = 'number_of_communities';
static const unsubscribe = 'unsubscribe'; static const unsubscribe = 'unsubscribe';
static const subscribe = 'subscribe'; static const subscribe = 'subscribe';
static const messages = 'messages'; static const messages = 'messages';
@ -145,6 +146,22 @@ abstract class L10nStrings {
static const bot_account = 'bot_account'; static const bot_account = 'bot_account';
static const show_bot_accounts = 'show_bot_accounts'; static const show_bot_accounts = 'show_bot_accounts';
static const show_read_posts = 'show_read_posts'; 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 { extension L10nFromString on String {
@ -406,6 +423,36 @@ extension L10nFromString on String {
return L10n.of(context).show_bot_accounts; return L10n.of(context).show_bot_accounts;
case L10nStrings.show_read_posts: case L10nStrings.show_read_posts:
return L10n.of(context).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: default:
return this; return this;

View File

@ -36,6 +36,36 @@ abstract class _CreatePostStore with Store {
bool nsfw; bool nsfw;
final submitState = AsyncStore<PostView>(); final submitState = AsyncStore<PostView>();
final searchCommunitiesState = AsyncStore<List<CommunityView>>();
@action
Future<List<CommunityView>?> 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 @action
Future<void> submit(Jwt token) async { Future<void> submit(Jwt token) async {
@ -61,3 +91,37 @@ abstract class _CreatePostStore with Store {
); );
} }
} }
class SearchCommunities implements LemmyApiQuery<List<CommunityView>> {
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<CommunityView> responseFactory(Map<String, dynamic> json) =>
base.responseFactory(json).communities;
@override
Map<String, dynamic> toJson() => base.toJson();
}

View File

@ -5,6 +5,8 @@ import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:lemmy_api_client/v3.dart'; import 'package:lemmy_api_client/v3.dart';
import 'package:mobx/mobx.dart'; import 'package:mobx/mobx.dart';
import '../l10n/l10n_from_string.dart';
part 'async_store.freezed.dart'; part 'async_store.freezed.dart';
part 'async_store.g.dart'; part 'async_store.g.dart';
@ -51,11 +53,10 @@ abstract class _AsyncStore<T> with Store {
return result; return result;
} on SocketException { } on SocketException {
// TODO: use an existing l10n key
if (data != null) { if (data != null) {
asyncState = data.copyWith(errorTerm: 'network_error'); asyncState = data.copyWith(errorTerm: L10nStrings.network_error);
} else { } else {
asyncState = const AsyncState.error('network_error'); asyncState = const AsyncState.error(L10nStrings.network_error);
} }
} catch (err) { } catch (err) {
if (data != null) { if (data != null) {

View File

@ -12,6 +12,7 @@ class Editor extends HookWidget {
final int? minLines; final int? minLines;
final int? maxLines; final int? maxLines;
final String? labelText; final String? labelText;
final String? initialValue;
final bool autofocus; final bool autofocus;
/// Whether the editor should be preview the contents /// Whether the editor should be preview the contents
@ -27,6 +28,7 @@ class Editor extends HookWidget {
this.minLines = 5, this.minLines = 5,
this.maxLines, this.maxLines,
this.labelText, this.labelText,
this.initialValue,
this.fancy = false, this.fancy = false,
required this.instanceHost, required this.instanceHost,
this.autofocus = false, this.autofocus = false,
@ -34,7 +36,7 @@ class Editor extends HookWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final defaultController = useTextEditingController(); final defaultController = useTextEditingController(text: initialValue);
final actualController = controller ?? defaultController; final actualController = controller ?? defaultController;
if (fancy) { if (fancy) {