refactor: share handler

This commit is contained in:
cogwheel0
2025-08-28 14:45:46 +05:30
parent 4a524d404e
commit 5c72537932
5 changed files with 51 additions and 8 deletions

View File

@@ -31,6 +31,8 @@
<integer>10</integer>
<key>NSExtensionActivationSupportsText</key>
<true/>
<key>NSExtensionActivationSupportsFileWithMaxCount</key>
<integer>20</integer>
</dict>
<key>PHSupportedMediaTypes</key>
<array>

View File

@@ -9,6 +9,7 @@ import '../../features/auth/providers/unified_auth_providers.dart';
import '../../features/chat/providers/chat_providers.dart';
import '../../features/chat/services/file_attachment_service.dart';
import '../../core/providers/app_providers.dart';
// No server chat creation here; follow chat flow on first send
/// Lightweight payload for a share event
class SharedPayload {
@@ -160,11 +161,16 @@ Future<void> _processPayload(Ref ref, SharedPayload payload) async {
}
}
// Prefill text in the composer (do not auto-send)
// Prefill text in the composer (do not auto-send) and request focus
final text = payload.text?.trim();
if (text != null && text.isNotEmpty) {
ref.read(prefilledInputTextProvider.notifier).state = text;
// Bump focus trigger to ensure input focuses after navigation/build
final current = ref.read(inputFocusTriggerProvider);
ref.read(inputFocusTriggerProvider.notifier).state = current + 1;
}
// Do NOT create a placeholder server chat here. The drawer will refresh
// when the user sends their first message, matching in-app behavior.
// This allows the user to add a caption before sending
} catch (e) {
debugPrint('ShareReceiver: failed to process payload: $e');

View File

@@ -24,6 +24,9 @@ final isLoadingConversationProvider = StateProvider<bool>((ref) => false);
// Prefilled input text (e.g., when sharing text from other apps)
final prefilledInputTextProvider = StateProvider<String?>((ref) => null);
// Trigger to request focus on the chat input (increment to signal)
final inputFocusTriggerProvider = StateProvider<int>((ref) => 0);
class ChatMessagesNotifier extends StateNotifier<List<ChatMessage>> {
final Ref _ref;
StreamSubscription? _messageStream;
@@ -533,7 +536,14 @@ Future<void> _sendMessageInternal(
// Invalidate conversations provider to refresh the list
// Adding a small delay to prevent rapid invalidations that could cause duplicates
Future.delayed(const Duration(milliseconds: 100), () {
ref.invalidate(conversationsProvider);
try {
// Guard against using ref after widget disposal
if (ref.mounted == true) {
ref.invalidate(conversationsProvider);
}
} catch (_) {
// If ref doesn't support mounted or is disposed, skip
}
});
} catch (e) {
// Still add the message locally
@@ -1742,7 +1752,11 @@ Future<void> _saveConversationToServer(dynamic ref) async {
// Refresh conversations list to show the updated conversation
// Adding a small delay to prevent rapid invalidations that could cause duplicates
Future.delayed(const Duration(milliseconds: 100), () {
ref.invalidate(conversationsProvider);
try {
if (ref.mounted == true) {
ref.invalidate(conversationsProvider);
}
} catch (_) {}
});
} catch (e) {
// Fallback to local storage

View File

@@ -204,6 +204,18 @@ class _EnhancedAttachmentState extends ConsumerState<EnhancedAttachment> {
.toString();
final size = _fileInfo?['size'];
final sizeLabel = size is num ? _formatSize(size.toInt()) : null;
final lowerName = filename.toLowerCase();
final fileExtension = lowerName.contains('.')
? lowerName.split('.').last
: '';
final List<String> metaParts = [];
if (fileExtension.isNotEmpty) {
metaParts.add('.${fileExtension.toUpperCase()}');
}
if (sizeLabel != null) {
metaParts.add(sizeLabel);
}
final metaLabel = metaParts.join('');
final card = Container(
constraints: widget.constraints,
@@ -217,15 +229,14 @@ class _EnhancedAttachmentState extends ConsumerState<EnhancedAttachment> {
),
),
child: Row(
mainAxisSize: MainAxisSize.min,
mainAxisSize: MainAxisSize.max,
children: [
Text(
_fileIconFor(filename),
style: const TextStyle(fontSize: AppTypography.headlineLarge),
),
const SizedBox(width: Spacing.sm),
ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 220),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
@@ -240,9 +251,9 @@ class _EnhancedAttachmentState extends ConsumerState<EnhancedAttachment> {
fontWeight: FontWeight.w600,
),
),
if (sizeLabel != null)
if (metaLabel.isNotEmpty)
Text(
sizeLabel,
metaLabel,
style: TextStyle(
color: context.conduitTheme.textSecondary.withValues(
alpha: 0.7,

View File

@@ -342,6 +342,16 @@ class _ModernChatInputState extends ConsumerState<ModernChatInput>
orElse: () => false,
);
// React to external focus requests (e.g., from share prefill)
final focusTick = ref.watch(inputFocusTriggerProvider);
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted) return;
if (focusTick > 0) {
_ensureFocusedIfEnabled();
if (!_isExpanded) _setExpanded(true);
}
});
return Container(
// Transparent wrapper so rounded corners are visible against page background
color: Colors.transparent,