import 'package:encrypt_shared_preferences/provider.dart'; import 'package:f0ckapp/utils/animatedtransition.dart'; import 'package:get/get.dart'; import 'package:f0ckapp/service/media_service.dart'; import 'package:f0ckapp/models/media_item.dart'; class MediaController extends GetxController { final EncryptedSharedPreferencesAsync storage = EncryptedSharedPreferencesAsync.getInstance(); final RxList mediaItems = [].obs; final RxBool isLoading = false.obs; final RxString errorMessage = ''.obs; final MediaService _mediaService = MediaService(); RxnString tag = RxnString(); RxInt type = 0.obs; RxInt mode = 0.obs; RxBool random = false.obs; late RxBool muted = false.obs; late RxInt crossAxisCount = 0.obs; late RxBool drawerSwipeEnabled = true.obs; final Rx transitionType = PageTransition.opacity.obs; @override void onInit() async { super.onInit(); await loadSettings(); } Future loadSettings() async { muted.value = await storage.getBoolean('muted') ?? false; crossAxisCount.value = await storage.getInt('crossAxisCount') ?? 0; drawerSwipeEnabled.value = await storage.getBoolean('drawerSwipeEnabled') ?? true; transitionType.value = PageTransition.values[await storage.getInt('transitionType') ?? 0]; } Future saveSettings() async { await storage.setBoolean('muted', muted.value); await storage.setInt('crossAxisCount', crossAxisCount.value); await storage.setBoolean('drawerSwipeEnabled', drawerSwipeEnabled.value); await storage.setInt('transitionType', transitionType.value.index); } Future setTag(String? newTag) async { tag.value = newTag; await loadMediaItems(); } Future setType(int newType) async { type.value = newType; await loadMediaItems(); } Future setMode(int newMode) async { mode.value = newMode; await loadMediaItems(); } Future toggleRandom() async { random.value = !random.value; await loadMediaItems(); } Future toggleMuted() async { muted.value = !muted.value; await saveSettings(); } Future setCrossAxisCount(int newCrossAxisCount) async { crossAxisCount.value = newCrossAxisCount; await saveSettings(); } Future setDrawerSwipeEnabled(bool newValue) async { drawerSwipeEnabled.value = newValue; await saveSettings(); } Future setTransitionType(PageTransition newType) async { transitionType.value = newType; await saveSettings(); } Future loadMediaItems({int? older, bool append = false}) async { if (isLoading.value) return; try { isLoading.value = true; final List items = await _mediaService.fetchMediaItems( type: type.value, mode: mode.value, random: random.value ? 1 : 0, tag: tag.value, older: older, ); append ? mediaItems.addAll(items) : mediaItems.assignAll(items); errorMessage.value = ''; } catch (e) { errorMessage.value = 'Fehler beim Laden der Daten: ${e.toString()}'; Get.snackbar('Error', e.toString()); } finally { isLoading.value = false; } } }