Skip to content

Commit 326d71d

Browse files
committed
feat: add new status api action, closes #11
1 parent ab3dae1 commit 326d71d

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

lib/screens/timeline_tabs.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ class _TimelineTabsState extends State<TimelineTabs> {
107107
onSuccessfulSubmit: () {
108108
// Hide the dialog box
109109
Navigator.of(context).pop();
110+
111+
// Show a success message
112+
showSnackBar(context, "Status posted successfully!");
110113
},
111114
),
112115
);

lib/services/api.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,28 @@ class ApiService {
257257
);
258258
}
259259

260+
/// Given a status content, requests the Mastodon API to post a new status
261+
/// on the user's timeline. Returns the (new) [Status] instance the API
262+
/// responds with.
263+
Future<Status> postStatus(String content) async {
264+
final apiUrl = "${instanceUrl!}/api/v1/statuses";
265+
// TODO: Support sensitivity, visibility, language, scheduling, polls and media
266+
http.Response resp = await helper!.post(
267+
apiUrl,
268+
body: {"status": content},
269+
httpClient: httpClient,
270+
);
271+
272+
if (resp.statusCode == 200) {
273+
Map<String, dynamic> jsonData = jsonDecode(resp.body);
274+
return Status.fromJson(jsonData);
275+
}
276+
277+
throw ApiException(
278+
"Unexpected status code ${resp.statusCode} on `postStatus`",
279+
);
280+
}
281+
260282
/// Given a [Status]'s ID, requests the Mastodon API to bookmark
261283
/// the status. Note that this is idempotent: an already-bookmarked
262284
/// status will remain bookmarked. Returns the (new) [Status] instance

lib/widgets/status_form.dart

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:feathr/services/api.dart';
2+
import 'package:feathr/utils/messages.dart';
23
import 'package:flutter/material.dart';
34

45
import 'package:feathr/widgets/buttons.dart';
@@ -53,9 +54,24 @@ class StatusFormState extends State<StatusForm> {
5354
: null,
5455
),
5556
FeathrActionButton(
56-
onPressed: () {
57+
onPressed: () async {
5758
if (_formKey.currentState!.validate()) {
58-
// TODO: Implement the API call to submit the status
59+
// Hide the button and show a spinner while the status is being
60+
// submitted
61+
62+
// Post the status to the server
63+
try {
64+
await widget.apiService.postStatus(statusController.text);
65+
} catch (e) {
66+
// Show an error message if the status couldn't be posted
67+
if (context.mounted) {
68+
showSnackBar(context, 'Failed to post status: $e');
69+
}
70+
71+
return;
72+
}
73+
74+
// Post was successful, call the success function!
5975
widget.onSuccessfulSubmit();
6076
}
6177
},

0 commit comments

Comments
 (0)