relatica/lib/screens/editor.dart

195 wiersze
6.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_widget_from_html_core/flutter_widget_from_html_core.dart';
import 'package:go_router/go_router.dart';
import 'package:logging/logging.dart';
import 'package:provider/provider.dart';
import '../controls/padding.dart';
import '../controls/timeline/status_header_control.dart';
import '../models/timeline_entry.dart';
import '../services/timeline_manager.dart';
import '../utils/snackbar_builder.dart';
class EditorScreen extends StatefulWidget {
final String id;
final String parentId;
const EditorScreen({super.key, this.id = '', this.parentId = ''});
@override
State<EditorScreen> createState() => _EditorScreenState();
}
class _EditorScreenState extends State<EditorScreen> {
static final _logger = Logger('$EditorScreen');
final contentController = TextEditingController();
final spoilerController = TextEditingController();
TimelineEntry? parentEntry;
var isSubmitting = false;
bool get isComment => widget.parentId.isNotEmpty;
String get statusType => widget.parentId.isEmpty ? 'Post' : 'Comment';
@override
void initState() {
if (!isComment) {
return;
}
final manager = context.read<TimelineManager>();
manager.getEntryById(widget.parentId).match(onSuccess: (entry) {
spoilerController.text = entry.spoilerText;
parentEntry = entry;
}, onError: (error) {
_logger.finest('Error trying to get parent entry: $error');
});
}
Future<void> createStatus(
BuildContext context, TimelineManager manager) async {
if (contentController.text.isEmpty) {
buildSnackbar(context, "Can't submit an empty post/comment");
return;
}
setState(() {
isSubmitting = false;
});
final result = await manager.createNewStatus(
contentController.text,
spoilerText: spoilerController.text,
inReplyToId: widget.parentId,
);
setState(() {
isSubmitting = false;
});
if (result.isFailure) {
buildSnackbar(context, 'Error posting: ${result.error}');
return;
}
context.pop();
}
@override
Widget build(BuildContext context) {
print('Build editor $isComment $parentEntry');
final manager = context.read<TimelineManager>();
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
title: Text(widget.id.isEmpty ? 'New $statusType' : 'Edit $statusType'),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Center(
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
if (isComment && parentEntry != null)
buildCommentPreview(context, parentEntry!),
TextFormField(
readOnly: isSubmitting,
enabled: !isSubmitting,
controller: spoilerController,
decoration: InputDecoration(
labelText: '$statusType Spoiler Text (optional)',
border: OutlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).backgroundColor,
),
borderRadius: BorderRadius.circular(5.0),
),
),
),
const VerticalPadding(),
TextFormField(
readOnly: isSubmitting,
enabled: !isSubmitting,
maxLines: 10,
controller: contentController,
decoration: InputDecoration(
labelText: '$statusType Content',
alignLabelWithHint: true,
border: OutlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).backgroundColor,
),
borderRadius: BorderRadius.circular(5.0),
),
),
),
const VerticalPadding(),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: isSubmitting
? null
: () async => createStatus(context, manager),
child: const Text('Submit'),
),
const HorizontalPadding(),
ElevatedButton(
onPressed: isSubmitting
? null
: () {
context.pop();
},
child: const Text('Cancel'),
),
],
),
],
),
),
),
),
);
}
Widget buildCommentPreview(BuildContext context, TimelineEntry entry) {
print('Build preview');
return Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Comment for status: ',
style: Theme.of(context).textTheme.bodyLarge,
),
Card(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
StatusHeaderControl(
entry: entry,
openRemote: false,
showOpenControl: false,
),
const VerticalPadding(height: 3),
if (entry.spoilerText.isNotEmpty) ...[
Text(
'Content Summary: ${entry.spoilerText}',
style: Theme.of(context).textTheme.bodyLarge,
),
const VerticalPadding(height: 3)
],
HtmlWidget(entry.body),
],
),
),
),
const VerticalPadding(),
],
);
}
}