Skip to content

Commit

Permalink
chore: sussy tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
MSOB7YY committed Dec 14, 2023
1 parent a9a8739 commit 8bdeec7
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 81 deletions.
12 changes: 12 additions & 0 deletions lib/controller/lifecycle_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,16 @@ class LifeCycleController {
void addOnSuspending(String key, FutureOr<void> Function() fn) {
_onSuspending[key] = fn;
}

void removeOnDestroy(String key) {
_onDestroy.remove(key);
}

void removeOnResume(String key) {
_onResume.remove(key);
}

void removeOnSuspending(String key) {
_onSuspending.remove(key);
}
}
19 changes: 18 additions & 1 deletion lib/controller/navigator_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,27 @@ class NamidaNavigator {
final GlobalKey<InnerDrawerState> innerDrawerKey = GlobalKey<InnerDrawerState>();
final heroController = HeroController();

bool isInLanscape = false;
bool _isInLanscape = false;
bool get isInLanscape => _isInLanscape;
set isInLanscape(bool val) {
_isInLanscape = val;
for (final fn in _onLandscapeEvents.values) {
fn();
}
}

static const _defaultRouteAnimationDurMS = 500;

final _onLandscapeEvents = <String, FutureOr<void> Function()>{};

void addOnLandScapeEvent(String key, FutureOr<void> Function() fn) {
_onLandscapeEvents[key] = fn;
}

void removeOnLandScapeEvent(String key) {
_onLandscapeEvents.remove(key);
}

Future<T?> showMenu<T>(Future? menuFunction) async {
_currentMenusNumber++;
_printMenus();
Expand Down
36 changes: 15 additions & 21 deletions lib/controller/waveform_controller.dart
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
import 'dart:math';

import 'package:get/get.dart';
import 'package:waveform_extractor/waveform_extractor.dart';

import 'package:namida/class/track.dart';
import 'package:namida/controller/player_controller.dart';
import 'package:namida/controller/settings_controller.dart';
import 'package:namida/core/constants.dart';
import 'package:namida/core/extensions.dart';

class WaveformController {
static WaveformController get inst => _instance;
static final WaveformController _instance = WaveformController._internal();
WaveformController._internal();

/// Trying to update [currentWaveformUI] while this is false will have no effect.
bool canModifyUIWaveform = false;
List<double> _currentWaveform = [];

List<double> _currentWaveform = kDefaultWaveFormData;
final currentWaveformUI = kDefaultWaveFormData.obs;
final currentWaveformUI = <double>[].obs;

final RxMap<int, double> _currentScaleMap = <int, double>{}.obs;

/// Extracts waveform data from a given track, or immediately read from .wave file if exists, then assigns wavedata to [_currentWaveform].
Future<void> generateWaveform(Track track) async {
_currentWaveform = kDefaultWaveFormData;
calculateUIWaveform(dummy: true);

const maxSampleRate = 400;
final scaledDuration = 0.4 * track.duration;
final scaledSampleRate = maxSampleRate * (exp(-scaledDuration / 100));
_currentWaveform = [];
currentWaveformUI.value = List.filled(settings.waveformTotalBars.value, 2.0);

final samplePerSecond = scaledSampleRate.toInt().clamp(1, maxSampleRate);
final samplePerSecond = _waveformExtractor.getSampleRateFromDuration(
audioDuration: Duration(seconds: track.duration),
maxSampleRate: 400,
scaleFactor: 0.4,
);

List<int> waveformData = [];
await Future.wait([
Expand All @@ -50,27 +45,26 @@ class WaveformController {
original: waveformData,
));

_currentWaveform = downscaledLists[maxWaveformCount]!;
_currentWaveform = downscaledLists[maxWaveformCount] ?? [];
calculateUIWaveform();

// ----- Updating [currentScale]
_updateScaleMap(downscaledLists[numberOfScales]!);
_updateScaleMap(downscaledLists[numberOfScales] ?? []);
}
}

void calculateUIWaveform({bool dummy = false}) async {
if (!canModifyUIWaveform) return;
void calculateUIWaveform() async {
if (_currentWaveform.isEmpty) return;
final userBars = settings.waveformTotalBars.value;
final waveform = await _calculateUIWaveformIsolate.thready((
targetSize: userBars,
original: dummy ? kDefaultWaveFormData : _currentWaveform,
original: _currentWaveform,
));
if (!canModifyUIWaveform) return;
currentWaveformUI.value = waveform;
}

static List<double> _calculateUIWaveformIsolate(({List<double> original, int targetSize}) params) {
final clamping = params.original.isEqualTo(kDefaultWaveFormData) ? null : 64.0;
final clamping = params.original.isEmpty ? null : 64.0;
final downscaled = params.original.changeListSize(
targetSize: params.targetSize,
multiplier: 0.9,
Expand Down
1 change: 0 additions & 1 deletion lib/core/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ int kSdkVersion = 21;

final Set<String> kStoragePaths = {};
final Set<String> kInitialDirectoriesToScan = {};
final List<double> kDefaultWaveFormData = List<double>.filled(1, 2.0);
final RegExp kYoutubeRegex = RegExp(
r'\b(?:https?://)?(?:www\.)?(?:youtube\.com/watch\?v=|youtu\.be/)([\w\-]+)(?:\S+)?',
caseSensitive: false,
Expand Down
2 changes: 1 addition & 1 deletion lib/core/namida_converter_ext.dart
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ extension PerformanceModeUtils on PerformanceMode {
enableBlurEffect: false,
enableGlowEffect: false,
enableMiniplayerParallaxEffect: true,
artworkCacheHeightMultiplier: 0.8,
artworkCacheHeightMultiplier: 0.85,
);
case PerformanceMode.goodLooking:
settings.save(
Expand Down
33 changes: 13 additions & 20 deletions lib/ui/dialogs/general_popup_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import 'package:namida/controller/lyrics_controller.dart';
import 'package:namida/controller/navigator_controller.dart';
import 'package:namida/controller/player_controller.dart';
import 'package:namida/controller/playlist_controller.dart';
import 'package:namida/controller/queue_controller.dart';
import 'package:namida/controller/scroll_search_controller.dart';
import 'package:namida/controller/settings_controller.dart';
import 'package:namida/core/constants.dart';
Expand Down Expand Up @@ -74,8 +73,12 @@ Future<void> showGeneralPopupDialog(
forceSingleArtwork ??= isSingle;
final doesTracksExist = !errorPlayingTrack && tracksExisting.isNotEmpty;

final trackToExtractColorFrom = forceSingleArtwork ? tracks[tracks.indexOfImage] : tracks.first;
final colorDelightened = extractColor ? await CurrentColor.inst.getTrackDelightnedColor(trackToExtractColorFrom) : CurrentColor.inst.color;
final trackToExtractColorFrom = tracks.isEmpty
? null
: forceSingleArtwork
? tracks[tracks.indexOfImage]
: tracks.first;
final colorDelightened = extractColor && trackToExtractColorFrom != null ? await CurrentColor.inst.getTrackDelightnedColor(trackToExtractColorFrom) : CurrentColor.inst.color;

/// name, identifier
final List<(String, String)> availableAlbums = tracks.mappedUniqued((e) {
Expand Down Expand Up @@ -202,9 +205,10 @@ Future<void> showGeneralPopupDialog(
);
}

final stats = tracks.first.stats.obs;
final stats = tracks.firstOrNull?.stats.obs;

void setTrackMoods() {
if (stats == null) return;
setMoodsOrTags(
stats.value.moods,
(moodsFinal) async {
Expand All @@ -214,6 +218,7 @@ Future<void> showGeneralPopupDialog(
}

void setTrackTags() {
if (stats == null) return;
cancelSkipTimer();
setMoodsOrTags(
stats.value.tags,
Expand All @@ -225,6 +230,7 @@ Future<void> showGeneralPopupDialog(
}

void setTrackRating() async {
if (stats == null) return;
final c = TextEditingController();
await openDialog(
onDisposing: () {
Expand Down Expand Up @@ -563,20 +569,7 @@ Future<void> showGeneralPopupDialog(
icon: Broken.pen_remove,
onTap: () {
cancelSkipTimer();
final oldQueue = queue;
QueueController.inst.removeQueue(oldQueue);
snackyy(
title: lang.UNDO_CHANGES,
message: lang.UNDO_CHANGES_DELETED_QUEUE,
displaySeconds: 3,
button: TextButton(
onPressed: () {
QueueController.inst.reAddQueue(oldQueue);
Get.closeAllSnackbars();
},
child: Text(lang.UNDO),
),
);
NamidaOnTaps.inst.onQueueDelete(queue);
NamidaNavigator.inst.closeDialog();
},
)
Expand All @@ -586,7 +579,7 @@ Future<void> showGeneralPopupDialog(
onDisposing: () {
numberOfRepeats.close();
isLoadingFilesToShare.close();
stats.close();
stats?.close();
},
colorScheme: colorDelightened,
lighterDialogColor: false,
Expand Down Expand Up @@ -1125,7 +1118,7 @@ Future<void> showGeneralPopupDialog(
Broken.grammerly,
lang.SET_RATING,
setTrackRating,
subtitle: stats.value.rating == 0 ? '' : ' ${stats.value.rating}%',
subtitle: stats == null || stats.value.rating == 0 ? '' : ' ${stats.value.rating}%',
),
),
),
Expand Down
3 changes: 1 addition & 2 deletions lib/ui/widgets/settings/advanced_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ class AdvancedSettings extends SettingSubpageProvider {
onTap: () {
e.execute();
settings.save(performanceMode: e);
NamidaNavigator.inst.popMenu(handleClosing: false);
Navigator.of(context).pop();
NamidaNavigator.inst.popMenu();
},
),
),
Expand Down
17 changes: 12 additions & 5 deletions lib/ui/widgets/waveform.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'package:flutter/material.dart';

import 'package:get/get.dart';

import 'package:namida/controller/lifecycle_controller.dart';
import 'package:namida/controller/navigator_controller.dart';
import 'package:namida/controller/waveform_controller.dart';
import 'package:namida/core/constants.dart';
import 'package:namida/core/extensions.dart';

class WaveformComponent extends StatefulWidget {
Expand Down Expand Up @@ -50,13 +50,20 @@ class _WaveformComponentState extends State<WaveformComponent> {
void initState() {
super.initState();
_fillWidget();
WaveformController.inst.canModifyUIWaveform = true;
if (WaveformController.inst.currentWaveformUI.isEqualTo(kDefaultWaveFormData)) WaveformController.inst.calculateUIWaveform();

// -- to refresh after coming resuming app
LifeCycleController.inst.addOnResume('waveform', WaveformController.inst.calculateUIWaveform);

// -- to refresh after coming back from landscape
NamidaNavigator.inst.addOnLandScapeEvent('waveform', () {
if (NamidaNavigator.inst.isInLanscape) WaveformController.inst.calculateUIWaveform();
});
}

@override
void dispose() {
WaveformController.inst.canModifyUIWaveform = false;
LifeCycleController.inst.removeOnResume('waveform');
NamidaNavigator.inst.removeOnLandScapeEvent('waveform');
super.dispose();
}

Expand Down
49 changes: 30 additions & 19 deletions lib/youtube/widgets/yt_comment_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,14 @@ class YTCommentCard extends StatelessWidget {
shimmerEnabled: author == null,
child: Row(
children: [
Text(
[
author,
if (uploadedFrom != null) uploadedFrom,
].join(' • '),
style: context.textTheme.displaySmall?.copyWith(fontWeight: FontWeight.w400, color: context.theme.colorScheme.onBackground.withAlpha(180)),
Expanded(
child: Text(
[
author,
if (uploadedFrom != null) uploadedFrom,
].join(' • '),
style: context.textTheme.displaySmall?.copyWith(fontWeight: FontWeight.w400, color: context.theme.colorScheme.onBackground.withAlpha(180)),
),
),
if (isHearted) ...[
const SizedBox(width: 4.0),
Expand Down Expand Up @@ -261,15 +263,17 @@ class YTCommentCardCompact extends StatelessWidget {
shimmerEnabled: author == null,
child: Row(
children: [
Text(
[
author,
if (uploadedFrom != null) uploadedFrom,
].join(' • '),
style: context.textTheme.displaySmall?.copyWith(
fontSize: 11.5.multipliedFontScale,
fontWeight: FontWeight.w400,
color: context.theme.colorScheme.onBackground.withAlpha(180),
Expanded(
child: Text(
[
author,
if (uploadedFrom != null) uploadedFrom,
].join(' • '),
style: context.textTheme.displaySmall?.copyWith(
fontSize: 11.5.multipliedFontScale,
fontWeight: FontWeight.w400,
color: context.theme.colorScheme.onBackground.withAlpha(180),
),
),
),
if (isPinned) ...[
Expand Down Expand Up @@ -337,19 +341,26 @@ class YTCommentCardCompact extends StatelessWidget {
likeCount?.formatDecimalShort() ?? '?',
style: context.textTheme.displaySmall?.copyWith(
fontSize: 11.5.multipliedFontScale,
fontWeight: FontWeight.w300,
fontWeight: FontWeight.w400,
),
),
),
const SizedBox(width: 12.0),
if (repliesCount != null && repliesCount > 0)
if (repliesCount != null && repliesCount > 0) ...[
Text(
' | ',
style: context.textTheme.displayMedium?.copyWith(fontWeight: FontWeight.w300),
),
Text(
[
lang.REPLIES,
repliesCount,
].join(' • '),
style: context.textTheme.displaySmall?.copyWith(fontWeight: FontWeight.w300),
style: context.textTheme.displaySmall?.copyWith(
fontSize: 11.5.multipliedFontScale,
fontWeight: FontWeight.w400,
),
),
],
],
),
],
Expand Down
5 changes: 4 additions & 1 deletion lib/youtube/youtube_miniplayer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,10 @@ class YoutubeMiniPlayer extends StatelessWidget {
onTap: () {
NamidaNavigator.inst.isInYTCommentsSubpage = true;
NamidaNavigator.inst.ytMiniplayerCommentsPageKey?.currentState?.push(
MaterialPageRoute(builder: (context) => const YTMiniplayerCommentsSubpage()),
GetPageRoute(
page: () => const YTMiniplayerCommentsSubpage(),
transition: Transition.cupertino,
),
);
},
child: Column(
Expand Down

0 comments on commit 8bdeec7

Please sign in to comment.