Skip to content

Commit

Permalink
chore: remove tuple and replace with records
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed May 28, 2023
1 parent 88137f0 commit 19d0ddc
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 67 deletions.
9 changes: 4 additions & 5 deletions lib/components/library/user_albums.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import 'package:spotube/provider/authentication_provider.dart';
import 'package:spotube/services/queries/queries.dart';

import 'package:spotube/utils/type_conversion_utils.dart';
import 'package:tuple/tuple.dart';

class UserAlbums extends HookConsumerWidget {
const UserAlbums({Key? key}) : super(key: key);
Expand All @@ -36,13 +35,13 @@ class UserAlbums extends HookConsumerWidget {
return albumsQuery.data?.toList() ?? [];
}
return albumsQuery.data
?.map((e) => Tuple2(
?.map((e) => (
weightedRatio(e.name!, searchText.value),
e,
))
.sorted((a, b) => b.item1.compareTo(a.item1))
.where((e) => e.item1 > 50)
.map((e) => e.item2)
.sorted((a, b) => b.$1.compareTo(a.$1))
.where((e) => e.$1 > 50)
.map((e) => e.$2)
.toList() ??
[];
}, [albumsQuery.data, searchText.value]);
Expand Down
9 changes: 4 additions & 5 deletions lib/components/library/user_artists.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import 'package:spotube/components/artist/artist_card.dart';
import 'package:spotube/extensions/context.dart';
import 'package:spotube/provider/authentication_provider.dart';
import 'package:spotube/services/queries/queries.dart';
import 'package:tuple/tuple.dart';

class UserArtists extends HookConsumerWidget {
const UserArtists({Key? key}) : super(key: key);
Expand All @@ -31,13 +30,13 @@ class UserArtists extends HookConsumerWidget {
return artists.toList();
}
return artists
.map((e) => Tuple2(
.map((e) => (
weightedRatio(e.name!, searchText.value),
e,
))
.sorted((a, b) => b.item1.compareTo(a.item1))
.where((e) => e.item1 > 50)
.map((e) => e.item2)
.sorted((a, b) => b.$1.compareTo(a.$1))
.where((e) => e.$1 > 50)
.map((e) => e.$2)
.toList();
}, [artistQuery.data, searchText.value]);

Expand Down
9 changes: 4 additions & 5 deletions lib/components/library/user_local_tracks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import 'package:spotube/utils/primitive_utils.dart';
import 'package:spotube/utils/service_utils.dart';
import 'package:spotube/utils/type_conversion_utils.dart';
import 'package:flutter_rust_bridge/flutter_rust_bridge.dart' show FfiException;
import 'package:tuple/tuple.dart';

const supportedAudioTypes = [
"audio/webm",
Expand Down Expand Up @@ -245,7 +244,7 @@ class UserLocalTracks extends HookConsumerWidget {
return sortedTracks;
}
return sortedTracks
.map((e) => Tuple2(
.map((e) => (
weightedRatio(
"${e.name} - ${TypeConversionUtils.artists_X_String<Artist>(e.artists ?? [])}",
searchText.value,
Expand All @@ -254,10 +253,10 @@ class UserLocalTracks extends HookConsumerWidget {
))
.toList()
.sorted(
(a, b) => b.item1.compareTo(a.item1),
(a, b) => b.$1.compareTo(a.$1),
)
.where((e) => e.item1 > 50)
.map((e) => e.item2)
.where((e) => e.$1 > 50)
.map((e) => e.$2)
.toList()
.toList();
}, [searchText.value, sortedTracks]);
Expand Down
12 changes: 4 additions & 8 deletions lib/components/library/user_playlists.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import 'package:spotube/hooks/use_breakpoint_value.dart';
import 'package:spotube/hooks/use_breakpoints.dart';
import 'package:spotube/provider/authentication_provider.dart';
import 'package:spotube/services/queries/queries.dart';
import 'package:tuple/tuple.dart';

class UserPlaylists extends HookConsumerWidget {
const UserPlaylists({Key? key}) : super(key: key);
Expand Down Expand Up @@ -61,13 +60,10 @@ class UserPlaylists extends HookConsumerWidget {
likedTracksPlaylist,
...?playlistsQuery.data,
]
.map((e) => Tuple2(
weightedRatio(e.name!, searchText.value),
e,
))
.sorted((a, b) => b.item1.compareTo(a.item1))
.where((e) => e.item1 > 50)
.map((e) => e.item2)
.map((e) => (weightedRatio(e.name!, searchText.value), e))
.sorted((a, b) => b.$1.compareTo(a.$1))
.where((e) => e.$1 > 50)
.map((e) => e.$2)
.toList();
},
[playlistsQuery.data, searchText.value],
Expand Down
13 changes: 7 additions & 6 deletions lib/components/player/player_controls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,12 @@ class PlayerControls extends HookConsumerWidget {
if (!compact)
HookBuilder(
builder: (context) {
final progressObj = useProgress(ref);

final progressStatic = progressObj.item1;
final position = progressObj.item2;
final duration = progressObj.item3;
final (
:bufferProgress,
:duration,
:position,
:progressStatic
) = useProgress(ref);

final totalMinutes = PrimitiveUtils.zeroPadNumStr(
duration.inMinutes.remainder(60),
Expand Down Expand Up @@ -144,7 +145,7 @@ class PlayerControls extends HookConsumerWidget {
// there's an edge case for value being bigger
// than total duration. Keeping it resolved
value: progress.value.toDouble(),
secondaryTrackValue: progressObj.item4,
secondaryTrackValue: bufferProgress,
onChanged: playlist.isFetching == true || buffering
? null
: (v) {
Expand Down
7 changes: 5 additions & 2 deletions lib/components/player/player_overlay.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class PlayerOverlay extends HookConsumerWidget {
@override
Widget build(BuildContext context, ref) {
final canShow = ref.watch(
ProxyPlaylistNotifier.provider.select((s) => s != null),
ProxyPlaylistNotifier.provider.select((s) => s.active != null),
);
final playlistNotifier = ref.watch(ProxyPlaylistNotifier.notifier);
final playlist = ref.watch(ProxyPlaylistNotifier.provider);
Expand Down Expand Up @@ -73,7 +73,10 @@ class PlayerOverlay extends HookConsumerWidget {
// animated
return TweenAnimationBuilder<double>(
duration: const Duration(milliseconds: 250),
tween: Tween<double>(begin: 0, end: progress.item1),
tween: Tween<double>(
begin: 0,
end: progress.progressStatic,
),
builder: (context, value, child) {
return LinearProgressIndicator(
value: value,
Expand Down
18 changes: 4 additions & 14 deletions lib/components/shared/track_table/track_collection_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import 'package:spotify/spotify.dart';
import 'package:spotube/provider/authentication_provider.dart';
import 'package:spotube/utils/platform.dart';
import 'package:spotube/utils/type_conversion_utils.dart';
import 'package:tuple/tuple.dart';

class TrackCollectionView<T> extends HookConsumerWidget {
final logger = getLogger(TrackCollectionView);
Expand Down Expand Up @@ -122,19 +121,10 @@ class TrackCollectionView<T> extends HookConsumerWidget {
return tracksSnapshot.data;
}
return tracksSnapshot.data
?.map((e) => Tuple2(
weightedRatio(
"${e.name} - ${TypeConversionUtils.artists_X_String<Artist>(e.artists ?? [])}",
searchText.value,
),
e,
))
.toList()
.sorted(
(a, b) => b.item1.compareTo(a.item1),
)
.where((e) => e.item1 > 50)
.map((e) => e.item2)
?.map((e) => (weightedRatio(e.name!, searchText.value), e))
.sorted((a, b) => b.$1.compareTo(a.$1))
.where((e) => e.$1 > 50)
.map((e) => e.$2)
.toList();
}, [tracksSnapshot.data, searchText.value]);

Expand Down
13 changes: 6 additions & 7 deletions lib/extensions/list.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:collection/collection.dart';
import 'package:tuple/tuple.dart';

extension MultiSortListMap on List<Map> {
/// [preference] - List of properties in which you want to sort the list
Expand Down Expand Up @@ -48,7 +47,7 @@ extension MultiSortListMap on List<Map> {
}
}

extension MultiSortListTupleMap<V> on List<Tuple2<Map, V>> {
extension MultiSortListTupleMap<V> on List<(Map, V)> {
/// [preference] - List of properties in which you want to sort the list
/// i.e.
/// ```
Expand All @@ -61,7 +60,7 @@ extension MultiSortListTupleMap<V> on List<Tuple2<Map, V>> {
/// ```
/// List<bool> criteria = [true. false];
/// ```
List<Tuple2<Map, V>> sortByProperties(
List<(Map, V)> sortByProperties(
List<bool> criteria, List<String> preference) {
if (preference.isEmpty || criteria.isEmpty || isEmpty) {
return this;
Expand All @@ -71,17 +70,17 @@ extension MultiSortListTupleMap<V> on List<Tuple2<Map, V>> {
return this;
}

int compare(int i, Tuple2<Map, V> a, Tuple2<Map, V> b) {
if (a.item1[preference[i]] == b.item1[preference[i]]) {
int compare(int i, (Map, V) a, (Map, V) b) {
if (a.$1[preference[i]] == b.$1[preference[i]]) {
return 0;
} else if (a.item1[preference[i]] > b.item1[preference[i]]) {
} else if (a.$1[preference[i]] > b.$1[preference[i]]) {
return criteria[i] ? 1 : -1;
} else {
return criteria[i] ? -1 : 1;
}
}

int sortAll(Tuple2<Map, V> a, Tuple2<Map, V> b) {
int sortAll((Map, V) a, (Map, V) b) {
int i = 0;
int result = 0;
while (i < preference.length) {
Expand Down
19 changes: 12 additions & 7 deletions lib/hooks/use_progress.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:spotube/provider/proxy_playlist/proxy_playlist_provider.dart';
import 'package:spotube/services/audio_player/audio_player.dart';
import 'package:tuple/tuple.dart';

Tuple4<double, Duration, Duration, double> useProgress(WidgetRef ref) {
({
double progressStatic,
Duration position,
Duration duration,
double bufferProgress
}) useProgress(WidgetRef ref) {
ref.watch(ProxyPlaylistNotifier.provider);

final bufferProgress =
Expand All @@ -25,11 +29,12 @@ Tuple4<double, Duration, Duration, double> useProgress(WidgetRef ref) {
final sliderMax = duration.inSeconds;
final sliderValue = position.inSeconds;

return Tuple4(
sliderMax == 0 || sliderValue > sliderMax ? 0 : sliderValue / sliderMax,
position,
duration,
sliderMax == 0 || bufferProgress > sliderMax
return (
progressStatic:
sliderMax == 0 || sliderValue > sliderMax ? 0 : sliderValue / sliderMax,
position: position,
duration: duration,
bufferProgress: sliderMax == 0 || bufferProgress > sliderMax
? 0
: bufferProgress / sliderMax,
);
Expand Down
17 changes: 9 additions & 8 deletions lib/pages/settings/blacklist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:spotube/collections/spotube_icons.dart';
import 'package:spotube/components/shared/page_window_title_bar.dart';
import 'package:spotube/provider/blacklist_provider.dart';
import 'package:tuple/tuple.dart';

class BlackListPage extends HookConsumerWidget {
const BlackListPage({Key? key}) : super(key: key);
Expand All @@ -23,13 +22,15 @@ class BlackListPage extends HookConsumerWidget {
return blacklist;
}
return blacklist
.map((e) => Tuple2(
weightedRatio("${e.name} ${e.type.name}", searchText.value),
e,
))
.sorted((a, b) => b.item1.compareTo(a.item1))
.where((e) => e.item1 > 50)
.map((e) => e.item2)
.map(
(e) => (
weightedRatio("${e.name} ${e.type.name}", searchText.value),
e,
),
)
.sorted((a, b) => b.$1.compareTo(a.$1))
.where((e) => e.$1 > 50)
.map((e) => e.$2)
.toList();
},
[blacklist, searchText.value],
Expand Down

0 comments on commit 19d0ddc

Please sign in to comment.