import 'package:dio/dio.dart'; import 'package:miaoji_zhang/shared/api/api_client.dart'; class PushPreferences { final bool system; final bool budget; final bool operations; const PushPreferences({ this.system = false, this.budget = false, this.operations = false, }); bool get anyEnabled => system || budget || operations; PushPreferences copyWith({bool? system, bool? budget, bool? operations}) => PushPreferences( system: system ?? this.system, budget: budget ?? this.budget, operations: operations ?? this.operations, ); factory PushPreferences.fromJson(Map json) => PushPreferences( system: json['system'] as bool? ?? false, budget: json['budget'] as bool? ?? false, operations: json['operations'] as bool? ?? false, ); Map toJson() => { 'system': system, 'budget': budget, 'operations': operations, }; } class PushRegistration { final int deviceId; final String unbindToken; const PushRegistration({required this.deviceId, required this.unbindToken}); factory PushRegistration.fromJson(Map json) => PushRegistration( deviceId: (json['deviceId'] as num).toInt(), unbindToken: json['unbindToken'] as String, ); } class PushApi { static final Dio _dio = ApiClient.instance.dio; static Future preferences() async { final response = await _dio.get('/api/push/preferences'); return PushPreferences.fromJson(response.data as Map); } static Future updatePreferences( PushPreferences preferences, ) async { final response = await _dio.put( '/api/push/preferences', data: preferences.toJson(), ); return PushPreferences.fromJson(response.data as Map); } static Future registerDevice({ required String installationId, required String provider, required String token, required String packageName, required String flavor, required String appVersion, required int versionCode, required bool notificationsAllowed, }) async { final response = await _dio.put( '/api/push/devices/$installationId', data: { 'provider': provider, 'token': token, 'packageName': packageName, 'flavor': flavor, 'appVersion': appVersion, 'versionCode': versionCode, 'notificationsAllowed': notificationsAllowed, }, ); return PushRegistration.fromJson(response.data as Map); } static Future unregisterDevice({ required String installationId, String? unbindToken, }) async { await _dio.delete( '/api/push/devices/$installationId', options: unbindToken == null ? null : Options(headers: {'X-Push-Unbind-Token': unbindToken}), ); } }