add window_manager plug in to get desktop active state to gate windows notifications; also add spam prevention to windows notifications
continuous-integration/drone/pr Build is failing Details

This commit is contained in:
Dan Ballard 2022-01-26 08:31:07 -08:00
parent c838176e3b
commit c3bc961a47
4 changed files with 61 additions and 8 deletions

View File

@ -12,6 +12,7 @@ import 'package:cwtch/settings.dart';
import 'package:cwtch/torstatus.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:window_manager/window_manager.dart';
import 'cwtch/cwtch.dart';
import 'cwtch/cwtchNotifier.dart';
import 'licenses.dart';
@ -45,7 +46,7 @@ class Flwtch extends StatefulWidget {
FlwtchState createState() => FlwtchState();
}
class FlwtchState extends State<Flwtch> {
class FlwtchState extends State<Flwtch> with WindowListener {
final TextStyle biggerFont = const TextStyle(fontSize: 18);
late Cwtch cwtch;
late ProfileListState profs;
@ -56,6 +57,7 @@ class FlwtchState extends State<Flwtch> {
@override
initState() {
print("initState: running...");
windowManager.addListener(this);
super.initState();
print("initState: registering notification, shutdown handlers...");
@ -203,8 +205,21 @@ class FlwtchState extends State<Flwtch> {
}
}
// using windowManager flutter plugin until proper lifecycle management lands in desktop
@override
void onWindowFocus() {
globalAppState.focus = true;
}
@override
void onWindowBlur() {
globalAppState.focus = false;
}
@override
void dispose() {
windowManager.removeListener(this);
cwtch.dispose();
super.dispose();
}

View File

@ -14,6 +14,7 @@ class AppState extends ChangeNotifier {
int? _selectedIndex;
bool _unreadMessagesBelow = false;
bool _disableFilePicker = false;
bool _focus = true;
void SetCwtchInit() {
cwtchInit = true;
@ -74,5 +75,11 @@ class AppState extends ChangeNotifier {
notifyListeners();
}
bool get focus => _focus;
set focus(bool newVal) {
_focus = newVal;
notifyListeners();
}
bool isLandscape(BuildContext c) => MediaQuery.of(c).size.width > MediaQuery.of(c).size.height;
}

View File

@ -1,5 +1,6 @@
import 'dart:io';
import 'package:cwtch/main.dart';
import 'package:desktoasts/desktoasts.dart';
import 'package:desktop_notifications/desktop_notifications.dart';
import 'package:path/path.dart' as path;
@ -22,9 +23,11 @@ class NullNotificationsManager implements NotificationsManager {
class LinuxNotificationsManager implements NotificationsManager {
int previous_id = 0;
late NotificationsClient client;
LinuxNotificationsManager(NotificationsClient client) {
this.client = client;
}
Future<void> notify(String message) async {
var iconPath = Uri.file(path.join(path.current, "cwtch.png"));
client.notify(message, appName: "cwtch", appIcon: iconPath.toString(), replacesId: this.previous_id).then((Notification value) => previous_id = value.id);
@ -35,22 +38,43 @@ class LinuxNotificationsManager implements NotificationsManager {
// windows notifications
class WindowsNotificationManager implements NotificationsManager {
late ToastService service;
bool active = false;
WindowsNotificationManager() {
service = new ToastService(
appName: 'Cwtch',
appName: 'cwtch',
companyName: 'Open Privacy Research Society',
productName: 'Cwtch',
);
service.stream.listen((event) {
if (event is ToastDismissed) {
print('Toast was dismissed.');
active = false;
}
if (event is ToastActivated) {
print('Toast was clicked.');
active = false;
}
if (event is ToastInteracted) {
print('${event.action} action in the toast was clicked.');
active = false;
}
});
}
Future<void> notify(String message) async {
Toast toast = new Toast(
type: ToastType.text01,
title: 'Cwtch',
subtitle: message,
);
service.show(toast);
if (!globalAppState.focus) {
if (!active) {
Toast toast = new Toast(
type: ToastType.text02,
title: 'Cwtch',
subtitle: message,
);
service.show(toast);
active = true;
}
}
}
}

View File

@ -474,6 +474,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.4"
window_manager:
dependency: "direct main"
description:
name: window_manager
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.4"
xdg_directories:
dependency: transitive
description: