fluffychat/lib/pages/views/settings_emotes_view.dart

262 lines
10 KiB
Dart
Raw Normal View History

import 'package:fluffychat/utils/platform_infos.dart';
2020-09-07 17:08:01 +02:00
import 'package:cached_network_image/cached_network_image.dart';
import 'package:matrix/matrix.dart';
2021-05-22 08:53:52 +02:00
import 'package:fluffychat/widgets/layouts/max_width_body.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
2021-05-22 08:53:52 +02:00
import '../../widgets/matrix.dart';
2021-04-24 07:40:13 +02: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
2021-05-22 09:13:47 +02: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;
final imageKeys = controller.pack.images.keys.toList();
2020-05-12 09:02:33 +02:00
return Scaffold(
appBar: AppBar(
2021-01-16 14:24:52 +01:00
leading: BackButton(),
2020-05-12 09:02:33 +02:00
title: Text(L10n.of(context).emoteSettings),
),
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-03-04 12:28:06 +01:00
child: 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(
padding: EdgeInsets.symmetric(
vertical: 8.0,
),
child: ListTile(
leading: Container(
width: 180.0,
height: 38,
padding: EdgeInsets.symmetric(horizontal: 8),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(10)),
color: Theme.of(context).secondaryHeaderColor,
),
child: TextField(
controller: controller.newImageCodeController,
autocorrect: false,
minLines: 1,
maxLines: 1,
decoration: InputDecoration(
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,
child: Icon(
Icons.add_outlined,
color: Colors.green,
size: 32.0,
2020-10-03 12:31:29 +02:00
),
),
),
),
if (controller.room != null)
ListTile(
title: Text(L10n.of(context).enableEmotesGlobally),
trailing: Switch(
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(
padding: EdgeInsets.all(16),
child: Text(
L10n.of(context).noEmotesFound,
style: TextStyle(fontSize: 20),
),
),
)
: ListView.separated(
separatorBuilder: (BuildContext context, int i) =>
Container(),
itemCount: imageKeys.length + 1,
itemBuilder: (BuildContext context, int i) {
if (i >= imageKeys.length) {
return Container(height: 70);
}
final imageCode = imageKeys[i];
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,
padding: EdgeInsets.symmetric(horizontal: 8),
decoration: BoxDecoration(
borderRadius:
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(
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),
child: Icon(
Icons.delete_forever_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;
2020-05-12 09:02:33 +02:00
_EmoteImage(this.mxc);
@override
Widget build(BuildContext context) {
final size = 38.0;
final devicePixelRatio = MediaQuery.of(context).devicePixelRatio;
final url = mxc?.getThumbnail(
2020-05-12 09:02:33 +02:00
Matrix.of(context).client,
width: size * devicePixelRatio,
height: size * devicePixelRatio,
method: ThumbnailMethod.scale,
);
return CachedNetworkImage(
2021-04-21 14:19:54 +02:00
imageUrl: url.toString(),
fit: BoxFit.contain,
width: size,
height: size,
);
2020-05-12 09:02:33 +02:00
}
}
class _ImagePicker extends StatefulWidget {
final ValueNotifier<ImagePackImageContent> controller;
2020-05-12 09:02:33 +02:00
final void Function(ValueNotifier<ImagePackImageContent>) onPressed;
2021-04-24 07:40:13 +02:00
_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),
2021-03-04 12:28:06 +01:00
child: Text(L10n.of(context).pickImage),
2020-05-12 09:02:33 +02:00
);
} else {
return _EmoteImage(widget.controller.value.url);
2020-05-12 09:02:33 +02:00
}
}
}
class SubmitLineIntent extends Intent {}