Skip to content

Commit

Permalink
add local_notifier plugin and show local notification on update avail…
Browse files Browse the repository at this point in the history
…able
  • Loading branch information
levkropp committed Oct 15, 2024
1 parent 521a43e commit 88645ec
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Build-Depends: build-essential,
curl,
google-mock,
libapparmor-dev,
libnotify-dev,
libvirt-dev,
libsystemd-dev,
pkg-config,
Expand Down
6 changes: 6 additions & 0 deletions src/client/gui/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:hotkey_manager/hotkey_manager.dart';
import 'package:local_notifier/local_notifier.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:window_manager/window_manager.dart';
import 'package:window_size/window_size.dart';
Expand All @@ -24,6 +25,11 @@ void main() async {

await setupLogger();

await localNotifier.setup(
appName: 'Multipass',
shortcutPolicy: ShortcutPolicy.requireCreate, // Only for Windows
);

// Get the current screen size
final screenSize = await getCurrentScreen().then((screen) {
return screen?.frame.size;
Expand Down
3 changes: 3 additions & 0 deletions src/client/gui/lib/platform/linux.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class LinuxPlatform extends MpPlatform {
@override
String get ffiLibraryName => 'libdart_ffi.so';

@override
bool get showLocalUpdateNotifications => false;

@override
bool get showToggleWindow => true;

Expand Down
3 changes: 3 additions & 0 deletions src/client/gui/lib/platform/macos.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class MacOSPlatform extends MpPlatform {
@override
String get ffiLibraryName => 'libdart_ffi.dylib';

@override
bool get showLocalUpdateNotifications => true;

@override
bool get showToggleWindow => false;

Expand Down
2 changes: 2 additions & 0 deletions src/client/gui/lib/platform/platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ abstract class MpPlatform {

Map<SingleActivator, Intent> get terminalShortcuts;

bool get showLocalUpdateNotifications;

bool get showToggleWindow;

String get altKey => 'Alt';
Expand Down
3 changes: 3 additions & 0 deletions src/client/gui/lib/platform/windows.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class WindowsPlatform extends MpPlatform {
@override
String get ffiLibraryName => 'dart_ffi.dll';

@override
bool get showLocalUpdateNotifications => true;

@override
bool get showToggleWindow => true;

Expand Down
26 changes: 26 additions & 0 deletions src/client/gui/lib/update_available.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import 'package:basics/basics.dart';
import 'package:flutter/material.dart';
import 'package:local_notifier/local_notifier.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/svg.dart';
import 'package:url_launcher/url_launcher.dart';

import 'notifications/notification_entries.dart';
import 'notifications/notifications_list.dart';
import 'notifications/notifications_provider.dart';
import 'platform/platform.dart';
import 'providers.dart';

class UpdateNotifier extends Notifier<UpdateInfo> {
Expand All @@ -15,14 +17,38 @@ class UpdateNotifier extends Notifier<UpdateInfo> {

void set(UpdateInfo updateInfo) {
if (updateInfo.version.isBlank) return;

final updateNotificationExists = ref.read(notificationsProvider).any((n) {
return n is UpdateAvailableNotification && n.updateInfo == updateInfo;
});
if (updateNotificationExists) return;

// In-app notification
ref
.read(notificationsProvider.notifier)
.add(UpdateAvailableNotification(updateInfo));

// Update the state
state = updateInfo;

// Create and show a local notification
_showLocalNotification(updateInfo);
}

void _showLocalNotification(UpdateInfo updateInfo) {
if (!mpPlatform.showLocalUpdateNotifications) return;

final notification = LocalNotification(
title: 'Multipass Update Available',
body: 'Version ${updateInfo.version} is available. Click to upgrade now.',
);

notification.onClick = () async {
await launchInstallUrl();
await notification.close();
};

notification.show();
}

@override
Expand Down
4 changes: 4 additions & 0 deletions src/client/gui/linux/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "generated_plugin_registrant.h"

#include <hotkey_manager_linux/hotkey_manager_linux_plugin.h>
#include <local_notifier/local_notifier_plugin.h>
#include <screen_retriever/screen_retriever_plugin.h>
#include <tray_menu/tray_menu_plugin.h>
#include <url_launcher_linux/url_launcher_plugin.h>
Expand All @@ -17,6 +18,9 @@ void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) hotkey_manager_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "HotkeyManagerLinuxPlugin");
hotkey_manager_linux_plugin_register_with_registrar(hotkey_manager_linux_registrar);
g_autoptr(FlPluginRegistrar) local_notifier_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "LocalNotifierPlugin");
local_notifier_plugin_register_with_registrar(local_notifier_registrar);
g_autoptr(FlPluginRegistrar) screen_retriever_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "ScreenRetrieverPlugin");
screen_retriever_plugin_register_with_registrar(screen_retriever_registrar);
Expand Down
1 change: 1 addition & 0 deletions src/client/gui/linux/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

list(APPEND FLUTTER_PLUGIN_LIST
hotkey_manager_linux
local_notifier
screen_retriever
tray_menu
url_launcher_linux
Expand Down
2 changes: 2 additions & 0 deletions src/client/gui/macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import FlutterMacOS
import Foundation

import hotkey_manager_macos
import local_notifier
import path_provider_foundation
import screen_retriever
import shared_preferences_foundation
Expand All @@ -16,6 +17,7 @@ import window_size

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
HotkeyManagerMacosPlugin.register(with: registry.registrar(forPlugin: "HotkeyManagerMacosPlugin"))
LocalNotifierPlugin.register(with: registry.registrar(forPlugin: "LocalNotifierPlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
ScreenRetrieverPlugin.register(with: registry.registrar(forPlugin: "ScreenRetrieverPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
Expand Down
8 changes: 8 additions & 0 deletions src/client/gui/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "5.0.0"
local_notifier:
dependency: "direct main"
description:
name: local_notifier
sha256: f6cfc933c6fbc961f4e52b5c880f68e41b2d3cd29aad557cc654fd211093a025
url: "https://pub.dev"
source: hosted
version: "0.1.6"
logger:
dependency: "direct main"
description:
Expand Down
1 change: 1 addition & 0 deletions src/client/gui/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies:
grpc: ^4.0.1
hotkey_manager: ^0.2.3
intl: ^0.19.0
local_notifier: ^0.1.6
logger: ^2.4.0
path_provider: ^2.1.4
protobuf: ^3.1.0
Expand Down
3 changes: 3 additions & 0 deletions src/client/gui/windows/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "generated_plugin_registrant.h"

#include <hotkey_manager_windows/hotkey_manager_windows_plugin_c_api.h>
#include <local_notifier/local_notifier_plugin.h>
#include <screen_retriever/screen_retriever_plugin.h>
#include <tray_menu/tray_menu_plugin_c_api.h>
#include <url_launcher_windows/url_launcher_windows.h>
Expand All @@ -16,6 +17,8 @@
void RegisterPlugins(flutter::PluginRegistry* registry) {
HotkeyManagerWindowsPluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("HotkeyManagerWindowsPluginCApi"));
LocalNotifierPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("LocalNotifierPlugin"));
ScreenRetrieverPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("ScreenRetrieverPlugin"));
TrayMenuPluginCApiRegisterWithRegistrar(
Expand Down
1 change: 1 addition & 0 deletions src/client/gui/windows/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

list(APPEND FLUTTER_PLUGIN_LIST
hotkey_manager_windows
local_notifier
screen_retriever
tray_menu
url_launcher_windows
Expand Down

0 comments on commit 88645ec

Please sign in to comment.