Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
3dfe225c
Commit
3dfe225c
authored
Apr 18, 2017
by
Sarah Zakarias
Committed by
GitHub
Apr 18, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add charging status to Swift version of Platform Channel example (#9436)
parent
b52d6594
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
119 additions
and
39 deletions
+119
-39
platform_channel.iml
examples/platform_channel/platform_channel.iml
+1
-0
AppDelegate.swift
examples/platform_channel_swift/ios/Runner/AppDelegate.swift
+68
-21
main.dart
examples/platform_channel_swift/lib/main.dart
+49
-18
platform_channel_swift.iml
examples/platform_channel_swift/platform_channel_swift.iml
+1
-0
No files found.
examples/platform_channel/platform_channel.iml
View file @
3dfe225c
...
...
@@ -11,5 +11,6 @@
<orderEntry
type=
"sourceFolder"
forTests=
"false"
/>
<orderEntry
type=
"library"
name=
"Dart SDK"
level=
"project"
/>
<orderEntry
type=
"library"
name=
"Dart Packages"
level=
"project"
/>
<orderEntry
type=
"library"
name=
"Dart SDK"
level=
"application"
/>
</component>
</module>
\ No newline at end of file
examples/platform_channel_swift/ios/Runner/AppDelegate.swift
View file @
3dfe225c
...
...
@@ -6,35 +6,82 @@ import UIKit
import
Flutter
@UIApplicationMain
@objc
class
AppDelegate
:
FlutterAppDelegate
{
@objc
class
AppDelegate
:
FlutterAppDelegate
,
FlutterStreamHandler
{
private
var
eventReceiver
:
FlutterEventReceiver
?;
override
func
application
(
_
application
:
UIApplication
,
didFinishLaunchingWithOptions
launchOptions
:
[
UIApplicationLaunchOptionsKey
:
Any
]?)
->
Bool
{
let
controller
:
FlutterViewController
=
window
?
.
rootViewController
as!
FlutterViewController
;
let
batteryChannel
=
FlutterMethodChannel
.
init
(
name
:
"samples.flutter.io/battery"
,
binaryMessenger
:
controller
);
batteryChannel
.
setMethodCallHandler
({
(
call
:
FlutterMethodCall
,
result
:
FlutterResultReceiver
)
->
Void
in
if
(
"getBatteryLevel"
==
call
.
method
)
{
receiveBatteryLevel
(
result
:
result
);
}
else
{
result
(
FlutterMethodNotImplemented
);
}
let
controller
:
FlutterViewController
=
window
?
.
rootViewController
as!
FlutterViewController
;
let
batteryChannel
=
FlutterMethodChannel
.
init
(
name
:
"samples.flutter.io/battery"
,
binaryMessenger
:
controller
);
batteryChannel
.
setMethodCallHandler
({
(
call
:
FlutterMethodCall
,
result
:
FlutterResultReceiver
)
->
Void
in
if
(
"getBatteryLevel"
==
call
.
method
)
{
self
.
receiveBatteryLevel
(
result
:
result
);
}
else
{
result
(
FlutterMethodNotImplemented
);
}
}
);
let
chargingChannel
=
FlutterEventChannel
.
init
(
name
:
"samples.flutter.io/charging"
,
binaryMessenger
:
controller
);
chargingChannel
.
setStreamHandler
(
self
);
return
true
}
}
private
func
receiveBatteryLevel
(
result
:
FlutterResultReceiver
)
{
let
device
=
UIDevice
.
current
;
device
.
isBatteryMonitoringEnabled
=
true
;
if
(
device
.
batteryState
==
UIDeviceBatteryState
.
unknown
)
{
result
(
FlutterError
.
init
(
code
:
"UNAVAILABLE"
,
message
:
"Battery info unavailable"
,
details
:
nil
));
}
else
{
result
(
Int
(
device
.
batteryLevel
*
100
));
private
func
receiveBatteryLevel
(
result
:
FlutterResultReceiver
)
{
let
device
=
UIDevice
.
current
;
device
.
isBatteryMonitoringEnabled
=
true
;
if
(
device
.
batteryState
==
UIDeviceBatteryState
.
unknown
)
{
result
(
FlutterError
.
init
(
code
:
"UNAVAILABLE"
,
message
:
"Battery info unavailable"
,
details
:
nil
));
}
else
{
result
(
Int
(
device
.
batteryLevel
*
100
));
}
}
public
func
onListen
(
withArguments
arguments
:
Any
?,
eventReceiver
:
@escaping
FlutterEventReceiver
)
->
FlutterError
?
{
self
.
eventReceiver
=
eventReceiver
;
UIDevice
.
current
.
isBatteryMonitoringEnabled
=
true
;
NotificationCenter
.
default
.
addObserver
(
self
,
selector
:
#selector(
onBatteryStateDidChange
)
,
name
:
NSNotification
.
Name
.
UIDeviceBatteryStateDidChange
,
object
:
nil
)
return
nil
;
}
@objc
private
func
onBatteryStateDidChange
(
notification
:
NSNotification
)
{
if
(
eventReceiver
==
nil
)
{
return
;
}
let
state
=
UIDevice
.
current
.
batteryState
;
switch
state
{
case
UIDeviceBatteryState
.
full
:
eventReceiver
!
(
"charging"
);
break
;
case
UIDeviceBatteryState
.
charging
:
eventReceiver
!
(
"charging"
);
break
;
case
UIDeviceBatteryState
.
unplugged
:
eventReceiver
!
(
"discharging"
);
break
;
default
:
eventReceiver
!
(
FlutterError
.
init
(
code
:
"UNAVAILABLE"
,
message
:
"Charging status unavailable"
,
details
:
nil
));
break
;
}
}
public
func
onCancel
(
withArguments
arguments
:
Any
?)
->
FlutterError
?
{
NotificationCenter
.
default
.
removeObserver
(
self
);
eventReceiver
=
nil
;
return
nil
;
}
}
examples/platform_channel_swift/lib/main.dart
View file @
3dfe225c
...
...
@@ -13,36 +13,67 @@ class PlatformChannel extends StatefulWidget {
}
class
_PlatformChannelState
extends
State
<
PlatformChannel
>
{
static
const
PlatformMethodChannel
platform
=
const
PlatformMethodChannel
(
'samples.flutter.io/battery'
);
String
_batteryLevel
=
''
;
static
const
PlatformMethodChannel
methodChannel
=
const
PlatformMethodChannel
(
'samples.flutter.io/battery'
);
static
const
PlatformEventChannel
eventChannel
=
const
PlatformEventChannel
(
'samples.flutter.io/charging'
);
String
_batteryLevel
=
'Battery level: unknown.'
;
String
_chargingStatus
=
'Battery status: unknown.'
;
Future
<
Null
>
_getBatteryLevel
()
async
{
String
batteryLevel
;
try
{
final
int
result
=
await
platform
.
invokeMethod
(
'getBatteryLevel'
);
batteryLevel
=
'Battery level
at
$result
%
.'
;
}
on
PlatformException
catch
(
e
)
{
batteryLevel
=
"Failed to get battery level
: '
${e.message}
'
."
;
}
final
int
result
=
await
methodChannel
.
invokeMethod
(
'getBatteryLevel'
);
batteryLevel
=
'Battery level
:
$result
%
.'
;
}
on
PlatformException
{
batteryLevel
=
"Failed to get battery level."
;
}
setState
(()
{
_batteryLevel
=
batteryLevel
;
});
}
@override
void
initState
()
{
super
.
initState
();
eventChannel
.
receiveBroadcastStream
().
listen
(
_onEvent
,
onError:
_onError
);
}
void
_onEvent
(
String
event
)
{
setState
(()
{
_chargingStatus
=
"Battery status:
${event == 'charging' ? '' : 'dis'}
charging."
;
});
}
void
_onError
(
PlatformException
error
)
{
setState
(()
{
_chargingStatus
=
"Battery status: unknown."
;
});
}
@override
Widget
build
(
BuildContext
context
)
{
return
new
Material
(
child:
new
Center
(
child:
new
Column
(
mainAxisAlignment:
MainAxisAlignment
.
spaceEvenly
,
children:
<
Widget
>[
new
RaisedButton
(
child:
const
Text
(
'Get Battery Level'
),
onPressed:
_getBatteryLevel
,
),
new
Text
(
_batteryLevel
,
key:
const
Key
(
'Battery level label'
)),
],
),
child:
new
Column
(
mainAxisAlignment:
MainAxisAlignment
.
spaceEvenly
,
children:
<
Widget
>[
new
Column
(
mainAxisAlignment:
MainAxisAlignment
.
center
,
children:
<
Widget
>[
new
Text
(
_batteryLevel
,
key:
const
Key
(
'Battery level label'
)),
new
Padding
(
padding:
const
EdgeInsets
.
all
(
16.0
),
child:
new
RaisedButton
(
child:
const
Text
(
'Refresh'
),
onPressed:
_getBatteryLevel
,
),
),
],
),
new
Text
(
_chargingStatus
),
],
),
);
}
...
...
examples/platform_channel_swift/platform_channel_swift.iml
View file @
3dfe225c
...
...
@@ -10,6 +10,7 @@
</content>
<orderEntry
type=
"sourceFolder"
forTests=
"false"
/>
<orderEntry
type=
"library"
name=
"Dart SDK"
level=
"project"
/>
<orderEntry
type=
"library"
name=
"Dart SDK"
level=
"application"
/>
<orderEntry
type=
"library"
name=
"Dart Packages"
level=
"project"
/>
</component>
</module>
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment