2022-04-24 04:08:45 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2023-11-29 06:20:00 +02:00
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
|
|
|
import 'package:immich_mobile/extensions/build_context_extensions.dart';
|
2024-02-23 07:18:02 +02:00
|
|
|
import 'package:immich_mobile/shared/ui/delayed_loading_indicator.dart';
|
2022-04-24 04:08:45 +02:00
|
|
|
|
2023-11-29 06:20:00 +02:00
|
|
|
final _loadingEntry = OverlayEntry(
|
|
|
|
builder: (context) => SizedBox.square(
|
|
|
|
dimension: double.infinity,
|
|
|
|
child: DecoratedBox(
|
|
|
|
decoration:
|
|
|
|
BoxDecoration(color: context.colorScheme.surface.withAlpha(200)),
|
2024-02-23 07:18:02 +02:00
|
|
|
child: const Center(
|
|
|
|
child: DelayedLoadingIndicator(
|
|
|
|
delay: Duration(seconds: 1),
|
|
|
|
fadeInDuration: Duration(milliseconds: 400),
|
|
|
|
),
|
|
|
|
),
|
2023-11-29 06:20:00 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
ValueNotifier<bool> useProcessingOverlay() {
|
|
|
|
return use(const _LoadingOverlay());
|
|
|
|
}
|
|
|
|
|
|
|
|
class _LoadingOverlay extends Hook<ValueNotifier<bool>> {
|
|
|
|
const _LoadingOverlay();
|
2022-04-24 04:08:45 +02:00
|
|
|
|
|
|
|
@override
|
2023-11-29 06:20:00 +02:00
|
|
|
_LoadingOverlayState createState() => _LoadingOverlayState();
|
2022-04-24 04:08:45 +02:00
|
|
|
}
|
|
|
|
|
2023-11-29 06:20:00 +02:00
|
|
|
class _LoadingOverlayState
|
|
|
|
extends HookState<ValueNotifier<bool>, _LoadingOverlay> {
|
2024-02-23 07:18:02 +02:00
|
|
|
late final _isLoading = ValueNotifier(false)..addListener(_listener);
|
|
|
|
OverlayEntry? _loadingOverlay;
|
2023-11-29 06:20:00 +02:00
|
|
|
|
|
|
|
void _listener() {
|
|
|
|
setState(() {
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
2024-02-23 07:18:02 +02:00
|
|
|
if (_isLoading.value) {
|
|
|
|
_loadingOverlay?.remove();
|
|
|
|
_loadingOverlay = _loadingEntry;
|
2023-11-29 06:20:00 +02:00
|
|
|
Overlay.of(context).insert(_loadingEntry);
|
|
|
|
} else {
|
2024-02-23 07:18:02 +02:00
|
|
|
_loadingOverlay?.remove();
|
|
|
|
_loadingOverlay = null;
|
2023-11-29 06:20:00 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2022-04-24 04:08:45 +02:00
|
|
|
|
2023-11-29 06:20:00 +02:00
|
|
|
@override
|
|
|
|
ValueNotifier<bool> build(BuildContext context) {
|
2024-02-23 07:18:02 +02:00
|
|
|
return _isLoading;
|
2022-04-24 04:08:45 +02:00
|
|
|
}
|
|
|
|
|
2023-11-29 06:20:00 +02:00
|
|
|
@override
|
|
|
|
void dispose() {
|
2024-02-23 07:18:02 +02:00
|
|
|
_isLoading.dispose();
|
2023-11-29 06:20:00 +02:00
|
|
|
super.dispose();
|
2022-04-24 04:08:45 +02:00
|
|
|
}
|
2023-11-29 06:20:00 +02:00
|
|
|
|
|
|
|
@override
|
2024-02-23 07:18:02 +02:00
|
|
|
Object? get debugValue => _isLoading.value;
|
2023-11-29 06:20:00 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
String get debugLabel => 'useProcessingOverlay<>';
|
2022-04-24 04:08:45 +02:00
|
|
|
}
|