52 lines
1.4 KiB
Dart
52 lines
1.4 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import '../models/rotation_config.dart';
|
|
import 'supabase_provider.dart';
|
|
|
|
/// Key used to store the rotation configuration in `app_settings`.
|
|
const _settingsKey = 'rotation_config';
|
|
|
|
/// Provides the current [RotationConfig] from `app_settings`.
|
|
final rotationConfigProvider = FutureProvider<RotationConfig>((ref) async {
|
|
final client = ref.watch(supabaseClientProvider);
|
|
final row = await client
|
|
.from('app_settings')
|
|
.select()
|
|
.eq('key', _settingsKey)
|
|
.maybeSingle();
|
|
if (row == null) return RotationConfig();
|
|
final value = row['value'];
|
|
if (value is Map<String, dynamic>) {
|
|
return RotationConfig.fromJson(value);
|
|
}
|
|
if (value is String) {
|
|
return RotationConfig.fromJson(jsonDecode(value) as Map<String, dynamic>);
|
|
}
|
|
return RotationConfig();
|
|
});
|
|
|
|
/// Controller for persisting [RotationConfig] changes.
|
|
final rotationConfigControllerProvider = Provider<RotationConfigController>((
|
|
ref,
|
|
) {
|
|
final client = ref.watch(supabaseClientProvider);
|
|
return RotationConfigController(client, ref);
|
|
});
|
|
|
|
class RotationConfigController {
|
|
RotationConfigController(this._client, this._ref);
|
|
|
|
final dynamic _client;
|
|
final Ref _ref;
|
|
|
|
Future<void> save(RotationConfig config) async {
|
|
await _client.from('app_settings').upsert({
|
|
'key': _settingsKey,
|
|
'value': config.toJson(),
|
|
});
|
|
_ref.invalidate(rotationConfigProvider);
|
|
}
|
|
}
|