Skip to content

Commit

Permalink
feat: left & right swipe actions for track tile
Browse files Browse the repository at this point in the history
ref: #333
  • Loading branch information
MSOB7YY committed Aug 31, 2024
1 parent dec830f commit c0ace94
Show file tree
Hide file tree
Showing 14 changed files with 218 additions and 54 deletions.
10 changes: 10 additions & 0 deletions lib/controller/settings_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ class _SettingsController with SettingsFileWriter {
final ytMostPlayedCustomisStartOfDay = true.obs;

/// Track Items
final onTrackSwipeLeft = OnTrackTileSwapActions.none.obs;
final onTrackSwipeRight = OnTrackTileSwapActions.none.obs;
final displayThirdRow = true.obs;
final displayThirdItemInEachRow = false.obs;
final trackTileSeparator = '•'.obs;
Expand Down Expand Up @@ -439,6 +441,8 @@ class _SettingsController with SettingsFileWriter {
ytMostPlayedCustomisStartOfDay.value = json['ytMostPlayedCustomisStartOfDay'] ?? ytMostPlayedCustomisStartOfDay.value;

/// Track Items
onTrackSwipeLeft.value = OnTrackTileSwapActions.values.getEnum(json['onTrackSwipeLeft']) ?? onTrackSwipeLeft.value;
onTrackSwipeRight.value = OnTrackTileSwapActions.values.getEnum(json['onTrackSwipeRight']) ?? onTrackSwipeRight.value;
displayThirdRow.value = json['displayThirdRow'] ?? displayThirdRow.value;
displayThirdItemInEachRow.value = json['displayThirdItemInEachRow'] ?? displayThirdItemInEachRow.value;
trackTileSeparator.value = json['trackTileSeparator'] ?? trackTileSeparator.value;
Expand Down Expand Up @@ -610,6 +614,8 @@ class _SettingsController with SettingsFileWriter {
'ytMostPlayedCustomisStartOfDay': ytMostPlayedCustomisStartOfDay.value,

/// Track Items
'onTrackSwipeLeft': onTrackSwipeLeft.value.name,
'onTrackSwipeRight': onTrackSwipeRight.value.name,
'displayThirdRow': displayThirdRow.value,
'displayThirdItemInEachRow': displayThirdItemInEachRow.value,
'trackTileSeparator': trackTileSeparator.value,
Expand Down Expand Up @@ -689,6 +695,8 @@ class _SettingsController with SettingsFileWriter {
bool? playlistSortReversed,
GroupSortType? ytPlaylistSort,
bool? ytPlaylistSortReversed,
OnTrackTileSwapActions? onTrackSwipeLeft,
OnTrackTileSwapActions? onTrackSwipeRight,
bool? displayThirdRow,
bool? displayThirdItemInEachRow,
String? trackTileSeparator,
Expand Down Expand Up @@ -859,6 +867,8 @@ class _SettingsController with SettingsFileWriter {
if (playlistSortReversed != null) this.playlistSortReversed.value = playlistSortReversed;
if (ytPlaylistSort != null) this.ytPlaylistSort.value = ytPlaylistSort;
if (ytPlaylistSortReversed != null) this.ytPlaylistSortReversed.value = ytPlaylistSortReversed;
if (onTrackSwipeLeft != null) this.onTrackSwipeLeft.value = onTrackSwipeLeft;
if (onTrackSwipeRight != null) this.onTrackSwipeRight.value = onTrackSwipeRight;
if (displayThirdRow != null) this.displayThirdRow.value = displayThirdRow;
if (displayThirdItemInEachRow != null) this.displayThirdItemInEachRow.value = displayThirdItemInEachRow;
if (trackTileSeparator != null) this.trackTileSeparator.value = trackTileSeparator;
Expand Down
8 changes: 8 additions & 0 deletions lib/core/enums.dart
Original file line number Diff line number Diff line change
Expand Up @@ -410,3 +410,11 @@ enum YTVisibleMixesPlaces {
search,
relatedVideos,
}

enum OnTrackTileSwapActions {
none,
playnext,
playlast,
playafter,
addtoplaylist,
}
11 changes: 11 additions & 0 deletions lib/core/namida_converter_ext.dart
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,10 @@ extension LocalVideoMatchingTypeText on LocalVideoMatchingType {
String toText() => _NamidaConverters.inst.getTitle(this);
}

extension OnTrackTileSwapActionsUtils on OnTrackTileSwapActions {
String toText() => _NamidaConverters.inst.getTitle(this);
}

extension TRACKPLAYMODE on TrackPlayMode {
String toText() => _NamidaConverters.inst.getTitle(this);
}
Expand Down Expand Up @@ -1177,6 +1181,13 @@ class _NamidaConverters {
TrackPlayMode.trackArtist: lang.TRACK_PLAY_MODE_TRACK_ARTIST,
TrackPlayMode.trackGenre: lang.TRACK_PLAY_MODE_TRACK_GENRE,
},
OnTrackTileSwapActions: {
OnTrackTileSwapActions.none: lang.NONE,
OnTrackTileSwapActions.playnext: lang.PLAY_NEXT,
OnTrackTileSwapActions.playlast: lang.PLAY_LAST,
OnTrackTileSwapActions.playafter: lang.PLAY_AFTER,
OnTrackTileSwapActions.addtoplaylist: lang.ADD_TO_PLAYLIST,
},
InsertionSortingType: {
InsertionSortingType.listenCount: lang.TOTAL_LISTENS,
InsertionSortingType.random: lang.RANDOM,
Expand Down
1 change: 1 addition & 0 deletions lib/core/translations/keys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ abstract class LanguageKeys {
String get ON_INTERRUPTION => _getKey('ON_INTERRUPTION');
String get ON_NOTIFICATION_TAP => _getKey('ON_NOTIFICATION_TAP');
String get ON_OPENING_YOUTUBE_LINK => _getKey('ON_OPENING_YOUTUBE_LINK');
String get ON_SWIPING => _getKey('ON_SWIPING');
String get ON_VOLUME_ZERO => _getKey('ON_VOLUME_ZERO');
String get OPEN => _getKey('OPEN');
String get OPEN_APP => _getKey('OPEN_APP');
Expand Down
1 change: 1 addition & 0 deletions lib/packages/miniplayer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ class NamidaMiniPlayerTrack extends StatelessWidget {
trackTileConfigs: const TrackTilePropertiesConfigs(
displayRightDragHandler: true,
draggableThumbnail: true,
horizontalGestures: false,
queueSource: QueueSource.playerQueue,
),
itemBuilder: (context, i, currentIndex, queue, properties) {
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/dialogs/common_dialogs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class NamidaDialogs {
required Folder folder,
required bool recursiveTracks,
}) async {
if (recursiveTracks) Vibration.vibrate(duration: 20, amplitude: 50);
if (recursiveTracks) Vibration.vibrate(duration: 20, amplitude: 40);
final tracks = recursiveTracks ? folder.tracksRecusive().toList() : folder.tracks();
await showGeneralPopupDialog(
tracks,
Expand Down
1 change: 1 addition & 0 deletions lib/ui/pages/subpages/playlist_tracks_subpage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ class _NormalPlaylistTracksPageState extends State<NormalPlaylistTracksPage> wit
queueSource: playlist.toQueueSource(),
playlistName: playlist.name,
draggableThumbnail: reorderable,
horizontalGestures: !reorderable,
selectable: () => !PlaylistController.inst.canReorderTracks.value,
),
builder: (properties) => NamidaListViewRaw(
Expand Down
44 changes: 35 additions & 9 deletions lib/ui/widgets/custom_widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2150,7 +2150,7 @@ class NamidaContainerDivider extends StatelessWidget {

class FadeDismissible extends StatefulWidget {
final Widget child;
final void Function(DismissDirection onDismissed) onDismissed;
final void Function(DismissDirection direction) onDismissed;
final void Function(DragStartDetails details)? onDismissStart;
final void Function(DragEndDetails details)? onDismissEnd;
final void Function(DragEndDetails details)? onDismissCancel;
Expand All @@ -2165,6 +2165,7 @@ class FadeDismissible extends StatefulWidget {
final bool Function()? draggable;
final RxBase<bool>? draggableRx;
final Widget? onTopWidget;
final bool removeOnDismiss;

const FadeDismissible({
required Key key,
Expand All @@ -2184,6 +2185,7 @@ class FadeDismissible extends StatefulWidget {
this.draggable,
this.draggableRx,
this.onTopWidget,
this.removeOnDismiss = true,
}) : super(key: key);

@override
Expand All @@ -2210,6 +2212,7 @@ class _FadeDismissibleState extends State<FadeDismissible> with SingleTickerProv

bool _draggable = true;
bool _inDismissRange = true;
bool? _canSwipeInternal; // calculate for direction once to allow swiping back.

void calculateInDismissRange(double positionDx, double maxWidth) {
final percentage = positionDx / maxWidth;
Expand All @@ -2221,17 +2224,27 @@ class _FadeDismissibleState extends State<FadeDismissible> with SingleTickerProv
}

Future<void> _dismissToRight(DragEndDetails d, {bool faster = false}) async {
await _animateDismiss(1, faster: faster);
widget.onDismissed(DismissDirection.horizontal);
if (widget.onDismissEnd != null) widget.onDismissEnd!(d);
_animation.animateTo(0, duration: Duration.zero); // fixes rendering issue
if (widget.removeOnDismiss) {
await _animateDismiss(1, faster: faster);
widget.onDismissed(DismissDirection.startToEnd);
if (widget.onDismissEnd != null) widget.onDismissEnd!(d);
_animation.animateTo(0, duration: Duration.zero); // fixes rendering issue
} else {
widget.onDismissed(DismissDirection.startToEnd);
await _resetToMiddle(d);
}
}

Future<void> _dismissToLeft(DragEndDetails d, {bool faster = false}) async {
await _animateDismiss(-1, faster: faster);
widget.onDismissed(DismissDirection.horizontal);
if (widget.onDismissEnd != null) widget.onDismissEnd!(d);
_animation.animateTo(0, duration: Duration.zero); // fixes rendering issue
if (widget.removeOnDismiss) {
await _animateDismiss(-1, faster: faster);
widget.onDismissed(DismissDirection.endToStart);
if (widget.onDismissEnd != null) widget.onDismissEnd!(d);
_animation.animateTo(0, duration: Duration.zero); // fixes rendering issue
} else {
widget.onDismissed(DismissDirection.endToStart);
await _resetToMiddle(d);
}
}

Future<void> _resetToMiddle(DragEndDetails d) async {
Expand All @@ -2253,12 +2266,25 @@ class _FadeDismissibleState extends State<FadeDismissible> with SingleTickerProv
: (d) {
if (!_draggable) return;
if (!_inDismissRange) return;
if (_canSwipeInternal == null) {
bool canSwipe = true;
if (d.delta.dx.isNegative) {
if (widget.direction == DismissDirection.startToEnd) canSwipe = false;
} else {
if (widget.direction == DismissDirection.endToStart) canSwipe = false;
}
if (canSwipe != _canSwipeInternal) _canSwipeInternal = canSwipe;
}
if (_canSwipeInternal == false) return;

_dragged += d.delta.dx;
_animation.animateTo(_dragged / maxWidth, duration: Duration.zero);
},
onEnd: !draggable
? null
: (d) {
_canSwipeInternal = null;

final velocity = d.velocity.pixelsPerSecond.dx;
if (velocity > 800) {
_dismissToRight(d, faster: true);
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/widgets/library/queue_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class QueueTile extends StatelessWidget {
FadeDismissible(
direction: DismissDirection.endToStart,
key: Key("${queue.date}"),
onDismissed: (onDismissed) {
onDismissed: (direction) {
final oldQueue = queue;
QueueController.inst.removeQueue(oldQueue);
snackyy(
Expand Down
Loading

0 comments on commit c0ace94

Please sign in to comment.