2021-04-24 07:40:13 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
|
|
|
|
import 'package:adaptive_dialog/adaptive_dialog.dart';
|
|
|
|
import 'package:file_picker_cross/file_picker_cross.dart';
|
2021-04-24 07:40:13 +02:00
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
|
|
|
import 'package:matrix/matrix.dart';
|
2021-06-24 16:39:17 +02:00
|
|
|
import 'package:vrouter/vrouter.dart';
|
2021-04-24 07:40:13 +02:00
|
|
|
|
2022-08-14 16:31:36 +02:00
|
|
|
import 'package:fluffychat/utils/client_manager.dart';
|
2021-11-09 21:32:16 +01:00
|
|
|
import '../../widgets/matrix.dart';
|
|
|
|
import 'settings_emotes_view.dart';
|
2021-04-24 07:40:13 +02:00
|
|
|
|
|
|
|
class EmotesSettings extends StatefulWidget {
|
2022-01-29 12:35:03 +01:00
|
|
|
const EmotesSettings({Key? key}) : super(key: key);
|
2021-04-24 07:40:13 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
EmotesSettingsController createState() => EmotesSettingsController();
|
|
|
|
}
|
|
|
|
|
|
|
|
class EmotesSettingsController extends State<EmotesSettings> {
|
2022-01-29 12:35:03 +01: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-06-24 16:39:17 +02:00
|
|
|
|
2021-04-24 07:40:13 +02:00
|
|
|
bool showSave = false;
|
2021-07-24 18:19:59 +02:00
|
|
|
TextEditingController newImageCodeController = TextEditingController();
|
2022-01-29 12:35:03 +01:00
|
|
|
ValueNotifier<ImagePackImageContent?> newImageController =
|
|
|
|
ValueNotifier<ImagePackImageContent?>(null);
|
2021-04-24 07:40:13 +02:00
|
|
|
|
2021-07-24 18:19:59 +02:00
|
|
|
ImagePackContent _getPack() {
|
2021-07-04 15:45:21 +02:00
|
|
|
final client = Matrix.of(context).client;
|
|
|
|
final event = (room != null
|
2022-01-29 12:35:03 +01:00
|
|
|
? room!.getState('im.ponies.room_emotes', stateKey ?? '')
|
2021-07-04 15:45:21 +02:00
|
|
|
: client.accountData['im.ponies.user_emotes']) ??
|
2022-02-06 17:09:45 +01:00
|
|
|
BasicEvent(
|
|
|
|
type: 'm.dummy',
|
|
|
|
content: {},
|
|
|
|
);
|
2021-07-04 15:45:21 +02:00
|
|
|
// make sure we work on a *copy* of the event
|
|
|
|
return BasicEvent.fromJson(event.toJson()).parsedImagePackContent;
|
|
|
|
}
|
|
|
|
|
2022-01-29 12:35:03 +01:00
|
|
|
ImagePackContent? _pack;
|
|
|
|
ImagePackContent? get pack {
|
2021-07-24 18:19:59 +02:00
|
|
|
if (_pack != null) {
|
|
|
|
return _pack;
|
|
|
|
}
|
|
|
|
_pack = _getPack();
|
|
|
|
return _pack;
|
|
|
|
}
|
|
|
|
|
2021-04-24 07:40:13 +02:00
|
|
|
Future<void> _save(BuildContext context) async {
|
|
|
|
if (readonly) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final client = Matrix.of(context).client;
|
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(
|
2022-01-29 12:35:03 +01:00
|
|
|
room!.id, 'im.ponies.room_emotes', stateKey ?? '', pack!.toJson()),
|
2021-04-24 07:40:13 +02:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
await showFutureLoadingDialog(
|
|
|
|
context: context,
|
|
|
|
future: () => client.setAccountData(
|
2022-01-29 12:35:03 +01: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) {
|
2021-10-14 18:09:30 +02:00
|
|
|
if (content['rooms'] is! Map) {
|
2021-04-24 07:40:13 +02:00
|
|
|
content['rooms'] = <String, dynamic>{};
|
|
|
|
}
|
2022-01-29 12:35:03 +01:00
|
|
|
if (content['rooms'][room!.id] is! Map) {
|
|
|
|
content['rooms'][room!.id] = <String, dynamic>{};
|
2021-04-24 07:40:13 +02:00
|
|
|
}
|
2022-01-29 12:35:03 +01:00
|
|
|
if (content['rooms'][room!.id][stateKey ?? ''] is! Map) {
|
|
|
|
content['rooms'][room!.id][stateKey ?? ''] = <String, dynamic>{};
|
2021-04-24 07:40:13 +02:00
|
|
|
}
|
2022-01-29 12:35:03 +01: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(
|
2022-01-29 12:35:03 +01:00
|
|
|
client.userID!, 'im.ponies.emote_rooms', content),
|
2021-04-24 07:40:13 +02:00
|
|
|
);
|
2022-01-29 12:35:03 +01:00
|
|
|
setState(() {});
|
2021-04-24 07:40:13 +02:00
|
|
|
}
|
|
|
|
|
2021-07-24 18:19:59 +02:00
|
|
|
void removeImageAction(String oldImageCode) => setState(() {
|
2022-01-29 12:35:03 +01:00
|
|
|
pack!.images.remove(oldImageCode);
|
2021-04-24 07:40:13 +02:00
|
|
|
showSave = true;
|
|
|
|
});
|
|
|
|
|
2021-07-24 18:19:59 +02:00
|
|
|
void submitImageAction(
|
|
|
|
String oldImageCode,
|
|
|
|
String imageCode,
|
|
|
|
ImagePackImageContent image,
|
2021-04-24 07:40:13 +02:00
|
|
|
TextEditingController controller,
|
|
|
|
) {
|
2022-01-29 12:35:03 +01:00
|
|
|
if (pack!.images.keys.any((k) => k == imageCode && k != oldImageCode)) {
|
2021-07-24 18:19:59 +02:00
|
|
|
controller.text = oldImageCode;
|
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,
|
2022-01-29 12:35:03 +01:00
|
|
|
message: L10n.of(context)!.emoteExists,
|
|
|
|
okLabel: L10n.of(context)!.ok,
|
2021-04-24 07:40:13 +02:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2021-07-24 18:19:59 +02:00
|
|
|
if (!RegExp(r'^[-\w]+$').hasMatch(imageCode)) {
|
|
|
|
controller.text = oldImageCode;
|
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,
|
2022-01-29 12:35:03 +01:00
|
|
|
message: L10n.of(context)!.emoteInvalid,
|
|
|
|
okLabel: L10n.of(context)!.ok,
|
2021-04-24 07:40:13 +02:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setState(() {
|
2022-01-29 12:35:03 +01:00
|
|
|
pack!.images[imageCode] = image;
|
|
|
|
pack!.images.remove(oldImageCode);
|
2021-04-24 07:40:13 +02:00
|
|
|
showSave = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-29 12:35:03 +01:00
|
|
|
bool isGloballyActive(Client? client) =>
|
2021-06-24 16:39:17 +02:00
|
|
|
room != null &&
|
2022-01-29 12:35:03 +01:00
|
|
|
client!.accountData['im.ponies.emote_rooms']?.content is Map &&
|
|
|
|
client.accountData['im.ponies.emote_rooms']!.content['rooms'] is Map &&
|
|
|
|
client.accountData['im.ponies.emote_rooms']!.content['rooms'][room!.id]
|
2021-06-24 16:39:17 +02:00
|
|
|
is Map &&
|
2022-01-29 12:35:03 +01:00
|
|
|
client.accountData['im.ponies.emote_rooms']!.content['rooms'][room!.id]
|
2021-06-24 16:39:17 +02:00
|
|
|
[stateKey ?? ''] is Map;
|
2021-04-24 07:40:13 +02:00
|
|
|
|
2021-06-24 16:39:17 +02:00
|
|
|
bool get readonly =>
|
2022-01-29 12:35:03 +01:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-24 18:19:59 +02:00
|
|
|
void addImageAction() async {
|
2022-01-29 12:35:03 +01:00
|
|
|
if (newImageCodeController.text.isEmpty ||
|
2021-07-24 18:19:59 +02:00
|
|
|
newImageController.value == null) {
|
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,
|
2022-01-29 12:35:03 +01:00
|
|
|
message: L10n.of(context)!.emoteWarnNeedToPick,
|
|
|
|
okLabel: L10n.of(context)!.ok,
|
2021-04-24 07:40:13 +02:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2021-07-24 18:19:59 +02:00
|
|
|
final imageCode = newImageCodeController.text;
|
2022-01-29 12:35:03 +01:00
|
|
|
if (pack!.images.containsKey(imageCode)) {
|
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,
|
2022-01-29 12:35:03 +01:00
|
|
|
message: L10n.of(context)!.emoteExists,
|
|
|
|
okLabel: L10n.of(context)!.ok,
|
2021-04-24 07:40:13 +02:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2021-07-24 18:19:59 +02:00
|
|
|
if (!RegExp(r'^[-\w]+$').hasMatch(imageCode)) {
|
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,
|
2022-01-29 12:35:03 +01:00
|
|
|
message: L10n.of(context)!.emoteInvalid,
|
|
|
|
okLabel: L10n.of(context)!.ok,
|
2021-04-24 07:40:13 +02:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2022-01-29 12:35:03 +01:00
|
|
|
pack!.images[imageCode] = newImageController.value!;
|
2021-04-24 07:40:13 +02:00
|
|
|
await _save(context);
|
|
|
|
setState(() {
|
2021-07-24 18:19:59 +02:00
|
|
|
newImageCodeController.text = '';
|
|
|
|
newImageController.value = null;
|
2021-04-24 07:40:13 +02:00
|
|
|
showSave = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-24 18:19:59 +02:00
|
|
|
void imagePickerAction(
|
2022-01-29 12:35:03 +01:00
|
|
|
ValueNotifier<ImagePackImageContent?> controller) async {
|
2021-07-24 18:19:59 +02:00
|
|
|
final result =
|
|
|
|
await FilePickerCross.importFromStorage(type: FileTypeCross.image);
|
2022-01-29 12:35:03 +01:00
|
|
|
if (result.fileName == null) return;
|
2021-07-24 18:19:59 +02:00
|
|
|
var file = MatrixImageFile(
|
|
|
|
bytes: result.toUint8List(),
|
2022-01-29 12:35:03 +01:00
|
|
|
name: result.fileName!,
|
2021-07-24 18:19:59 +02:00
|
|
|
);
|
|
|
|
try {
|
2022-02-03 07:35:44 +01:00
|
|
|
file = (await file.generateThumbnail(
|
2022-08-14 16:31:36 +02:00
|
|
|
nativeImplementations: ClientManager.nativeImplementations,
|
2022-02-03 07:35:44 +01:00
|
|
|
))!;
|
2022-08-14 16:31:36 +02:00
|
|
|
} catch (e, s) {
|
|
|
|
Logs().w('Unable to create thumbnail', e, s);
|
2021-04-24 07:40:13 +02:00
|
|
|
}
|
|
|
|
final uploadResp = await showFutureLoadingDialog(
|
|
|
|
context: context,
|
2022-02-03 07:35:44 +01:00
|
|
|
future: () => Matrix.of(context).client.uploadContent(
|
|
|
|
file.bytes,
|
|
|
|
filename: file.name,
|
|
|
|
contentType: file.mimeType,
|
|
|
|
),
|
2021-04-24 07:40:13 +02:00
|
|
|
);
|
|
|
|
if (uploadResp.error == null) {
|
|
|
|
setState(() {
|
2021-07-24 18:19:59 +02:00
|
|
|
final info = <String, dynamic>{
|
|
|
|
...file.info,
|
|
|
|
};
|
|
|
|
// normalize width / height to 256, required for stickers
|
|
|
|
if (info['w'] is int && info['h'] is int) {
|
|
|
|
final ratio = info['w'] / info['h'];
|
|
|
|
if (info['w'] > info['h']) {
|
|
|
|
info['w'] = 256;
|
|
|
|
info['h'] = (256.0 / ratio).round();
|
|
|
|
} else {
|
|
|
|
info['h'] = 256;
|
|
|
|
info['w'] = (ratio * 256.0).round();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
controller.value = ImagePackImageContent.fromJson(<String, dynamic>{
|
2021-09-16 14:36:28 +02:00
|
|
|
'url': uploadResp.result.toString(),
|
2021-07-24 18:19:59 +02:00
|
|
|
'info': info,
|
|
|
|
});
|
2021-04-24 07:40:13 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2021-05-22 09:13:47 +02:00
|
|
|
return EmotesSettingsView(this);
|
2021-04-24 07:40:13 +02:00
|
|
|
}
|
|
|
|
}
|