refactor(apps): 主题系统迁移至 ColorScheme + 扩展架构并支持 Dark Mode
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import 'package:social_app/core/network/i_api_client.dart';
|
||||
|
||||
import 'models/schedule_item_model.dart';
|
||||
import 'package:social_app/data/repositories/models/schedule_item_model.dart';
|
||||
|
||||
class CalendarApi {
|
||||
final IApiClient _client;
|
||||
|
||||
@@ -1,306 +0,0 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
enum ScheduleSourceType { manual, imported, agentGenerated }
|
||||
|
||||
enum ScheduleStatus { active, archived }
|
||||
|
||||
class ScheduleItemModel {
|
||||
final String id;
|
||||
final String ownerId;
|
||||
final int permission;
|
||||
final bool isOwner;
|
||||
final String title;
|
||||
final String? description;
|
||||
final DateTime startAt;
|
||||
final DateTime? endAt;
|
||||
final String timezone;
|
||||
final ScheduleMetadata? metadata;
|
||||
final ScheduleSourceType sourceType;
|
||||
final ScheduleStatus status;
|
||||
final DateTime createdAt;
|
||||
final DateTime updatedAt;
|
||||
|
||||
static const int permissionView = 1;
|
||||
static const int permissionInvite = 2;
|
||||
static const int permissionEdit = 4;
|
||||
|
||||
bool get canEdit => isOwner || (permission & permissionEdit) != 0;
|
||||
bool get canInvite => isOwner || (permission & permissionInvite) != 0;
|
||||
bool get canDelete => isOwner;
|
||||
|
||||
ScheduleItemModel({
|
||||
required this.id,
|
||||
required this.ownerId,
|
||||
this.permission = 1,
|
||||
this.isOwner = false,
|
||||
required this.title,
|
||||
this.description,
|
||||
required this.startAt,
|
||||
this.endAt,
|
||||
this.timezone = 'Asia/Shanghai',
|
||||
this.metadata,
|
||||
this.sourceType = ScheduleSourceType.manual,
|
||||
this.status = ScheduleStatus.active,
|
||||
DateTime? createdAt,
|
||||
DateTime? updatedAt,
|
||||
}) : createdAt = createdAt ?? DateTime.now(),
|
||||
updatedAt = updatedAt ?? DateTime.now();
|
||||
|
||||
ScheduleItemModel copyWith({
|
||||
String? id,
|
||||
String? ownerId,
|
||||
int? permission,
|
||||
bool? isOwner,
|
||||
String? title,
|
||||
String? description,
|
||||
DateTime? startAt,
|
||||
DateTime? endAt,
|
||||
String? timezone,
|
||||
ScheduleMetadata? metadata,
|
||||
ScheduleSourceType? sourceType,
|
||||
ScheduleStatus? status,
|
||||
DateTime? createdAt,
|
||||
DateTime? updatedAt,
|
||||
}) {
|
||||
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,
|
||||
endAt: endAt ?? this.endAt,
|
||||
timezone: timezone ?? this.timezone,
|
||||
metadata: metadata ?? this.metadata,
|
||||
sourceType: sourceType ?? this.sourceType,
|
||||
status: status ?? this.status,
|
||||
createdAt: createdAt ?? this.createdAt,
|
||||
updatedAt: updatedAt ?? this.updatedAt,
|
||||
);
|
||||
}
|
||||
|
||||
factory ScheduleItemModel.fromJson(Map<String, dynamic> json) {
|
||||
return ScheduleItemModel(
|
||||
id: json['id'] as String,
|
||||
ownerId: json['owner_id'] as String? ?? '',
|
||||
permission: json['permission'] as int? ?? 1,
|
||||
isOwner: json['is_owner'] as bool? ?? false,
|
||||
title: json['title'] as String,
|
||||
description: json['description'] as String?,
|
||||
startAt: DateTime.parse(json['start_at'] as String).toLocal(),
|
||||
endAt: json['end_at'] != null
|
||||
? DateTime.parse(json['end_at'] as String).toLocal()
|
||||
: null,
|
||||
timezone: (json['timezone'] as String?) ?? 'UTC',
|
||||
metadata: json['metadata'] is Map<String, dynamic>
|
||||
? ScheduleMetadata.fromJson(json['metadata'] as Map<String, dynamic>)
|
||||
: null,
|
||||
sourceType: _sourceTypeFromApi(json['source_type'] as String?),
|
||||
status: _statusFromApi(json['status'] as String?),
|
||||
createdAt: json['created_at'] != null
|
||||
? DateTime.parse(json['created_at'] as String).toLocal()
|
||||
: DateTime.now(),
|
||||
updatedAt: json['updated_at'] != null
|
||||
? DateTime.parse(json['updated_at'] as String).toLocal()
|
||||
: DateTime.now(),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toCreateJson() {
|
||||
return {
|
||||
'title': title,
|
||||
'description': description,
|
||||
'start_at': startAt.toUtc().toIso8601String(),
|
||||
'end_at': endAt?.toUtc().toIso8601String(),
|
||||
'timezone': timezone,
|
||||
'metadata': metadata?.toJson(),
|
||||
};
|
||||
}
|
||||
|
||||
Map<String, dynamic> toUpdateJson() {
|
||||
return {
|
||||
'title': title,
|
||||
'description': description,
|
||||
'start_at': startAt.toUtc().toIso8601String(),
|
||||
'end_at': endAt?.toUtc().toIso8601String(),
|
||||
'timezone': timezone,
|
||||
'metadata': metadata?.toJson(),
|
||||
'status': _statusToApi(status),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class ScheduleMetadata {
|
||||
final String? color;
|
||||
final String? location;
|
||||
final String? notes;
|
||||
final int? reminderMinutes;
|
||||
final List<Attachment> attachments;
|
||||
final int version;
|
||||
final Map<String, dynamic> raw;
|
||||
|
||||
ScheduleMetadata({
|
||||
this.color,
|
||||
this.location,
|
||||
this.notes,
|
||||
this.reminderMinutes,
|
||||
List<Attachment>? attachments,
|
||||
this.version = 1,
|
||||
Map<String, dynamic>? raw,
|
||||
}) : attachments = attachments ?? const [],
|
||||
raw = raw ?? const {};
|
||||
|
||||
ScheduleMetadata copyWith({
|
||||
String? color,
|
||||
String? location,
|
||||
String? notes,
|
||||
int? reminderMinutes,
|
||||
List<Attachment>? attachments,
|
||||
int? version,
|
||||
Map<String, dynamic>? raw,
|
||||
}) {
|
||||
return ScheduleMetadata(
|
||||
color: color ?? this.color,
|
||||
location: location ?? this.location,
|
||||
notes: notes ?? this.notes,
|
||||
reminderMinutes: reminderMinutes ?? this.reminderMinutes,
|
||||
attachments: attachments ?? this.attachments,
|
||||
version: version ?? this.version,
|
||||
raw: raw ?? this.raw,
|
||||
);
|
||||
}
|
||||
|
||||
factory ScheduleMetadata.fromJson(Map<String, dynamic> json) {
|
||||
final rawAttachments = json['attachments'];
|
||||
final attachments = rawAttachments is List
|
||||
? rawAttachments
|
||||
.whereType<Map<String, dynamic>>()
|
||||
.map(Attachment.fromJson)
|
||||
.toList()
|
||||
: <Attachment>[];
|
||||
return ScheduleMetadata(
|
||||
color: json['color'] as String?,
|
||||
location: json['location'] as String?,
|
||||
notes: json['notes'] as String?,
|
||||
reminderMinutes: json['reminder_minutes'] as int?,
|
||||
attachments: attachments,
|
||||
version: (json['version'] as int?) ?? 1,
|
||||
raw: Map<String, dynamic>.from(json),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'color': color,
|
||||
'location': location,
|
||||
'notes': notes,
|
||||
'reminder_minutes': reminderMinutes,
|
||||
'attachments': attachments.map((item) => item.toJson()).toList(),
|
||||
'version': version,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class Attachment {
|
||||
final String name;
|
||||
final List<String> visibleTo;
|
||||
final String? url;
|
||||
final String? note;
|
||||
final String? content;
|
||||
final String type;
|
||||
|
||||
Attachment({
|
||||
required this.name,
|
||||
this.visibleTo = const [],
|
||||
this.url,
|
||||
this.note,
|
||||
this.content,
|
||||
this.type = 'document',
|
||||
});
|
||||
|
||||
Attachment copyWith({
|
||||
String? name,
|
||||
List<String>? visibleTo,
|
||||
String? url,
|
||||
String? note,
|
||||
String? content,
|
||||
String? type,
|
||||
}) {
|
||||
return Attachment(
|
||||
name: name ?? this.name,
|
||||
visibleTo: visibleTo ?? this.visibleTo,
|
||||
url: url ?? this.url,
|
||||
note: note ?? this.note,
|
||||
content: content ?? this.content,
|
||||
type: type ?? this.type,
|
||||
);
|
||||
}
|
||||
|
||||
factory Attachment.fromJson(Map<String, dynamic> json) {
|
||||
final rawVisibleTo = json['visible_to'];
|
||||
final visibleTo = rawVisibleTo is List
|
||||
? rawVisibleTo.map((item) => item.toString()).toList()
|
||||
: <String>[];
|
||||
return Attachment(
|
||||
name: (json['name'] as String?) ?? '',
|
||||
visibleTo: visibleTo,
|
||||
url: json['url'] as String?,
|
||||
note: json['note'] as String?,
|
||||
content: json['content'] as String?,
|
||||
type: (json['type'] as String?) ?? 'document',
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'name': name,
|
||||
'visible_to': visibleTo,
|
||||
'url': url,
|
||||
'note': note,
|
||||
'content': content,
|
||||
'type': type,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
ScheduleSourceType _sourceTypeFromApi(String? raw) {
|
||||
switch (raw) {
|
||||
case 'imported':
|
||||
return ScheduleSourceType.imported;
|
||||
case 'agent_generated':
|
||||
return ScheduleSourceType.agentGenerated;
|
||||
case 'manual':
|
||||
default:
|
||||
return ScheduleSourceType.manual;
|
||||
}
|
||||
}
|
||||
|
||||
ScheduleStatus _statusFromApi(String? raw) {
|
||||
switch (raw) {
|
||||
case 'completed':
|
||||
case 'canceled':
|
||||
case 'archived':
|
||||
return ScheduleStatus.archived;
|
||||
case 'active':
|
||||
default:
|
||||
return ScheduleStatus.active;
|
||||
}
|
||||
}
|
||||
|
||||
String _statusToApi(ScheduleStatus status) {
|
||||
switch (status) {
|
||||
case ScheduleStatus.active:
|
||||
return 'active';
|
||||
case ScheduleStatus.archived:
|
||||
return 'archived';
|
||||
}
|
||||
}
|
||||
|
||||
const defaultColors = [
|
||||
Color(0xFF3B82F6),
|
||||
Color(0xFF8B5CF6),
|
||||
Color(0xFF10B981),
|
||||
Color(0xFFF59E0B),
|
||||
Color(0xFFEF4444),
|
||||
];
|
||||
@@ -1,144 +0,0 @@
|
||||
import 'dart:async';
|
||||
|
||||
import '../../../../core/cache/cache_entry.dart';
|
||||
import '../../../../core/cache/cache_policy.dart';
|
||||
import '../../../../core/cache/hybrid_cache_store.dart';
|
||||
import '../models/schedule_item_model.dart';
|
||||
|
||||
class CalendarRepository {
|
||||
final HybridCacheStore store;
|
||||
final CachePolicy policy;
|
||||
final DateTime Function() now;
|
||||
final Future<List<ScheduleItemModel>> Function(DateTime date)
|
||||
loadDayFromRemote;
|
||||
final Future<List<ScheduleItemModel>> Function(DateTime start, DateTime end)
|
||||
loadMonthFromRemote;
|
||||
|
||||
final Map<String, Future<void>> _refreshInFlight = <String, Future<void>>{};
|
||||
|
||||
CalendarRepository({
|
||||
required this.store,
|
||||
required this.loadDayFromRemote,
|
||||
required this.loadMonthFromRemote,
|
||||
CachePolicy? policy,
|
||||
DateTime Function()? now,
|
||||
}) : policy =
|
||||
policy ??
|
||||
const CachePolicy(
|
||||
softTtl: Duration(minutes: 2),
|
||||
hardTtl: Duration(minutes: 30),
|
||||
minRefreshInterval: Duration(minutes: 1),
|
||||
),
|
||||
now = now ?? DateTime.now;
|
||||
|
||||
static String dayKey(DateTime date) {
|
||||
final day =
|
||||
'${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
|
||||
return 'calendar:day:$day';
|
||||
}
|
||||
|
||||
static String monthKey(DateTime date) {
|
||||
return 'calendar:month:${date.year}-${date.month.toString().padLeft(2, '0')}';
|
||||
}
|
||||
|
||||
Future<List<ScheduleItemModel>> getDayEvents(
|
||||
DateTime date, {
|
||||
bool forceRefresh = false,
|
||||
}) async {
|
||||
final key = dayKey(date);
|
||||
if (forceRefresh) {
|
||||
return _refreshDayAndRead(date, key);
|
||||
}
|
||||
|
||||
final cached = await store.read<CacheEntry<List<ScheduleItemModel>>>(key);
|
||||
if (cached == null) {
|
||||
return _refreshDayAndRead(date, key);
|
||||
}
|
||||
|
||||
final decision = policy.evaluate(now: now(), fetchedAt: cached.fetchedAt);
|
||||
if (decision.shouldRefreshInBackground) {
|
||||
_refreshDayInBackground(date, key);
|
||||
}
|
||||
if (decision.mustBlockForNetwork || !decision.canUseCached) {
|
||||
return _refreshDayAndRead(date, key);
|
||||
}
|
||||
return cached.value;
|
||||
}
|
||||
|
||||
Future<List<ScheduleItemModel>> getMonthEvents(
|
||||
DateTime monthStart, {
|
||||
bool forceRefresh = false,
|
||||
}) async {
|
||||
final key = monthKey(monthStart);
|
||||
if (forceRefresh) {
|
||||
return _refreshMonthAndRead(monthStart, key);
|
||||
}
|
||||
final cached = await store.read<CacheEntry<List<ScheduleItemModel>>>(key);
|
||||
if (cached == null) {
|
||||
return _refreshMonthAndRead(monthStart, key);
|
||||
}
|
||||
final decision = policy.evaluate(now: now(), fetchedAt: cached.fetchedAt);
|
||||
if (decision.shouldRefreshInBackground) {
|
||||
_refreshMonthInBackground(monthStart, key);
|
||||
}
|
||||
if (decision.mustBlockForNetwork || !decision.canUseCached) {
|
||||
return _refreshMonthAndRead(monthStart, key);
|
||||
}
|
||||
return cached.value;
|
||||
}
|
||||
|
||||
Future<List<ScheduleItemModel>> _refreshDayAndRead(
|
||||
DateTime date,
|
||||
String key,
|
||||
) async {
|
||||
await _refreshDay(date, key);
|
||||
final cached = await store.read<CacheEntry<List<ScheduleItemModel>>>(key);
|
||||
return cached?.value ?? const <ScheduleItemModel>[];
|
||||
}
|
||||
|
||||
Future<List<ScheduleItemModel>> _refreshMonthAndRead(
|
||||
DateTime monthStart,
|
||||
String key,
|
||||
) async {
|
||||
await _refreshMonth(monthStart, key);
|
||||
final cached = await store.read<CacheEntry<List<ScheduleItemModel>>>(key);
|
||||
return cached?.value ?? const <ScheduleItemModel>[];
|
||||
}
|
||||
|
||||
Future<void> _refreshDay(DateTime date, String key) async {
|
||||
final remote = await loadDayFromRemote(date);
|
||||
await store.write<CacheEntry<List<ScheduleItemModel>>>(
|
||||
key,
|
||||
CacheEntry<List<ScheduleItemModel>>(value: remote, fetchedAt: now()),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _refreshMonth(DateTime monthStart, String key) async {
|
||||
final start = DateTime(monthStart.year, monthStart.month, 1);
|
||||
final end = DateTime(monthStart.year, monthStart.month + 1, 0, 23, 59, 59);
|
||||
final remote = await loadMonthFromRemote(start, end);
|
||||
await store.write<CacheEntry<List<ScheduleItemModel>>>(
|
||||
key,
|
||||
CacheEntry<List<ScheduleItemModel>>(value: remote, fetchedAt: now()),
|
||||
);
|
||||
}
|
||||
|
||||
void _refreshDayInBackground(DateTime date, String key) {
|
||||
_refreshInBackground(key, () => _refreshDay(date, key));
|
||||
}
|
||||
|
||||
void _refreshMonthInBackground(DateTime monthStart, String key) {
|
||||
_refreshInBackground(key, () => _refreshMonth(monthStart, key));
|
||||
}
|
||||
|
||||
void _refreshInBackground(String key, Future<void> Function() taskFactory) {
|
||||
if (_refreshInFlight.containsKey(key)) {
|
||||
return;
|
||||
}
|
||||
final task = taskFactory().whenComplete(() {
|
||||
_refreshInFlight.remove(key);
|
||||
});
|
||||
_refreshInFlight[key] = task;
|
||||
unawaited(task);
|
||||
}
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
import 'package:social_app/core/network/i_api_client.dart';
|
||||
import 'package:social_app/core/cache/cache_invalidator.dart';
|
||||
import 'package:social_app/app/di/injection.dart';
|
||||
|
||||
import '../calendar_api.dart';
|
||||
import '../models/schedule_item_model.dart';
|
||||
|
||||
class CalendarService {
|
||||
final IApiClient _apiClient;
|
||||
CalendarApi? _calendarApi;
|
||||
|
||||
CalendarService({required IApiClient apiClient}) : _apiClient = apiClient;
|
||||
|
||||
CalendarApi get _api {
|
||||
final api = _calendarApi;
|
||||
if (api != null) {
|
||||
return api;
|
||||
}
|
||||
final created = CalendarApi(_apiClient);
|
||||
_calendarApi = created;
|
||||
return created;
|
||||
}
|
||||
|
||||
Future<List<ScheduleItemModel>> getEventsForDay(DateTime date) async {
|
||||
final start = DateTime(date.year, date.month, date.day);
|
||||
final end = DateTime(date.year, date.month, date.day, 23, 59, 59);
|
||||
return getEventsForRange(start, end);
|
||||
}
|
||||
|
||||
Future<List<ScheduleItemModel>> getEventsForRange(
|
||||
DateTime start,
|
||||
DateTime end,
|
||||
) async {
|
||||
return _api.listByRange(startAt: start, endAt: end);
|
||||
}
|
||||
|
||||
Future<ScheduleItemModel?> getEventById(String id) async {
|
||||
return _api.getById(id);
|
||||
}
|
||||
|
||||
Future<ScheduleItemModel> addEvent(ScheduleItemModel event) async {
|
||||
final created = await _api.create(event);
|
||||
_invalidateEventCache(created);
|
||||
return created;
|
||||
}
|
||||
|
||||
Future<ScheduleItemModel> updateEvent(ScheduleItemModel event) async {
|
||||
final updated = await _api.update(event);
|
||||
_invalidateEventCache(updated);
|
||||
return updated;
|
||||
}
|
||||
|
||||
Future<ScheduleItemModel?> archiveEvent(String id) async {
|
||||
final event = await getEventById(id);
|
||||
if (event == null) {
|
||||
return null;
|
||||
}
|
||||
final updatedEvent = await updateEvent(
|
||||
event.copyWith(status: ScheduleStatus.archived),
|
||||
);
|
||||
_invalidateEventCache(updatedEvent);
|
||||
return updatedEvent;
|
||||
}
|
||||
|
||||
void _invalidateEventCache(ScheduleItemModel event) {
|
||||
try {
|
||||
final invalidator = sl<CacheInvalidator>();
|
||||
var current = DateTime(
|
||||
event.startAt.year,
|
||||
event.startAt.month,
|
||||
event.startAt.day,
|
||||
);
|
||||
final end = DateTime(
|
||||
event.endAt?.year ?? event.startAt.year,
|
||||
event.endAt?.month ?? event.startAt.month,
|
||||
event.endAt?.day ?? event.startAt.day,
|
||||
);
|
||||
while (!current.isAfter(end)) {
|
||||
invalidator.invalidateCalendarDay(current);
|
||||
current = current.add(const Duration(days: 1));
|
||||
}
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
Future<void> deleteEvent(String id) async {
|
||||
final event = await getEventById(id);
|
||||
if (event != null) {
|
||||
_invalidateEventCache(event);
|
||||
}
|
||||
await _api.delete(id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user