- [Intent] renamed ReceivedIntent -> Intent

- [ReceiveIntent] renamed `giveResult` -> `setResult`
- [ReceiveIntentPlugin] removed `intentToMap`, in favour of creating it inside `handleIntent`
This commit is contained in:
Harsh Bhikadia 2021-04-25 09:20:20 +05:30
parent 07f81ea602
commit a5541aa248
3 changed files with 24 additions and 27 deletions

View file

@ -34,8 +34,10 @@ class ReceiveIntentPlugin : FlutterPlugin, MethodCallHandler, EventChannel.Strea
private var latestIntentMap: Map<String, Any?>? = null private var latestIntentMap: Map<String, Any?>? = null
private var initialIntent = true private var initialIntent = true
private fun intentToMap(intent: Intent, fromPackageName: String?): Map<String, Any?> { private fun handleIntent(intent: Intent, fromPackageName: String?) {
return mapOf( //Log.e("ReceiveIntentPlugin", "intent: $intent")
//Log.e("ReceiveIntentPlugin", "fromPackageName: $fromPackageName")
val intentMap = mapOf<String, Any?>(
"fromPackageName" to fromPackageName, "fromPackageName" to fromPackageName,
"fromSignatures" to fromPackageName?.let { getApplicationSignature(context, it) }, "fromSignatures" to fromPackageName?.let { getApplicationSignature(context, it) },
"action" to intent.action, "action" to intent.action,
@ -43,21 +45,16 @@ class ReceiveIntentPlugin : FlutterPlugin, MethodCallHandler, EventChannel.Strea
"categories" to intent.categories, "categories" to intent.categories,
"extra" to intent.extras?.let { bundleToJSON(it).toString() } "extra" to intent.extras?.let { bundleToJSON(it).toString() }
) )
} //Log.e("ReceiveIntentPlugin", "intentMap: $intentMap")
private fun handleIntent(intent: Intent, fromPackageName: String?) {
//Log.e("ReceiveIntentPlugin", "intent: $intent")
//Log.e("ReceiveIntentPlugin", "fromPackageName: $fromPackageName")
//Log.e("ReceiveIntentPlugin", "intentMap: " + intentToMap(intent, fromPackageName))
if (initialIntent) { if (initialIntent) {
initialIntentMap = intentToMap(intent, fromPackageName) initialIntentMap = intentMap
initialIntent = false initialIntent = false
} }
latestIntentMap = intentToMap(intent, fromPackageName) latestIntentMap = intentMap
eventSink?.success(latestIntentMap) eventSink?.success(latestIntentMap)
} }
private fun giveResult(result: Result, resultCode: Int?, data: String?, shouldFinish: Boolean?) { private fun setResult(result: Result, resultCode: Int?, data: String?, shouldFinish: Boolean?) {
if (resultCode != null) { if (resultCode != null) {
if (data == null) { if (data == null) {
activity?.setResult(resultCode) activity?.setResult(resultCode)
@ -65,10 +62,10 @@ class ReceiveIntentPlugin : FlutterPlugin, MethodCallHandler, EventChannel.Strea
val json = JSONObject(data) val json = JSONObject(data)
activity?.setResult(resultCode, jsonToIntent(json)) activity?.setResult(resultCode, jsonToIntent(json))
} }
if(shouldFinish ?: false){ if (shouldFinish ?: false) {
activity?.finish() activity?.finish()
} }
result.success(null) return result.success(null)
} }
result.error("InvalidArg", "resultCode can not be null", null) result.error("InvalidArg", "resultCode can not be null", null)
} }
@ -89,8 +86,8 @@ class ReceiveIntentPlugin : FlutterPlugin, MethodCallHandler, EventChannel.Strea
"getInitialIntent" -> { "getInitialIntent" -> {
result.success(initialIntentMap) result.success(initialIntentMap)
} }
"giveResult" -> { "setResult" -> {
giveResult(result, call.argument("resultCode"), call.argument("data"), call.argument("shouldFinish")) setResult(result, call.argument("resultCode"), call.argument("data"), call.argument("shouldFinish"))
} }
else -> { else -> {
result.notImplemented() result.notImplemented()

View file

@ -13,7 +13,7 @@ class MyApp extends StatefulWidget {
} }
class _MyAppState extends State<MyApp> { class _MyAppState extends State<MyApp> {
ReceivedIntent _initialIntent; Intent _initialIntent;
@override @override
void initState() { void initState() {
@ -31,7 +31,7 @@ class _MyAppState extends State<MyApp> {
}); });
} }
Widget _buildFromIntent(String label, ReceivedIntent intent) { Widget _buildFromIntent(String label, Intent intent) {
return Center( return Center(
child: Column( child: Column(
children: [ children: [
@ -59,7 +59,7 @@ class _MyAppState extends State<MyApp> {
mainAxisAlignment: MainAxisAlignment.spaceEvenly, mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [ children: [
_buildFromIntent("INITIAL", _initialIntent), _buildFromIntent("INITIAL", _initialIntent),
StreamBuilder<ReceivedIntent>( StreamBuilder<Intent>(
stream: ReceiveIntent.receivedIntentStream, stream: ReceiveIntent.receivedIntentStream,
builder: (context, snapshot) => builder: (context, snapshot) =>
_buildFromIntent("STREAMED", snapshot.data), _buildFromIntent("STREAMED", snapshot.data),

View file

@ -3,7 +3,7 @@ import 'dart:convert';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
class ReceivedIntent { class Intent {
final bool isNull; final bool isNull;
final String? fromPackageName; final String? fromPackageName;
final List<String>? fromSignatures; final List<String>? fromSignatures;
@ -14,7 +14,7 @@ class ReceivedIntent {
bool get isNotNull => !isNull; bool get isNotNull => !isNull;
const ReceivedIntent({ const Intent({
this.isNull = true, this.isNull = true,
this.fromPackageName, this.fromPackageName,
this.fromSignatures, this.fromSignatures,
@ -24,7 +24,7 @@ class ReceivedIntent {
this.extra, this.extra,
}); });
factory ReceivedIntent.fromMap(Map? map) => ReceivedIntent( factory Intent.fromMap(Map? map) => Intent(
isNull: map == null, isNull: map == null,
fromPackageName: map?["fromPackageName"], fromPackageName: map?["fromPackageName"],
fromSignatures: map?["fromSignatures"] != null fromSignatures: map?["fromSignatures"] != null
@ -59,19 +59,19 @@ class ReceiveIntent {
static const EventChannel _eventChannel = static const EventChannel _eventChannel =
const EventChannel("receive_intent/event"); const EventChannel("receive_intent/event");
static Future<ReceivedIntent?> getInitialIntent() async { static Future<Intent?> getInitialIntent() async {
final renameMap = await _methodChannel.invokeMapMethod('getInitialIntent'); final renameMap = await _methodChannel.invokeMapMethod('getInitialIntent');
//print("result: $renameMap"); //print("result: $renameMap");
return ReceivedIntent.fromMap(renameMap); return Intent.fromMap(renameMap);
} }
static Stream<ReceivedIntent?> receivedIntentStream = _eventChannel static Stream<Intent?> receivedIntentStream = _eventChannel
.receiveBroadcastStream() .receiveBroadcastStream()
.map<ReceivedIntent?>((event) => ReceivedIntent.fromMap(event as Map?)); .map<Intent?>((event) => Intent.fromMap(event as Map?));
static Future<void> giveResult(int resultCode, static Future<void> setResult(int resultCode,
{Map<String, dynamic?>? data, bool shouldFinish: false}) async { {Map<String, dynamic?>? data, bool shouldFinish: false}) async {
await _methodChannel.invokeMethod('giveResult', <String, dynamic>{ await _methodChannel.invokeMethod('setResult', <String, dynamic>{
"resultCode": resultCode, "resultCode": resultCode,
if (data != null) "data": json.encode(data), if (data != null) "data": json.encode(data),
"shouldFinish": shouldFinish, "shouldFinish": shouldFinish,