|
|
@ -2,16 +2,15 @@ import 'package:adaptive_dialog/adaptive_dialog.dart';
|
|
|
|
|
|
|
|
|
|
|
|
import 'package:matrix/matrix.dart';
|
|
|
|
import 'package:matrix/matrix.dart';
|
|
|
|
import 'package:file_picker_cross/file_picker_cross.dart';
|
|
|
|
import 'package:file_picker_cross/file_picker_cross.dart';
|
|
|
|
import 'package:fluffychat/utils/platform_infos.dart';
|
|
|
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
|
|
|
import 'package:vrouter/vrouter.dart';
|
|
|
|
import 'package:vrouter/vrouter.dart';
|
|
|
|
|
|
|
|
|
|
|
|
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
|
|
|
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
|
|
|
import 'package:image_picker/image_picker.dart';
|
|
|
|
|
|
|
|
import 'views/settings_emotes_view.dart';
|
|
|
|
import 'views/settings_emotes_view.dart';
|
|
|
|
import '../widgets/matrix.dart';
|
|
|
|
import '../widgets/matrix.dart';
|
|
|
|
|
|
|
|
import '../utils/resize_image.dart';
|
|
|
|
|
|
|
|
|
|
|
|
class EmotesSettings extends StatefulWidget {
|
|
|
|
class EmotesSettings extends StatefulWidget {
|
|
|
|
EmotesSettings({Key key}) : super(key: key);
|
|
|
|
EmotesSettings({Key key}) : super(key: key);
|
|
|
@ -20,24 +19,18 @@ class EmotesSettings extends StatefulWidget {
|
|
|
|
EmotesSettingsController createState() => EmotesSettingsController();
|
|
|
|
EmotesSettingsController createState() => EmotesSettingsController();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class EmoteEntry {
|
|
|
|
|
|
|
|
String emote;
|
|
|
|
|
|
|
|
String mxc;
|
|
|
|
|
|
|
|
EmoteEntry({this.emote, this.mxc});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class EmotesSettingsController extends State<EmotesSettings> {
|
|
|
|
class EmotesSettingsController extends State<EmotesSettings> {
|
|
|
|
String get roomId => VRouter.of(context).pathParameters['roomid'];
|
|
|
|
String get roomId => VRouter.of(context).pathParameters['roomid'];
|
|
|
|
Room get room =>
|
|
|
|
Room get room =>
|
|
|
|
roomId != null ? Matrix.of(context).client.getRoomById(roomId) : null;
|
|
|
|
roomId != null ? Matrix.of(context).client.getRoomById(roomId) : null;
|
|
|
|
String get stateKey => VRouter.of(context).pathParameters['state_key'];
|
|
|
|
String get stateKey => VRouter.of(context).pathParameters['state_key'];
|
|
|
|
|
|
|
|
|
|
|
|
List<EmoteEntry> emotes;
|
|
|
|
|
|
|
|
bool showSave = false;
|
|
|
|
bool showSave = false;
|
|
|
|
TextEditingController newEmoteController = TextEditingController();
|
|
|
|
TextEditingController newImageCodeController = TextEditingController();
|
|
|
|
TextEditingController newMxcController = TextEditingController();
|
|
|
|
ValueNotifier<ImagePackImageContent> newImageController =
|
|
|
|
|
|
|
|
ValueNotifier<ImagePackImageContent>(null);
|
|
|
|
|
|
|
|
|
|
|
|
ImagePackContent _getPack(BuildContext context) {
|
|
|
|
ImagePackContent _getPack() {
|
|
|
|
final client = Matrix.of(context).client;
|
|
|
|
final client = Matrix.of(context).client;
|
|
|
|
final event = (room != null
|
|
|
|
final event = (room != null
|
|
|
|
? room.getState('im.ponies.room_emotes', stateKey ?? '')
|
|
|
|
? room.getState('im.ponies.room_emotes', stateKey ?? '')
|
|
|
@ -50,33 +43,20 @@ class EmotesSettingsController extends State<EmotesSettings> {
|
|
|
|
return BasicEvent.fromJson(event.toJson()).parsedImagePackContent;
|
|
|
|
return BasicEvent.fromJson(event.toJson()).parsedImagePackContent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ImagePackContent _pack;
|
|
|
|
|
|
|
|
ImagePackContent get pack {
|
|
|
|
|
|
|
|
if (_pack != null) {
|
|
|
|
|
|
|
|
return _pack;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_pack = _getPack();
|
|
|
|
|
|
|
|
return _pack;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> _save(BuildContext context) async {
|
|
|
|
Future<void> _save(BuildContext context) async {
|
|
|
|
if (readonly) {
|
|
|
|
if (readonly) {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
final client = Matrix.of(context).client;
|
|
|
|
final client = Matrix.of(context).client;
|
|
|
|
final pack = _getPack(context);
|
|
|
|
|
|
|
|
// add / update changed emotes
|
|
|
|
|
|
|
|
final allowedShortcodes = <String>{};
|
|
|
|
|
|
|
|
for (final emote in emotes) {
|
|
|
|
|
|
|
|
allowedShortcodes.add(emote.emote);
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
for (final shortcode in pack.images.keys.toList()) {
|
|
|
|
|
|
|
|
if (!allowedShortcodes.contains(shortcode)) {
|
|
|
|
|
|
|
|
pack.images.remove(shortcode);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (room != null) {
|
|
|
|
if (room != null) {
|
|
|
|
await showFutureLoadingDialog(
|
|
|
|
await showFutureLoadingDialog(
|
|
|
|
context: context,
|
|
|
|
context: context,
|
|
|
@ -121,19 +101,19 @@ class EmotesSettingsController extends State<EmotesSettings> {
|
|
|
|
setState(() => null);
|
|
|
|
setState(() => null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void removeEmoteAction(EmoteEntry emote) => setState(() {
|
|
|
|
void removeImageAction(String oldImageCode) => setState(() {
|
|
|
|
emotes.removeWhere((e) => e.emote == emote.emote);
|
|
|
|
pack.images.remove(oldImageCode);
|
|
|
|
showSave = true;
|
|
|
|
showSave = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
void submitEmoteAction(
|
|
|
|
void submitImageAction(
|
|
|
|
String emoteCode,
|
|
|
|
String oldImageCode,
|
|
|
|
EmoteEntry emote,
|
|
|
|
String imageCode,
|
|
|
|
|
|
|
|
ImagePackImageContent image,
|
|
|
|
TextEditingController controller,
|
|
|
|
TextEditingController controller,
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
if (emotes.indexWhere((e) => e.emote == emoteCode && e.mxc != emote.mxc) !=
|
|
|
|
if (pack.images.keys.any((k) => k == imageCode && k != oldImageCode)) {
|
|
|
|
-1) {
|
|
|
|
controller.text = oldImageCode;
|
|
|
|
controller.text = emote.emote;
|
|
|
|
|
|
|
|
showOkAlertDialog(
|
|
|
|
showOkAlertDialog(
|
|
|
|
useRootNavigator: false,
|
|
|
|
useRootNavigator: false,
|
|
|
|
context: context,
|
|
|
|
context: context,
|
|
|
@ -142,8 +122,8 @@ class EmotesSettingsController extends State<EmotesSettings> {
|
|
|
|
);
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!RegExp(r'^[-\w]+$').hasMatch(emoteCode)) {
|
|
|
|
if (!RegExp(r'^[-\w]+$').hasMatch(imageCode)) {
|
|
|
|
controller.text = emote.emote;
|
|
|
|
controller.text = oldImageCode;
|
|
|
|
showOkAlertDialog(
|
|
|
|
showOkAlertDialog(
|
|
|
|
useRootNavigator: false,
|
|
|
|
useRootNavigator: false,
|
|
|
|
context: context,
|
|
|
|
context: context,
|
|
|
@ -153,7 +133,8 @@ class EmotesSettingsController extends State<EmotesSettings> {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setState(() {
|
|
|
|
setState(() {
|
|
|
|
emote.emote = emoteCode;
|
|
|
|
pack.images[imageCode] = image;
|
|
|
|
|
|
|
|
pack.images.remove(oldImageCode);
|
|
|
|
showSave = true;
|
|
|
|
showSave = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -177,11 +158,10 @@ class EmotesSettingsController extends State<EmotesSettings> {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void addEmoteAction() async {
|
|
|
|
void addImageAction() async {
|
|
|
|
if (newEmoteController.text == null ||
|
|
|
|
if (newImageCodeController.text == null ||
|
|
|
|
newEmoteController.text.isEmpty ||
|
|
|
|
newImageCodeController.text.isEmpty ||
|
|
|
|
newMxcController.text == null ||
|
|
|
|
newImageController.value == null) {
|
|
|
|
newMxcController.text.isEmpty) {
|
|
|
|
|
|
|
|
await showOkAlertDialog(
|
|
|
|
await showOkAlertDialog(
|
|
|
|
useRootNavigator: false,
|
|
|
|
useRootNavigator: false,
|
|
|
|
context: context,
|
|
|
|
context: context,
|
|
|
@ -190,9 +170,8 @@ class EmotesSettingsController extends State<EmotesSettings> {
|
|
|
|
);
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
final emoteCode = '${newEmoteController.text}';
|
|
|
|
final imageCode = newImageCodeController.text;
|
|
|
|
final mxc = newMxcController.text;
|
|
|
|
if (pack.images.containsKey(imageCode)) {
|
|
|
|
if (emotes.indexWhere((e) => e.emote == emoteCode && e.mxc != mxc) != -1) {
|
|
|
|
|
|
|
|
await showOkAlertDialog(
|
|
|
|
await showOkAlertDialog(
|
|
|
|
useRootNavigator: false,
|
|
|
|
useRootNavigator: false,
|
|
|
|
context: context,
|
|
|
|
context: context,
|
|
|
@ -201,7 +180,7 @@ class EmotesSettingsController extends State<EmotesSettings> {
|
|
|
|
);
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!RegExp(r'^[-\w]+$').hasMatch(emoteCode)) {
|
|
|
|
if (!RegExp(r'^[-\w]+$').hasMatch(imageCode)) {
|
|
|
|
await showOkAlertDialog(
|
|
|
|
await showOkAlertDialog(
|
|
|
|
useRootNavigator: false,
|
|
|
|
useRootNavigator: false,
|
|
|
|
context: context,
|
|
|
|
context: context,
|
|
|
@ -210,41 +189,28 @@ class EmotesSettingsController extends State<EmotesSettings> {
|
|
|
|
);
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
emotes.add(EmoteEntry(emote: emoteCode, mxc: mxc));
|
|
|
|
pack.images[imageCode] = newImageController.value;
|
|
|
|
await _save(context);
|
|
|
|
await _save(context);
|
|
|
|
setState(() {
|
|
|
|
setState(() {
|
|
|
|
newEmoteController.text = '';
|
|
|
|
newImageCodeController.text = '';
|
|
|
|
newMxcController.text = '';
|
|
|
|
newImageController.value = null;
|
|
|
|
showSave = false;
|
|
|
|
showSave = false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void emoteImagePickerAction(TextEditingController controller) async {
|
|
|
|
void imagePickerAction(
|
|
|
|
if (kIsWeb) {
|
|
|
|
ValueNotifier<ImagePackImageContent> controller) async {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
|
|
|
|
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 =
|
|
|
|
final result =
|
|
|
|
await FilePickerCross.importFromStorage(type: FileTypeCross.image);
|
|
|
|
await FilePickerCross.importFromStorage(type: FileTypeCross.image);
|
|
|
|
if (result == null) return;
|
|
|
|
if (result == null) return;
|
|
|
|
file = MatrixFile(
|
|
|
|
var file = MatrixImageFile(
|
|
|
|
bytes: result.toUint8List(),
|
|
|
|
bytes: result.toUint8List(),
|
|
|
|
name: result.fileName,
|
|
|
|
name: result.fileName,
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
file = await resizeImage(file, max: 1600);
|
|
|
|
|
|
|
|
} catch (_) {
|
|
|
|
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
final uploadResp = await showFutureLoadingDialog(
|
|
|
|
final uploadResp = await showFutureLoadingDialog(
|
|
|
|
context: context,
|
|
|
|
context: context,
|
|
|
@ -253,21 +219,30 @@ class EmotesSettingsController extends State<EmotesSettings> {
|
|
|
|
);
|
|
|
|
);
|
|
|
|
if (uploadResp.error == null) {
|
|
|
|
if (uploadResp.error == null) {
|
|
|
|
setState(() {
|
|
|
|
setState(() {
|
|
|
|
controller.text = uploadResp.result;
|
|
|
|
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>{
|
|
|
|
|
|
|
|
'url': uploadResp.result,
|
|
|
|
|
|
|
|
'info': info,
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
if (emotes == null) {
|
|
|
|
|
|
|
|
emotes = <EmoteEntry>[];
|
|
|
|
|
|
|
|
final pack = _getPack(context);
|
|
|
|
|
|
|
|
for (final entry in pack.images.entries) {
|
|
|
|
|
|
|
|
emotes
|
|
|
|
|
|
|
|
.add(EmoteEntry(emote: entry.key, mxc: entry.value.url.toString()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return EmotesSettingsView(this);
|
|
|
|
return EmotesSettingsView(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|