Mike Schwörer b91ddc172d
Some checks failed
Build Docker and Deploy / Build Docker Container (push) Successful in 51s
Build Docker and Deploy / Run Unit-Tests (push) Failing after 11m17s
Build Docker and Deploy / Deploy to Server (push) Has been skipped
Implement settings
2025-04-19 02:02:37 +02:00

148 lines
5.7 KiB
Dart

import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:simplecloudnotifier/api/api_client.dart';
import 'package:simplecloudnotifier/state/app_settings.dart';
import 'package:simplecloudnotifier/state/app_auth.dart';
import 'package:simplecloudnotifier/state/application_log.dart';
import 'package:simplecloudnotifier/utils/notifier.dart';
import 'package:simplecloudnotifier/utils/toaster.dart';
import 'package:simplecloudnotifier/utils/ui.dart';
class DebugActionsPage extends StatefulWidget {
@override
_DebugActionsPageState createState() => _DebugActionsPageState();
}
class _DebugActionsPageState extends State<DebugActionsPage> {
@override
Widget build(BuildContext context) {
return Container(
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
UI.button(
big: false,
onPressed: () => Toaster.success("Hello World", "This was a triumph!"),
text: 'Show Success Notification',
),
SizedBox(height: 4),
UI.button(
big: false,
onPressed: () => Toaster.info("Hello World", "This was a triumph!"),
text: 'Show Info Notification',
),
SizedBox(height: 4),
UI.button(
big: false,
onPressed: () => Toaster.warn("Hello World", "This was a triumph!"),
text: 'Show Warn Notification',
),
SizedBox(height: 4),
UI.button(
big: false,
onPressed: () => Toaster.error("Hello World", "This was a triumph!"),
text: 'Show Info Notification',
),
SizedBox(height: 4),
UI.button(
big: false,
onPressed: () => Toaster.simple("Hello World"),
text: 'Show Simple Notification',
),
SizedBox(height: 20),
UI.button(
big: false,
onPressed: _copyToken,
text: 'Query+Copy FCM Token',
),
SizedBox(height: 4),
UI.button(
big: false,
onPressed: _sendTokenToServer,
text: 'Send FCM Token to Server',
),
SizedBox(height: 20),
UI.button(
big: false,
onPressed: () => Notifier.showLocalNotification('', 'TEST_CHANNEL', "Test Channel", "Channel for testing", "Hello World", "Local Notification test", null, null),
text: 'Show local notification (generic)',
),
UI.button(
big: false,
onPressed: () => Notifier.showLocalNotification('', 'TEST_CHANNEL', "Test Channel", "Channel for testing", "Hello World", "Local Notification test", null, 0),
text: 'Show local notification (Prio = 0)',
),
UI.button(
big: false,
onPressed: () => Notifier.showLocalNotification('', 'TEST_CHANNEL', "Test Channel", "Channel for testing", "Hello World", "Local Notification test", null, 1),
text: 'Show local notification (Prio = 1)',
),
UI.button(
big: false,
onPressed: () => Notifier.showLocalNotification('', 'TEST_CHANNEL', "Test Channel", "Channel for testing", "Hello World", "Local Notification test", null, 2),
text: 'Show local notification (Prio = 2)',
),
SizedBox(height: 20),
UI.button(
big: false,
onPressed: () {
AppSettings().update((p) => p.reset());
Toaster.success("Success", "AppSettings reset to default");
},
text: 'Reset AppSettings to default',
),
],
),
),
),
);
}
void _sendTokenToServer() async {
try {
final auth = AppAuth();
final clientID = auth.getClientID();
if (clientID == null) {
Toaster.error("Error", "No Client set");
return;
}
final fcmToken = await FirebaseMessaging.instance.getToken();
if (fcmToken == null) {
Toaster.error("Error", "No FCM token returned from Firebase");
return;
}
var newClient = await APIClient.updateClient(auth, clientID, fcmToken: fcmToken);
auth.setClientAndClientID(newClient);
Toaster.success("Success", "Token sent to server");
} catch (exc, trace) {
Toaster.error("Error", "An error occurred while sending the token: ${exc.toString()}");
ApplicationLog.error("An error occurred while sending the token: ${exc.toString()}", trace: trace);
}
}
void _copyToken() async {
try {
final fcmToken = await FirebaseMessaging.instance.getToken();
if (fcmToken == null) {
Toaster.error("Error", "No FCM token returned from Firebase");
return;
}
Clipboard.setData(new ClipboardData(text: fcmToken));
Toaster.info("Clipboard", 'Copied text to Clipboard');
print('================= [CLIPBOARD] =================\n${fcmToken}\n================= [/CLIPBOARD] =================');
} catch (exc, trace) {
Toaster.error("Error", "An error occurred while sending the token: ${exc.toString()}");
ApplicationLog.error("An error occurred while sending the token: ${exc.toString()}", trace: trace);
}
}
}