99 lines
2.4 KiB
Dart
99 lines
2.4 KiB
Dart
// lib/features/tournament/presentation/provider/shuffle_settings_provider.dart
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:hive/hive.dart';
|
|
|
|
part 'shuffle_settings_provider.g.dart'; // Für den Hive-Generator
|
|
|
|
// Enum mit Hive-Annotationen
|
|
@HiveType(typeId: 5) // Freie typeId wählen (z. B. 5, nicht bereits vergeben)
|
|
enum ShuffleMode {
|
|
@HiveField(0)
|
|
random,
|
|
|
|
@HiveField(1)
|
|
balanced,
|
|
|
|
@HiveField(2)
|
|
aggressive,
|
|
|
|
@HiveField(3)
|
|
positionBased,
|
|
}
|
|
|
|
// ShuffleSettings-Klasse
|
|
@HiveType(typeId: 4)
|
|
class ShuffleSettings extends HiveObject {
|
|
@HiveField(0)
|
|
ShuffleMode mode;
|
|
|
|
@HiveField(1)
|
|
bool prioritizeGenderBalance;
|
|
|
|
@HiveField(2)
|
|
bool prioritizeLevelBalance;
|
|
|
|
@HiveField(3)
|
|
bool usePositions;
|
|
|
|
ShuffleSettings({
|
|
this.mode = ShuffleMode.balanced,
|
|
this.prioritizeGenderBalance = true,
|
|
this.prioritizeLevelBalance = true,
|
|
this.usePositions = true,
|
|
});
|
|
|
|
// copyWith für immutable Updates
|
|
ShuffleSettings copyWith({
|
|
ShuffleMode? mode,
|
|
bool? prioritizeGenderBalance,
|
|
bool? prioritizeLevelBalance,
|
|
bool? usePositions,
|
|
}) {
|
|
return ShuffleSettings(
|
|
mode: mode ?? this.mode,
|
|
prioritizeGenderBalance: prioritizeGenderBalance ?? this.prioritizeGenderBalance,
|
|
prioritizeLevelBalance: prioritizeLevelBalance ?? this.prioritizeLevelBalance,
|
|
usePositions: usePositions ?? this.usePositions,
|
|
);
|
|
}
|
|
}
|
|
|
|
// Provider
|
|
final shuffleSettingsProvider = StateNotifierProvider<ShuffleSettingsNotifier, ShuffleSettings>((ref) {
|
|
return ShuffleSettingsNotifier();
|
|
});
|
|
|
|
class ShuffleSettingsNotifier extends StateNotifier<ShuffleSettings> {
|
|
static const String _boxName = 'shuffle_settings';
|
|
static const String _key = 'settings';
|
|
|
|
ShuffleSettingsNotifier() : super(ShuffleSettings()) {
|
|
_load();
|
|
}
|
|
|
|
Future<void> _load() async {
|
|
final box = await Hive.openBox<ShuffleSettings>(_boxName);
|
|
final saved = box.get(_key);
|
|
if (saved != null) {
|
|
state = saved;
|
|
}
|
|
}
|
|
|
|
Future<void> update({
|
|
ShuffleMode? mode,
|
|
bool? prioritizeGenderBalance,
|
|
bool? prioritizeLevelBalance,
|
|
bool? usePositions,
|
|
}) async {
|
|
state = state.copyWith(
|
|
mode: mode,
|
|
prioritizeGenderBalance: prioritizeGenderBalance,
|
|
prioritizeLevelBalance: prioritizeLevelBalance,
|
|
usePositions: usePositions,
|
|
);
|
|
|
|
final box = await Hive.openBox<ShuffleSettings>(_boxName);
|
|
await box.put(_key, state);
|
|
}
|
|
} |