Skip to content

Commit

Permalink
Properly start an stop busy animations. Suppress notifications about …
Browse files Browse the repository at this point in the history
…connection loss if app was paused.
  • Loading branch information
Kern, Thomas committed Jan 18, 2024
1 parent d7c5a5e commit 11d30b0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
26 changes: 22 additions & 4 deletions lib/components/busy_wrapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class _BusyWrapperState extends State<BusyWrapper> with TickerProviderStateMixin

double opacity = 0;
Timer? timer;
late bool busy;

late final AnimationController _primaryController = AnimationController(
duration: const Duration(seconds: 5),
Expand All @@ -51,26 +52,33 @@ class _BusyWrapperState extends State<BusyWrapper> with TickerProviderStateMixin

void _startAnimation() {
_stopAnimation();
timer = Timer(const Duration(milliseconds: 1000), () {
timer = Timer(const Duration(milliseconds: 800), () {
_primaryController.forward(from: 0);
_secondaryController.forward(from: 0);
setState(() {
busy = true;
opacity = 0.3;
});
});
}

void _stopAnimation() {
opacity = 0;
timer?.cancel();
_primaryController.reset();
_secondaryController.reset();
setState(() {
busy = false;
opacity = 0;
});
}

@override
initState() {
super.initState();
_startAnimation();
busy = widget.busy;
if (busy) {
_startAnimation();
}
}

@override
Expand All @@ -81,6 +89,16 @@ class _BusyWrapperState extends State<BusyWrapper> with TickerProviderStateMixin
super.dispose();
}

@override
void didUpdateWidget(covariant BusyWrapper oldWidget) {
super.didUpdateWidget(oldWidget);
// Update busy state and animations if widget was updated
if (widget.busy != oldWidget.busy) {
busy = widget.busy;
busy ? _startAnimation() : _stopAnimation();
}
}

void stop() async {
_stopAnimation();
mopidyService.stop();
Expand All @@ -89,7 +107,7 @@ class _BusyWrapperState extends State<BusyWrapper> with TickerProviderStateMixin

@override
Widget build(BuildContext context) {
if (!widget.busy && mopidyService.connected) {
if (!busy && mopidyService.connected) {
return widget.child;
}

Expand Down
19 changes: 17 additions & 2 deletions lib/services/mopidy_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,18 @@ class MopidyServiceImpl extends MopidyService {

final Mopidy _mopidy;

// indicator whether application is currently connected to a Mopidy server
bool _connected = false;

// Trying to establish a connection was explicitly stopped by user
bool _stopped = false;

// nested busy level tracking
int _busyLevel = 0;

// application is in paused application state
bool _applicationPaused = false;

// cached current volume
int? _savedVolume;

Expand All @@ -263,10 +271,12 @@ class MopidyServiceImpl extends MopidyService {
// When the app was resumed, update
// tracklist state.
if (msg == 'AppLifecycleState.resumed') {
_applicationPaused = false;
if (!connected) {
resume();
}
} else if (msg == 'AppLifecycleState.paused') {
_applicationPaused = true;
stop();
}
return Future.value(null);
Expand All @@ -282,11 +292,16 @@ class MopidyServiceImpl extends MopidyService {
break;
case ClientState.offline:
_connected = false;
_connectionState$.add(MopidyConnectionState.offline);
// only notify subscribers if application is not paused
if (!_applicationPaused) {
_connectionState$.add(MopidyConnectionState.offline);
}
break;
case ClientState.reconnecting:
_connected = false;
_connectionState$.add(MopidyConnectionState.reconnecting);
if (!_applicationPaused) {
_connectionState$.add(MopidyConnectionState.reconnecting);
}
break;
case ClientState.reconnectionPending:
_connected = false;
Expand Down

0 comments on commit 11d30b0

Please sign in to comment.