lemmur-app-android/lib/pages/create_post/create_post_store.dart

172 lines
4.0 KiB
Dart
Raw Normal View History

2022-01-16 16:37:25 +01:00
import 'package:lemmy_api_client/pictrs.dart';
2022-01-15 16:15:55 +01:00
import 'package:lemmy_api_client/v3.dart';
import 'package:mobx/mobx.dart';
import '../../util/async_store.dart';
2022-01-16 16:37:25 +01:00
import '../../util/pictrs.dart';
2022-01-15 16:15:55 +01:00
part 'create_post_store.g.dart';
class CreatePostStore = _CreatePostStore with _$CreatePostStore;
abstract class _CreatePostStore with Store {
final Post? postToEdit;
bool get isEdit => postToEdit != null;
_CreatePostStore({
required this.instanceHost,
this.postToEdit,
2022-05-13 15:58:01 +02:00
// ignore: unused_element
2022-01-15 16:15:55 +01:00
this.selectedCommunity,
}) : title = postToEdit?.name ?? '',
nsfw = postToEdit?.nsfw ?? false,
body = postToEdit?.body ?? '',
url = postToEdit?.url ?? '';
@observable
bool showFancy = false;
@observable
String instanceHost;
@observable
CommunityView? selectedCommunity;
@observable
String url;
@observable
String title;
@observable
String body;
@observable
bool nsfw;
final submitState = AsyncStore<PostView>();
2022-01-16 14:16:24 +01:00
final searchCommunitiesState = AsyncStore<List<CommunityView>>();
2022-01-16 16:37:25 +01:00
final imageUploadState = AsyncStore<PictrsUploadFile>();
@computed
bool get hasUploadedImage => imageUploadState.map(
loading: () => false,
error: (_) => false,
data: (_) => true,
);
2022-01-16 14:16:24 +01:00
@action
Future<List<CommunityView>?> searchCommunities(
String searchTerm,
Jwt? token,
) {
if (searchTerm.isEmpty) {
return searchCommunitiesState.runLemmy(
instanceHost,
ListCommunities(
type: PostListingType.all,
sort: SortType.topAll,
2022-05-03 09:44:07 +02:00
limit: 10,
2022-01-16 14:16:24 +01:00
auth: token?.raw,
),
);
} else {
return searchCommunitiesState.runLemmy(
instanceHost,
SearchCommunities(
q: searchTerm,
sort: SortType.topAll,
listingType: PostListingType.all,
2022-05-03 09:44:07 +02:00
limit: 10,
2022-01-16 14:16:24 +01:00
auth: token?.raw,
),
);
}
}
2022-01-15 16:15:55 +01:00
@action
Future<void> submit(Jwt token) async {
await submitState.runLemmy(
instanceHost,
isEdit
? EditPost(
url: url.isEmpty ? null : url,
body: body.isEmpty ? null : body,
nsfw: nsfw,
name: title,
postId: postToEdit!.id,
auth: token.raw,
)
: CreatePost(
url: url.isEmpty ? null : url,
body: body.isEmpty ? null : body,
nsfw: nsfw,
name: title,
communityId: selectedCommunity!.community.id,
auth: token.raw,
),
);
}
2022-01-16 16:37:25 +01:00
@action
Future<void> uploadImage(String filePath, Jwt token) async {
final instanceHost = this.instanceHost;
final upload = await imageUploadState.run(
() => PictrsApi(instanceHost)
.upload(
filePath: filePath,
auth: token.raw,
)
.then((value) => value.files.single),
);
if (upload != null) {
url = pathToPictrs(instanceHost, upload.file);
}
}
@action
void removeImage() {
final pictrsFile = imageUploadState.map<PictrsUploadFile?>(
data: (data) => data,
loading: () => null,
error: (_) => null,
);
if (pictrsFile == null) return;
PictrsApi(instanceHost).delete(pictrsFile).catchError((_) {});
imageUploadState.reset();
url = '';
}
2022-01-15 16:15:55 +01:00
}
2022-01-16 14:16:24 +01:00
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();
}