106 lines
2.9 KiB
Dart
106 lines
2.9 KiB
Dart
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<String, dynamic> json) =>
|
|
PushPreferences(
|
|
system: json['system'] as bool? ?? false,
|
|
budget: json['budget'] as bool? ?? false,
|
|
operations: json['operations'] as bool? ?? false,
|
|
);
|
|
|
|
Map<String, dynamic> 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<String, dynamic> json) =>
|
|
PushRegistration(
|
|
deviceId: (json['deviceId'] as num).toInt(),
|
|
unbindToken: json['unbindToken'] as String,
|
|
);
|
|
}
|
|
|
|
class PushApi {
|
|
static final Dio _dio = ApiClient.instance.dio;
|
|
|
|
static Future<PushPreferences> preferences() async {
|
|
final response = await _dio.get('/api/push/preferences');
|
|
return PushPreferences.fromJson(response.data as Map<String, dynamic>);
|
|
}
|
|
|
|
static Future<PushPreferences> updatePreferences(
|
|
PushPreferences preferences,
|
|
) async {
|
|
final response = await _dio.put(
|
|
'/api/push/preferences',
|
|
data: preferences.toJson(),
|
|
);
|
|
return PushPreferences.fromJson(response.data as Map<String, dynamic>);
|
|
}
|
|
|
|
static Future<PushRegistration> 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<String, dynamic>);
|
|
}
|
|
|
|
static Future<void> unregisterDevice({
|
|
required String installationId,
|
|
String? unbindToken,
|
|
}) async {
|
|
await _dio.delete<void>(
|
|
'/api/push/devices/$installationId',
|
|
options: unbindToken == null
|
|
? null
|
|
: Options(headers: {'X-Push-Unbind-Token': unbindToken}),
|
|
);
|
|
}
|
|
}
|