kopia lustrzana https://gitlab.com/mysocialportal/relatica
Add basic post editing with content summary
rodzic
ad14eb25ae
commit
4d7cf7fd98
|
@ -140,10 +140,14 @@ class FriendicaClient {
|
|||
));
|
||||
}
|
||||
|
||||
FutureResult<TimelineEntry, ExecError> createNewPost(String text) async {
|
||||
FutureResult<TimelineEntry, ExecError> createNewPost(
|
||||
{required String text, String spoilerText = ''}) async {
|
||||
_logger.finest(() => 'Creating post');
|
||||
final url = Uri.parse('https://$serverName/api/v1/statuses');
|
||||
final body = {'status': text, 'spoiler_text': 'For Testing Only'};
|
||||
final body = {
|
||||
'status': text,
|
||||
if (spoilerText.isNotEmpty) 'spoiler_text': spoilerText,
|
||||
};
|
||||
print(body);
|
||||
final result = await _postUrl(url, body);
|
||||
if (result.isFailure) {
|
||||
|
|
|
@ -2,8 +2,8 @@ import 'package:flutter/material.dart';
|
|||
import 'package:go_router/go_router.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../routes.dart';
|
||||
import '../services/auth_service.dart';
|
||||
import '../controls/padding.dart';
|
||||
import '../services/timeline_manager.dart';
|
||||
import '../utils/snackbar_builder.dart';
|
||||
|
||||
class EditorScreen extends StatefulWidget {
|
||||
|
@ -16,53 +16,86 @@ class EditorScreen extends StatefulWidget {
|
|||
}
|
||||
|
||||
class _EditorScreenState extends State<EditorScreen> {
|
||||
final controller = TextEditingController();
|
||||
final contentController = TextEditingController();
|
||||
final spoilerController = TextEditingController();
|
||||
|
||||
String get statusType => 'Post';
|
||||
|
||||
Future<void> createPost(BuildContext context, TimelineManager manager) async {
|
||||
if (contentController.text.isEmpty) {
|
||||
buildSnackbar(context, "Can't submit an empty post/comment");
|
||||
return;
|
||||
}
|
||||
final result = await manager.createNewPost(
|
||||
contentController.text,
|
||||
spoilerText: spoilerController.text,
|
||||
);
|
||||
|
||||
if (result.isFailure) {
|
||||
buildSnackbar(context, 'Error posting: ${result.error}');
|
||||
return;
|
||||
}
|
||||
context.pop();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final clientResult = context.read<AuthService>().currentClient;
|
||||
if (clientResult.isFailure) {
|
||||
context.goNamed(ScreenPaths.home);
|
||||
}
|
||||
final client = clientResult.value;
|
||||
final manager = context.read<TimelineManager>();
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
automaticallyImplyLeading: false,
|
||||
title: Text(widget.id.isEmpty ? 'New Post' : 'Edit Post'),
|
||||
title: Text(widget.id.isEmpty ? 'New $statusType' : 'Edit $statusType'),
|
||||
),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Center(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
children: [
|
||||
TextFormField(
|
||||
controller: spoilerController,
|
||||
decoration: InputDecoration(
|
||||
hintText: '$statusType spoiler text (optional)',
|
||||
border: OutlineInputBorder(
|
||||
borderSide: BorderSide(
|
||||
color: Theme.of(context).backgroundColor,
|
||||
),
|
||||
borderRadius: BorderRadius.circular(5.0),
|
||||
),
|
||||
),
|
||||
),
|
||||
const VerticalPadding(),
|
||||
TextFormField(
|
||||
maxLines: 10,
|
||||
controller: controller,
|
||||
controller: contentController,
|
||||
decoration: InputDecoration(
|
||||
hintText: '$statusType content',
|
||||
border: OutlineInputBorder(
|
||||
borderSide: BorderSide(
|
||||
color: Theme.of(context).backgroundColor,
|
||||
),
|
||||
borderRadius: BorderRadius.circular(5.0),
|
||||
),
|
||||
),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
if (controller.text.isEmpty) {
|
||||
buildSnackbar(
|
||||
context, "Can't submit an empty post/comment");
|
||||
return;
|
||||
}
|
||||
final result = await client.createNewPost(controller.text);
|
||||
|
||||
if (result.isFailure) {
|
||||
buildSnackbar(context, 'Error posting: ${result.error}');
|
||||
return;
|
||||
}
|
||||
context.pop();
|
||||
},
|
||||
child: Text('Submit'),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
context.pop();
|
||||
},
|
||||
child: Text('Cancel'),
|
||||
const VerticalPadding(),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
ElevatedButton(
|
||||
onPressed: () async => createPost(context, manager),
|
||||
child: const Text('Submit'),
|
||||
),
|
||||
const HorizontalPadding(),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
context.pop();
|
||||
},
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
|
|
|
@ -21,6 +21,41 @@ class EntryManagerService extends ChangeNotifier {
|
|||
_parentPostIds.clear();
|
||||
}
|
||||
|
||||
FutureResult<EntryTreeItem, ExecError> createNewPost(String text,
|
||||
{String spoilerText = ''}) async {
|
||||
_logger.finest('Creating new post: $text');
|
||||
final auth = getIt<AuthService>();
|
||||
final clientResult = auth.currentClient;
|
||||
if (clientResult.isFailure) {
|
||||
_logger.severe('Error getting Friendica client: ${clientResult.error}');
|
||||
return clientResult.errorCast();
|
||||
}
|
||||
|
||||
final client = clientResult.value;
|
||||
final result = await client
|
||||
.createNewPost(
|
||||
text: text,
|
||||
spoilerText: spoilerText,
|
||||
)
|
||||
.andThenSuccessAsync((item) async {
|
||||
await processNewItems([item], client.credentials.username, null);
|
||||
return item;
|
||||
});
|
||||
|
||||
return result.mapValue((post) {
|
||||
_logger.finest('${post.id} post updated after reshare');
|
||||
return _nodeToTreeItem(_postNodes[post.id]!);
|
||||
}).mapError(
|
||||
(error) {
|
||||
_logger.finest('Error creating post: $error');
|
||||
return ExecError(
|
||||
type: ErrorType.localError,
|
||||
message: error.toString(),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
FutureResult<List<EntryTreeItem>, ExecError> updateTimeline(
|
||||
TimelineIdentifiers type, int maxId, int sinceId) async {
|
||||
_logger.fine(() => 'Updating timeline');
|
||||
|
|
|
@ -26,6 +26,18 @@ class TimelineManager extends ChangeNotifier {
|
|||
notifyListeners();
|
||||
}
|
||||
|
||||
FutureResult<EntryTreeItem, ExecError> createNewPost(String text,
|
||||
{String spoilerText = ''}) async {
|
||||
final result = await getIt<EntryManagerService>().createNewPost(
|
||||
text,
|
||||
spoilerText: spoilerText,
|
||||
);
|
||||
if (result.isSuccess) {
|
||||
notifyListeners();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// refresh timeline gets statuses newer than the newest in that timeline
|
||||
Result<List<EntryTreeItem>, ExecError> getTimeline(TimelineIdentifiers type) {
|
||||
_logger.finest('Getting timeline $type');
|
||||
|
|
Ładowanie…
Reference in New Issue