Skip to content

Commit

Permalink
perf: avoid recolering the player whereas possible
Browse files Browse the repository at this point in the history
like when reordering items or when playing same item
  • Loading branch information
MSOB7YY committed Nov 26, 2024
1 parent 032701e commit 161359a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
10 changes: 10 additions & 0 deletions lib/class/color_m.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'package:namida/core/extensions.dart';
Expand Down Expand Up @@ -30,4 +31,13 @@ class NamidaColor {
'palette': palette.mapped((e) => e.value),
};
}

@override
bool operator ==(covariant NamidaColor other) {
if (identical(this, other)) return true;
return used == other.used && mix == other.mix && listEquals(palette, other.palette);
}

@override
int get hashCode => used.hashCode ^ mix.hashCode ^ palette.hashCode;
}
16 changes: 12 additions & 4 deletions lib/controller/current_color.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class CurrentColor {
final currentPlayingTrack = Rxn<Selectable>();
final currentPlayingIndex = 0.obs;

YoutubeID? _currentPlayingVideo;

ColorScheme? deviceWallpaperColorScheme;

final isGeneratingAllColorPalettes = false.obs;
Expand Down Expand Up @@ -170,8 +172,10 @@ class CurrentColor {
);
}
if (track != null) {
currentPlayingTrack.value = null; // nullifying to re-assign safely if subtype has changed
currentPlayingTrack.value = track;
currentPlayingTrack
..set(null) // nullifying to re-assign safely if subtype has changed
..set(track)
..refresh();
}
if (index != null) {
currentPlayingIndex.value = index;
Expand All @@ -182,6 +186,9 @@ class CurrentColor {
final id = ytIdItem.id;
if (id == '') return;

if (_currentPlayingVideo == ytIdItem) return;
_currentPlayingVideo = ytIdItem;

// -- only extract if same item is still playing, i.e. user didn't skip.
bool stillPlaying() => ytIdItem == Player.inst.currentItem.value;

Expand Down Expand Up @@ -214,15 +221,15 @@ class CurrentColor {

final trColors = await getColorPalette();
if (trColors == null || !stillPlaying()) return; // -- check current item
_namidaColorMiniplayer.value = trColors.color;
if (trColors.color != _namidaColorMiniplayer.value) _namidaColorMiniplayer.value = trColors.color;

if (settings.autoColor.value) {
if (_shouldUpdateFromDeviceWallpaper) {
namidaColor = await getPlayerColorFromDeviceWallpaper();
} else {
namidaColor = trColors;
}
if (namidaColor != null) {
if (namidaColor != null && namidaColor != _namidaColor.value) {
_namidaColor.set(namidaColor);
_namidaColor.refresh(); // force refresh for pitch black/etc
_updateCurrentPaletteHalfs(
Expand All @@ -237,6 +244,7 @@ class CurrentColor {
void resetCurrentPlayingTrack() {
currentPlayingTrack.value = null;
_namidaColorMiniplayer.value = null;
_currentPlayingVideo = null;
}

bool _checkDummyColor(NamidaColor value) => value.palette.isEmpty || (value.palette.length == 1 && value.color == value.palette.first && value.color == value.palette.last);
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: namida
description: A Beautiful and Feature-rich Music Player, With YouTube & Video Support Built in Flutter
publish_to: "none"
version: 4.7.29-beta+241126210
version: 4.7.3-beta+241126215

environment:
sdk: ">=3.4.0 <4.0.0"
Expand Down

0 comments on commit 161359a

Please sign in to comment.