Skip to content

Commit

Permalink
perf: significant ui performance improvement, achieved by
Browse files Browse the repository at this point in the history
- using child in AnimatedBuilder, smooth yt miniplayer & queue in normal miniplayer
- keys for ArtworkWidget & YoutubeThumbnail, loading now only done in initState
- using Container instead of ClipRRect
- some refactor
  • Loading branch information
MSOB7YY committed Dec 18, 2023
1 parent 28035eb commit ba70591
Show file tree
Hide file tree
Showing 29 changed files with 1,367 additions and 1,398 deletions.
14 changes: 12 additions & 2 deletions lib/controller/current_color.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ class CurrentColor {
final response = await paletteFile.readAsJson();
if (response != null) {
final nc = NamidaColor.fromJson(response);
printy("Color Read From File");
_printie("Color Read From File");
return nc;
} else {
await paletteFile.deleteIfExists();
Expand All @@ -277,7 +277,7 @@ class CurrentColor {
final nc = NamidaColor(used: null, mix: mixIntColors(pcolors), palette: pcolors.toList());
await paletteFile.writeAsJson(nc.toJson()); // writing the file bothways, to prevent reduntant re-extraction.
Indexer.inst.updateColorPalettesSizeInStorage(newPalettePath: paletteFile.path);
printy("Color Extracted From Image (${pcolors.length})");
_printie("Color Extracted From Image (${pcolors.length})");
return pcolors.isEmpty ? null : nc;
}

Expand Down Expand Up @@ -401,6 +401,16 @@ class CurrentColor {
void stopGeneratingColorPalettes() => isGeneratingAllColorPalettes.value = false;

static const _defaultUseIsolate = false;

void _printie(
dynamic message, {
bool isError = false,
bool dumpshit = false,
}) {
if (logsEnabled) printy(message, isError: isError, dumpshit: dumpshit);
}

bool logsEnabled = false;
}

extension ColorUtils on Color {
Expand Down
35 changes: 24 additions & 11 deletions lib/controller/video_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart' hide Response;
import 'package:namida/core/functions.dart';
import 'package:newpipeextractor_dart/models/streams.dart';
import 'package:html/parser.dart' as parser;
import 'package:http/http.dart' as http;
import 'package:newpipeextractor_dart/newpipeextractor_dart.dart';
import 'package:newpipeextractor_dart/utils/httpClient.dart';
import 'package:picture_in_picture/picture_in_picture.dart';
import 'package:queue/queue.dart';
import 'package:video_player/video_player.dart';
Expand All @@ -25,6 +27,7 @@ import 'package:namida/controller/settings_controller.dart';
import 'package:namida/core/constants.dart';
import 'package:namida/core/enums.dart';
import 'package:namida/core/extensions.dart';
import 'package:namida/core/functions.dart';
import 'package:namida/core/namida_converter_ext.dart';
import 'package:namida/ui/widgets/custom_widgets.dart';
import 'package:namida/ui/widgets/video_widget.dart';
Expand Down Expand Up @@ -372,7 +375,7 @@ class VideoController {
try {
await execute();
} catch (e) {
printy(e, isError: true);
_printie(e, isError: true);
}
}

Expand Down Expand Up @@ -416,7 +419,7 @@ class VideoController {
_downloadTimerCancel();
void updateCurrentBytes() {
currentDownloadedBytes.value = downloaded == 0 ? null : downloaded;
printy('Video Download: ${currentDownloadedBytes.value?.fileSizeFormatted}');
_printie('Video Download: ${currentDownloadedBytes.value?.fileSizeFormatted}');
}

_downloadTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
Expand Down Expand Up @@ -563,7 +566,7 @@ class VideoController {
nv = vid;
}
} catch (e) {
printy(e, isError: true);
_printie(e, isError: true);
continue;
}
}
Expand All @@ -586,7 +589,7 @@ class VideoController {

Future<void> fetchCachedVideos() async {
final cachedVideos = await _checkIfVideosInMapValid(_videoCacheIDMap);
printy('videos cached: ${cachedVideos.length}');
_printie('videos cached: ${cachedVideos.length}');
_videoCacheIDMap.clear();
cachedVideos.entries.toList().loop((videoEntry, _) {
videoEntry.value.loop((e, _) {
Expand All @@ -595,7 +598,7 @@ class VideoController {
});

final newCachedVideos = await _checkForNewVideosInCache(cachedVideos);
printy('videos cached new: ${newCachedVideos.length}');
_printie('videos cached new: ${newCachedVideos.length}');
newCachedVideos.entries.toList().loop((videoEntry, _) {
videoEntry.value.loop((e, _) {
_videoCacheIDMap.addForce(videoEntry.key, e);
Expand Down Expand Up @@ -643,7 +646,7 @@ class VideoController {
localVideoExtractTotal.value = total;
},
);
printy('videos local: ${localVideos.length}');
_printie('videos local: ${localVideos.length}');
localVideos.loop((e, index) {
_videoPathsMap[e.path] = e;
});
Expand Down Expand Up @@ -817,7 +820,7 @@ class VideoController {
namidaVideos.add(nv);
}
} catch (e) {
printy(e, isError: true);
_printie(e, isError: true);
continue;
}

Expand Down Expand Up @@ -878,7 +881,7 @@ class VideoController {

for (final link in links) {
if (_runningRequestsMap[link] != null) {
printy('getYoutubeThumbnailAsBytes: Same link is being requested right now, ignoring');
_printie('getYoutubeThumbnailAsBytes: Same link is being requested right now, ignoring');
return await _runningRequestsMap[link]!.future; // return and not continue, cuz if requesting hq image, continue will make it request lower one
}

Expand All @@ -899,7 +902,7 @@ class VideoController {
requestRes = (res.data ?? Uint8List.fromList([]), res.statusCode ?? 404);
}
} catch (e) {
printy('getYoutubeThumbnailAsBytes: Error getting thumbnail at $link, trying again with lower quality.\n$e', isError: true);
_printie('getYoutubeThumbnailAsBytes: Error getting thumbnail at $link, trying again with lower quality.\n$e', isError: true);
}
});

Expand Down Expand Up @@ -1115,6 +1118,16 @@ class VideoController {
// final excludedByNoMedia = mapResult['pathsExcludedByNoMedia'] as Set<String>;
return allVideoPaths;
}

void _printie(
dynamic message, {
bool isError = false,
bool dumpshit = false,
}) {
if (logsEnabled) printy(message, isError: isError, dumpshit: dumpshit);
}

bool logsEnabled = false;
}

class _NamidaVideoPlayer {
Expand Down
83 changes: 35 additions & 48 deletions lib/packages/drop_shadow.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class DropShadow extends StatelessWidget {
required this.child,
this.bottomChild,
this.blurRadius = 10.0,
this.borderRadius = 0.0,
this.borderRadius,
this.offset = const Offset(0, 8),
this.spread = 1.0,
this.boxShadow,
Expand All @@ -26,7 +26,7 @@ class DropShadow extends StatelessWidget {
final double blurRadius;

/// BorderRadius to the image and the shadow
final double borderRadius;
final BorderRadius? borderRadius;

/// Position of the shadow
final Offset offset;
Expand All @@ -39,57 +39,44 @@ class DropShadow extends StatelessWidget {

@override
Widget build(BuildContext context) {
double left = 0.0;
double right = 0.0;
double top = 0.0;
double bottom = 0.0;

left = (offset.dx.abs() + (blurRadius * 2)) * spread;
right = (offset.dx + (blurRadius * 2)) * spread;
top = (offset.dy.abs() + (blurRadius * 2)) * spread;
bottom = (offset.dy + (blurRadius * 2)) * spread;

/// [ClipRRect] to isolate [BackDropFilter] from other widgets
return ClipRRect(
child: Container(
decoration: BoxDecoration(boxShadow: boxShadow),

/// Calculate Shadow's effect field
padding: EdgeInsets.fromLTRB(left, top, right, bottom),
child: Stack(
children: [
/// Arrange shadow position
Transform.translate(
offset: offset,
final left = (offset.dx.abs() + (blurRadius * 2)) * spread;
final right = (offset.dx + (blurRadius * 2)) * spread;
final top = (offset.dy.abs() + (blurRadius * 2)) * spread;
final bottom = (offset.dy + (blurRadius * 2)) * spread;

return Container(
clipBehavior: Clip.hardEdge,
decoration: BoxDecoration(
boxShadow: boxShadow,
borderRadius: borderRadius,
),

/// Apply [BorderRadius] to the shadow
child: ClipRRect(
borderRadius: BorderRadius.circular(borderRadius),
child: bottomChild ?? child,
/// Calculate Shadow's effect field
padding: EdgeInsets.fromLTRB(left, top, right, bottom),
child: Stack(
children: [
/// Arrange shadow position
Transform.translate(
offset: offset,
child: bottomChild ?? child,
),

/// Apply filter the whole [Stack] space
Positioned.fill(
/// Apply blur effect to the layer
child: BackdropFilter(
filter: ImageFilter.blur(
sigmaX: blurRadius,
sigmaY: blurRadius,
),
),

/// Apply filter the whole [Stack] space
Positioned.fill(
/// Apply blur effect to the layer
child: BackdropFilter(
filter: ImageFilter.blur(
sigmaX: blurRadius,
sigmaY: blurRadius,
),

/// Filter effect field
child: const ColoredBox(color: Colors.transparent),
),
/// Filter effect field
child: const ColoredBox(color: Colors.transparent),
),
),

/// [Widget] itself with given [BorderRadius]
ClipRRect(
borderRadius: BorderRadius.circular(borderRadius),
child: child,
),
],
),
child,
],
),
);
}
Expand Down
89 changes: 43 additions & 46 deletions lib/packages/focused_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -258,53 +258,50 @@ class FocusedMenuDetails extends StatelessWidget {
color: Colors.grey.shade200,
borderRadius: BorderRadius.all(Radius.circular(borderRadius)),
boxShadow: const [BoxShadow(color: Colors.black38, blurRadius: 10, spreadRadius: 1)]),
child: ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(borderRadius)),
child: menuWidget ??
ListView.builder(
itemCount: menuItems.length,
padding: EdgeInsets.zero,
physics: const BouncingScrollPhysics(),
itemBuilder: (context, index) {
final FocusedMenuItem item = menuItems[index];
final Widget listItem = GestureDetector(
onTap: () {
if (popOnItemTap) Navigator.pop(context);
item.onPressed();
child: menuWidget ??
ListView.builder(
itemCount: menuItems.length,
padding: EdgeInsets.zero,
physics: const BouncingScrollPhysics(),
itemBuilder: (context, index) {
final FocusedMenuItem item = menuItems[index];
final Widget listItem = GestureDetector(
onTap: () {
if (popOnItemTap) Navigator.pop(context);
item.onPressed();
},
child: Container(
alignment: Alignment.center,
margin: const EdgeInsets.only(bottom: 1),
color: item.backgroundColor,
height: itemExtent,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 14),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
item.title,
if (item.trailingIcon != null) ...[item.trailingIcon!]
],
),
)));
if (animateMenu) {
return TweenAnimationBuilder(
builder: (context, dynamic value, child) {
return Transform(
transform: Matrix4.rotationX(1.5708 * value),
alignment: Alignment.bottomCenter,
child: child,
);
},
child: Container(
alignment: Alignment.center,
margin: const EdgeInsets.only(bottom: 1),
color: item.backgroundColor,
height: itemExtent,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 14),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
item.title,
if (item.trailingIcon != null) ...[item.trailingIcon!]
],
),
)));
if (animateMenu) {
return TweenAnimationBuilder(
builder: (context, dynamic value, child) {
return Transform(
transform: Matrix4.rotationX(1.5708 * value),
alignment: Alignment.bottomCenter,
child: child,
);
},
tween: Tween(begin: 1.0, end: 0.0),
duration: Duration(milliseconds: index * itemsAnimationDurationMS),
child: listItem);
} else {
return listItem;
}
},
),
),
tween: Tween(begin: 1.0, end: 0.0),
duration: Duration(milliseconds: index * itemsAnimationDurationMS),
child: listItem);
} else {
return listItem;
}
},
),
),
),
),
Expand Down
9 changes: 6 additions & 3 deletions lib/packages/inner_drawer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,12 @@ class InnerDrawerState extends State<InnerDrawer> with SingleTickerProviderState
],
),
child: widget.borderRadius != 0
? ClipRRect(
borderRadius: BorderRadius.circular(
(1 - _controller.value) * widget.borderRadius,
? Container(
clipBehavior: Clip.antiAlias,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(
(1 - _controller.value) * widget.borderRadius,
),
),
child: scaffoldChild,
)
Expand Down
Loading

0 comments on commit ba70591

Please sign in to comment.