feat: add local file management and incoming share support
This commit is contained in:
1 parent
731a83fcbc
commit
ed875ef2e1
43 files changed
+2674
-36
No files matched your search
@@ -0,0 +1,190 @@
|
||||
import 'package:crypto/crypto.dart';
|
||||
import '../data/clipboard_repository.dart';
|
||||
import '../domain/douyin_share.dart';
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:receive_sharing_intent/receive_sharing_intent.dart';
|
||||
import '../data/share_storage.dart';
|
||||
import '../domain/shared_inbox.dart';
|
||||
|
||||
ProviderContainer? _sharingContainer;
|
||||
|
||||
/// Local-phone inbox is independent of account caches.
|
||||
ProviderContainer get sharingContainer =>
|
||||
_sharingContainer ??= ProviderContainer();
|
||||
void disposeSharingContainer() {
|
||||
_sharingContainer?.dispose();
|
||||
_sharingContainer = null;
|
||||
}
|
||||
|
||||
final clipboardRepositoryProvider = Provider((ref) => ClipboardRepository());
|
||||
|
||||
final sharingControllerProvider =
|
||||
NotifierProvider.autoDispose<SharingController, SharedInbox>(
|
||||
SharingController.new,
|
||||
);
|
||||
|
||||
/// Kept alive by ShareReceiver, independent of account-scoped business providers.
|
||||
class SharingController extends AutoDisposeNotifier<SharedInbox> {
|
||||
Future<void> _pending = Future.value();
|
||||
Future<void> _initialShareReady = Future.value();
|
||||
String? _lastBatch;
|
||||
DateTime? _lastReceived;
|
||||
bool _disposed = false;
|
||||
bool _opening = false;
|
||||
bool _checkingClipboard = false;
|
||||
bool _mobile = false;
|
||||
final Set<String> _handledClipboard = {};
|
||||
|
||||
Future<void> checkClipboard() async {
|
||||
if (!_mobile ||
|
||||
_disposed ||
|
||||
_checkingClipboard ||
|
||||
state.clipboardUrl != null ||
|
||||
state.importing) {
|
||||
return;
|
||||
}
|
||||
_checkingClipboard = true;
|
||||
try {
|
||||
// Finish loading saved shares before checking for an already imported URL.
|
||||
await _initialShareReady;
|
||||
await _pending;
|
||||
if (_disposed || state.importing) return;
|
||||
final text = await ref.read(clipboardRepositoryProvider).readText();
|
||||
if (_disposed || text == null || text.length > 20000) return;
|
||||
final url = findDouyinShareUrl(text);
|
||||
if (url == null || state.importing || state.clipboardUrl != null) return;
|
||||
final fingerprint = sha256.convert(utf8.encode(url)).toString();
|
||||
final repository = ref.read(clipboardRepositoryProvider);
|
||||
if (_handledClipboard.contains(fingerprint) ||
|
||||
repository.lastHandledFingerprint == fingerprint ||
|
||||
state.items.any(
|
||||
(item) =>
|
||||
(item.type == 'url' || item.type == 'text') &&
|
||||
findDouyinShareUrl(item.path) == url,
|
||||
)) {
|
||||
return;
|
||||
}
|
||||
state = state.copyWith(clipboardText: text.trim(), clipboardUrl: url);
|
||||
} catch (_) {
|
||||
// Clipboard denial/unavailability must not interrupt returning to the app.
|
||||
} finally {
|
||||
_checkingClipboard = false;
|
||||
}
|
||||
}
|
||||
|
||||
void resolveClipboard({required bool share}) {
|
||||
final url = state.clipboardUrl;
|
||||
final text = state.clipboardText;
|
||||
if (url == null || text == null) return;
|
||||
final fingerprint = sha256.convert(utf8.encode(url)).toString();
|
||||
_handledClipboard.add(fingerprint);
|
||||
// Keep only hashes, never persist unrelated clipboard content.
|
||||
if (_handledClipboard.length > 64) {
|
||||
_handledClipboard.remove(_handledClipboard.first);
|
||||
}
|
||||
ref.read(clipboardRepositoryProvider).markHandled(fingerprint);
|
||||
state = state.copyWith(clipboardText: null, clipboardUrl: null);
|
||||
if (share) {
|
||||
receive([
|
||||
SharedMediaFile(path: url, type: SharedMediaType.url, message: text),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> open(String path) async {
|
||||
if (_opening) return true;
|
||||
_opening = true;
|
||||
try {
|
||||
return await openSharedItem(path);
|
||||
} finally {
|
||||
_opening = false;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
SharedInbox build() {
|
||||
if (kIsWeb ||
|
||||
(defaultTargetPlatform != TargetPlatform.android &&
|
||||
defaultTargetPlatform != TargetPlatform.iOS)) {
|
||||
return const SharedInbox();
|
||||
}
|
||||
_mobile = true;
|
||||
_pending = loadSharedItems().then(
|
||||
(items) {
|
||||
if (!_disposed) {
|
||||
state = state.copyWith(
|
||||
items: items
|
||||
.map((item) => SharedItem.fromJson(item.toMap()))
|
||||
.toList(),
|
||||
);
|
||||
}
|
||||
},
|
||||
onError: (Object error) {
|
||||
if (!_disposed) state = state.copyWith(failed: true);
|
||||
},
|
||||
);
|
||||
final subscription = ReceiveSharingIntent.instance.getMediaStream().listen(
|
||||
receive,
|
||||
onError: (Object error) => _error(),
|
||||
);
|
||||
_initialShareReady = ReceiveSharingIntent.instance.getInitialMedia().then(
|
||||
receive,
|
||||
onError: (Object error) => _error(),
|
||||
);
|
||||
ref.onDispose(() {
|
||||
_disposed = true;
|
||||
subscription.cancel();
|
||||
});
|
||||
return const SharedInbox();
|
||||
}
|
||||
|
||||
void _error() {
|
||||
if (!_disposed) {
|
||||
state = state.copyWith(failed: true, delivery: state.delivery + 1);
|
||||
}
|
||||
}
|
||||
|
||||
void receive(List<SharedMediaFile> items) {
|
||||
if (_disposed || items.isEmpty) return;
|
||||
final signature = jsonEncode(items.map((item) => item.toMap()).toList());
|
||||
final now = DateTime.now();
|
||||
if (_lastBatch == signature &&
|
||||
_lastReceived != null &&
|
||||
now.difference(_lastReceived!).inSeconds < 2) {
|
||||
return;
|
||||
}
|
||||
_lastBatch = signature;
|
||||
_lastReceived = now;
|
||||
_pending = _pending.then((_) async {
|
||||
if (_disposed) return;
|
||||
state = state.copyWith(
|
||||
importing: true,
|
||||
failed: false,
|
||||
delivery: state.delivery + 1,
|
||||
);
|
||||
try {
|
||||
final saved = await saveSharedItems(items);
|
||||
if (!_disposed) {
|
||||
state = state.copyWith(
|
||||
items: [
|
||||
...state.items,
|
||||
...saved.map((item) => SharedItem.fromJson(item.toMap())),
|
||||
],
|
||||
);
|
||||
}
|
||||
} catch (_) {
|
||||
if (!_disposed) state = state.copyWith(failed: true);
|
||||
} finally {
|
||||
if (!_disposed) state = state.copyWith(importing: false);
|
||||
try {
|
||||
await ReceiveSharingIntent.instance.reset();
|
||||
} catch (_) {
|
||||
/* Do not block subsequent batches. */
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user