fluffychat/lib/pages/views/settings_emotes_view.dart

235 lines
9.8 KiB
Dart
Raw Normal View History

2020-09-07 17:08:01 +02:00
import 'package:cached_network_image/cached_network_image.dart';
2020-05-12 09:02:33 +02:00
import 'package:famedlysdk/famedlysdk.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_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;
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: StreamBuilder(
2021-04-24 07:40:13 +02:00
stream: controller.widget.room?.onUpdate?.stream,
2021-04-09 18:26:44 +02:00
builder: (context, snapshot) {
return Column(
children: <Widget>[
2021-04-24 07:40:13 +02:00
if (!controller.readonly)
2021-04-09 18:26:44 +02:00
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(
2021-04-24 07:40:13 +02:00
controller: controller.newEmoteController,
2021-04-09 18:26:44 +02:00
autocorrect: false,
minLines: 1,
maxLines: 1,
decoration: InputDecoration(
hintText: L10n.of(context).emoteShortcode,
prefixText: ': ',
suffixText: ':',
prefixStyle: TextStyle(
2021-05-24 10:59:00 +02:00
color: Theme.of(context).colorScheme.secondary,
2021-04-09 18:26:44 +02:00
fontWeight: FontWeight.bold,
),
suffixStyle: TextStyle(
2021-05-24 10:59:00 +02:00
color: Theme.of(context).colorScheme.secondary,
2021-04-09 18:26:44 +02:00
fontWeight: FontWeight.bold,
),
border: InputBorder.none,
2020-05-12 09:02:33 +02:00
),
2021-04-09 18:26:44 +02:00
),
),
2021-04-24 07:40:13 +02:00
title: _EmoteImagePicker(
controller: controller.newMxcController,
onPressed: controller.emoteImagePickerAction,
),
2021-04-09 18:26:44 +02:00
trailing: InkWell(
2021-04-24 07:40:13 +02:00
onTap: controller.addEmoteAction,
2021-04-09 18:26:44 +02:00
child: Icon(
Icons.add_outlined,
color: Colors.green,
size: 32.0,
2020-05-12 09:02:33 +02:00
),
),
),
2021-04-09 18:26:44 +02:00
),
2021-04-24 07:40:13 +02:00
if (controller.widget.room != null)
2021-04-09 18:26:44 +02:00
ListTile(
title: Text(L10n.of(context).enableEmotesGlobally),
trailing: Switch(
2021-04-24 07:40:13 +02:00
value: controller.isGloballyActive(client),
onChanged: controller.setIsGloballyActive,
2020-05-12 09:02:33 +02:00
),
),
2021-04-24 07:40:13 +02:00
if (!controller.readonly || controller.widget.room != null)
2021-04-09 18:26:44 +02:00
Divider(
height: 2,
thickness: 2,
color: Theme.of(context).primaryColor,
2020-10-03 12:31:29 +02:00
),
2021-04-09 18:26:44 +02:00
Expanded(
2021-04-24 07:40:13 +02:00
child: controller.emotes.isEmpty
2021-04-09 18:26:44 +02:00
? Center(
child: Padding(
padding: EdgeInsets.all(16),
child: Text(
L10n.of(context).noEmotesFound,
style: TextStyle(fontSize: 20),
),
2020-05-12 09:02:33 +02:00
),
2021-04-09 18:26:44 +02:00
)
: ListView.separated(
separatorBuilder: (BuildContext context, int i) =>
Container(),
2021-04-24 07:40:13 +02:00
itemCount: controller.emotes.length + 1,
2021-04-09 18:26:44 +02:00
itemBuilder: (BuildContext context, int i) {
2021-04-24 07:40:13 +02:00
if (i >= controller.emotes.length) {
2021-04-09 18:26:44 +02:00
return Container(height: 70);
}
2021-04-24 07:40:13 +02:00
final emote = controller.emotes[i];
final textEditingController =
TextEditingController();
textEditingController.text = emote.emoteClean;
2021-04-09 18:26:44 +02:00
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,
),
child: TextField(
2021-04-24 07:40:13 +02:00
readOnly: controller.readonly,
controller: textEditingController,
2021-04-09 18:26:44 +02:00
autocorrect: false,
minLines: 1,
maxLines: 1,
decoration: InputDecoration(
hintText: L10n.of(context).emoteShortcode,
prefixText: ': ',
suffixText: ':',
prefixStyle: TextStyle(
2021-05-24 10:59:00 +02:00
color: Theme.of(context)
.colorScheme
.secondary,
2021-04-09 18:26:44 +02:00
fontWeight: FontWeight.bold,
),
suffixStyle: TextStyle(
2021-05-24 10:59:00 +02:00
color: Theme.of(context)
.colorScheme
.secondary,
2021-04-09 18:26:44 +02:00
fontWeight: FontWeight.bold,
),
border: InputBorder.none,
2020-05-12 09:02:33 +02:00
),
2021-04-24 07:40:13 +02:00
onSubmitted: (s) =>
controller.submitEmoteAction(
s,
emote,
textEditingController,
),
2020-05-12 09:02:33 +02:00
),
),
2021-04-09 18:26:44 +02:00
title: _EmoteImage(emote.mxc),
2021-04-24 07:40:13 +02:00
trailing: controller.readonly
2021-04-09 18:26:44 +02:00
? null
: InkWell(
2021-04-24 07:40:13 +02:00
onTap: () =>
controller.removeEmoteAction(emote),
2021-04-09 18:26:44 +02:00
child: Icon(
Icons.delete_forever_outlined,
color: Colors.red,
size: 32.0,
),
2021-03-04 12:28:06 +01:00
),
2021-04-09 18:26:44 +02:00
);
},
),
),
],
);
}),
),
2020-05-12 09:02:33 +02:00
);
}
}
class _EmoteImage extends StatelessWidget {
final String mxc;
_EmoteImage(this.mxc);
@override
Widget build(BuildContext context) {
final size = 38.0;
final devicePixelRatio = MediaQuery.of(context).devicePixelRatio;
final url = Uri.parse(mxc)?.getThumbnail(
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 _EmoteImagePicker extends StatefulWidget {
final TextEditingController controller;
2021-04-24 07:40:13 +02:00
final void Function(TextEditingController) onPressed;
_EmoteImagePicker({@required this.controller, @required this.onPressed});
2020-05-12 09:02:33 +02:00
@override
_EmoteImagePickerState createState() => _EmoteImagePickerState();
}
class _EmoteImagePickerState extends State<_EmoteImagePicker> {
@override
Widget build(BuildContext context) {
if (widget.controller.text == null || widget.controller.text.isEmpty) {
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.text);
}
}
}