prepare for multiple apps

This commit is contained in:
Fabian Freund
2026-04-06 12:23:11 +02:00
parent bd1600e8dc
commit 5afc323f04
904 changed files with 29 additions and 29 deletions
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2024-2026 Fabian Freund.
*
* This file is part of WebLibre
* (see https://weblibre.eu).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:flutter/widgets.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
AsyncSnapshot<T> useCachedFuture<T>(
Future<T> Function() valueBuilder, [
List<Object?> keys = const <Object>[],
]) {
// ignore: discarded_futures is used
final cachedFuture = useMemoized(valueBuilder, keys);
return useFuture(cachedFuture);
}
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2024-2026 Fabian Freund.
*
* This file is part of WebLibre
* (see https://weblibre.eu).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:weblibre/utils/debouncer.dart';
/// A hook that creates a [Debouncer] with automatic disposal.
///
/// The debouncer will be created once and automatically disposed when the
/// widget is unmounted.
///
/// Example:
/// ```dart
/// final debouncer = useDebouncer(const Duration(milliseconds: 300));
///
/// // Use the debouncer
/// debouncer.eventOccured(() {
/// // Your debounced action
/// });
/// ```
Debouncer useDebouncer(Duration duration) {
final debouncer = useMemoized(() => Debouncer(duration));
useEffect(() => debouncer.dispose, [debouncer]);
return debouncer;
}
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2024-2026 Fabian Freund.
*
* This file is part of WebLibre
* (see https://weblibre.eu).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
MenuController useMenuController() {
return useMemoized(() => MenuController());
}
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2024-2026 Fabian Freund.
*
* This file is part of WebLibre
* (see https://weblibre.eu).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:flutter/foundation.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
void useOnDispose(VoidCallback onDispose) {
useEffect(() {
return onDispose;
}, const []);
}
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2024-2026 Fabian Freund.
*
* This file is part of WebLibre
* (see https://weblibre.eu).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'dart:async';
import 'package:flutter_hooks/flutter_hooks.dart';
void useOnInitialization(FutureOr<void> Function() callback) {
useEffect(() {
unawaited(Future(callback));
return null;
}, []);
}
@@ -0,0 +1,104 @@
/*
* Copyright (c) 2024-2026 Fabian Freund.
*
* This file is part of WebLibre
* (see https://weblibre.eu).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
/// Adds a given [listener] to a [Listenable] and removes it when the hook is
/// disposed. The listener is only called when the [selector] result changes.
///
/// As opposed to `useListenable`, this hook does not mark the widget as needing
/// build when the listener is called. Use this for side effects that do not
/// require a rebuild.
///
/// See also:
/// * [Listenable]
/// * [ValueListenable]
/// * [useOnListenableChange]
void useOnListenableChangeSelector<R>(
Listenable? listenable,
R Function() selector,
VoidCallback listener,
) {
return use(_OnListenableChangeSelectorHook(listenable, selector, listener));
}
class _OnListenableChangeSelectorHook<R> extends Hook<void> {
const _OnListenableChangeSelectorHook(
this.listenable,
this.selector,
this.listener,
);
final Listenable? listenable;
final R Function() selector;
final VoidCallback listener;
@override
_OnListenableChangeSelectorHookState<R> createState() =>
_OnListenableChangeSelectorHookState<R>();
}
class _OnListenableChangeSelectorHookState<R>
extends HookState<void, _OnListenableChangeSelectorHook<R>> {
late R _selectorResult = hook.selector();
@override
void initHook() {
super.initHook();
hook.listenable?.addListener(_listener);
}
@override
void didUpdateHook(_OnListenableChangeSelectorHook<R> oldHook) {
super.didUpdateHook(oldHook);
if (hook.selector != oldHook.selector) {
_selectorResult = hook.selector();
}
if (hook.listenable != oldHook.listenable) {
oldHook.listenable?.removeListener(_listener);
hook.listenable?.addListener(_listener);
_selectorResult = hook.selector();
}
}
@override
void build(BuildContext context) {}
void _listener() {
final latestSelectorResult = hook.selector();
if (_selectorResult != latestSelectorResult) {
_selectorResult = latestSelectorResult;
hook.listener();
}
}
@override
void dispose() {
hook.listenable?.removeListener(_listener);
}
@override
String get debugLabel => 'useOnListenableChangeSelector<$R>';
@override
Object? get debugValue => hook.listenable;
}
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2024-2026 Fabian Freund.
*
* This file is part of WebLibre
* (see https://weblibre.eu).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:flutter/widgets.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:weblibre/utils/sampled_value_notifier.dart';
/// A custom hook that creates a sampled ValueNotifier from another ValueNotifier
ValueNotifier<T> useSampledValueNotifier<T>({
required ValueNotifier<T> source,
required Duration sampleDuration,
}) {
return use(
_SampledValueNotifierHook<T>(
source: source,
sampleDuration: sampleDuration,
),
);
}
/// Hook implementation for sampled ValueNotifier
class _SampledValueNotifierHook<T> extends Hook<ValueNotifier<T>> {
final ValueNotifier<T> source;
final Duration sampleDuration;
const _SampledValueNotifierHook({
required this.source,
required this.sampleDuration,
});
@override
_SampledValueNotifierHookState<T> createState() =>
_SampledValueNotifierHookState<T>();
}
class _SampledValueNotifierHookState<T>
extends HookState<ValueNotifier<T>, _SampledValueNotifierHook<T>> {
late SampledValueNotifier<T> _sampledNotifier;
@override
void initHook() {
super.initHook();
_sampledNotifier = SampledValueNotifier<T>(
source: hook.source,
sampleDuration: hook.sampleDuration,
);
}
@override
ValueNotifier<T> build(BuildContext context) {
return _sampledNotifier;
}
@override
void dispose() {
_sampledNotifier.dispose();
super.dispose();
}
@override
String get debugLabel => 'useSampledValueNotifier<$T>';
}
@@ -0,0 +1,102 @@
/*
* Copyright (c) 2024-2026 Fabian Freund.
*
* This file is part of WebLibre
* (see https://weblibre.eu).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
/// A hook that manages visibility state based on scroll direction.
///
/// Returns a [ValueNotifier<bool>] that automatically updates based on scroll behavior:
/// - Hides when scrolling down beyond [hideThreshold]
/// - Shows when scrolling up beyond [showThreshold] or at the top of the scroll view
/// - Ignores scroll events during [initializationDelay] to prevent hiding during jumpTo/animateTo
///
/// Example:
/// ```dart
/// final scrollController = useScrollController();
/// final isVisible = useScrollVisibility(scrollController);
///
/// return AnimatedOpacity(
/// opacity: isVisible.value ? 1.0 : 0.0,
/// child: FloatingActionButton(...),
/// );
/// ```
ValueNotifier<bool> useScrollVisibility(
ScrollController scrollController, {
double hideThreshold = 5.0,
double showThreshold = 5.0,
Duration initializationDelay = const Duration(milliseconds: 1000),
bool disableAnimations = false,
}) {
final isVisible = useState(true);
final lastScrollOffset = useRef(0.0);
final isInitialized = useRef(false);
useEffect(
() {
// Start timer to enable scroll listener after initialization delay
final timer = Timer(
disableAnimations ? Duration.zero : initializationDelay,
() {
if (scrollController.hasClients) {
lastScrollOffset.value = scrollController.offset;
}
isInitialized.value = true;
},
);
void scrollListener() {
// Ignore scroll events until initialization is complete
if (!isInitialized.value) return;
final currentOffset = scrollController.offset;
final difference = currentOffset - lastScrollOffset.value;
// Hide when scrolling down beyond threshold
if (difference > hideThreshold && isVisible.value) {
isVisible.value = false;
}
// Show when scrolling up beyond threshold or at top
else if ((difference < -showThreshold || currentOffset <= 0) &&
!isVisible.value) {
isVisible.value = true;
}
lastScrollOffset.value = currentOffset;
}
scrollController.addListener(scrollListener);
return () {
timer.cancel();
scrollController.removeListener(scrollListener);
};
},
[
scrollController,
hideThreshold,
showThreshold,
initializationDelay,
disableAnimations,
],
);
return isVisible;
}
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2024-2026 Fabian Freund.
*
* This file is part of WebLibre
* (see https://weblibre.eu).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
void useSyncPageWithTab(
TabController tabController,
PageController pageController, {
void Function(int index)? onIndexChanged,
bool disableAnimations = false,
}) {
useEffect(() {
Future<void> syncPage() async {
if (disableAnimations) {
pageController.jumpToPage(tabController.index);
} else {
await pageController.animateToPage(
tabController.index,
curve: Curves.linear,
duration: const Duration(milliseconds: 300),
);
}
onIndexChanged?.call(tabController.index);
}
void syncTab() {
if (!tabController.indexIsChanging) {
final index = pageController.page!.round();
tabController.animateTo(
index,
duration: disableAnimations ? Duration.zero : kTabScrollDuration,
);
onIndexChanged?.call(index);
}
}
tabController.addListener(syncPage);
pageController.addListener(syncTab);
return () {
tabController.removeListener(syncPage);
pageController.removeListener(syncTab);
};
}, [tabController, pageController, disableAnimations]);
}