Skip to content

Commit

Permalink
feat: similar station button, auto data safe mode, move handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Feichtmeier committed Jan 23, 2025
1 parent b09a955 commit 49c7ef2
Show file tree
Hide file tree
Showing 16 changed files with 262 additions and 220 deletions.
12 changes: 12 additions & 0 deletions lib/app/app_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import 'package:package_info_plus/package_info_plus.dart';
import 'package:safe_change_notifier/safe_change_notifier.dart';

import '../app_config.dart';
import '../common/view/snackbars.dart';
import '../constants.dart';
import '../expose/expose_service.dart';
import '../settings/settings_service.dart';
import 'view/discord_connect_content.dart';

class AppModel extends SafeChangeNotifier {
AppModel({
Expand Down Expand Up @@ -163,3 +165,13 @@ class AppModel extends SafeChangeNotifier {
];
}
}

void discordConnectedHandler(context, snapshot, cancel) {
if (snapshot.data == true) {
showSnackBar(
context: context,
duration: const Duration(seconds: 3),
content: DiscordConnectContent(connected: snapshot.data == true),
);
}
}
37 changes: 36 additions & 1 deletion lib/app/connectivity_model.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import 'dart:async';

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/material.dart';
import 'package:safe_change_notifier/safe_change_notifier.dart';
import 'package:watch_it/watch_it.dart';

import '../common/data/audio_type.dart';
import '../common/view/snackbars.dart';
import '../extensions/connectivity_x.dart';
import '../player/player_model.dart';
import '../player/player_service.dart';
import '../settings/settings_model.dart';

class ConnectivityModel extends SafeChangeNotifier {
ConnectivityModel({
Expand All @@ -16,6 +21,8 @@ class ConnectivityModel extends SafeChangeNotifier {

final PlayerService _playerService;
final Connectivity _connectivity;
Stream<List<ConnectivityResult>> get onConnectivityChanged =>
_connectivity.onConnectivityChanged;
StreamSubscription<List<ConnectivityResult>>? _connectivitySubscription;

Future<void> init() async {
Expand All @@ -35,7 +42,7 @@ class ConnectivityModel extends SafeChangeNotifier {

bool get isOnline => _connectivity.isOnline(_result);

bool get isMaybeLowBandWidth => _connectivity.isMaybeLowBandWidth(_result);
bool get isMaybeLowBandWidth => _connectivity.isNotWifiNorEthernet(_result);

List<ConnectivityResult>? get result => _result;
List<ConnectivityResult>? _result;
Expand All @@ -54,3 +61,31 @@ class ConnectivityModel extends SafeChangeNotifier {
super.dispose();
}
}

void onConnectivityChangedHandler(context, res, cancel) {
final l10n = context.l10n;
final dataSafeMode = di<PlayerModel>().dataSafeMode;
final notifyDataSafeMode = di<SettingsModel>().notifyDataSafeMode;
if (!res.hasData || !context.mounted || !notifyDataSafeMode) {
return;
}

if (!dataSafeMode && di<Connectivity>().isNotWifiNorEthernet(res.data)) {
di<PlayerModel>().setDataSafeMode(true);
showSnackBar(
context: context,
snackBar: SnackBar(
content: Text(l10n.dataSafeModeEnabled),
),
);
} else if (dataSafeMode &&
!di<Connectivity>().isNotWifiNorEthernet(res.data)) {
di<PlayerModel>().setDataSafeMode(false);
showSnackBar(
context: context,
snackBar: SnackBar(
content: Text(l10n.dataSafeModeDisabled),
),
);
}
}
82 changes: 8 additions & 74 deletions lib/app/view/desktop_home_page.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import 'package:flutter/material.dart';
import 'package:flutter_tabler_icons/flutter_tabler_icons.dart';
import 'package:watch_it/watch_it.dart';
import 'package:yaru/yaru.dart';

import '../../app_config.dart';
import '../../common/view/snackbars.dart';
import '../../common/view/theme.dart';
import '../../common/view/ui_constants.dart';
import '../../extensions/build_context_x.dart';
import '../../l10n/l10n.dart';
import '../../patch_notes/patch_notes_dialog.dart';
import '../../player/player_model.dart';
import '../../player/view/player_view.dart';
import '../../podcasts/download_model.dart';
import '../../podcasts/podcast_model.dart';
import '../../podcasts/podcast_search_state.dart';
import '../../podcasts/view/podcast_snackbar_contents.dart';
import '../../settings/settings_model.dart';
import '../app_model.dart';
import '../connectivity_model.dart';
Expand Down Expand Up @@ -58,55 +51,25 @@ class _DesktopHomePageState extends State<DesktopHomePage> {
if (allowDiscordRPC && enableDiscordRPC) {
registerStreamHandler(
select: (AppModel m) => m.isDiscordConnectedStream,
handler: (context, snapshot, cancel) {
if (snapshot.data == true) {
showSnackBar(
context: context,
duration: const Duration(seconds: 3),
content: _DiscordConnectContent(connected: snapshot.data == true),
);
}
},
handler: discordConnectedHandler,
);
}

registerStreamHandler(
select: (DownloadModel m) => m.messageStream,
initialValue: null,
handler: (context, snapshot, cancel) {
if (snapshot.hasData) {
showSnackBar(context: context, content: Text(snapshot.data ?? ''));
}
},
handler: downloadMessageStreamHandler,
);

registerStreamHandler(
select: (PodcastModel m) => m.stateStream,
initialValue: null,
handler: (context, newValue, cancel) {
if (newValue.hasData) {
if (newValue.data == PodcastSearchState.done) {
ScaffoldMessenger.of(context).clearSnackBars();
} else {
showSnackBar(
context: context,
content: switch (newValue.data) {
PodcastSearchState.loading =>
const PodcastSearchLoadingSnackBarContent(),
PodcastSearchState.empty =>
const PodcastSearchEmptyFeedSnackBarContent(),
PodcastSearchState.timeout =>
const PodcastSearchTimeoutSnackBarContent(),
_ => const SizedBox.shrink()
},
duration: switch (newValue.data) {
PodcastSearchState.loading => const Duration(seconds: 1000),
_ => const Duration(seconds: 3),
},
);
}
}
},
handler: podcastStateStreamHandler,
);

registerStreamHandler(
select: (ConnectivityModel m) => m.onConnectivityChanged,
handler: onConnectivityChangedHandler,
);

return Stack(
Expand Down Expand Up @@ -139,32 +102,3 @@ class _DesktopHomePageState extends State<DesktopHomePage> {
);
}
}

class _DiscordConnectContent extends StatelessWidget {
const _DiscordConnectContent({required this.connected});

final bool connected;

@override
Widget build(BuildContext context) {
final l10n = context.l10n;
return Row(
mainAxisSize: MainAxisSize.min,
children: space(
widthGap: 10,
children: [
Text(
'${connected ? l10n.connectedTo : l10n.disconnectedFrom}'
' ${l10n.exposeToDiscordTitle}',
),
Icon(
TablerIcons.brand_discord_filled,
color: context.theme.snackBarTheme.backgroundColor != null
? contrastColor(context.theme.snackBarTheme.backgroundColor!)
: null,
),
],
),
);
}
}
35 changes: 35 additions & 0 deletions lib/app/view/discord_connect_content.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:flutter/material.dart';
import 'package:flutter_tabler_icons/flutter_tabler_icons.dart';
import 'package:yaru/yaru.dart';
import '../../common/view/theme.dart';
import '../../extensions/build_context_x.dart';
import '../../l10n/l10n.dart';

class DiscordConnectContent extends StatelessWidget {
const DiscordConnectContent({super.key, required this.connected});

final bool connected;

@override
Widget build(BuildContext context) {
final l10n = context.l10n;
return Row(
mainAxisSize: MainAxisSize.min,
children: space(
widthGap: 10,
children: [
Text(
'${connected ? l10n.connectedTo : l10n.disconnectedFrom}'
' ${l10n.exposeToDiscordTitle}',
),
Icon(
TablerIcons.brand_discord_filled,
color: context.theme.snackBarTheme.backgroundColor != null
? contrastColor(context.theme.snackBarTheme.backgroundColor!)
: null,
),
],
),
);
}
}
69 changes: 7 additions & 62 deletions lib/app/view/mobile_page.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/material.dart';
import 'package:watch_it/watch_it.dart';

import '../../common/view/bandwidth_dialog.dart';
import '../../common/view/snackbars.dart';
import '../../common/view/theme.dart';
import '../../extensions/build_context_x.dart';
import '../../extensions/connectivity_x.dart';
import '../../player/player_model.dart';
import '../../player/view/full_height_player.dart';
import '../../player/view/player_view.dart';
import '../../podcasts/download_model.dart';
import '../../podcasts/podcast_model.dart';
import '../../podcasts/podcast_search_state.dart';
import '../../podcasts/view/podcast_snackbar_contents.dart';
import '../../settings/settings_model.dart';
import '../app_model.dart';
import '../connectivity_model.dart';
import 'mobile_bottom_bar.dart';

class MobilePage extends StatelessWidget with WatchItMixin {
Expand All @@ -34,67 +28,18 @@ class MobilePage extends StatelessWidget with WatchItMixin {
registerStreamHandler(
select: (DownloadModel m) => m.messageStream,
initialValue: null,
handler: (context, snapshot, cancel) {
if (snapshot.hasData) {
showSnackBar(context: context, content: Text(snapshot.data ?? ''));
}
},
handler: downloadMessageStreamHandler,
);

final dataSafeMode = watchPropertyValue((PlayerModel m) => m.dataSafeMode);
final notifyDataSafeMode =
watchPropertyValue((SettingsModel m) => m.notifyDataSafeMode);

registerStreamHandler(
select: (Connectivity m) => m.onConnectivityChanged,
handler: (context, res, cancel) {
if (notifyDataSafeMode && res.hasData) {
if (di<Connectivity>().isMaybeLowBandWidth(res.data) &&
!dataSafeMode) {
showDialog(
context: context,
builder: (context) => const BandwidthDialog(),
);
} else if (!di<Connectivity>().isMaybeLowBandWidth(res.data) &&
dataSafeMode) {
showDialog(
context: context,
builder: (context) => const BandwidthDialog(
backOnBetterConnection: true,
),
);
}
}
},
select: (PodcastModel m) => m.stateStream,
initialValue: null,
handler: podcastStateStreamHandler,
);

registerStreamHandler(
select: (PodcastModel m) => m.stateStream,
initialValue: null,
handler: (context, newValue, cancel) {
if (newValue.hasData) {
if (newValue.data == PodcastSearchState.done) {
ScaffoldMessenger.of(context).clearSnackBars();
} else {
showSnackBar(
context: context,
content: switch (newValue.data) {
PodcastSearchState.loading =>
const PodcastSearchLoadingSnackBarContent(),
PodcastSearchState.empty =>
const PodcastSearchEmptyFeedSnackBarContent(),
PodcastSearchState.timeout =>
const PodcastSearchTimeoutSnackBarContent(),
_ => const SizedBox.shrink()
},
duration: switch (newValue.data) {
PodcastSearchState.loading => const Duration(seconds: 1000),
_ => const Duration(seconds: 3),
},
);
}
}
},
select: (ConnectivityModel m) => m.onConnectivityChanged,
handler: onConnectivityChangedHandler,
);

return Scaffold(
Expand Down
37 changes: 0 additions & 37 deletions lib/common/view/bandwidth_dialog.dart

This file was deleted.

Loading

0 comments on commit 49c7ef2

Please sign in to comment.