111 lines
3.2 KiB
Dart
111 lines
3.2 KiB
Dart
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<MediaItem> mediaItems = <MediaItem>[].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<PageTransition> transitionType = PageTransition.opacity.obs;
|
|
|
|
@override
|
|
void onInit() async {
|
|
super.onInit();
|
|
await loadSettings();
|
|
}
|
|
|
|
Future<void> 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<void> 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<void> setTag(String? newTag) async {
|
|
tag.value = newTag;
|
|
await loadMediaItems();
|
|
}
|
|
|
|
Future<void> setType(int newType) async {
|
|
type.value = newType;
|
|
await loadMediaItems();
|
|
}
|
|
|
|
Future<void> setMode(int newMode) async {
|
|
mode.value = newMode;
|
|
await loadMediaItems();
|
|
}
|
|
|
|
Future<void> toggleRandom() async {
|
|
random.value = !random.value;
|
|
await loadMediaItems();
|
|
}
|
|
|
|
Future<void> toggleMuted() async {
|
|
muted.value = !muted.value;
|
|
await saveSettings();
|
|
}
|
|
|
|
Future<void> setCrossAxisCount(int newCrossAxisCount) async {
|
|
crossAxisCount.value = newCrossAxisCount;
|
|
await saveSettings();
|
|
}
|
|
|
|
Future<void> setDrawerSwipeEnabled(bool newValue) async {
|
|
drawerSwipeEnabled.value = newValue;
|
|
await saveSettings();
|
|
}
|
|
|
|
Future<void> setTransitionType(PageTransition newType) async {
|
|
transitionType.value = newType;
|
|
await saveSettings();
|
|
}
|
|
|
|
Future<void> loadMediaItems({int? older, bool append = false}) async {
|
|
if (isLoading.value) return;
|
|
try {
|
|
isLoading.value = true;
|
|
final List<MediaItem> 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;
|
|
}
|
|
}
|
|
}
|