Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/flutter_app_badge_control_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,38 @@ class MethodChannelFlutterAppBadgeControl

@override
Future<String?> getPlatformVersion() async {
if (kIsWeb) {
return 'Web';
}
final version =
await methodChannel.invokeMethod<String>('getPlatformVersion');
return version;
}

@override
Future<void> updateBadgeCount(int count) async {
if (kIsWeb) {
// Web doesn't support app badge count
return;
}
return await methodChannel.invokeMethod<void>('updateBadgeCount', count);
}

@override
Future<void> removeBadge() async {
if (kIsWeb) {
// Web doesn't support app badge removal
return;
}
return await methodChannel.invokeMethod<void>('removeBadge');
}

@override
Future<bool> isAppBadgeSupported() async {
if (kIsWeb) {
// Web doesn't support app badges
return false;
}
final isSupported =
await methodChannel.invokeMethod<bool>('isAppBadgeSupported');
return isSupported ?? false;
Expand Down
41 changes: 41 additions & 0 deletions macos/Classes/FlutterAppBadgeControlPlugin.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Cocoa
import FlutterMacOS

public class FlutterAppBadgeControlPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "flutter_app_badge_control", binaryMessenger: registrar.messenger)
let instance = FlutterAppBadgeControlPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "getPlatformVersion":
result("macOS " + ProcessInfo.processInfo.operatingSystemVersionString)
case "updateBadgeCount":
if let count = call.arguments as? Int {
updateBadgeCount(count: count)
result(nil)
} else {
result(FlutterError(code: "INVALID_ARGUMENT", message: "Invalid count value", details: nil))
}
case "removeBadge":
updateBadgeCount(count: 0)
result(nil)
case "isAppBadgeSupported":
result(true)
default:
result(FlutterMethodNotImplemented)
}
}

private func updateBadgeCount(count: Int) {
DispatchQueue.main.async {
if count > 0 {
NSApp.dockTile.badgeLabel = "\(count)"
} else {
NSApp.dockTile.badgeLabel = nil
}
}
}
}
22 changes: 22 additions & 0 deletions macos/flutter_app_badge_control.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html.
# Run `pod lib lint flutter_app_badge_control.podspec` to validate before publishing.
#
Pod::Spec.new do |s|
s.name = 'flutter_app_badge_control'
s.version = '0.0.1'
s.summary = 'A new Flutter plugin project.'
s.description = <<-DESC
A new Flutter plugin project.
DESC
s.homepage = 'http://example.com'
s.license = { :file => '../LICENSE' }
s.author = { 'Your Company' => 'email@example.com' }
s.source = { :path => '.' }
s.source_files = 'Classes/**/*'
s.dependency 'FlutterMacOS'

s.platform = :osx, '10.11'
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' }
s.swift_version = '5.0'
end
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ flutter:
pluginClass: FlutterAppBadgeControlPlugin
ios:
pluginClass: FlutterAppBadgeControlPlugin
macos:
pluginClass: FlutterAppBadgeControlPlugin

# To add assets to your plugin package, add an assets section, like this:
# assets:
Expand Down