Skip to content
This repository has been archived by the owner on Sep 22, 2024. It is now read-only.

Commit

Permalink
feat: improve upload animation
Browse files Browse the repository at this point in the history
  • Loading branch information
iamalper committed Sep 23, 2023
1 parent 61f44a8 commit b6ea511
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
18 changes: 11 additions & 7 deletions lib/classes/sender.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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<void> response;
try {
response = await _dio.post<void>(device.uri.toString(),
Expand All @@ -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();
}
Expand Down
2 changes: 2 additions & 0 deletions lib/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
10 changes: 7 additions & 3 deletions lib/screens/send_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ class _SendPageInnerState extends ConsumerState<SendPageInner>

@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();
}
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit b6ea511

Please sign in to comment.