Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Update utils

vishnukvmdc392ad5dcb94
diff --git a/mobile/packages/utils/lib/debouncer.dart b/mobile/packages/utils/lib/debouncer.dart
new file mode 100644
index 0000000000..fe1d300cbf
--- /dev/null
+++ b/mobile/packages/utils/lib/debouncer.dart
@@ -0,0 +1,34 @@
+import 'dart:async';
+
+import 'package:flutter/material.dart';
+
+class Debouncer {
+ final Duration _duration;
+ final ValueNotifier<bool> _debounceActiveNotifier = ValueNotifier(false);
+ Timer? _debounceTimer;
+
+ Debouncer(this._duration);
+
+ void run(Future<void> Function() fn) {
+ if (isActive()) {
+ _debounceTimer!.cancel();
+ }
+ _debounceTimer = Timer(_duration, () async {
+ await fn();
+ _debounceActiveNotifier.value = false;
+ });
+ _debounceActiveNotifier.value = true;
+ }
+
+ void cancelDebounce() {
+ if (_debounceTimer != null) {
+ _debounceTimer!.cancel();
+ }
+ }
+
+ bool isActive() => _debounceTimer != null && _debounceTimer!.isActive;
+
+ ValueNotifier<bool> get debounceActiveNotifier {
+ return _debounceActiveNotifier;
+ }
+}
diff --git a/mobile/packages/utils/lib/ente_utils.dart b/mobile/packages/utils/lib/ente_utils.dart
index b431dc7ff9..1f18b2af82 100644
--- a/mobile/packages/utils/lib/ente_utils.dart
+++ b/mobile/packages/utils/lib/ente_utils.dart
@@ -1,6 +1,9 @@
library ente_utils;
+export 'debouncer.dart';
export 'directory_utils.dart';
export 'email_util.dart';
+export 'fake_progress.dart';
export 'platform_util.dart';
export 'share_utils.dart';
+export 'navigation_util.dart';
diff --git a/mobile/packages/utils/lib/fake_progress.dart b/mobile/packages/utils/lib/fake_progress.dart
new file mode 100644
index 0000000000..9f4a847918
--- /dev/null
+++ b/mobile/packages/utils/lib/fake_progress.dart
@@ -0,0 +1,44 @@
+import 'dart:async';
+
+import "package:flutter/foundation.dart";
+
+typedef FakeProgressCallback = void Function(int count);
+
+class FakePeriodicProgress {
+ final FakeProgressCallback? callback;
+ final Duration duration;
+ Timer? _timer;
+ bool _shouldRun = true;
+ int runCount = 0;
+
+ FakePeriodicProgress({
+ required this.callback,
+ required this.duration,
+ });
+
+ void start() {
+ assert(_shouldRun, "Cannot start a stopped FakePeriodicProgress");
+ Future.delayed(duration, _invokePeriodically);
+ }
+
+ void stop() {
+ if (_shouldRun) {
+ _shouldRun = false;
+ _timer?.cancel();
+ }
+ }
+
+ void _invokePeriodically() {
+ if (_shouldRun) {
+ try {
+ runCount++;
+ callback?.call(runCount);
+ } catch (e) {
+ debugPrint("Error in FakePeriodicProgress callback: $e");
+ stop();
+ return;
+ }
+ _timer = Timer(duration, _invokePeriodically);
+ }
+ }
+}
diff --git a/mobile/packages/utils/lib/navigation_util.dart b/mobile/packages/utils/lib/navigation_util.dart
new file mode 100644
index 0000000000..58aa0fb852
--- /dev/null
+++ b/mobile/packages/utils/lib/navigation_util.dart
@@ -0,0 +1,149 @@
+import 'dart:io';
+
+import 'package:flutter/material.dart';
+
+Future<T?> routeToPage<T extends Object>(
+ BuildContext context,
+ Widget page, {
+ bool forceCustomPageRoute = false,
+}) {
+ if (Platform.isAndroid || forceCustomPageRoute) {
+ return Navigator.of(context).push(
+ _buildPageRoute(page),
+ );
+ } else {
+ return Navigator.of(context).push(
+ SwipeableRouteBuilder(
+ pageBuilder: (context, animation, secondaryAnimation) {
+ return page;
+ },
+ ),
+ );
+ }
+}
+
+void replacePage(BuildContext context, Widget page) {
+ Navigator.of(context).pushReplacement(
+ _buildPageRoute(page),
+ );
+}
+
+PageRouteBuilder<T> _buildPageRoute<T extends Object>(Widget page) {
+ return PageRouteBuilder(
+ pageBuilder: (
+ BuildContext context,
+ Animation<double> animation,
+ Animation<double> secondaryAnimation,
+ ) {
+ return page;
+ },
+ transitionsBuilder: (
+ BuildContext context,
+ Animation<double> animation,
+ Animation<double> secondaryAnimation,
+ Widget child,
+ ) {
+ return Align(
+ child: FadeTransition(
+ opacity: animation,
+ child: child,
+ ),
+ );
+ },
+ transitionDuration: const Duration(milliseconds: 200),
+ opaque: false,
+ );
+}
+
+class SwipeableRouteBuilder<T> extends PageRoute<T> {
+ final RoutePageBuilder pageBuilder;
+ final PageTransitionsBuilder matchingBuilder =
+ const CupertinoPageTransitionsBuilder(); // Default iOS/macOS (to get the swipe right to go back gesture)
+ // final PageTransitionsBuilder matchingBuilder = const FadeUpwardsPageTransitionsBuilder(); // Default Android/Linux/Windows
+
+ SwipeableRouteBuilder({required this.pageBuilder});
+
+ @override
+ Null get barrierColor => null;
+
+ @override
+ Null get barrierLabel => null;
+
+ @override
+ Widget buildPage(
+ BuildContext context,
+ Animation<double> animation,
+ Animation<double> secondaryAnimation,
+ ) {
+ return pageBuilder(context, animation, secondaryAnimation);
+ }
+
+ @override
+ bool get maintainState => true;
+
+ @override
+ Duration get transitionDuration => const Duration(
+ milliseconds: 300,
+ ); // Can give custom Duration, unlike in MaterialPageRoute
+
+ @override
+ Widget buildTransitions(
+ BuildContext context,
+ Animation<double> animation,
+ Animation<double> secondaryAnimation,
+ Widget child,
+ ) {
+ return matchingBuilder.buildTransitions<T>(
+ this,
+ context,
+ animation,
+ secondaryAnimation,
+ child,
+ );
+ }
+
+ @override
+ bool get opaque => false;
+}
+
+class TransparentRoute extends PageRoute<void> {
+ TransparentRoute({
+ required this.builder,
+ super.settings,
+ }) : assert(builder != null),
+ super(fullscreenDialog: false);
+
+ final WidgetBuilder? builder;
+
+ @override
+ bool get opaque => false;
+
+ @override
+ Null get barrierColor => null;
+
+ @override
+ Null get barrierLabel => null;
+
+ @override
+ bool get maintainState => true;
+
+ @override
+ Duration get transitionDuration => const Duration(milliseconds: 200);
+
+ @override
+ Widget buildPage(
+ BuildContext context,
+ Animation<double> animation,
+ Animation<double> secondaryAnimation,
+ ) {
+ final result = builder!(context);
+ return FadeTransition(
+ opacity: Tween<double>(begin: 0, end: 1).animate(animation),
+ child: Semantics(
+ scopesRoute: true,
+ explicitChildNodes: true,
+ child: result,
+ ),
+ );
+ }
+}