feat: support import of Emoji packs as zip file
Signed-off-by: The one with the braid <the-one@with-the-braid.cf> Signed-off-by: TheOneWithTheBraid <the-one@with-the-braid.cf>
This commit is contained in:
parent
54ea513f36
commit
0c70017cd8
6 changed files with 493 additions and 4 deletions
|
|
@ -1,3 +1,7 @@
|
|||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:adaptive_dialog/adaptive_dialog.dart';
|
||||
|
|
@ -5,13 +9,21 @@ import 'package:collection/collection.dart';
|
|||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
||||
import 'package:http/http.dart' hide Client;
|
||||
import 'package:matrix/matrix.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:share_plus/share_plus.dart';
|
||||
import 'package:vrouter/vrouter.dart';
|
||||
|
||||
import 'package:fluffychat/utils/client_manager.dart';
|
||||
import 'package:fluffychat/utils/platform_infos.dart';
|
||||
import '../../widgets/matrix.dart';
|
||||
import 'import_archive_dialog.dart';
|
||||
import 'settings_emotes_view.dart';
|
||||
|
||||
import 'package:archive/archive.dart'
|
||||
if (dart.library.io) 'package:archive/archive_io.dart';
|
||||
|
||||
class EmotesSettings extends StatefulWidget {
|
||||
const EmotesSettings({Key? key}) : super(key: key);
|
||||
|
||||
|
|
@ -21,8 +33,10 @@ class EmotesSettings extends StatefulWidget {
|
|||
|
||||
class EmotesSettingsController extends State<EmotesSettings> {
|
||||
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'];
|
||||
|
||||
bool showSave = false;
|
||||
|
|
@ -44,6 +58,7 @@ class EmotesSettingsController extends State<EmotesSettings> {
|
|||
}
|
||||
|
||||
ImagePackContent? _pack;
|
||||
|
||||
ImagePackContent? get pack {
|
||||
if (_pack != null) {
|
||||
return _pack;
|
||||
|
|
@ -52,7 +67,7 @@ class EmotesSettingsController extends State<EmotesSettings> {
|
|||
return _pack;
|
||||
}
|
||||
|
||||
Future<void> _save(BuildContext context) async {
|
||||
Future<void> save(BuildContext context) async {
|
||||
if (readonly) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -161,7 +176,7 @@ class EmotesSettingsController extends State<EmotesSettings> {
|
|||
room == null ? false : !(room!.canSendEvent('im.ponies.room_emotes'));
|
||||
|
||||
void saveAction() async {
|
||||
await _save(context);
|
||||
await save(context);
|
||||
setState(() {
|
||||
showSave = false;
|
||||
});
|
||||
|
|
@ -198,7 +213,7 @@ class EmotesSettingsController extends State<EmotesSettings> {
|
|||
return;
|
||||
}
|
||||
pack!.images[imageCode] = newImageController.value!;
|
||||
await _save(context);
|
||||
await save(context);
|
||||
setState(() {
|
||||
newImageCodeController.text = '';
|
||||
newImageController.value = null;
|
||||
|
|
@ -262,4 +277,99 @@ class EmotesSettingsController extends State<EmotesSettings> {
|
|||
Widget build(BuildContext context) {
|
||||
return EmotesSettingsView(this);
|
||||
}
|
||||
|
||||
Future<void> importEmojiZip() async {
|
||||
final result = await showFutureLoadingDialog<Archive?>(
|
||||
context: context,
|
||||
future: () async {
|
||||
final result = await FilePicker.platform.pickFiles(
|
||||
type: FileType.custom,
|
||||
allowedExtensions: [
|
||||
'zip',
|
||||
// TODO: add further encoders
|
||||
],
|
||||
// TODO: migrate to stream, currently brrrr because of `archive_io`.
|
||||
withData: true,
|
||||
);
|
||||
|
||||
if (result == null) return null;
|
||||
|
||||
final buffer = InputStream(result.files.single.bytes);
|
||||
|
||||
final archive = ZipDecoder().decodeBuffer(buffer);
|
||||
|
||||
return archive;
|
||||
},
|
||||
);
|
||||
|
||||
final archive = result.result;
|
||||
if (archive == null) return;
|
||||
|
||||
await showDialog(
|
||||
context: context,
|
||||
builder: (context) => ImportEmoteArchiveDialog(
|
||||
controller: this,
|
||||
archive: archive,
|
||||
),
|
||||
);
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
Future<void> exportAsZip() async {
|
||||
final client = Matrix.of(context).client;
|
||||
|
||||
await showFutureLoadingDialog(
|
||||
context: context,
|
||||
future: () async {
|
||||
final pack = _getPack();
|
||||
final archive = Archive();
|
||||
for (final entry in pack.images.entries) {
|
||||
final emote = entry.value;
|
||||
final name = entry.key;
|
||||
final url = emote.url.getDownloadLink(client);
|
||||
final response = await get(url);
|
||||
|
||||
archive.addFile(
|
||||
ArchiveFile(
|
||||
name,
|
||||
response.bodyBytes.length,
|
||||
response.bodyBytes,
|
||||
),
|
||||
);
|
||||
}
|
||||
final fileName =
|
||||
'${pack.pack.displayName ?? client.userID?.localpart ?? 'emotes'}.zip';
|
||||
final output = ZipEncoder().encode(archive);
|
||||
|
||||
if (output == null) return;
|
||||
|
||||
if (kIsWeb || PlatformInfos.isMobile) {
|
||||
await Share.shareXFiles(
|
||||
[XFile(fileName, bytes: Uint8List.fromList(output))],
|
||||
);
|
||||
} else {
|
||||
String? savePath = await FilePicker.platform
|
||||
.saveFile(fileName: fileName, allowedExtensions: ['zip']);
|
||||
|
||||
if (savePath == null) {
|
||||
// workaround for broken `xdg-desktop-portal-termfilechooser`
|
||||
if (PlatformInfos.isLinux) {
|
||||
final dir = await getDownloadsDirectory();
|
||||
if (dir == null) return;
|
||||
savePath = dir.uri.resolve(fileName).toFilePath();
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
final file = File(savePath);
|
||||
await file.writeAsBytes(output);
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(L10n.of(context)!.savedEmotePack(savePath))),
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue