Skip to content

Commit

Permalink
Merge pull request #66 from shounakmulay/v0.1.1
Browse files Browse the repository at this point in the history
v0.1.1
  • Loading branch information
shounakmulay authored Mar 28, 2021
2 parents e43a062 + 98f8cc7 commit 6262126
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.1
* Added background instance for executing telephony methods in background.
* Fix type cast issues.

## 0.1.0
* Feature equivalent of v0.0.9
* Enabled null-safety
Expand Down
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,62 @@ SimState simState = await telephony.simState;

Check out the [detailed documentation](https://shounakmulay.gitbook.io/telephony/network-data-and-metrics) to know all possible metrics and their values.

### Executing in background
If you want to call the `telephony` methods in background, you can do in the following ways.

#### 1. Using only `Telephony.instance`
If you want to continue using `Telephony.insatnce` in the background, you will need to make sure that once the app comes back to the front, it again calls `Telephony.insatnce`.
```dart
backgrounMessageHandler(SmsMessage message) async {
// Handle background message
Telephony.insatnce.sendSms(to: "123456789", message: "Message from background")
}
void main() {
runApp(MyApp());
}
class _MyAppState extends State<MyApp> {
String _message;
// This will not work as the instance will be replaced by
// the one in background.
final telephony = Telephony.instance;
@override
void initState() {
super.initState();
// You should make sure call to instance is made every time
// app comes to foreground
final inbox = Telephony.insatnce.getInboxSms()
}
```

#### 2. Use `backgroundInstance`
If you cannot make sure that the call to instance would be made every time app comes to foreground, or if you would prefer to maintain a separate background instance,
you can use `Telephony.backgroundInstance` in the background execution context.
```dart
backgrounMessageHandler(SmsMessage message) async {
// Handle background message
Telephony.backgroundInstance.sendSms(to: "123456789", message: "Message from background")
}
void main() {
runApp(MyApp());
}
class _MyAppState extends State<MyApp> {
String _message;
final telephony = Telephony.instance;
@override
void initState() {
super.initState();
final inbox = telephony.getInboxSms()
}
```


## Features

Expand Down
8 changes: 4 additions & 4 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MyApp extends StatefulWidget {
}

class _MyAppState extends State<MyApp> {
String _message;
String _message = "";
final telephony = Telephony.instance;

@override
Expand All @@ -27,7 +27,7 @@ class _MyAppState extends State<MyApp> {

onMessage(SmsMessage message) async {
setState(() {
_message = message.body;
_message = message.body ?? "Error reading message body.";
});
}

Expand All @@ -44,9 +44,9 @@ class _MyAppState extends State<MyApp> {
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.

final bool result = await telephony.requestPhoneAndSmsPermissions;
final bool? result = await telephony.requestPhoneAndSmsPermissions;

if (result) {
if (result != null && result) {
telephony.listenIncomingSms(
onNewMessage: onMessage, onBackgroundMessage: onBackgroundMessage);
}
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description: Demonstrates how to use the telephony plugin.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

environment:
sdk: ">=2.7.0 <3.0.0"
sdk: ">=2.12.0 <3.0.0"
flutter: ">=1.10.0"

dependencies:
Expand Down
2 changes: 1 addition & 1 deletion example/test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void main() {
expect(
find.byWidgetPredicate(
(Widget widget) =>
widget is Text && widget.data.startsWith('Running on:'),
widget is Text && widget.data!.startsWith('Running on:'),
),
findsOneWidget,
);
Expand Down
3 changes: 3 additions & 0 deletions lib/telephony.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class Telephony {
///
static Telephony get instance => _instance;

///
/// Gets a singleton instance of the [Telephony] class to be used in background execution context.
///
static Telephony get backgroundInstance => _backgroundInstance;

/// ## Do not call this method. This method is visible only for testing.
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: telephony
description: A Flutter plugin to use telephony features such as fetch network
info, start phone calls, send and receive SMS, and listen for incoming SMS.
version: 0.1.0
version: 0.1.1
homepage: https://github.com/shounakmulay/Telephony
repository: https://github.com/shounakmulay/Telephony

Expand Down

0 comments on commit 6262126

Please sign in to comment.