Skip to content

Commit

Permalink
fix: Fix platform initialization to allow mocking (#2418)
Browse files Browse the repository at this point in the history
Signed-off-by: provokateurin <kate@provokateurin.de>
  • Loading branch information
provokateurin authored Oct 7, 2024
1 parent 34c1630 commit d9789d8
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,7 @@ class FlutterLocalNotificationsPlugin {
/// Factory for create an instance of [FlutterLocalNotificationsPlugin].
factory FlutterLocalNotificationsPlugin() => _instance;

FlutterLocalNotificationsPlugin._() {
if (kIsWeb) {
return;
}
if (defaultTargetPlatform == TargetPlatform.android) {
FlutterLocalNotificationsPlatform.instance =
AndroidFlutterLocalNotificationsPlugin();
} else if (defaultTargetPlatform == TargetPlatform.iOS) {
FlutterLocalNotificationsPlatform.instance =
IOSFlutterLocalNotificationsPlugin();
} else if (defaultTargetPlatform == TargetPlatform.macOS) {
FlutterLocalNotificationsPlatform.instance =
MacOSFlutterLocalNotificationsPlugin();
}
}
FlutterLocalNotificationsPlugin._();

static final FlutterLocalNotificationsPlugin _instance =
FlutterLocalNotificationsPlugin._();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ class MethodChannelFlutterLocalNotificationsPlugin
/// Android implementation of the local notifications plugin.
class AndroidFlutterLocalNotificationsPlugin
extends MethodChannelFlutterLocalNotificationsPlugin {
/// Registers this implementation as the plugin instance.
static void registerWith() {
FlutterLocalNotificationsPlatform.instance =
AndroidFlutterLocalNotificationsPlugin();
}

DidReceiveNotificationResponseCallback? _ondidReceiveNotificationResponse;

/// Initializes the plugin.
Expand Down Expand Up @@ -596,6 +602,12 @@ class AndroidFlutterLocalNotificationsPlugin
/// iOS implementation of the local notifications plugin.
class IOSFlutterLocalNotificationsPlugin
extends MethodChannelFlutterLocalNotificationsPlugin {
/// Registers this implementation as the plugin instance.
static void registerWith() {
FlutterLocalNotificationsPlatform.instance =
IOSFlutterLocalNotificationsPlugin();
}

DidReceiveNotificationResponseCallback? _onDidReceiveNotificationResponse;
DidReceiveLocalNotificationCallback? _onDidReceiveLocalNotification;

Expand Down Expand Up @@ -827,6 +839,12 @@ class IOSFlutterLocalNotificationsPlugin
/// macOS implementation of the local notifications plugin.
class MacOSFlutterLocalNotificationsPlugin
extends MethodChannelFlutterLocalNotificationsPlugin {
/// Registers this implementation as the plugin instance.
static void registerWith() {
FlutterLocalNotificationsPlatform.instance =
MacOSFlutterLocalNotificationsPlugin();
}

DidReceiveNotificationResponseCallback? _onDidReceiveNotificationResponse;

/// Initializes the plugin.
Expand Down
3 changes: 3 additions & 0 deletions flutter_local_notifications/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ flutter:
android:
package: com.dexterous.flutterlocalnotifications
pluginClass: FlutterLocalNotificationsPlugin
dartPluginClass: AndroidFlutterLocalNotificationsPlugin
ios:
pluginClass: FlutterLocalNotificationsPlugin
dartPluginClass: IOSFlutterLocalNotificationsPlugin
macos:
pluginClass: FlutterLocalNotificationsPlugin
dartPluginClass: MacOSFlutterLocalNotificationsPlugin
linux:
default_package: flutter_local_notifications_linux

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:timezone/timezone.dart' as tz;
import 'utils/date_formatter.dart';

void main() {
AndroidFlutterLocalNotificationsPlugin.registerWith();
TestWidgetsFlutterBinding.ensureInitialized();
late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:timezone/timezone.dart' as tz;
import 'utils/date_formatter.dart';

void main() {
IOSFlutterLocalNotificationsPlugin.registerWith();
TestWidgetsFlutterBinding.ensureInitialized();
late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:timezone/timezone.dart' as tz;
import 'utils/date_formatter.dart';

void main() {
MacOSFlutterLocalNotificationsPlugin.registerWith();
TestWidgetsFlutterBinding.ensureInitialized();
late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
group('macOS', () {
Expand Down

0 comments on commit d9789d8

Please sign in to comment.