// SPDX-License-Identifier: AGPL-3.0-or-later import 'package:flutter/services.dart'; /// Text or a link shared to the app from another app. class SharedText { const SharedText(this.text, [this.subject = '']); final String text; /// The sender's title for it, e.g. a web page's title; often empty. final String subject; } /// "Save to Docwell" from the Android share sheet (see MainActivity.kt). class ShareReceiver { static const _channel = MethodChannel('docwell/share'); /// Calls [onShare] for the share that started the app and every later one. Future listen(void Function(SharedText) onShare) async { _channel.setMethodCallHandler((call) async { if (call.method == 'shared') onShare(_read(call.arguments)); }); try { final first = await _channel.invokeMethod('takeShared'); if (first != null) onShare(_read(first)); } on MissingPluginException { // Not on Android. } } static SharedText _read(Object? raw) { final m = (raw as Map).cast(); return SharedText( m['text'] as String? ?? '', m['subject'] as String? ?? '', ); } }