Skip to content

Commit

Permalink
perf: migrate to TapDetector & LongPressDetector
Browse files Browse the repository at this point in the history
  • Loading branch information
MSOB7YY committed Feb 25, 2024
1 parent d3cb34b commit 7b81c7c
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/controller/navigator_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ class NamidaNavigator {
await Get.to(
() => WillPopScope(
onWillPop: onWillPop,
child: GestureDetector(
child: TapDetector(
onTap: onWillPop,
child: NamidaBgBlur(
blur: 5.0,
Expand Down
7 changes: 4 additions & 3 deletions lib/packages/focused_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ library focused_menu;

import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:namida/ui/widgets/custom_widgets.dart';

class FocusedMenuHolder extends StatefulWidget {
final Widget child;
Expand Down Expand Up @@ -226,7 +227,7 @@ class FocusedMenuDetails extends StatelessWidget {
child: Stack(
fit: StackFit.expand,
children: <Widget>[
GestureDetector(
TapDetector(
onTap: () => _onDismiss(context),
child: enableBackgroundEffects
? BackdropFilter(
Expand Down Expand Up @@ -264,7 +265,7 @@ class FocusedMenuDetails extends StatelessWidget {
padding: EdgeInsets.zero,
itemBuilder: (context, index) {
final FocusedMenuItem item = menuItems[index];
final Widget listItem = GestureDetector(
final Widget listItem = TapDetector(
onTap: () {
if (popOnItemTap) Navigator.pop(context);
item.onPressed();
Expand Down Expand Up @@ -307,7 +308,7 @@ class FocusedMenuDetails extends StatelessWidget {
Positioned(
top: childOffset.dy,
left: childOffset.dx,
child: GestureDetector(
child: TapDetector(
onTap: () => _onDismiss(context),
child: AbsorbPointer(
absorbing: true,
Expand Down
2 changes: 1 addition & 1 deletion lib/packages/miniplayer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ class _NamidaMiniPlayerState extends State<NamidaMiniPlayer> {
),
),
),
GestureDetector(
TapDetector(
onTap: () => Player.inst.seekSecondsForward(),
child: Padding(
padding: const EdgeInsets.all(8.0),
Expand Down
5 changes: 3 additions & 2 deletions lib/packages/searchbar_animation.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:namida/ui/widgets/custom_widgets.dart';

class SearchBarAnimation extends StatefulWidget {
/// This gives the width to the searchbar by default it will take the size of whole screen.
Expand Down Expand Up @@ -306,7 +307,7 @@ class SearchBarAnimationState extends State<SearchBarAnimation> with SingleTicke
),
child: DecoratedBoxTransition(
decoration: decorationTween.animate(_animationController),
child: GestureDetector(
child: TapDetector(
onTap: () {
widget.onPressButton?.call(!switcher);
_onTapFunctionOriginalAnim();
Expand Down Expand Up @@ -335,7 +336,7 @@ class SearchBarAnimationState extends State<SearchBarAnimation> with SingleTicke
]
: null,
),
child: GestureDetector(
child: TapDetector(
onTap: () {
widget.onPressButton?.call(!switcher);
_onTapFunction();
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/dialogs/edit_tags_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ Future<void> _editMultipleTracksTags(List<Track> tracksPre) async {
getText('${lang.FAILED}: ${failedEditsTracks.length}'),
const SizedBox(width: 4.0),
if (failedEditsTracks.isNotEmpty)
GestureDetector(
TapDetector(
onTap: showFailedTracksDialogs,
child: getText(
lang.CHECK_LIST,
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/dialogs/track_advanced_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ void _showTrackColorPaletteDialog({
return Row(
children: [
getText('$title '),
GestureDetector(onTap: () => finalColorToBeUsed.value = mix, child: getColorWidget(mix)),
TapDetector(onTap: () => finalColorToBeUsed.value = mix, child: getColorWidget(mix)),
],
);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/ui/dialogs/track_info_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,11 @@ Future<void> showTrackInfoDialog(
child: Row(
children: [
const SizedBox(width: 2.0),
GestureDetector(
TapDetector(
onTap: () => NamidaNavigator.inst.navigateDialog(
scale: 1.0,
blackBg: true,
dialog: GestureDetector(
dialog: LongPressDetector(
onLongPress: () async {
final saveDirPath = await EditDeleteController.inst.saveArtworkToStorage(track);
String title = lang.COPIED_ARTWORK;
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ class _HomePageState extends State<HomePage> with SingleTickerProviderStateMixin
return years.map(
(e) => Padding(
padding: const EdgeInsets.symmetric(horizontal: 2.0),
child: GestureDetector(
child: TapDetector(
onTap: () {
_updateSameTimeNYearsAgo(DateTime.now(), e);
if (mounted) setState(() {});
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/pages/subpages/most_played_subpage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class MostPlayedItemsPage<T extends ItemWithDate, E> extends StatelessWidget {
final textColor = _isEnabled(mptr) ? const Color.fromARGB(200, 255, 255, 255) : null;
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 2.0),
child: GestureDetector(
child: TapDetector(
onTap: () => _onSelectingTimeRange(
dateCustom: dateCustom,
mptr: mptr,
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/widgets/circular_percentages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ParsingJsonPercentage extends StatelessWidget {
Widget build(BuildContext context) {
return Obx(
() => JsonToHistoryParser.inst.isParsing.value && (forceDisplay ? true : source == JsonToHistoryParser.inst.currentParsingSource.value)
? GestureDetector(
? TapDetector(
onTap: () => JsonToHistoryParser.inst.showParsingProgressDialog(),
child: NamidaHero(
tag: 'parsingjsonper',
Expand Down
39 changes: 37 additions & 2 deletions lib/ui/widgets/custom_widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ class CustomBlurryDialog extends StatelessWidget {
surfaceTintColor: Colors.transparent,
insetPadding: insetPadding,
clipBehavior: Clip.antiAlias,
child: GestureDetector(
child: TapDetector(
onTap: () {},
child: Container(
color: Colors.transparent,
Expand Down Expand Up @@ -3666,7 +3666,7 @@ class EqualizerIconButton extends StatelessWidget {
}

class TapDetector extends StatelessWidget {
final VoidCallback onTap;
final VoidCallback? onTap;
final void Function(TapGestureRecognizer instance)? initializer;
final Widget? child;
final HitTestBehavior? behavior;
Expand Down Expand Up @@ -3700,6 +3700,41 @@ class TapDetector extends StatelessWidget {
}
}

class LongPressDetector extends StatelessWidget {
final VoidCallback? onLongPress;
final void Function(LongPressGestureRecognizer instance)? initializer;
final Widget? child;
final HitTestBehavior? behavior;

const LongPressDetector({
super.key,
required this.onLongPress,
this.initializer,
this.child,
this.behavior,
});

@override
Widget build(BuildContext context) {
final Map<Type, GestureRecognizerFactory> gestures = <Type, GestureRecognizerFactory>{};
gestures[LongPressGestureRecognizer] = GestureRecognizerFactoryWithHandlers<LongPressGestureRecognizer>(
() => LongPressGestureRecognizer(debugOwner: this),
initializer ??
(LongPressGestureRecognizer instance) {
instance
..onLongPress = onLongPress
..gestureSettings = MediaQuery.maybeGestureSettingsOf(context);
},
);

return RawGestureDetector(
behavior: behavior,
gestures: gestures,
child: child,
);
}
}

class ScaleDetector extends StatelessWidget {
final GestureScaleStartCallback? onScaleStart;
final GestureScaleUpdateCallback? onScaleUpdate;
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/widgets/settings/customization_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ class CustomizationSettings extends SettingSubpageProvider {
const Text(
'this one is actually supposed to be for supporters, if you don\'t mind u can support namida and get the power to unleash this cool feature',
),
GestureDetector(
TapDetector(
onTap: () {
NamidaNavigator.inst.closeDialog();
NamidaNavigator.inst.navigateDialog(
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/widgets/sort_by_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class SortByMenuTracksSearch extends StatelessWidget {
),
),
const SizedBox(height: 4.0),
GestureDetector(
TapDetector(
onTap: isAuto ? () {} : null,
child: ColoredBox(
color: Colors.transparent,
Expand Down
4 changes: 2 additions & 2 deletions lib/ui/widgets/video_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ class NamidaVideoControlsState extends State<NamidaVideoControls> with TickerPro
right: false,
child: Padding(
padding: horizontalControlsPadding,
child: GestureDetector(
child: TapDetector(
onTap: () {},
child: Align(
alignment: Alignment.topCenter,
Expand Down Expand Up @@ -1108,7 +1108,7 @@ class NamidaVideoControlsState extends State<NamidaVideoControls> with TickerPro
// ---- Bottom Row ----
Padding(
padding: horizontalControlsPadding,
child: GestureDetector(
child: TapDetector(
onTap: () {},
child: Align(
alignment: Alignment.bottomCenter,
Expand Down
6 changes: 3 additions & 3 deletions lib/youtube/pages/yt_channel_subpage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class _YTChannelSubpageState extends YoutubeChannelController<YTChannelSubpage>
NamidaNavigator.inst.navigateDialog(
scale: 1.0,
blackBg: true,
dialog: GestureDetector(
dialog: LongPressDetector(
onLongPress: () async {
final saveDirPath = await EditDeleteController.inst.saveImageToStorage(file!);
String title = lang.COPIED_ARTWORK;
Expand Down Expand Up @@ -128,7 +128,7 @@ class _YTChannelSubpageState extends YoutubeChannelController<YTChannelSubpage>
Stack(
children: [
if (bannerUrl != null)
GestureDetector(
TapDetector(
onTap: () => _onImageTap(context, channelID, bannerUrl, true),
child: NamidaHero(
tag: 'true_${channelID}_$bannerUrl',
Expand All @@ -151,7 +151,7 @@ class _YTChannelSubpageState extends YoutubeChannelController<YTChannelSubpage>
const SizedBox(width: 12.0),
Transform.translate(
offset: bannerUrl == null ? const Offset(0, 0) : const Offset(0, -bannerHeight * 0.1),
child: GestureDetector(
child: TapDetector(
onTap: () => _onImageTap(context, channelID, avatarUrl, false),
child: NamidaHero(
tag: 'false_${channelID}_$avatarUrl',
Expand Down

0 comments on commit 7b81c7c

Please sign in to comment.