Skip to content

Commit

Permalink
feat: add new status api action, closes #11
Browse files Browse the repository at this point in the history
  • Loading branch information
aitorres committed Jan 19, 2025
1 parent ab3dae1 commit 326d71d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/screens/timeline_tabs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ class _TimelineTabsState extends State<TimelineTabs> {
onSuccessfulSubmit: () {
// Hide the dialog box
Navigator.of(context).pop();

// Show a success message
showSnackBar(context, "Status posted successfully!");
},
),
);
Expand Down
22 changes: 22 additions & 0 deletions lib/services/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,28 @@ class ApiService {
);
}

/// Given a status content, requests the Mastodon API to post a new status
/// on the user's timeline. Returns the (new) [Status] instance the API
/// responds with.
Future<Status> postStatus(String content) async {
final apiUrl = "${instanceUrl!}/api/v1/statuses";
// TODO: Support sensitivity, visibility, language, scheduling, polls and media
http.Response resp = await helper!.post(
apiUrl,
body: {"status": content},
httpClient: httpClient,
);

if (resp.statusCode == 200) {
Map<String, dynamic> jsonData = jsonDecode(resp.body);
return Status.fromJson(jsonData);
}

throw ApiException(
"Unexpected status code ${resp.statusCode} on `postStatus`",
);
}

/// Given a [Status]'s ID, requests the Mastodon API to bookmark
/// the status. Note that this is idempotent: an already-bookmarked
/// status will remain bookmarked. Returns the (new) [Status] instance
Expand Down
20 changes: 18 additions & 2 deletions lib/widgets/status_form.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:feathr/services/api.dart';
import 'package:feathr/utils/messages.dart';
import 'package:flutter/material.dart';

import 'package:feathr/widgets/buttons.dart';
Expand Down Expand Up @@ -53,9 +54,24 @@ class StatusFormState extends State<StatusForm> {
: null,
),
FeathrActionButton(
onPressed: () {
onPressed: () async {
if (_formKey.currentState!.validate()) {
// TODO: Implement the API call to submit the status
// Hide the button and show a spinner while the status is being
// submitted

// Post the status to the server
try {
await widget.apiService.postStatus(statusController.text);
} catch (e) {
// Show an error message if the status couldn't be posted
if (context.mounted) {
showSnackBar(context, 'Failed to post status: $e');
}

return;
}

// Post was successful, call the success function!
widget.onSuccessfulSubmit();
}
},
Expand Down

0 comments on commit 326d71d

Please sign in to comment.