Files
SpotiFLAC-Mobile/lib/utils/home_greeting.dart
T
zarzet 67a346c15b feat(home): greet in the header and gather listening shortcuts
Without a feed, the Material Home now greets in its header with a
prompt picked once per launch, keeps import and paste out of the
resting search field, and fills the page with Continue listening,
player shortcuts, Recently added tracks from the whole Library and the
most played tracks of the last four weeks.
2026-10-18 18:08:13 +07:00

58 lines
1.7 KiB
Dart

import 'dart:math';
import 'package:spotiflac_android/l10n/app_localizations.dart';
/// Picked once per launch: the greeting holds still while browsing and
/// changes the next time the app opens.
final int _sessionVariant = Random().nextInt(1 << 16);
/// A time-of-day Home greeting, addressed by the profile's first name when
/// one is set.
String homeGreeting(
AppLocalizations l10n, {
String name = '',
DateTime? now,
int? variant,
}) {
final hour = (now ?? DateTime.now()).hour;
final options = switch (hour) {
>= 5 && < 12 => [
l10n.homeGreetingMorning1,
l10n.homeGreetingMorning2,
l10n.homeGreetingMorning3,
],
>= 12 && < 17 => [
l10n.homeGreetingAfternoon1,
l10n.homeGreetingAfternoon2,
l10n.homeGreetingAfternoon3,
],
>= 17 && < 21 => [
l10n.homeGreetingEvening1,
l10n.homeGreetingEvening2,
l10n.homeGreetingEvening3,
],
_ => [
l10n.homeGreetingNight1,
l10n.homeGreetingNight2,
l10n.homeGreetingNight3,
],
};
final phrase = options[(variant ?? _sessionVariant) % options.length];
final trimmed = name.trim();
if (trimmed.isEmpty) return phrase;
// A full name reads stiff in a headline.
return l10n.homeGreetingNamed(phrase, trimmed.split(RegExp(r'\s+')).first);
}
/// The prompt under the Home greeting. Like the greeting, it holds for the
/// whole launch.
String homeTagline(AppLocalizations l10n, {int? variant}) {
final options = [
l10n.homeTaglineListening,
l10n.homeTaglineLink,
l10n.homeTaglineDiscover,
];
// Offset from the greeting's pick so the two do not always pair up.
return options[((variant ?? _sessionVariant) ~/ 3) % options.length];
}