Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use enum result rather than string #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ Create a function for sending messages.

``` dart
void _sendSMS(String message, List<String> recipents) async {
String _result = await sendSMS(message: message, recipients: recipents)
.catchError((onError) {
print(onError);
});
print(_result);
SendSMSResult result = await sendSMS(
message: message,
recipients: recipents,
).catchError((onError) => print(onError));

print(result);
}
```

Expand All @@ -69,11 +70,13 @@ On Android, you can skip the additional dialog with the sendDirect parameter.
String message = "This is a test message!";
List<String> recipents = ["1234567890", "5556787676"];

String _result = await sendSMS(message: message, recipients: recipents, sendDirect: true)
.catchError((onError) {
print(onError);
});
print(_result);
SendSMSResult result = await sendSMS(
message: message,
recipients: recipents,
sendDirect: true,
).catchError((onError) => print(onError));

print(result);
```

NOTE: This also requires the SEND_SMS permission to be added to the AndroidManifest.xml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class FlutterSmsPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
}
}

result.success("SMS Sent!")
result.success("sent")
}

private fun sendSMSDialog(result: Result, phones: String, message: String) {
Expand All @@ -134,6 +134,6 @@ class FlutterSmsPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
intent.putExtra("sms_body", message)
intent.putExtra(Intent.EXTRA_TEXT, message)
activity?.startActivityForResult(intent, REQUEST_CODE_SEND_SMS)
result.success("SMS Sent!")
result.success("sent")
}
}
19 changes: 17 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,27 @@ class _MyAppState extends State<MyApp> {

Future<void> _sendSMS(List<String> recipients) async {
try {
String _result = await sendSMS(
SendSMSResult _result = await sendSMS(
message: _controllerMessage.text,
recipients: recipients,
sendDirect: sendDirect,
);
setState(() => _message = _result);
setState(() {
switch (_result) {
case SendSMSResult.sent:
_message = 'SMS Sent!';
break;
case SendSMSResult.failed:
_message = 'Failed';
break;
case SendSMSResult.cancelled:
_message = 'Cancelled';
break;
case SendSMSResult.unknownError:
_message = 'Not sent';
break;
}
});
} catch (error) {
setState(() => _message = error.toString());
}
Expand Down
4 changes: 3 additions & 1 deletion lib/flutter_sms.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import 'dart:async';

import 'src/flutter_sms_platform.dart';

enum SendSMSResult { sent, failed, cancelled, unknownError }

/// Open SMS Dialog on iOS/Android/Web
Future<String> sendSMS({
Future<SendSMSResult> sendSMS({
required String message,
required List<String> recipients,
bool sendDirect = false,
Expand Down
8 changes: 4 additions & 4 deletions lib/flutter_sms_web.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';

import 'flutter_sms.dart';
import 'src/flutter_sms_platform.dart';

class FlutterSmsPlugin extends FlutterSmsPlatform {
Expand All @@ -12,15 +12,15 @@ class FlutterSmsPlugin extends FlutterSmsPlatform {
}

@override
Future<String> sendSMS({
Future<SendSMSResult> sendSMS({
required String message,
required List<String> recipients,
bool sendDirect = false,
}) async {
bool _messageSent =
await FlutterSmsPlatform.instance.launchSmsMulti(recipients, message);
if (_messageSent) return 'Message Sent!';
return 'Error Sending Message!';
if (_messageSent) return SendSMSResult.sent;
return SendSMSResult.unknownError;
}

@override
Expand Down
29 changes: 22 additions & 7 deletions lib/src/flutter_sms_platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:flutter/services.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
import 'package:url_launcher/url_launcher.dart';

import '../flutter_sms.dart';
import 'user_agent/io.dart' if (dart.library.html) 'user_agent/web.dart';

const MethodChannel _channel = MethodChannel('flutter_sms');
Expand Down Expand Up @@ -34,7 +35,7 @@ class FlutterSmsPlatform extends PlatformInterface {

///
///
Future<String> sendSMS({
Future<SendSMSResult> sendSMS({
required String message,
required List<String> recipients,
bool sendDirect = false,
Expand All @@ -43,16 +44,30 @@ class FlutterSmsPlatform extends PlatformInterface {
mapData['message'] = message;
if (!kIsWeb && Platform.isIOS) {
mapData['recipients'] = recipients;
return _channel
.invokeMethod<String>('sendSMS', mapData)
.then((value) => value ?? 'Error sending sms');
return _channel.invokeMethod<String>('sendSMS', mapData).then((value) {
switch (value) {
case 'sent':
return SendSMSResult.sent;
case 'cancelled':
return SendSMSResult.cancelled;
case 'failed':
return SendSMSResult.failed;
default:
return SendSMSResult.unknownError;
}
});
} else {
String _phones = recipients.join(';');
mapData['recipients'] = _phones;
mapData['sendDirect'] = sendDirect;
return _channel
.invokeMethod<String>('sendSMS', mapData)
.then((value) => value ?? 'Error sending sms');
return _channel.invokeMethod<String>('sendSMS', mapData).then((value) {
switch (value) {
case 'sent':
return SendSMSResult.sent;
default:
return SendSMSResult.unknownError;
}
});
}
}

Expand Down