feat: 添加日历事件订阅者功能及权限重构
This commit is contained in:
@@ -2,11 +2,53 @@ enum ScheduleSourceType { manual, imported, agentGenerated }
|
||||
|
||||
enum ScheduleStatus { active, archived }
|
||||
|
||||
class Subscriber {
|
||||
final String userId;
|
||||
final String? username;
|
||||
final String? avatarUrl;
|
||||
final String? phone;
|
||||
final int permission;
|
||||
final String status;
|
||||
final DateTime subscribedAt;
|
||||
|
||||
static const int permissionView = 1;
|
||||
static const int permissionInvite = 2;
|
||||
static const int permissionEdit = 4;
|
||||
static const int permissionDelete = 8;
|
||||
static const int permissionOwner = 15;
|
||||
|
||||
bool get canEdit => (permission & permissionEdit) != 0;
|
||||
bool get canInvite => (permission & permissionInvite) != 0;
|
||||
bool get canDelete => (permission & permissionDelete) != 0;
|
||||
bool get canView => (permission & permissionView) != 0;
|
||||
|
||||
Subscriber({
|
||||
required this.userId,
|
||||
this.username,
|
||||
this.avatarUrl,
|
||||
this.phone,
|
||||
required this.permission,
|
||||
required this.status,
|
||||
required this.subscribedAt,
|
||||
});
|
||||
|
||||
factory Subscriber.fromJson(Map<String, dynamic> json) {
|
||||
return Subscriber(
|
||||
userId: json['user_id'] as String,
|
||||
username: json['username'] as String?,
|
||||
avatarUrl: json['avatar_url'] as String?,
|
||||
phone: json['phone'] as String?,
|
||||
permission: json['permission'] as int,
|
||||
status: json['status'] as String,
|
||||
subscribedAt: DateTime.parse(json['subscribed_at'] as String).toLocal(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ScheduleItemModel {
|
||||
final String id;
|
||||
final String ownerId;
|
||||
final int permission;
|
||||
final bool isOwner;
|
||||
final String title;
|
||||
final String? description;
|
||||
final DateTime startAt;
|
||||
@@ -17,20 +59,23 @@ class ScheduleItemModel {
|
||||
final ScheduleStatus status;
|
||||
final DateTime createdAt;
|
||||
final DateTime updatedAt;
|
||||
final List<Subscriber> subscribers;
|
||||
|
||||
static const int permissionView = 1;
|
||||
static const int permissionInvite = 2;
|
||||
static const int permissionEdit = 4;
|
||||
static const int permissionDelete = 8;
|
||||
static const int permissionOwner = 15;
|
||||
|
||||
bool get canEdit => isOwner || (permission & permissionEdit) != 0;
|
||||
bool get canInvite => isOwner || (permission & permissionInvite) != 0;
|
||||
bool get canDelete => isOwner;
|
||||
bool get canEdit => (permission & permissionEdit) != 0;
|
||||
bool get canInvite => (permission & permissionInvite) != 0;
|
||||
bool get canDelete => (permission & permissionDelete) != 0;
|
||||
bool get isOwner => (permission & permissionOwner) != 0;
|
||||
|
||||
ScheduleItemModel({
|
||||
required this.id,
|
||||
required this.ownerId,
|
||||
this.permission = 1,
|
||||
this.isOwner = false,
|
||||
required this.title,
|
||||
this.description,
|
||||
required this.startAt,
|
||||
@@ -41,6 +86,7 @@ class ScheduleItemModel {
|
||||
this.status = ScheduleStatus.active,
|
||||
DateTime? createdAt,
|
||||
DateTime? updatedAt,
|
||||
this.subscribers = const [],
|
||||
}) : createdAt = createdAt ?? DateTime.now(),
|
||||
updatedAt = updatedAt ?? DateTime.now();
|
||||
|
||||
@@ -48,7 +94,6 @@ class ScheduleItemModel {
|
||||
String? id,
|
||||
String? ownerId,
|
||||
int? permission,
|
||||
bool? isOwner,
|
||||
String? title,
|
||||
String? description,
|
||||
DateTime? startAt,
|
||||
@@ -59,12 +104,12 @@ class ScheduleItemModel {
|
||||
ScheduleStatus? status,
|
||||
DateTime? createdAt,
|
||||
DateTime? updatedAt,
|
||||
List<Subscriber>? subscribers,
|
||||
}) {
|
||||
return ScheduleItemModel(
|
||||
id: id ?? this.id,
|
||||
ownerId: ownerId ?? this.ownerId,
|
||||
permission: permission ?? this.permission,
|
||||
isOwner: isOwner ?? this.isOwner,
|
||||
title: title ?? this.title,
|
||||
description: description ?? this.description,
|
||||
startAt: startAt ?? this.startAt,
|
||||
@@ -75,15 +120,21 @@ class ScheduleItemModel {
|
||||
status: status ?? this.status,
|
||||
createdAt: createdAt ?? this.createdAt,
|
||||
updatedAt: updatedAt ?? this.updatedAt,
|
||||
subscribers: subscribers ?? this.subscribers,
|
||||
);
|
||||
}
|
||||
|
||||
factory ScheduleItemModel.fromJson(Map<String, dynamic> json) {
|
||||
final subscribersList = json['subscribers'] as List<dynamic>?;
|
||||
final subscribers =
|
||||
subscribersList
|
||||
?.map((s) => Subscriber.fromJson(s as Map<String, dynamic>))
|
||||
.toList() ??
|
||||
[];
|
||||
return ScheduleItemModel(
|
||||
id: json['id'] as String,
|
||||
ownerId: json['owner_id'] as String,
|
||||
permission: json['permission'] as int,
|
||||
isOwner: json['is_owner'] as bool,
|
||||
title: json['title'] as String,
|
||||
description: json['description'] as String?,
|
||||
startAt: DateTime.parse(json['start_at'] as String).toLocal(),
|
||||
@@ -98,6 +149,7 @@ class ScheduleItemModel {
|
||||
status: _statusFromApi(json['status'] as String),
|
||||
createdAt: DateTime.parse(json['created_at'] as String).toLocal(),
|
||||
updatedAt: DateTime.parse(json['updated_at'] as String).toLocal(),
|
||||
subscribers: subscribers,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -138,7 +138,6 @@ class CalendarRepository extends CachedRepository<List<ScheduleItemModel>> {
|
||||
'id': item.id,
|
||||
'owner_id': item.ownerId,
|
||||
'permission': item.permission,
|
||||
'is_owner': item.isOwner,
|
||||
'title': item.title,
|
||||
'description': item.description,
|
||||
'start_at': item.startAt.toIso8601String(),
|
||||
|
||||
@@ -149,6 +149,10 @@ class _CalendarEventDetailScreenState extends State<CalendarEventDetailScreen> {
|
||||
const SizedBox(height: AppSpacing.md),
|
||||
_buildExtraSurface(event),
|
||||
],
|
||||
if (event.subscribers.isNotEmpty) ...[
|
||||
const SizedBox(height: AppSpacing.md),
|
||||
_buildSubscribersSurface(event),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
@@ -465,6 +469,131 @@ class _CalendarEventDetailScreenState extends State<CalendarEventDetailScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSubscribersSurface(ScheduleItemModel event) {
|
||||
if (event.subscribers.isEmpty) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(AppSpacing.lg),
|
||||
decoration: BoxDecoration(
|
||||
color: _colorScheme.surface,
|
||||
borderRadius: BorderRadius.circular(AppRadius.xl),
|
||||
border: Border.all(color: _colorScheme.outlineVariant),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
context.l10n.calendarDetailSubscribers(event.subscribers.length),
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: _colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: AppSpacing.md),
|
||||
...event.subscribers.map(
|
||||
(sub) => _buildSubscriberRow(sub, event.ownerId),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSubscriberRow(Subscriber subscriber, String ownerId) {
|
||||
final palette = Theme.of(context).extension<AppColorPalette>()!;
|
||||
final avatarColor =
|
||||
palette.avatarColors[subscriber.userId.hashCode.abs() %
|
||||
palette.avatarColors.length];
|
||||
final isOwner = subscriber.userId == ownerId;
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: AppSpacing.xs),
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
width: 32,
|
||||
height: 32,
|
||||
decoration: BoxDecoration(
|
||||
color: _colorScheme.surfaceContainerHighest,
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
),
|
||||
child: subscriber.avatarUrl != null
|
||||
? ClipRRect(
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
child: Image.network(
|
||||
subscriber.avatarUrl!,
|
||||
width: 32,
|
||||
height: 32,
|
||||
fit: BoxFit.cover,
|
||||
errorBuilder: (context, error, stackTrace) =>
|
||||
Icon(Icons.person, size: 16, color: avatarColor),
|
||||
),
|
||||
)
|
||||
: Icon(Icons.person, size: 16, color: avatarColor),
|
||||
),
|
||||
const SizedBox(width: AppSpacing.sm),
|
||||
Expanded(
|
||||
child: Row(
|
||||
children: [
|
||||
Flexible(
|
||||
child: Text(
|
||||
subscriber.phone ??
|
||||
subscriber.username ??
|
||||
subscriber.userId.substring(0, 8),
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: _colorScheme.onSurface,
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
if (isOwner) ...[
|
||||
const SizedBox(width: AppSpacing.xs),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: AppSpacing.xs,
|
||||
vertical: 2,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: _colorScheme.primaryContainer,
|
||||
borderRadius: BorderRadius.circular(AppRadius.full),
|
||||
),
|
||||
child: Text(
|
||||
context.l10n.calendarOwnerBadge,
|
||||
style: TextStyle(
|
||||
fontSize: 10,
|
||||
fontWeight: FontWeight.w700,
|
||||
color: _colorScheme.onPrimaryContainer,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
if (subscriber.canEdit)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: AppSpacing.xs),
|
||||
child: Icon(
|
||||
LucideIcons.pencil,
|
||||
size: 14,
|
||||
color: _colorScheme.primary,
|
||||
),
|
||||
),
|
||||
if (subscriber.canInvite)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: AppSpacing.xs),
|
||||
child: Icon(
|
||||
LucideIcons.userPlus,
|
||||
size: 14,
|
||||
color: _colorScheme.primary,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _formatReminderText(int? reminderMinutes) {
|
||||
if (reminderMinutes == null) {
|
||||
return context.l10n.calendarDetailReminderNone;
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
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';
|
||||
@@ -50,6 +53,7 @@ class CalendarShareDialog extends StatefulWidget {
|
||||
|
||||
class _CalendarShareDialogState extends State<CalendarShareDialog> {
|
||||
final _phoneController = TextEditingController();
|
||||
String _dialCode = kDialCodes.first.value;
|
||||
final bool _permissionView = true;
|
||||
bool _permissionEdit = false;
|
||||
bool _permissionInvite = false;
|
||||
@@ -73,13 +77,14 @@ class _CalendarShareDialogState extends State<CalendarShareDialog> {
|
||||
return;
|
||||
}
|
||||
|
||||
final fullPhone = '$_dialCode$phone';
|
||||
setState(() => _isLoading = true);
|
||||
|
||||
try {
|
||||
final api = sl<CalendarApi>();
|
||||
await api.share(
|
||||
widget.eventId,
|
||||
phone: phone,
|
||||
phone: fullPhone,
|
||||
view: _permissionView,
|
||||
edit: _permissionEdit,
|
||||
invite: _permissionInvite,
|
||||
@@ -147,14 +152,37 @@ class _CalendarShareDialogState extends State<CalendarShareDialog> {
|
||||
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(
|
||||
labelText: l10n.calendarSharePhoneLabel,
|
||||
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.md),
|
||||
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),
|
||||
),
|
||||
),
|
||||
keyboardType: TextInputType.phone,
|
||||
),
|
||||
const SizedBox(height: AppSpacing.lg),
|
||||
Text(
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:lucide_icons/lucide_icons.dart';
|
||||
import 'package:social_app/core/l10n/l10n.dart';
|
||||
import '../../../../app/di/injection.dart';
|
||||
import '../../../../app/router/app_routes.dart';
|
||||
import '../../../../core/theme/design_tokens.dart';
|
||||
import '../../../../shared/widgets/app_loading_indicator.dart';
|
||||
import '../../../../shared/widgets/app_selection_sheet.dart';
|
||||
@@ -215,7 +217,7 @@ class _CreateEventSheetState extends State<CreateEventSheet>
|
||||
title: _isEditing
|
||||
? context.l10n.calendarCreateEditTitle
|
||||
: context.l10n.calendarCreateNewTitle,
|
||||
onBack: () => Navigator.of(context).pop(),
|
||||
onBack: () => _closeSheet(),
|
||||
trailing: ValueListenableBuilder<TextEditingValue>(
|
||||
valueListenable: _titleController,
|
||||
builder: (context, value, child) {
|
||||
@@ -264,7 +266,7 @@ class _CreateEventSheetState extends State<CreateEventSheet>
|
||||
width: AppSpacing.xxl * 2,
|
||||
height: AppSpacing.xxl * 2,
|
||||
child: TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
onPressed: _closeSheet,
|
||||
style: TextButton.styleFrom(
|
||||
padding: const EdgeInsets.all(AppSpacing.none),
|
||||
shape: RoundedRectangleBorder(
|
||||
@@ -783,6 +785,8 @@ class _CreateEventSheetState extends State<CreateEventSheet>
|
||||
metadata: metadata,
|
||||
);
|
||||
|
||||
var saved = false;
|
||||
|
||||
try {
|
||||
final service = sl<CalendarService>();
|
||||
|
||||
@@ -792,10 +796,9 @@ class _CreateEventSheetState extends State<CreateEventSheet>
|
||||
await service.addEvent(event);
|
||||
}
|
||||
|
||||
saved = true;
|
||||
|
||||
widget.onSaved?.call();
|
||||
if (mounted) {
|
||||
Navigator.pop(context, true);
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
Toast.show(
|
||||
@@ -811,5 +814,29 @@ class _CreateEventSheetState extends State<CreateEventSheet>
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (saved && mounted) {
|
||||
_closeSheet(result: true);
|
||||
}
|
||||
}
|
||||
|
||||
void _closeSheet({Object? result}) {
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
|
||||
final navigator = Navigator.of(context);
|
||||
if (navigator.canPop()) {
|
||||
navigator.pop(result);
|
||||
return;
|
||||
}
|
||||
|
||||
final router = GoRouter.of(context);
|
||||
if (router.canPop()) {
|
||||
context.pop(result);
|
||||
return;
|
||||
}
|
||||
|
||||
context.go(AppRoutes.homeMain);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user