fluffychat/lib/pages/settings_emotes/settings_emotes_view.dart

259 lines
10 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
2021-10-26 18:50:34 +02:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
2021-10-26 18:50:34 +02:00
import 'package:matrix/matrix.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/widgets/layouts/max_width_body.dart';
import 'package:fluffychat/widgets/mxc_image.dart';
2021-05-22 08:53:52 +02:00
import '../../widgets/matrix.dart';
2021-11-09 21:32:16 +01:00
import 'settings_emotes.dart';
2020-05-12 09:02:33 +02:00
2021-05-22 09:13:47 +02:00
class EmotesSettingsView extends StatelessWidget {
2021-04-24 07:40:13 +02:00
final EmotesSettingsController controller;
2020-10-03 12:31:29 +02:00
2022-01-29 12:35:03 +01:00
const EmotesSettingsView(this.controller, {Key? key}) : super(key: key);
2020-05-12 09:02:33 +02:00
@override
Widget build(BuildContext context) {
2021-04-14 10:37:15 +02:00
final client = Matrix.of(context).client;
2022-01-29 12:35:03 +01:00
final imageKeys = controller.pack!.images.keys.toList();
2020-05-12 09:02:33 +02:00
return Scaffold(
appBar: AppBar(
2021-10-14 18:09:30 +02:00
leading: const BackButton(),
2022-01-29 12:35:03 +01:00
title: Text(L10n.of(context)!.emoteSettings),
2020-05-12 09:02:33 +02:00
),
2021-04-24 07:40:13 +02:00
floatingActionButton: controller.showSave
2020-05-12 09:02:33 +02:00
? FloatingActionButton(
2021-04-24 07:40:13 +02:00
onPressed: controller.saveAction,
2021-11-14 13:24:01 +01:00
child: const Icon(Icons.save_outlined, color: Colors.white),
2020-05-12 09:02:33 +02:00
)
: null,
2021-04-09 18:26:44 +02:00
body: MaxWidthBody(
child: Column(
children: <Widget>[
if (!controller.readonly)
Container(
2021-10-14 18:09:30 +02:00
padding: const EdgeInsets.symmetric(
vertical: 8.0,
),
child: ListTile(
leading: Container(
width: 180.0,
height: 38,
2021-10-14 18:09:30 +02:00
padding: const EdgeInsets.symmetric(horizontal: 8),
decoration: BoxDecoration(
2021-10-14 18:09:30 +02:00
borderRadius: const BorderRadius.all(Radius.circular(10)),
color: Theme.of(context).secondaryHeaderColor,
),
child: TextField(
controller: controller.newImageCodeController,
autocorrect: false,
minLines: 1,
maxLines: 1,
decoration: InputDecoration(
2022-01-29 12:35:03 +01:00
hintText: L10n.of(context)!.emoteShortcode,
prefixText: ': ',
suffixText: ':',
prefixStyle: TextStyle(
color: Theme.of(context).colorScheme.secondary,
fontWeight: FontWeight.bold,
2021-04-24 07:40:13 +02:00
),
suffixStyle: TextStyle(
color: Theme.of(context).colorScheme.secondary,
fontWeight: FontWeight.bold,
2020-05-12 09:02:33 +02:00
),
border: InputBorder.none,
2020-05-12 09:02:33 +02:00
),
2021-04-09 18:26:44 +02:00
),
),
title: _ImagePicker(
controller: controller.newImageController,
onPressed: controller.imagePickerAction,
),
trailing: InkWell(
onTap: controller.addImageAction,
2021-10-14 18:09:30 +02:00
child: const Icon(
2021-11-14 13:24:01 +01:00
Icons.add_outlined,
color: Colors.green,
size: 32.0,
2020-10-03 12:31:29 +02:00
),
),
),
),
if (controller.room != null)
2021-11-27 10:10:29 +01:00
SwitchListTile.adaptive(
2022-01-29 12:35:03 +01:00
title: Text(L10n.of(context)!.enableEmotesGlobally),
2021-11-27 10:10:29 +01:00
value: controller.isGloballyActive(client),
onChanged: controller.setIsGloballyActive,
),
if (!controller.readonly || controller.room != null)
Divider(
height: 2,
thickness: 2,
color: Theme.of(context).primaryColor,
),
Expanded(
child: imageKeys.isEmpty
? Center(
child: Padding(
2021-10-14 18:09:30 +02:00
padding: const EdgeInsets.all(16),
child: Text(
2022-01-29 12:35:03 +01:00
L10n.of(context)!.noEmotesFound,
2021-10-14 18:09:30 +02:00
style: const TextStyle(fontSize: 20),
),
),
)
: ListView.separated(
separatorBuilder: (BuildContext context, int i) =>
const SizedBox.shrink(),
itemCount: imageKeys.length + 1,
itemBuilder: (BuildContext context, int i) {
if (i >= imageKeys.length) {
return Container(height: 70);
}
final imageCode = imageKeys[i];
2022-01-29 12:35:03 +01:00
final image = controller.pack!.images[imageCode]!;
final textEditingController = TextEditingController();
textEditingController.text = imageCode;
final useShortCuts =
(PlatformInfos.isWeb || PlatformInfos.isDesktop);
return ListTile(
leading: Container(
width: 180.0,
height: 38,
2021-10-14 18:09:30 +02:00
padding: const EdgeInsets.symmetric(horizontal: 8),
decoration: BoxDecoration(
borderRadius:
2021-10-14 18:09:30 +02:00
const BorderRadius.all(Radius.circular(10)),
color: Theme.of(context).secondaryHeaderColor,
2020-05-12 09:02:33 +02:00
),
child: Shortcuts(
shortcuts: !useShortCuts
? {}
: {
LogicalKeySet(LogicalKeyboardKey.enter):
SubmitLineIntent(),
},
child: Actions(
actions: !useShortCuts
? {}
: {
SubmitLineIntent: CallbackAction(
onInvoke: (i) {
controller.submitImageAction(
imageCode,
textEditingController.text,
image,
textEditingController,
);
return null;
},
),
},
child: TextField(
readOnly: controller.readonly,
controller: textEditingController,
autocorrect: false,
minLines: 1,
maxLines: 1,
decoration: InputDecoration(
2022-01-29 12:35:03 +01:00
hintText: L10n.of(context)!.emoteShortcode,
prefixText: ': ',
suffixText: ':',
prefixStyle: TextStyle(
color: Theme.of(context)
.colorScheme
.secondary,
fontWeight: FontWeight.bold,
),
suffixStyle: TextStyle(
color: Theme.of(context)
.colorScheme
.secondary,
fontWeight: FontWeight.bold,
2021-04-24 07:40:13 +02:00
),
border: InputBorder.none,
),
onSubmitted: (s) =>
controller.submitImageAction(
imageCode,
s,
image,
textEditingController,
2020-05-12 09:02:33 +02:00
),
),
),
),
2021-04-09 18:26:44 +02:00
),
title: _EmoteImage(image.url),
trailing: controller.readonly
? null
: InkWell(
onTap: () =>
controller.removeImageAction(imageCode),
2021-10-14 18:09:30 +02:00
child: const Icon(
2021-11-14 13:24:01 +01:00
Icons.delete_outlined,
color: Colors.red,
size: 32.0,
),
),
);
},
),
),
],
),
2021-04-09 18:26:44 +02:00
),
2020-05-12 09:02:33 +02:00
);
}
}
class _EmoteImage extends StatelessWidget {
final Uri mxc;
2021-10-14 18:09:30 +02:00
const _EmoteImage(this.mxc);
2020-05-12 09:02:33 +02:00
@override
Widget build(BuildContext context) {
2021-10-14 18:09:30 +02:00
const size = 38.0;
return SizedBox.square(
dimension: size,
child: MxcImage(
uri: mxc,
fit: BoxFit.contain,
width: size,
height: size,
),
);
2020-05-12 09:02:33 +02:00
}
}
class _ImagePicker extends StatefulWidget {
2022-01-29 12:35:03 +01:00
final ValueNotifier<ImagePackImageContent?> controller;
2020-05-12 09:02:33 +02:00
2022-01-29 12:35:03 +01:00
final void Function(ValueNotifier<ImagePackImageContent?>) onPressed;
2021-04-24 07:40:13 +02:00
2022-01-29 12:35:03 +01:00
const _ImagePicker({required this.controller, required this.onPressed});
2020-05-12 09:02:33 +02:00
@override
_ImagePickerState createState() => _ImagePickerState();
2020-05-12 09:02:33 +02:00
}
class _ImagePickerState extends State<_ImagePicker> {
2020-05-12 09:02:33 +02:00
@override
Widget build(BuildContext context) {
if (widget.controller.value == null) {
2021-03-04 12:28:06 +01:00
return ElevatedButton(
onPressed: () => widget.onPressed(widget.controller),
2022-01-29 12:35:03 +01:00
child: Text(L10n.of(context)!.pickImage),
2020-05-12 09:02:33 +02:00
);
} else {
2022-01-29 12:35:03 +01:00
return _EmoteImage(widget.controller.value!.url);
2020-05-12 09:02:33 +02:00
}
}
}
class SubmitLineIntent extends Intent {}