Skip to content

Commit

Permalink
refactor(battery_plus)!: platform implementation refactor into a sing…
Browse files Browse the repository at this point in the history
…le package (#1169)

* unfederate battery_plus_linux

* unfederate battery_plus_macos

* unfederate battery_plus_web

* unfederate battery_plus_windows

* Unfederate the platform interface

* move all files one dir below

* fix dependency

* bump version

* add missing flutter_web_plugins

* windows naming fix

* Fix windows build

* Move battery_plus_web.dart to src folder

* remove pluginClass: none

* moving all files to src

* export platform interface

* remove unnecessary import

* cleanup

* sort imports

* move battery_plus_platform_interface.dart to root

* export enums directly instead of the battery_plus_platform_interface

* rename BatteryPlusWebPlugin

* renamed BatteryPlusLinux to BatteryPlusLinuxPlugin for consistency

* move battery_plus into battery_plus

* re-add battery_plus_platform_interface

* fix battery_plus dependency to the platform interface

* typo

* format

* remove new line from changelog, let's try melos release process

* revert forcing package version

BREAKING CHANGE: battery_plus plugin architecture changed
  • Loading branch information
miquelbeltran committed Oct 8, 2022
1 parent aeadfca commit 143204b
Show file tree
Hide file tree
Showing 44 changed files with 125 additions and 684 deletions.
Expand Up @@ -26,10 +26,7 @@
33CC10F32044A3C60003C045 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 33CC10F22044A3C60003C045 /* Assets.xcassets */; };
33CC10F62044A3C60003C045 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 33CC10F42044A3C60003C045 /* MainMenu.xib */; };
33CC11132044BFA00003C045 /* MainFlutterWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 33CC11122044BFA00003C045 /* MainFlutterWindow.swift */; };
33D1A10422148B71006C7A3E /* FlutterMacOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 33D1A10322148B71006C7A3E /* FlutterMacOS.framework */; };
33D1A10522148B93006C7A3E /* FlutterMacOS.framework in Bundle Framework */ = {isa = PBXBuildFile; fileRef = 33D1A10322148B71006C7A3E /* FlutterMacOS.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
D73912F022F37F9E000D13A0 /* App.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D73912EF22F37F9E000D13A0 /* App.framework */; };
D73912F222F3801D000D13A0 /* App.framework in Bundle Framework */ = {isa = PBXBuildFile; fileRef = D73912EF22F37F9E000D13A0 /* App.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
EB30D1BDD1652B1637007EB4 /* Pods_Runner.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CFC44BEB0C124E4CD00CAA07 /* Pods_Runner.framework */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -49,18 +46,17 @@
dstPath = "";
dstSubfolderSpec = 10;
files = (
D73912F222F3801D000D13A0 /* App.framework in Bundle Framework */,
33D1A10522148B93006C7A3E /* FlutterMacOS.framework in Bundle Framework */,
);
name = "Bundle Framework";
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
06F31FBE7F0395D04EA40A20 /* Pods-Runner.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.debug.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"; sourceTree = "<group>"; };
333000ED22D3DE5D00554162 /* Warnings.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Warnings.xcconfig; sourceTree = "<group>"; };
335BBD1A22A9A15E00E9071D /* GeneratedPluginRegistrant.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GeneratedPluginRegistrant.swift; sourceTree = "<group>"; };
33CC10ED2044A3C60003C045 /* example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "example.app"; sourceTree = BUILT_PRODUCTS_DIR; };
33CC10ED2044A3C60003C045 /* example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = example.app; sourceTree = BUILT_PRODUCTS_DIR; };
33CC10F02044A3C60003C045 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
33CC10F22044A3C60003C045 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = Assets.xcassets; path = Runner/Assets.xcassets; sourceTree = "<group>"; };
33CC10F52044A3C60003C045 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/MainMenu.xib; sourceTree = "<group>"; };
Expand All @@ -69,22 +65,22 @@
33CEB47222A05771004F2AC0 /* Flutter-Debug.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Flutter-Debug.xcconfig"; sourceTree = "<group>"; };
33CEB47422A05771004F2AC0 /* Flutter-Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Flutter-Release.xcconfig"; sourceTree = "<group>"; };
33CEB47722A0578A004F2AC0 /* Flutter-Generated.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = "Flutter-Generated.xcconfig"; path = "ephemeral/Flutter-Generated.xcconfig"; sourceTree = "<group>"; };
33D1A10322148B71006C7A3E /* FlutterMacOS.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = FlutterMacOS.framework; path = Flutter/ephemeral/FlutterMacOS.framework; sourceTree = SOURCE_ROOT; };
33E51913231747F40026EE4D /* DebugProfile.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = DebugProfile.entitlements; sourceTree = "<group>"; };
33E51914231749380026EE4D /* Release.entitlements */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.entitlements; path = Release.entitlements; sourceTree = "<group>"; };
33E5194F232828860026EE4D /* AppInfo.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = AppInfo.xcconfig; sourceTree = "<group>"; };
4AFA7DCBB075C90D95997775 /* Pods-Runner.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"; sourceTree = "<group>"; };
7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Release.xcconfig; sourceTree = "<group>"; };
7DE20167D4C93662FF106CF0 /* Pods-Runner.profile.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.profile.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.profile.xcconfig"; sourceTree = "<group>"; };
9740EEB21CF90195004384FC /* Debug.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = Debug.xcconfig; sourceTree = "<group>"; };
D73912EF22F37F9E000D13A0 /* App.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = App.framework; path = Flutter/ephemeral/App.framework; sourceTree = SOURCE_ROOT; };
CFC44BEB0C124E4CD00CAA07 /* Pods_Runner.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Runner.framework; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
33CC10EA2044A3C60003C045 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
D73912F022F37F9E000D13A0 /* App.framework in Frameworks */,
33D1A10422148B71006C7A3E /* FlutterMacOS.framework in Frameworks */,
EB30D1BDD1652B1637007EB4 /* Pods_Runner.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -109,6 +105,7 @@
33CEB47122A05771004F2AC0 /* Flutter */,
33CC10EE2044A3C60003C045 /* Products */,
D73912EC22F37F3D000D13A0 /* Frameworks */,
C2CC9754B893E4B0EC9D75D7 /* Pods */,
);
sourceTree = "<group>";
};
Expand Down Expand Up @@ -138,8 +135,6 @@
33CEB47222A05771004F2AC0 /* Flutter-Debug.xcconfig */,
33CEB47422A05771004F2AC0 /* Flutter-Release.xcconfig */,
33CEB47722A0578A004F2AC0 /* Flutter-Generated.xcconfig */,
D73912EF22F37F9E000D13A0 /* App.framework */,
33D1A10322148B71006C7A3E /* FlutterMacOS.framework */,
);
path = Flutter;
sourceTree = "<group>";
Expand All @@ -157,9 +152,21 @@
path = Runner;
sourceTree = "<group>";
};
C2CC9754B893E4B0EC9D75D7 /* Pods */ = {
isa = PBXGroup;
children = (
06F31FBE7F0395D04EA40A20 /* Pods-Runner.debug.xcconfig */,
4AFA7DCBB075C90D95997775 /* Pods-Runner.release.xcconfig */,
7DE20167D4C93662FF106CF0 /* Pods-Runner.profile.xcconfig */,
);
name = Pods;
path = Pods;
sourceTree = "<group>";
};
D73912EC22F37F3D000D13A0 /* Frameworks */ = {
isa = PBXGroup;
children = (
CFC44BEB0C124E4CD00CAA07 /* Pods_Runner.framework */,
);
name = Frameworks;
sourceTree = "<group>";
Expand All @@ -171,11 +178,13 @@
isa = PBXNativeTarget;
buildConfigurationList = 33CC10FB2044A3C60003C045 /* Build configuration list for PBXNativeTarget "Runner" */;
buildPhases = (
6B992185E66A6A4A21EB5C39 /* [CP] Check Pods Manifest.lock */,
33CC10E92044A3C60003C045 /* Sources */,
33CC10EA2044A3C60003C045 /* Frameworks */,
33CC10EB2044A3C60003C045 /* Resources */,
33CC110E2044A8840003C045 /* Bundle Framework */,
3399D490228B24CF009A79C7 /* ShellScript */,
9F05A0CF161FB9CAF4897EAB /* [CP] Embed Pods Frameworks */,
);
buildRules = (
);
Expand Down Expand Up @@ -260,7 +269,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "echo \"$PRODUCT_NAME.app\" > \"$PROJECT_DIR\"/Flutter/ephemeral/.app_filename\n";
shellScript = "echo \"$PRODUCT_NAME.app\" > \"$PROJECT_DIR\"/Flutter/ephemeral/.app_filename && \"$FLUTTER_ROOT\"/packages/flutter_tools/bin/macos_assemble.sh embed\n";
};
33CC111E2044C6BF0003C045 /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
Expand All @@ -282,6 +291,46 @@
shellPath = /bin/sh;
shellScript = "\"$FLUTTER_ROOT\"/packages/flutter_tools/bin/macos_assemble.sh && touch Flutter/ephemeral/tripwire";
};
6B992185E66A6A4A21EB5C39 /* [CP] Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
"${PODS_PODFILE_DIR_PATH}/Podfile.lock",
"${PODS_ROOT}/Manifest.lock",
);
name = "[CP] Check Pods Manifest.lock";
outputFileListPaths = (
);
outputPaths = (
"$(DERIVED_FILE_DIR)/Pods-Runner-checkManifestLockResult.txt",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
showEnvVarsInLog = 0;
};
9F05A0CF161FB9CAF4897EAB /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/battery_plus/battery_plus.framework",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/battery_plus.framework",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
/* End PBXShellScriptBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions packages/battery_plus/battery_plus/example/pubspec.yaml
Expand Up @@ -10,18 +10,6 @@ dependencies:
battery_plus:
path: ../

dependency_overrides:
battery_plus_linux:
path: ../../battery_plus_linux
battery_plus_macos:
path: ../../battery_plus_macos
battery_plus_platform_interface:
path: ../../battery_plus_platform_interface
battery_plus_web:
path: ../../battery_plus_web
battery_plus_windows:
path: ../../battery_plus_windows

dev_dependencies:
flutter_driver:
sdk: flutter
Expand Down
5 changes: 3 additions & 2 deletions packages/battery_plus/battery_plus/lib/battery_plus.dart
Expand Up @@ -6,10 +6,11 @@ import 'dart:async';

import 'package:battery_plus_platform_interface/battery_plus_platform_interface.dart';

// Export enums from the platform_interface so plugin users can use them directly.
export 'package:battery_plus_platform_interface/battery_plus_platform_interface.dart'
show BatteryState;

export 'src/battery_plus_linux.dart';

/// API for accessing information about the battery of the device the Flutter app is running on.
class Battery {
/// Constructs a singleton instance of [Battery].
Expand Down Expand Up @@ -38,7 +39,7 @@ class Battery {

/// check if device is on battery save mode
///
/// Currently only impemented on Android, IOS and Windows.
/// Currently only implemented on Android, iOS and Windows.
Future<bool> get isInBatterySaveMode {
return _platform.isInBatterySaveMode;
}
Expand Down
Expand Up @@ -24,10 +24,10 @@ extension _ToBatteryState on UPowerDeviceState {
typedef UPowerClientFactory = UPowerClient Function();

/// The Linux implementation of BatteryPlatform.
class BatteryPlusLinux extends BatteryPlatform {
class BatteryPlusLinuxPlugin extends BatteryPlatform {
/// Register this dart class as the platform implementation for linux
static void registerWith() {
BatteryPlatform.instance = BatteryPlusLinux();
BatteryPlatform.instance = BatteryPlusLinuxPlugin();
}

/// Returns the current battery level in percent.
Expand Down
Expand Up @@ -6,9 +6,9 @@ import 'package:battery_plus_platform_interface/battery_plus_platform_interface.
import 'package:flutter_web_plugins/flutter_web_plugins.dart';

/// The web implementation of the BatteryPlatform of the Battery plugin.
class BatteryPlusPlugin extends BatteryPlatform {
class BatteryPlusWebPlugin extends BatteryPlatform {
/// Constructs a BatteryPlusPlugin.
BatteryPlusPlugin(html.Navigator navigator)
BatteryPlusWebPlugin(html.Navigator navigator)
: _getBattery = navigator.getBattery;

/// A check to determine if this version of the plugin can be used.
Expand All @@ -20,7 +20,7 @@ class BatteryPlusPlugin extends BatteryPlatform {
/// Factory method that initializes the Battery plugin platform with an instance
/// of the plugin for the web.
static void registerWith(Registrar registrar) {
BatteryPlatform.instance = BatteryPlusPlugin(html.window.navigator);
BatteryPlatform.instance = BatteryPlusWebPlugin(html.window.navigator);
}

/// Returns the current battery level in percent.
Expand Down
@@ -1,42 +1,42 @@
//
// BatteryPlusChargingHandler.swift
// battery_plus_macos
// battery_plus
//
// Created by Neevash Ramdial on 08/09/2020.
//
import Foundation
import FlutterMacOS

class BatteryPlusChargingHandler: NSObject, FlutterStreamHandler {

private var context: Int = 0

private var source: CFRunLoopSource?
private var runLoop: CFRunLoop?
private var eventSink: FlutterEventSink?

func onListen(withArguments arguments: Any?, eventSink event: @escaping FlutterEventSink) -> FlutterError? {
self.eventSink = event
start()
return nil
}

func onCancel(withArguments arguments: Any?) -> FlutterError? {
stop()
self.eventSink = nil
return nil
}

func start() {
// If there is an added loop, we remove it before adding a new one.
stop()

// Gets the initial battery status
let initialStatus = getBatteryStatus()
if let sink = self.eventSink {
sink(initialStatus)
}

// Registers a run loop which is notified when the battery status changes
let context = UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())
source = IOPSNotificationCreateRunLoopSource({ (context) in
Expand All @@ -47,35 +47,35 @@ class BatteryPlusChargingHandler: NSObject, FlutterStreamHandler {
sink(status)
}
}, context).takeUnretainedValue()

// Adds loop to source
runLoop = RunLoop.current.getCFRunLoop()
CFRunLoopAddSource(runLoop, source, .defaultMode)
}

func stop() {
guard let runLoop = runLoop, let source = source else {
return
}
CFRunLoopRemoveSource(runLoop, source, .defaultMode)
}

func getBatteryStatus()-> String {
let powerSourceSnapshot = IOPSCopyPowerSourcesInfo().takeRetainedValue()
let sources = IOPSCopyPowerSourcesList(powerSourceSnapshot).takeRetainedValue() as [CFTypeRef]

// Desktops do not have battery sources and are always charging.
if sources.isEmpty {
return "charging"
}
let description = IOPSGetPowerSourceDescription(powerSourceSnapshot, sources[0]).takeUnretainedValue() as! [String: AnyObject]

if let currentCapacity = description[kIOPSCurrentCapacityKey] as? Int {
if currentCapacity >= 95 {
return "full"
}
}

// Returns nil when battery is discharging
if let isCharging = (description[kIOPSPowerSourceStateKey] as? String) {
if isCharging == kIOPSACPowerValue {
Expand Down
23 changes: 12 additions & 11 deletions packages/battery_plus/battery_plus/macos/battery_plus.podspec
@@ -1,21 +1,22 @@
#
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html.
# Run `pod lib lint battery_plus_macos.podspec' to validate before publishing.
#
Pod::Spec.new do |s|
s.name = 'battery_plus'
s.version = '0.0.1'
s.summary = 'No-op implementation of the macos battery_plus to avoid build issues on macos'
s.summary = 'Flutter Battery Plus'
s.description = <<-DESC
No-op implementation of the battery_plus plugin to avoid build issues on macos.
https://github.com/flutter/flutter/issues/46618
DESC
s.homepage = 'https://github.com/fluttercommunity/plus_plugins/tree/master/packages/battery_plus'
A Flutter plugin for accessing information about the battery state(full, charging, discharging).
DESC
s.homepage = 'https://github.com/fluttercommunity/plus_plugins/tree/main/packages/battery_plus'
s.license = { :file => '../LICENSE' }
s.author = { 'Flutter Community' => 'authors@fluttercommunity.dev' }
s.source = { :path => '.' }
s.source_files = 'Classes/**/*'
s.public_header_files = 'Classes/**/*.h'
s.source = { :path => 'https://github.com/fluttercommunity/plus_plugins/tree/main/packages/battery_plus' }
s.source_files = 'Classes/**/*'
s.dependency 'FlutterMacOS'

s.platform = :osx
s.osx.deployment_target = '10.11'
s.platform = :osx, '10.11'
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' }
s.swift_version = '5.0'
end

0 comments on commit 143204b

Please sign in to comment.