Skip to content

Commit

Permalink
feat: pull to refresh m3u playlists
Browse files Browse the repository at this point in the history
for main playlists page and m3u playlist subpage
ref #125
  • Loading branch information
MSOB7YY committed Feb 9, 2024
1 parent 4177354 commit 19bbf11
Show file tree
Hide file tree
Showing 5 changed files with 486 additions and 330 deletions.
94 changes: 94 additions & 0 deletions lib/base/pull_to_refresh.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'package:namida/core/icon_fonts/broken_icons.dart';

mixin PullToRefreshMixin<T extends StatefulWidget> on State<T> implements TickerProvider {
final turnsTween = Tween<double>(begin: 0.0, end: 1.0);
late final animation = AnimationController(vsync: this, duration: Duration.zero);
AnimationController get animation2;

final _minTrigger = 20;

num get pullNormalizer => 20;

bool onScrollNotification(ScrollMetricsNotification notification) {
final pixels = notification.metrics.pixels;
if (pixels < -_minTrigger) {
animation.animateTo(((pixels + _minTrigger).abs() / pullNormalizer).clamp(0, 1));
} else if (animation.value > 0) {
animation.animateTo(0);
}
return true;
}

double _distanceDragged = 0;
bool onVerticalDragUpdate(double dy) {
_distanceDragged -= dy;
if (_distanceDragged < -_minTrigger) {
animation.animateTo(((_distanceDragged + _minTrigger).abs() / pullNormalizer).clamp(0, 1));
} else if (animation.value > 0) {
animation.animateTo(0);
}
return true;
}

void onVerticalDragFinish() {
animation.animateTo(0, duration: const Duration(milliseconds: 100));
_distanceDragged = 0;
}

Future<void> showRefreshingAnimation(Future<void> Function() whileExecuting) async {
animation2.repeat();
await whileExecuting();
await animation2.fling();
animation2.stop();
}

Widget get pullToRefreshWidget {
return Positioned(
left: 0,
right: 0,
child: AnimatedBuilder(
animation: animation,
child: CircleAvatar(
radius: 24.0,
backgroundColor: context.theme.colorScheme.secondaryContainer,
child: const Icon(Broken.refresh_2),
),
builder: (context, circleAvatar) {
final p = animation.value;
if (!animation2.isAnimating && p == 0) return const SizedBox();
const multiplier = 4.5;
const minus = multiplier / 3;
return Padding(
padding: EdgeInsets.only(top: 12.0 + p * 128.0),
child: Transform.rotate(
angle: (p * multiplier) - minus,
child: AnimatedBuilder(
animation: animation2,
child: circleAvatar,
builder: (context, circleAvatar) {
return Opacity(
opacity: animation2.status == AnimationStatus.forward ? 1.0 : p,
child: RotationTransition(
key: const Key('rotatie'),
turns: turnsTween.animate(animation2),
child: circleAvatar,
),
);
},
),
),
);
},
),
);
}

@override
void dispose() {
animation.dispose();
super.dispose();
}
}
26 changes: 15 additions & 11 deletions lib/controller/playlist_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ class PlaylistController extends PlaylistManager<TrackWithDate> {
// -- similar name exists, so m3u overrides it
// -- this can produce in an outdated playlist version in cache
// -- which will be seen if the m3u file got deleted/renamed
await _prepareM3UPlaylists();
await prepareM3UPlaylists();
}

Future<List<Track>> readM3UFiles(Set<String> filesPaths) async {
Expand All @@ -270,19 +270,23 @@ class PlaylistController extends PlaylistManager<TrackWithDate> {
return listy;
}

Future<void> _prepareM3UPlaylists() async {
Future<void> prepareM3UPlaylists({Set<String> forPaths = const {}}) async {
final allAvailableDirectories = await Indexer.inst.getAvailableDirectories(strictNoMedia: false);

final parameters = {
'allAvailableDirectories': allAvailableDirectories,
'directoriesToExclude': <String>[],
'extensions': kM3UPlaylistsExtensions,
'respectNoMedia': false,
};

final mapResult = await getFilesTypeIsolate.thready(parameters);
late final Set<String> allPaths;
if (forPaths.isNotEmpty) {
allPaths = forPaths;
} else {
final parameters = {
'allAvailableDirectories': allAvailableDirectories,
'directoriesToExclude': <String>[],
'extensions': kM3UPlaylistsExtensions,
'respectNoMedia': false,
};
final mapResult = await getFilesTypeIsolate.thready(parameters);
allPaths = mapResult['allPaths'] as Set<String>;
}

final allPaths = mapResult['allPaths'] as Set<String>;
final resBoth = await _parseM3UPlaylistFiles.thready({
'paths': allPaths,
'libraryTracks': allTracksInLibrary,
Expand Down
Loading

0 comments on commit 19bbf11

Please sign in to comment.