Skip to content

Commit

Permalink
Track progress only in the tile too reduce CPU usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Feichtmeier committed Aug 3, 2023
1 parent f3a7026 commit 986d720
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 30 deletions.
3 changes: 1 addition & 2 deletions lib/app/common/audio_page_body.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ class _AudioPageBodyState extends State<AudioPageBody> {
@override
Widget build(BuildContext context) {
final isPlaying = context.select((PlayerModel m) => m.isPlaying);
final position = context.select((PlayerModel m) => m.position);

final playerModel = context.read<PlayerModel>();
final startPlaylist = playerModel.startPlaylist;
Expand Down Expand Up @@ -278,7 +277,7 @@ class _AudioPageBodyState extends State<AudioPageBody> {
widget.audios.toString(),
),
play: play,
lastPosition: audioSelected ? position : null,
lastPosition: null,
);
}

Expand Down
65 changes: 37 additions & 28 deletions lib/app/podcasts/podcast_audio_tile.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:intl/intl.dart';
import 'package:musicpod/app/player/player_model.dart';
import 'package:musicpod/data/audio.dart';
import 'package:musicpod/utils.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:yaru_icons/yaru_icons.dart';
import 'package:yaru_widgets/yaru_widgets.dart';
Expand Down Expand Up @@ -161,13 +163,12 @@ class _Bottom extends StatelessWidget {
width: 150,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 10),
child: RepaintBoundary(
child: _AudioProgress(
lastPosition: lastPosition,
duration: audio.durationMs == null
? null
: Duration(milliseconds: audio.durationMs!.toInt()),
),
child: _AudioProgress(
selected: selected,
lastPosition: lastPosition,
duration: audio.durationMs == null
? null
: Duration(milliseconds: audio.durationMs!.toInt()),
),
),
),
Expand Down Expand Up @@ -308,38 +309,46 @@ class _AudioProgress extends StatelessWidget {
const _AudioProgress({
this.lastPosition,
this.duration,
required this.selected,
});

final Duration? lastPosition;
final Duration? duration;
final bool selected;

@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
bool sliderActive = duration != null &&
lastPosition != null &&
duration!.inSeconds > lastPosition!.inSeconds;
return SliderTheme(
data: theme.sliderTheme.copyWith(
thumbColor: Colors.white,
thumbShape: const RoundSliderThumbShape(
elevation: 0,
enabledThumbRadius: 0,
disabledThumbRadius: 0,
final position = context.select((PlayerModel m) => m.position);

final pos = lastPosition ?? (selected ? position : Duration.zero);

bool sliderActive =
duration != null && pos != null && duration!.inSeconds > pos.inSeconds;

return RepaintBoundary(
child: SliderTheme(
data: theme.sliderTheme.copyWith(
thumbColor: Colors.white,
thumbShape: const RoundSliderThumbShape(
elevation: 0,
enabledThumbRadius: 0,
disabledThumbRadius: 0,
),
trackHeight: 1,
overlayShape: const RoundSliderThumbShape(
elevation: 0,
enabledThumbRadius: 0,
disabledThumbRadius: 0,
),
),
trackHeight: 1,
overlayShape: const RoundSliderThumbShape(
elevation: 0,
enabledThumbRadius: 0,
disabledThumbRadius: 0,
child: Slider(
min: 0,
max: sliderActive ? duration!.inSeconds.toDouble() : 1.0,
value: sliderActive ? pos.inSeconds.toDouble() : 0,
onChanged: (value) {},
),
),
child: Slider(
min: 0,
max: sliderActive ? duration!.inSeconds.toDouble() : 1.0,
value: sliderActive ? lastPosition!.inSeconds.toDouble() : 0,
onChanged: (value) {},
),
);
}
}

0 comments on commit 986d720

Please sign in to comment.