2022-10-04 21:37:32 +00:00
|
|
|
import 'dart:io';
|
2023-07-27 18:45:09 +00:00
|
|
|
import 'dart:math';
|
2022-10-04 21:37:32 +00:00
|
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:file_picker/file_picker.dart';
|
|
|
|
import 'package:moxplatform/moxplatform.dart';
|
2023-07-27 18:45:09 +00:00
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
|
|
|
|
/// The id of the notification channel.
|
|
|
|
const channelId = "me.polynom.moxplatform.testing3";
|
2023-07-29 11:12:41 +00:00
|
|
|
const otherChannelId = "me.polynom.moxplatform.testing4";
|
2022-10-04 21:37:32 +00:00
|
|
|
|
|
|
|
void main() {
|
|
|
|
runApp(const MyApp());
|
|
|
|
}
|
|
|
|
|
2023-07-27 18:45:09 +00:00
|
|
|
class Sender {
|
|
|
|
const Sender(this.name, this.jid);
|
|
|
|
|
|
|
|
final String name;
|
|
|
|
|
|
|
|
final String jid;
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyApp extends StatefulWidget {
|
2022-10-04 21:37:32 +00:00
|
|
|
const MyApp({Key? key}) : super(key: key);
|
|
|
|
|
2023-07-27 18:45:09 +00:00
|
|
|
@override
|
|
|
|
MyAppState createState() => MyAppState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyAppState extends State<MyApp> {
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
|
|
|
|
initStateAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> initStateAsync() async {
|
|
|
|
await Permission.notification.request();
|
|
|
|
|
2023-07-28 15:32:14 +00:00
|
|
|
await MoxplatformPlugin.notifications.createNotificationChannel(
|
|
|
|
"Test notification channel",
|
2023-07-30 18:40:59 +00:00
|
|
|
"Test1",
|
2023-07-28 15:32:14 +00:00
|
|
|
channelId,
|
|
|
|
false,
|
2023-07-29 10:34:40 +00:00
|
|
|
);
|
2023-07-29 11:12:41 +00:00
|
|
|
await MoxplatformPlugin.notifications.createNotificationChannel(
|
|
|
|
"Test notification channel for warnings",
|
2023-07-30 18:40:59 +00:00
|
|
|
"Test2",
|
2023-07-29 11:12:41 +00:00
|
|
|
otherChannelId,
|
|
|
|
false,
|
|
|
|
);
|
2023-07-29 10:34:40 +00:00
|
|
|
await MoxplatformPlugin.notifications.setI18n(
|
2023-07-28 15:32:14 +00:00
|
|
|
NotificationI18nData(
|
|
|
|
reply: "答える",
|
|
|
|
markAsRead: "読みた",
|
|
|
|
you: "あなた",
|
|
|
|
),
|
|
|
|
);
|
2023-07-28 11:54:57 +00:00
|
|
|
|
|
|
|
MoxplatformPlugin.notifications.getEventStream().listen((event) {
|
2023-08-04 11:38:11 +00:00
|
|
|
// ignore: avoid_print
|
|
|
|
print(
|
|
|
|
'NotificationEvent(type: ${event.type}, jid: ${event.jid}, payload: ${event.payload}, extras: ${event.extra})',
|
|
|
|
);
|
2023-07-28 11:54:57 +00:00
|
|
|
});
|
2023-07-27 18:45:09 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 21:37:32 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return MaterialApp(
|
2023-07-21 15:30:59 +00:00
|
|
|
title: 'Moxplatform Demo',
|
2022-10-04 21:37:32 +00:00
|
|
|
theme: ThemeData(
|
|
|
|
primarySwatch: Colors.blue,
|
|
|
|
),
|
2023-08-04 11:38:11 +00:00
|
|
|
home: const MyHomePage(),
|
2022-10-04 21:37:32 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-04 11:38:11 +00:00
|
|
|
class MyHomePage extends StatefulWidget {
|
|
|
|
const MyHomePage({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
MyHomePageState createState() => MyHomePageState();
|
|
|
|
}
|
2023-07-27 18:45:09 +00:00
|
|
|
|
2023-08-04 11:38:11 +00:00
|
|
|
class MyHomePageState extends State<MyHomePage> {
|
2023-07-27 18:45:09 +00:00
|
|
|
/// List of "Message senders".
|
|
|
|
final List<Sender> senders = const [
|
|
|
|
Sender('Mash Kyrielight', 'mash@example.org'),
|
|
|
|
Sender('Rio Tsukatsuki', 'rio@millenium'),
|
|
|
|
Sender('Raiden Shogun', 'raiden@tevhat'),
|
|
|
|
];
|
|
|
|
|
|
|
|
/// List of sent messages.
|
2023-08-04 11:38:11 +00:00
|
|
|
List<NotificationMessage> messages =
|
|
|
|
List<NotificationMessage>.empty(growable: true);
|
2022-10-04 21:37:32 +00:00
|
|
|
|
2023-07-21 15:30:59 +00:00
|
|
|
Future<void> _cryptoTest() async {
|
2022-10-04 21:37:32 +00:00
|
|
|
final result = await FilePicker.platform.pickFiles();
|
|
|
|
if (result == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-05 13:05:43 +00:00
|
|
|
final start = DateTime.now();
|
2022-10-04 21:37:32 +00:00
|
|
|
final path = result.files.single.path;
|
2022-10-05 13:05:43 +00:00
|
|
|
final enc = await MoxplatformPlugin.crypto.encryptFile(
|
2022-10-04 21:37:32 +00:00
|
|
|
path!,
|
2023-07-21 15:30:59 +00:00
|
|
|
'$path.enc',
|
2022-10-04 21:37:32 +00:00
|
|
|
Uint8List.fromList(List.filled(32, 1)),
|
|
|
|
Uint8List.fromList(List.filled(16, 2)),
|
|
|
|
CipherAlgorithm.aes256CbcPkcs7,
|
2022-10-05 13:05:43 +00:00
|
|
|
'SHA-256',
|
2022-10-04 21:37:32 +00:00
|
|
|
);
|
2022-10-05 13:05:43 +00:00
|
|
|
final end = DateTime.now();
|
|
|
|
|
|
|
|
final diff = end.millisecondsSinceEpoch - start.millisecondsSinceEpoch;
|
2023-08-04 11:38:11 +00:00
|
|
|
// ignore: avoid_print
|
2022-10-05 13:05:43 +00:00
|
|
|
print('TIME: ${diff / 1000}s');
|
2023-08-04 11:38:11 +00:00
|
|
|
// ignore: avoid_print
|
2022-10-05 13:05:43 +00:00
|
|
|
print('DONE (${enc != null})');
|
2023-07-21 15:30:59 +00:00
|
|
|
final lengthEnc = await File('$path.enc').length();
|
2022-10-04 21:37:32 +00:00
|
|
|
final lengthOrig = await File(path).length();
|
2023-08-04 11:38:11 +00:00
|
|
|
// ignore: avoid_print
|
2022-10-04 21:37:32 +00:00
|
|
|
print('Encrypted file is $lengthEnc Bytes large (Orig $lengthOrig)');
|
|
|
|
|
|
|
|
await MoxplatformPlugin.crypto.decryptFile(
|
2023-07-21 15:30:59 +00:00
|
|
|
'$path.enc',
|
|
|
|
'$path.dec',
|
2022-10-04 21:37:32 +00:00
|
|
|
Uint8List.fromList(List.filled(32, 1)),
|
|
|
|
Uint8List.fromList(List.filled(16, 2)),
|
|
|
|
CipherAlgorithm.aes256CbcPkcs7,
|
2022-10-05 13:05:43 +00:00
|
|
|
'SHA-256',
|
2022-10-04 21:37:32 +00:00
|
|
|
);
|
2023-08-04 11:38:11 +00:00
|
|
|
// ignore: avoid_print
|
2022-10-04 21:37:32 +00:00
|
|
|
print('DONE');
|
|
|
|
|
2023-07-21 15:30:59 +00:00
|
|
|
final lengthDec = await File('$path.dec').length();
|
2023-08-04 11:38:11 +00:00
|
|
|
// ignore: avoid_print
|
2022-10-04 21:37:32 +00:00
|
|
|
print('Decrypted file is $lengthDec Bytes large (Orig $lengthOrig)');
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
2023-07-21 15:30:59 +00:00
|
|
|
title: const Text('Moxplatform Demo'),
|
2022-10-04 21:37:32 +00:00
|
|
|
),
|
|
|
|
body: Center(
|
2023-07-29 10:34:40 +00:00
|
|
|
child: ListView(
|
|
|
|
children: [
|
2023-07-21 15:30:59 +00:00
|
|
|
ElevatedButton(
|
|
|
|
onPressed: _cryptoTest,
|
|
|
|
child: const Text('Test cryptography'),
|
2022-10-04 21:37:32 +00:00
|
|
|
),
|
2023-07-21 11:04:44 +00:00
|
|
|
ElevatedButton(
|
|
|
|
onPressed: () {
|
|
|
|
MoxplatformPlugin.contacts.recordSentMessage('Hallo', 'Welt');
|
|
|
|
},
|
2023-07-21 15:30:59 +00:00
|
|
|
child: const Text('Test recordSentMessage (no fallback)'),
|
2023-07-21 11:04:44 +00:00
|
|
|
),
|
|
|
|
ElevatedButton(
|
|
|
|
onPressed: () {
|
2023-08-04 11:38:11 +00:00
|
|
|
MoxplatformPlugin.contacts.recordSentMessage('Person', 'Person',
|
|
|
|
fallbackIcon: FallbackIconType.person);
|
2023-07-21 11:04:44 +00:00
|
|
|
},
|
2023-07-21 15:30:59 +00:00
|
|
|
child: const Text('Test recordSentMessage (person fallback)'),
|
2023-07-21 11:04:44 +00:00
|
|
|
),
|
|
|
|
ElevatedButton(
|
|
|
|
onPressed: () {
|
2023-08-04 11:38:11 +00:00
|
|
|
MoxplatformPlugin.contacts.recordSentMessage('Notes', 'Notes',
|
|
|
|
fallbackIcon: FallbackIconType.notes);
|
2023-07-21 11:04:44 +00:00
|
|
|
},
|
2023-07-21 15:30:59 +00:00
|
|
|
child: const Text('Test recordSentMessage (notes fallback)'),
|
2023-07-21 11:04:44 +00:00
|
|
|
),
|
2023-07-27 18:45:09 +00:00
|
|
|
ElevatedButton(
|
|
|
|
onPressed: () async {
|
|
|
|
final result = await FilePicker.platform.pickFiles(
|
|
|
|
type: FileType.image,
|
|
|
|
);
|
2023-08-04 11:38:11 +00:00
|
|
|
// ignore: avoid_print
|
2023-07-27 18:45:09 +00:00
|
|
|
print('Picked file: ${result?.files.single.path}');
|
|
|
|
|
|
|
|
// Create a new message.
|
|
|
|
final senderIndex = Random().nextInt(senders.length);
|
|
|
|
final time = DateTime.now().millisecondsSinceEpoch;
|
2023-08-04 11:38:11 +00:00
|
|
|
messages.add(NotificationMessage(
|
|
|
|
jid: senders[senderIndex].jid,
|
|
|
|
sender: senders[senderIndex].name,
|
|
|
|
content: NotificationMessageContent(
|
|
|
|
body: result != null ? null : 'Message #${messages.length}',
|
|
|
|
mime: 'image/jpeg',
|
|
|
|
path: result?.files.single.path,
|
|
|
|
),
|
|
|
|
timestamp: time,
|
|
|
|
));
|
2023-07-27 18:45:09 +00:00
|
|
|
|
|
|
|
await Future<void>.delayed(const Duration(seconds: 4));
|
|
|
|
await MoxplatformPlugin.notifications.showMessagingNotification(
|
|
|
|
MessagingNotification(
|
|
|
|
id: 2343,
|
|
|
|
title: 'Test conversation',
|
|
|
|
messages: messages,
|
|
|
|
channelId: channelId,
|
2023-07-28 10:46:02 +00:00
|
|
|
jid: 'testjid',
|
2023-07-30 18:40:59 +00:00
|
|
|
isGroupchat: true,
|
2023-07-29 13:32:33 +00:00
|
|
|
extra: {
|
|
|
|
'jid': 'testjid',
|
|
|
|
'avatarPath': 'lol',
|
|
|
|
'rio': 'cute',
|
|
|
|
},
|
2023-07-27 18:45:09 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
child: const Text('Show messaging notification'),
|
|
|
|
),
|
2023-07-29 11:12:41 +00:00
|
|
|
ElevatedButton(
|
|
|
|
onPressed: () {
|
|
|
|
MoxplatformPlugin.notifications.showNotification(
|
|
|
|
RegularNotification(
|
|
|
|
id: 4384,
|
|
|
|
title: 'Warning',
|
|
|
|
body: 'Something brokey',
|
|
|
|
channelId: otherChannelId,
|
|
|
|
icon: NotificationIcon.warning,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
child: const Text('Show warning notification'),
|
|
|
|
),
|
|
|
|
ElevatedButton(
|
|
|
|
onPressed: () {
|
|
|
|
MoxplatformPlugin.notifications.showNotification(
|
|
|
|
RegularNotification(
|
|
|
|
id: 4384,
|
|
|
|
title: 'Error',
|
|
|
|
body: "Lol, you're on your own",
|
|
|
|
channelId: otherChannelId,
|
|
|
|
icon: NotificationIcon.error,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
child: const Text('Show error notification'),
|
|
|
|
),
|
2023-07-28 19:46:47 +00:00
|
|
|
ElevatedButton(
|
|
|
|
onPressed: () async {
|
|
|
|
final result = await FilePicker.platform.pickFiles(
|
|
|
|
type: FileType.image,
|
|
|
|
);
|
|
|
|
if (result == null) return;
|
|
|
|
|
2023-08-04 11:38:11 +00:00
|
|
|
MoxplatformPlugin.notifications
|
|
|
|
.setNotificationSelfAvatar(result.files.single.path!);
|
2023-07-28 19:46:47 +00:00
|
|
|
},
|
|
|
|
child: const Text('Set notification self-avatar'),
|
|
|
|
),
|
2023-07-27 22:46:19 +00:00
|
|
|
ElevatedButton(
|
|
|
|
onPressed: () async {
|
2023-08-04 11:38:11 +00:00
|
|
|
// ignore: avoid_print
|
2023-07-27 22:46:19 +00:00
|
|
|
print(await MoxplatformPlugin.platform.getPersistentDataPath());
|
|
|
|
},
|
|
|
|
child: const Text('Get data directory'),
|
|
|
|
),
|
|
|
|
ElevatedButton(
|
|
|
|
onPressed: () async {
|
2023-08-04 11:38:11 +00:00
|
|
|
// ignore: avoid_print
|
2023-07-27 22:46:19 +00:00
|
|
|
print(await MoxplatformPlugin.platform.getCacheDataPath());
|
|
|
|
},
|
|
|
|
child: const Text('Get cache directory'),
|
|
|
|
),
|
2022-10-04 21:37:32 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|