2021-04-24 07:40:13 +02:00
|
|
|
import 'package:adaptive_dialog/adaptive_dialog.dart';
|
2021-05-23 13:11:55 +02:00
|
|
|
|
2021-06-18 10:29:48 +02:00
|
|
|
import 'package:matrix/matrix.dart';
|
2021-04-24 07:40:13 +02:00
|
|
|
import 'package:file_picker_cross/file_picker_cross.dart';
|
|
|
|
import 'package:fluffychat/utils/platform_infos.dart';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
2021-06-24 16:39:17 +02:00
|
|
|
import 'package:vrouter/vrouter.dart';
|
2021-04-24 07:40:13 +02:00
|
|
|
|
|
|
|
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
|
|
|
import 'package:image_picker/image_picker.dart';
|
2021-05-22 09:08:23 +02:00
|
|
|
import 'views/settings_emotes_view.dart';
|
2021-05-22 08:53:52 +02:00
|
|
|
import '../widgets/matrix.dart';
|
2021-04-24 07:40:13 +02:00
|
|
|
|
|
|
|
class EmotesSettings extends StatefulWidget {
|
2021-06-24 16:39:17 +02:00
|
|
|
EmotesSettings({Key key}) : super(key: key);
|
2021-04-24 07:40:13 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
EmotesSettingsController createState() => EmotesSettingsController();
|
|
|
|
}
|
|
|
|
|
|
|
|
class EmoteEntry {
|
|
|
|
String emote;
|
|
|
|
String mxc;
|
|
|
|
EmoteEntry({this.emote, this.mxc});
|
|
|
|
}
|
|
|
|
|
|
|
|
class EmotesSettingsController extends State<EmotesSettings> {
|
2021-06-24 16:39:17 +02:00
|
|
|
String get roomId => VRouter.of(context).pathParameters['roomid'];
|
|
|
|
Room get room =>
|
|
|
|
roomId != null ? Matrix.of(context).client.getRoomById(roomId) : null;
|
|
|
|
String get stateKey => VRouter.of(context).pathParameters['state_key'];
|
|
|
|
|
2021-04-24 07:40:13 +02:00
|
|
|
List<EmoteEntry> emotes;
|
|
|
|
bool showSave = false;
|
|
|
|
TextEditingController newEmoteController = TextEditingController();
|
|
|
|
TextEditingController newMxcController = TextEditingController();
|
|
|
|
|
2021-07-04 15:45:21 +02:00
|
|
|
ImagePackContent _getPack(BuildContext context) {
|
|
|
|
final client = Matrix.of(context).client;
|
|
|
|
final event = (room != null
|
|
|
|
? room.getState('im.ponies.room_emotes', stateKey ?? '')
|
|
|
|
: client.accountData['im.ponies.user_emotes']) ??
|
|
|
|
BasicEvent.fromJson(<String, dynamic>{
|
|
|
|
'type': 'm.dummy',
|
|
|
|
'content': <String, dynamic>{},
|
|
|
|
});
|
|
|
|
// make sure we work on a *copy* of the event
|
|
|
|
return BasicEvent.fromJson(event.toJson()).parsedImagePackContent;
|
|
|
|
}
|
|
|
|
|
2021-04-24 07:40:13 +02:00
|
|
|
Future<void> _save(BuildContext context) async {
|
|
|
|
if (readonly) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final client = Matrix.of(context).client;
|
2021-07-04 15:45:21 +02:00
|
|
|
final pack = _getPack(context);
|
2021-04-24 07:40:13 +02:00
|
|
|
// add / update changed emotes
|
|
|
|
final allowedShortcodes = <String>{};
|
|
|
|
for (final emote in emotes) {
|
|
|
|
allowedShortcodes.add(emote.emote);
|
2021-07-04 15:45:21 +02:00
|
|
|
if (pack.images.containsKey(emote.emote)) {
|
|
|
|
pack.images[emote.emote].url = Uri.parse(emote.mxc);
|
|
|
|
} else {
|
|
|
|
pack.images[emote.emote] =
|
|
|
|
ImagePackImageContent.fromJson(<String, dynamic>{
|
|
|
|
'url': emote.mxc,
|
|
|
|
});
|
2021-04-24 07:40:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// remove emotes no more needed
|
|
|
|
// we make the iterator .toList() here so that we don't get into trouble modifying the very
|
|
|
|
// thing we are iterating over
|
2021-07-04 15:45:21 +02:00
|
|
|
for (final shortcode in pack.images.keys.toList()) {
|
2021-04-24 07:40:13 +02:00
|
|
|
if (!allowedShortcodes.contains(shortcode)) {
|
2021-07-04 15:45:21 +02:00
|
|
|
pack.images.remove(shortcode);
|
2021-04-24 07:40:13 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-24 16:39:17 +02:00
|
|
|
if (room != null) {
|
2021-04-24 07:40:13 +02:00
|
|
|
await showFutureLoadingDialog(
|
|
|
|
context: context,
|
2021-06-24 16:39:17 +02:00
|
|
|
future: () => client.setRoomStateWithKey(
|
2021-07-04 15:45:21 +02:00
|
|
|
room.id, 'im.ponies.room_emotes', pack.toJson(), stateKey ?? ''),
|
2021-04-24 07:40:13 +02:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
await showFutureLoadingDialog(
|
|
|
|
context: context,
|
|
|
|
future: () => client.setAccountData(
|
2021-07-04 15:45:21 +02:00
|
|
|
client.userID, 'im.ponies.user_emotes', pack.toJson()),
|
2021-04-24 07:40:13 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> setIsGloballyActive(bool active) async {
|
2021-06-24 16:39:17 +02:00
|
|
|
if (room == null) {
|
2021-04-24 07:40:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
final client = Matrix.of(context).client;
|
|
|
|
final content = client.accountData['im.ponies.emote_rooms']?.content ??
|
|
|
|
<String, dynamic>{};
|
|
|
|
if (active) {
|
|
|
|
if (!(content['rooms'] is Map)) {
|
|
|
|
content['rooms'] = <String, dynamic>{};
|
|
|
|
}
|
2021-06-24 16:39:17 +02:00
|
|
|
if (!(content['rooms'][room.id] is Map)) {
|
|
|
|
content['rooms'][room.id] = <String, dynamic>{};
|
2021-04-24 07:40:13 +02:00
|
|
|
}
|
2021-06-24 16:39:17 +02:00
|
|
|
if (!(content['rooms'][room.id][stateKey ?? ''] is Map)) {
|
|
|
|
content['rooms'][room.id][stateKey ?? ''] = <String, dynamic>{};
|
2021-04-24 07:40:13 +02:00
|
|
|
}
|
2021-06-24 16:39:17 +02:00
|
|
|
} else if (content['rooms'] is Map && content['rooms'][room.id] is Map) {
|
|
|
|
content['rooms'][room.id].remove(stateKey ?? '');
|
2021-04-24 07:40:13 +02:00
|
|
|
}
|
|
|
|
// and save
|
|
|
|
await showFutureLoadingDialog(
|
|
|
|
context: context,
|
|
|
|
future: () => client.setAccountData(
|
|
|
|
client.userID, 'im.ponies.emote_rooms', content),
|
|
|
|
);
|
|
|
|
setState(() => null);
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeEmoteAction(EmoteEntry emote) => setState(() {
|
|
|
|
emotes.removeWhere((e) => e.emote == emote.emote);
|
|
|
|
showSave = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
void submitEmoteAction(
|
2021-07-04 15:45:21 +02:00
|
|
|
String emoteCode,
|
2021-04-24 07:40:13 +02:00
|
|
|
EmoteEntry emote,
|
|
|
|
TextEditingController controller,
|
|
|
|
) {
|
|
|
|
if (emotes.indexWhere((e) => e.emote == emoteCode && e.mxc != emote.mxc) !=
|
|
|
|
-1) {
|
2021-07-04 15:45:21 +02:00
|
|
|
controller.text = emote.emote;
|
2021-04-24 07:40:13 +02:00
|
|
|
showOkAlertDialog(
|
2021-05-23 15:02:36 +02:00
|
|
|
useRootNavigator: false,
|
2021-04-24 07:40:13 +02:00
|
|
|
context: context,
|
|
|
|
message: L10n.of(context).emoteExists,
|
|
|
|
okLabel: L10n.of(context).ok,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2021-07-04 15:45:21 +02:00
|
|
|
if (!RegExp(r'^[-\w]+$').hasMatch(emoteCode)) {
|
|
|
|
controller.text = emote.emote;
|
2021-04-24 07:40:13 +02:00
|
|
|
showOkAlertDialog(
|
2021-05-23 15:02:36 +02:00
|
|
|
useRootNavigator: false,
|
2021-04-24 07:40:13 +02:00
|
|
|
context: context,
|
|
|
|
message: L10n.of(context).emoteInvalid,
|
|
|
|
okLabel: L10n.of(context).ok,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setState(() {
|
|
|
|
emote.emote = emoteCode;
|
|
|
|
showSave = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isGloballyActive(Client client) =>
|
2021-06-24 16:39:17 +02:00
|
|
|
room != null &&
|
2021-04-24 07:40:13 +02:00
|
|
|
client.accountData['im.ponies.emote_rooms']?.content is Map &&
|
|
|
|
client.accountData['im.ponies.emote_rooms'].content['rooms'] is Map &&
|
2021-06-24 16:39:17 +02:00
|
|
|
client.accountData['im.ponies.emote_rooms'].content['rooms'][room.id]
|
|
|
|
is Map &&
|
|
|
|
client.accountData['im.ponies.emote_rooms'].content['rooms'][room.id]
|
|
|
|
[stateKey ?? ''] is Map;
|
2021-04-24 07:40:13 +02:00
|
|
|
|
2021-06-24 16:39:17 +02:00
|
|
|
bool get readonly =>
|
|
|
|
room == null ? false : !(room.canSendEvent('im.ponies.room_emotes'));
|
2021-04-24 07:40:13 +02:00
|
|
|
|
|
|
|
void saveAction() async {
|
|
|
|
await _save(context);
|
|
|
|
setState(() {
|
|
|
|
showSave = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void addEmoteAction() async {
|
|
|
|
if (newEmoteController.text == null ||
|
|
|
|
newEmoteController.text.isEmpty ||
|
|
|
|
newMxcController.text == null ||
|
|
|
|
newMxcController.text.isEmpty) {
|
|
|
|
await showOkAlertDialog(
|
2021-05-23 15:02:36 +02:00
|
|
|
useRootNavigator: false,
|
2021-04-24 07:40:13 +02:00
|
|
|
context: context,
|
|
|
|
message: L10n.of(context).emoteWarnNeedToPick,
|
|
|
|
okLabel: L10n.of(context).ok,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2021-07-04 15:45:21 +02:00
|
|
|
final emoteCode = '${newEmoteController.text}';
|
2021-04-24 07:40:13 +02:00
|
|
|
final mxc = newMxcController.text;
|
|
|
|
if (emotes.indexWhere((e) => e.emote == emoteCode && e.mxc != mxc) != -1) {
|
|
|
|
await showOkAlertDialog(
|
2021-05-23 15:02:36 +02:00
|
|
|
useRootNavigator: false,
|
2021-04-24 07:40:13 +02:00
|
|
|
context: context,
|
|
|
|
message: L10n.of(context).emoteExists,
|
|
|
|
okLabel: L10n.of(context).ok,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2021-07-04 15:45:21 +02:00
|
|
|
if (!RegExp(r'^[-\w]+$').hasMatch(emoteCode)) {
|
2021-04-24 07:40:13 +02:00
|
|
|
await showOkAlertDialog(
|
2021-05-23 15:02:36 +02:00
|
|
|
useRootNavigator: false,
|
2021-04-24 07:40:13 +02:00
|
|
|
context: context,
|
|
|
|
message: L10n.of(context).emoteInvalid,
|
|
|
|
okLabel: L10n.of(context).ok,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
emotes.add(EmoteEntry(emote: emoteCode, mxc: mxc));
|
|
|
|
await _save(context);
|
|
|
|
setState(() {
|
|
|
|
newEmoteController.text = '';
|
|
|
|
newMxcController.text = '';
|
|
|
|
showSave = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void emoteImagePickerAction(TextEditingController controller) async {
|
|
|
|
if (kIsWeb) {
|
2021-05-23 13:11:55 +02:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
2021-04-24 07:40:13 +02:00
|
|
|
SnackBar(content: Text(L10n.of(context).notSupportedInWeb)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MatrixFile file;
|
|
|
|
if (PlatformInfos.isMobile) {
|
|
|
|
final result = await ImagePicker().getImage(
|
|
|
|
source: ImageSource.gallery,
|
|
|
|
imageQuality: 50,
|
|
|
|
maxWidth: 1600,
|
|
|
|
maxHeight: 1600);
|
|
|
|
if (result == null) return;
|
|
|
|
file = MatrixFile(
|
|
|
|
bytes: await result.readAsBytes(),
|
|
|
|
name: result.path,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
final result =
|
|
|
|
await FilePickerCross.importFromStorage(type: FileTypeCross.image);
|
|
|
|
if (result == null) return;
|
|
|
|
file = MatrixFile(
|
|
|
|
bytes: result.toUint8List(),
|
|
|
|
name: result.fileName,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
final uploadResp = await showFutureLoadingDialog(
|
|
|
|
context: context,
|
2021-05-20 13:59:55 +02:00
|
|
|
future: () =>
|
|
|
|
Matrix.of(context).client.uploadContent(file.bytes, file.name),
|
2021-04-24 07:40:13 +02:00
|
|
|
);
|
|
|
|
if (uploadResp.error == null) {
|
|
|
|
setState(() {
|
|
|
|
controller.text = uploadResp.result;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
if (emotes == null) {
|
|
|
|
emotes = <EmoteEntry>[];
|
2021-07-04 15:45:21 +02:00
|
|
|
final pack = _getPack(context);
|
|
|
|
for (final entry in pack.images.entries) {
|
|
|
|
emotes
|
|
|
|
.add(EmoteEntry(emote: entry.key, mxc: entry.value.url.toString()));
|
2021-04-24 07:40:13 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-22 09:13:47 +02:00
|
|
|
return EmotesSettingsView(this);
|
2021-04-24 07:40:13 +02:00
|
|
|
}
|
|
|
|
}
|