refactor(apps): 主题系统迁移至 ColorScheme + 扩展架构并支持 Dark Mode
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import '../../data/models/reminder_payload.dart';
|
||||
|
||||
class AppPreferences {
|
||||
static const String _pendingNotificationsKey =
|
||||
'calendar_reminder_pending_notification_responses_v1';
|
||||
static const String _pendingNotificationPayloadKey =
|
||||
'pending_notification_payload';
|
||||
|
||||
final SharedPreferences _prefs;
|
||||
|
||||
AppPreferences(this._prefs);
|
||||
|
||||
List<PendingNotificationResponse> get pendingNotifications {
|
||||
final list = _prefs.getStringList(_pendingNotificationsKey) ?? [];
|
||||
return list
|
||||
.map(_decodePendingNotification)
|
||||
.whereType<PendingNotificationResponse>()
|
||||
.toList();
|
||||
}
|
||||
|
||||
Future<void> setPendingNotifications(
|
||||
List<PendingNotificationResponse> value,
|
||||
) {
|
||||
return _prefs.setStringList(
|
||||
_pendingNotificationsKey,
|
||||
value.map(_encodePendingNotification).toList(),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> clearPendingNotifications() {
|
||||
return _prefs.remove(_pendingNotificationsKey);
|
||||
}
|
||||
|
||||
ReminderPayload? get pendingNotificationPayload {
|
||||
final raw = _prefs.getString(_pendingNotificationPayloadKey);
|
||||
if (raw == null || raw.isEmpty) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
final json = Map<String, dynamic>.from(jsonDecode(raw) as Map);
|
||||
return ReminderPayload.fromJson(json);
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> setPendingNotificationPayload(ReminderPayload payload) {
|
||||
return _prefs.setString(
|
||||
_pendingNotificationPayloadKey,
|
||||
jsonEncode(payload.toJson()),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> clearPendingNotificationPayload() {
|
||||
return _prefs.remove(_pendingNotificationPayloadKey);
|
||||
}
|
||||
|
||||
static String _encodePendingNotification(
|
||||
PendingNotificationResponse response,
|
||||
) {
|
||||
return jsonEncode({
|
||||
'id': response.id,
|
||||
'actionId': response.actionId,
|
||||
'payload': response.payload,
|
||||
'type': response.notificationResponseType.index,
|
||||
'input': response.input,
|
||||
});
|
||||
}
|
||||
|
||||
static PendingNotificationResponse? _decodePendingNotification(String raw) {
|
||||
try {
|
||||
final parsed = Map<String, dynamic>.from(jsonDecode(raw) as Map);
|
||||
final id = parsed['id'] as int?;
|
||||
final actionId = parsed['actionId'] as String?;
|
||||
final payload = parsed['payload'] as String?;
|
||||
final typeIndex = (parsed['type'] as int?) ?? 0;
|
||||
final input = parsed['input'] as String?;
|
||||
final type = NotificationResponseType.values[typeIndex.clamp(0, 1)];
|
||||
return NotificationResponse(
|
||||
id: id,
|
||||
actionId: actionId,
|
||||
payload: payload,
|
||||
input: input,
|
||||
notificationResponseType: type,
|
||||
);
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
typedef PendingNotificationResponse = NotificationResponse;
|
||||
Reference in New Issue
Block a user