88 lines
2.7 KiB
Dart
88 lines
2.7 KiB
Dart
import 'package:f0ckapp/providers/MediaProvider.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import 'package:f0ckapp/screens/MediaGrid.dart';
|
|
import 'package:f0ckapp/screens/DetailView.dart';
|
|
import 'package:f0ckapp/utils/AppVersion.dart';
|
|
import 'package:f0ckapp/providers/ThemeProvider.dart';
|
|
|
|
void main() async {
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
|
|
await AppVersion.init();
|
|
|
|
runApp(ProviderScope(child: F0ckApp()));
|
|
}
|
|
|
|
class F0ckApp extends ConsumerWidget {
|
|
F0ckApp({super.key});
|
|
|
|
final GoRouter _router = GoRouter(
|
|
initialLocation: '/',
|
|
routes: [
|
|
GoRoute(
|
|
path: '/',
|
|
builder: (context, state) {
|
|
return const MediaGrid();
|
|
},
|
|
),
|
|
GoRoute(
|
|
path: '/:rest(.*)',
|
|
builder: (context, state) {
|
|
final isInternalLink = (state.extra is bool && state.extra == true);
|
|
final fullPath = state.matchedLocation;
|
|
|
|
final regExp = RegExp(
|
|
r'^(?:/tag/(?<tag>.+?))?(?:/(?<mime>image|audio|video))?(?:/(?<itemid>\d+))?$',
|
|
);
|
|
final match = regExp.firstMatch(fullPath);
|
|
|
|
if (match == null) {
|
|
return const Scaffold(body: Center(child: Text('Ungültiger Link')));
|
|
}
|
|
|
|
final String? tag = match.namedGroup('tag');
|
|
final String? mime = match.namedGroup('mime');
|
|
final String? idStr = match.namedGroup('itemid');
|
|
final int? itemId = idStr != null ? int.tryParse(idStr) : null;
|
|
const preloadOffset = 50;
|
|
|
|
return Consumer(
|
|
builder: (context, ref, child) {
|
|
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
|
final mediaNotifier = ref.read(mediaProvider.notifier);
|
|
if (!isInternalLink) {
|
|
mediaNotifier.setType(mime ?? "alles");
|
|
mediaNotifier.setTag(tag);
|
|
}
|
|
if (itemId != null) {
|
|
await mediaNotifier.loadMedia(id: itemId + preloadOffset);
|
|
}
|
|
});
|
|
if (itemId != null) {
|
|
return DetailView(initialItemId: itemId);
|
|
} else {
|
|
return MediaGrid();
|
|
}
|
|
},
|
|
);
|
|
},
|
|
),
|
|
],
|
|
);
|
|
|
|
@override
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
final theme = ref.watch(themeNotifierProvider);
|
|
return MaterialApp.router(
|
|
debugShowCheckedModeBanner: false,
|
|
routerConfig: _router,
|
|
theme: theme,
|
|
);
|
|
}
|
|
}
|