From b6ea511fa0acb0fba4f6ef402d6511307a66f703 Mon Sep 17 00:00:00 2001 From: alper <29778028+iamalper@users.noreply.github.com> Date: Sat, 23 Sep 2023 17:01:08 +0300 Subject: [PATCH] feat: improve upload animation --- lib/classes/sender.dart | 18 +++++++++++------- lib/constants.dart | 2 ++ lib/screens/send_page.dart | 10 +++++++--- pubspec.lock | 8 ++++++++ pubspec.yaml | 1 + 5 files changed, 29 insertions(+), 10 deletions(-) diff --git a/lib/classes/sender.dart b/lib/classes/sender.dart index 0949ded..0f1bc4d 100644 --- a/lib/classes/sender.dart +++ b/lib/classes/sender.dart @@ -2,9 +2,11 @@ import 'dart:async'; import 'package:flutter/animation.dart'; import 'package:dio/dio.dart'; import 'package:weepy/classes/exceptions.dart'; +import 'package:weepy/constants.dart'; import '../models.dart'; import 'package:file_picker/file_picker.dart'; import 'database.dart'; +import 'package:num_remap/num_remap.dart'; ///Class for all Sending jobs. /// @@ -39,7 +41,7 @@ class Sender { .map((e) => MultipartFile.fromFileSync(e.path!, filename: e.name)) .toList(), }); - int uploadedPer100 = 0; + uploadAnimC?.animateTo(Assets.uploadAnimStart); final Response response; try { response = await _dio.post(device.uri.toString(), @@ -49,13 +51,15 @@ class Sender { Headers.contentLengthHeader: formData.length, }, ), onSendProgress: ((count, total) { - final totalPer100 = total / 100; - uploadedPer100 += count; - if (uploadedPer100 >= totalPer100) { - uploadAnimC?.value += 0.01; - uploadedPer100 - totalPer100; - } + final newValue = count / total; + assert(newValue <= 1.0 && newValue >= 0.0); + final mappedValue = newValue.remapAndClamp( + 0.0, 1.0, Assets.uploadAnimStart, Assets.uploadAnimEnd); + assert(mappedValue <= Assets.uploadAnimEnd && + mappedValue >= Assets.uploadAnimStart); + uploadAnimC?.animateTo(mappedValue.toDouble()); })); + uploadAnimC?.animateTo(1.0); } catch (_) { throw ConnectionLostException(); } diff --git a/lib/constants.dart b/lib/constants.dart index 989e76d..f497f6a 100644 --- a/lib/constants.dart +++ b/lib/constants.dart @@ -77,4 +77,6 @@ class Assets { void Function(LottieComposition) onLoaded) => Lottie.asset("assets/lottie/upload-animation.json", controller: animationController, onLoaded: onLoaded); + static const uploadAnimStart = 0.16; + static const uploadAnimEnd = 0.63; } diff --git a/lib/screens/send_page.dart b/lib/screens/send_page.dart index 4f33c1f..9248a65 100644 --- a/lib/screens/send_page.dart +++ b/lib/screens/send_page.dart @@ -45,12 +45,16 @@ class _SendPageInnerState extends ConsumerState @override void initState() { - _uploadAnimC = AnimationController(vsync: this) + _uploadAnimC = AnimationController( + vsync: this, + debugLabel: "Upload Animation Controller", + duration: const Duration(seconds: 1)) ..addListener(() { setState(() {}); }); - animation = Assets.upload(_uploadAnimC, - (composition) => _uploadAnimC.duration = composition.duration); + animation = Assets.upload(_uploadAnimC, (composition) { + _uploadAnimC.duration = composition.duration; + }); super.initState(); _discover(); } diff --git a/pubspec.lock b/pubspec.lock index 71b040d..ab935a6 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -501,6 +501,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.0.0" + num_remap: + dependency: "direct main" + description: + name: num_remap + sha256: "65da281ae014640e8c09a797d3c6a32902204168785f061f75ced6258caeacd7" + url: "https://pub.dev" + source: hosted + version: "1.0.1" open_filex: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index b621a6a..76e1115 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -38,6 +38,7 @@ dependencies: http: ^1.1.0 flutter_riverpod: ^2.4.0 cupertino_icons: ^1.0.6 + num_remap: ^1.0.1 dev_dependencies: flutter_test: