268 lines
8.2 KiB
Dart
268 lines
8.2 KiB
Dart
import 'package:flutter/material.dart' hide BackButton;
|
|
import 'package:flutter/services.dart';
|
|
import 'package:social_app/core/l10n/l10n.dart';
|
|
|
|
import '../../../../app/di/injection.dart';
|
|
import '../../../../data/models/dial_codes.dart';
|
|
import '../../../../core/theme/design_tokens.dart';
|
|
import '../../../../shared/widgets/app_button.dart';
|
|
import '../../../../shared/widgets/phone_prefix_selector.dart';
|
|
import '../../../../shared/widgets/toast/toast.dart';
|
|
import '../../../../shared/widgets/toast/toast_type.dart';
|
|
import '../../data/apis/calendar_api.dart';
|
|
|
|
class CalendarShareDialog extends StatefulWidget {
|
|
final String eventId;
|
|
final String eventTitle;
|
|
final bool canInvite;
|
|
final bool canEdit;
|
|
|
|
const CalendarShareDialog({
|
|
super.key,
|
|
required this.eventId,
|
|
required this.eventTitle,
|
|
this.canInvite = false,
|
|
this.canEdit = false,
|
|
});
|
|
|
|
static Future<void> show(
|
|
BuildContext context,
|
|
String eventId,
|
|
String eventTitle, {
|
|
bool canInvite = false,
|
|
bool canEdit = false,
|
|
}) {
|
|
return showModalBottomSheet(
|
|
context: context,
|
|
isScrollControlled: true,
|
|
backgroundColor: Theme.of(
|
|
context,
|
|
).colorScheme.surface.withValues(alpha: 0),
|
|
builder: (context) => CalendarShareDialog(
|
|
eventId: eventId,
|
|
eventTitle: eventTitle,
|
|
canInvite: canInvite,
|
|
canEdit: canEdit,
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
State<CalendarShareDialog> createState() => _CalendarShareDialogState();
|
|
}
|
|
|
|
class _CalendarShareDialogState extends State<CalendarShareDialog> {
|
|
final _phoneController = TextEditingController();
|
|
String _dialCode = kDialCodes.first.value;
|
|
final bool _permissionView = true;
|
|
bool _permissionEdit = false;
|
|
bool _permissionInvite = false;
|
|
bool _isLoading = false;
|
|
|
|
@override
|
|
void dispose() {
|
|
_phoneController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _handleShare() async {
|
|
final l10n = context.l10n;
|
|
final phone = _phoneController.text.trim();
|
|
if (phone.isEmpty) {
|
|
Toast.show(
|
|
context,
|
|
l10n.calendarSharePhoneRequired,
|
|
type: ToastType.error,
|
|
);
|
|
return;
|
|
}
|
|
|
|
final fullPhone = '$_dialCode$phone';
|
|
setState(() => _isLoading = true);
|
|
|
|
try {
|
|
final api = sl<CalendarApi>();
|
|
await api.share(
|
|
widget.eventId,
|
|
phone: fullPhone,
|
|
view: _permissionView,
|
|
edit: _permissionEdit,
|
|
invite: _permissionInvite,
|
|
);
|
|
if (mounted) {
|
|
Toast.show(
|
|
context,
|
|
l10n.calendarShareInviteSent,
|
|
type: ToastType.success,
|
|
);
|
|
Navigator.of(context).pop();
|
|
}
|
|
} catch (e) {
|
|
if (mounted) {
|
|
Toast.show(
|
|
context,
|
|
l10n.calendarShareInviteFailed,
|
|
type: ToastType.error,
|
|
);
|
|
}
|
|
} finally {
|
|
if (mounted) {
|
|
setState(() => _isLoading = false);
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final l10n = context.l10n;
|
|
final colorScheme = Theme.of(context).colorScheme;
|
|
|
|
return Container(
|
|
padding: EdgeInsets.only(
|
|
bottom: MediaQuery.of(context).viewInsets.bottom,
|
|
),
|
|
decoration: BoxDecoration(
|
|
color: colorScheme.surface,
|
|
borderRadius: const BorderRadius.vertical(
|
|
top: Radius.circular(AppRadius.lg),
|
|
),
|
|
),
|
|
child: SafeArea(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(AppSpacing.lg),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Text(
|
|
l10n.calendarShareTitle,
|
|
style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600),
|
|
),
|
|
IconButton(
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
icon: const Icon(Icons.close),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: AppSpacing.md),
|
|
Text(widget.eventTitle, style: const TextStyle(fontSize: 16)),
|
|
const SizedBox(height: AppSpacing.lg),
|
|
TextField(
|
|
controller: _phoneController,
|
|
keyboardType: TextInputType.phone,
|
|
inputFormatters: [
|
|
FilteringTextInputFormatter.digitsOnly,
|
|
LengthLimitingTextInputFormatter(14),
|
|
],
|
|
style: TextStyle(fontSize: 16, color: colorScheme.onSurface),
|
|
decoration: InputDecoration(
|
|
hintText: l10n.calendarSharePhoneHint,
|
|
filled: true,
|
|
fillColor: colorScheme.surface,
|
|
contentPadding: const EdgeInsets.symmetric(
|
|
horizontal: AppSpacing.lg,
|
|
vertical: AppSpacing.lg,
|
|
),
|
|
prefixIcon: PhonePrefixSelector(
|
|
value: _dialCode,
|
|
onChanged: (value) => setState(() => _dialCode = value),
|
|
),
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(AppRadius.lg),
|
|
borderSide: BorderSide.none,
|
|
),
|
|
enabledBorder: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(AppRadius.lg),
|
|
borderSide: BorderSide(color: colorScheme.outlineVariant),
|
|
),
|
|
focusedBorder: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(AppRadius.lg),
|
|
borderSide: BorderSide(color: colorScheme.primary),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: AppSpacing.lg),
|
|
Text(
|
|
l10n.calendarSharePermissionTitle,
|
|
style: const TextStyle(fontWeight: FontWeight.w600),
|
|
),
|
|
const SizedBox(height: AppSpacing.sm),
|
|
_buildPermissionSwitch(
|
|
l10n.calendarSharePermissionView,
|
|
l10n.calendarSharePermissionViewDesc,
|
|
true,
|
|
null,
|
|
),
|
|
_buildPermissionSwitch(
|
|
l10n.calendarSharePermissionEdit,
|
|
l10n.calendarSharePermissionEditDesc,
|
|
_permissionEdit,
|
|
widget.canEdit
|
|
? (v) => setState(() => _permissionEdit = v)
|
|
: null,
|
|
),
|
|
_buildPermissionSwitch(
|
|
l10n.calendarSharePermissionInvite,
|
|
l10n.calendarSharePermissionInviteDesc,
|
|
_permissionInvite,
|
|
widget.canInvite
|
|
? (v) => setState(() => _permissionInvite = v)
|
|
: null,
|
|
),
|
|
const SizedBox(height: AppSpacing.lg),
|
|
AppButton(
|
|
text: l10n.calendarShareSendInvite,
|
|
onPressed: _isLoading ? null : _handleShare,
|
|
isLoading: _isLoading,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildPermissionSwitch(
|
|
String title,
|
|
String description,
|
|
bool value,
|
|
ValueChanged<bool>? onChanged,
|
|
) {
|
|
final colorScheme = Theme.of(context).colorScheme;
|
|
final enabled = onChanged != null;
|
|
return Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: AppSpacing.xs),
|
|
child: Row(
|
|
children: [
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
title,
|
|
style: TextStyle(
|
|
color: enabled
|
|
? colorScheme.onSurface
|
|
: colorScheme.onSurfaceVariant,
|
|
),
|
|
),
|
|
Text(
|
|
description,
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
color: colorScheme.onSurfaceVariant,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
Switch(value: value, onChanged: enabled ? onChanged : null),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|