Skip to content

Commit

Permalink
feat: web compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Aug 5, 2023
1 parent 7053d8f commit cf7b849
Show file tree
Hide file tree
Showing 35 changed files with 462 additions and 176 deletions.
12 changes: 7 additions & 5 deletions lib/collections/routes.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:catcher/catcher.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:go_router/go_router.dart';
import 'package:spotify/spotify.dart' hide Search;
Expand Down Expand Up @@ -84,12 +85,13 @@ final router = GoRouter(
child: const BlackListPage(),
),
),
GoRoute(
path: "logs",
pageBuilder: (context, state) => SpotubeSlidePage(
child: const LogsPage(),
if (!kIsWeb)
GoRoute(
path: "logs",
pageBuilder: (context, state) => SpotubeSlidePage(
child: const LogsPage(),
),
),
),
GoRoute(
path: "about",
pageBuilder: (context, state) => SpotubeSlidePage(
Expand Down
26 changes: 12 additions & 14 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import 'package:media_kit/media_kit.dart';
import 'package:metadata_god/metadata_god.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:spotube/collections/env.dart';
import 'package:spotube/collections/routes.dart';
import 'package:spotube/collections/intents.dart';
import 'package:spotube/hooks/use_disable_battery_optimizations.dart';
Expand All @@ -30,7 +29,6 @@ import 'package:spotube/provider/user_preferences_provider.dart';
import 'package:spotube/services/audio_player/audio_player.dart';
import 'package:spotube/themes/theme.dart';
import 'package:spotube/utils/persisted_state_notifier.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
import 'package:system_theme/system_theme.dart';
import 'package:path_provider/path_provider.dart';
import 'package:spotube/hooks/use_init_sys_tray.dart';
Expand Down Expand Up @@ -75,11 +73,6 @@ Future<void> main(List<String> rawArgs) async {
exit(0);
}

await Supabase.initialize(
url: Env.supabaseUrl,
anonKey: Env.supabaseAnonKey,
);

final widgetsBinding = WidgetsFlutterBinding.ensureInitialized();

FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
Expand All @@ -101,9 +94,13 @@ Future<void> main(List<String> rawArgs) async {
);

await SystemTheme.accentColor.load();
MetadataGod.initialize();

final hiveCacheDir = (await getApplicationSupportDirectory()).path;
if (!kIsWeb) {
MetadataGod.initialize();
}

final hiveCacheDir =
kIsWeb ? null : (await getApplicationSupportDirectory()).path;

await QueryClient.initialize(
cachePrefix: "oss.krtirtho.spotube",
Expand Down Expand Up @@ -139,17 +136,18 @@ Future<void> main(List<String> rawArgs) async {
enableDeviceParameters: false,
enableApplicationParameters: false,
),
FileHandler(await getLogsPath(), printLogs: false),
if (!kIsWeb) FileHandler(await getLogsPath(), printLogs: false),
],
),
releaseConfig: CatcherOptions(
SilentReportMode(),
[
if (arguments["verbose"] ?? false) ConsoleHandler(),
FileHandler(
await getLogsPath(),
printLogs: false,
),
if (!kIsWeb)
FileHandler(
await getLogsPath(),
printLogs: false,
),
],
),
runAppFunction: () {
Expand Down
28 changes: 15 additions & 13 deletions lib/pages/settings/settings.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:auto_size_text/auto_size_text.dart';
import 'package:collection/collection.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_desktop_tools/flutter_desktop_tools.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
Expand Down Expand Up @@ -506,19 +507,20 @@ class SettingsPage extends HookConsumerWidget {
),
],
),
SectionCardWithHeading(
heading: context.l10n.developers,
children: [
ListTile(
leading: const Icon(SpotubeIcons.logs),
title: Text(context.l10n.logs),
trailing: const Icon(SpotubeIcons.angleRight),
onTap: () {
GoRouter.of(context).push("/settings/logs");
},
)
],
),
if (!kIsWeb)
SectionCardWithHeading(
heading: context.l10n.developers,
children: [
ListTile(
leading: const Icon(SpotubeIcons.logs),
title: Text(context.l10n.logs),
trailing: const Icon(SpotubeIcons.angleRight),
onTap: () {
GoRouter.of(context).push("/settings/logs");
},
)
],
),
SectionCardWithHeading(
heading: context.l10n.about,
children: [
Expand Down
3 changes: 3 additions & 0 deletions lib/provider/download_manager_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:io';

import 'package:background_downloader/background_downloader.dart';
import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:http/http.dart';
import 'package:metadata_god/metadata_god.dart';
Expand Down Expand Up @@ -30,6 +31,8 @@ class DownloadManagerProvider extends StateNotifier<List<SpotubeTrack>> {
: activeDownloadProgress = StreamController.broadcast(),
failedDownloads = StreamController.broadcast(),
super([]) {
if (kIsWeb) return;

FileDownloader().registerCallbacks(
group: FileDownloader.defaultGroup,
taskNotificationTapCallback: (task, notificationType) {
Expand Down
10 changes: 7 additions & 3 deletions lib/provider/user_preferences_provider.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';
import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:path_provider/path_provider.dart';
Expand Down Expand Up @@ -85,7 +86,7 @@ class UserPreferences extends PersistedChangeNotifier {
this.skipNonMusic = true,
this.youtubeApiType = YoutubeApiType.piped,
}) : super() {
if (downloadLocation.isEmpty) {
if (downloadLocation.isEmpty && !kIsWeb) {
_getDefaultDownloadDirectory().then(
(value) {
downloadLocation = value;
Expand Down Expand Up @@ -222,8 +223,11 @@ class UserPreferences extends PersistedChangeNotifier {
audioQuality = map["audioQuality"] != null
? AudioQuality.values[map["audioQuality"]]
: audioQuality;
downloadLocation =
map["downloadLocation"] ?? await _getDefaultDownloadDirectory();

if (!kIsWeb) {
downloadLocation =
map["downloadLocation"] ?? await _getDefaultDownloadDirectory();
}

layoutMode = LayoutMode.values.firstWhere(
(mode) => mode.name == map["layoutMode"],
Expand Down
28 changes: 14 additions & 14 deletions lib/services/audio_player/mk_state_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ class MkPlayerWithState extends Player {
}

@override
FutureOr<void> dispose({int code = 0}) {
Future<void> dispose() {
for (var element in _subscriptions) {
element.cancel();
}
return super.dispose(code: code);
return super.dispose();
}

@override
Expand All @@ -153,9 +153,9 @@ class MkPlayerWithState extends Player {
}

@override
FutureOr<void> next() {
Future<void> next() async {
if (_playlist == null || _playlist!.index + 1 >= _playlist!.medias.length) {
return null;
return;
}

final isLast = _playlist!.index == _playlist!.medias.length - 1;
Expand All @@ -170,8 +170,8 @@ class MkPlayerWithState extends Player {
}

@override
FutureOr<void> previous() {
if (_playlist == null || _playlist!.index - 1 < 0) return null;
Future<void> previous() async {
if (_playlist == null || _playlist!.index - 1 < 0) return;

if (loopMode == PlaylistMode.loop && _playlist!.index == 0) {
playlist = _playlist!.copyWith(index: _playlist!.medias.length - 1);
Expand All @@ -183,7 +183,7 @@ class MkPlayerWithState extends Player {
}

@override
FutureOr<void> jump(int index) {
Future<void> jump(int index) async {
if (_playlist == null || index < 0 || index >= _playlist!.medias.length) {
return null;
}
Expand All @@ -193,10 +193,10 @@ class MkPlayerWithState extends Player {
}

@override
FutureOr<void> move(int from, int to) {
Future<void> move(int from, int to) async {
if (_playlist == null ||
from >= _playlist!.medias.length ||
to >= _playlist!.medias.length) return null;
to >= _playlist!.medias.length) return;

final active = _playlist!.medias[_playlist!.index];
final newPlaylist = _playlist!.copyWith(
Expand Down Expand Up @@ -255,8 +255,8 @@ class MkPlayerWithState extends Player {
}

@override
FutureOr<void> add(Media media) {
if (_playlist == null) return null;
Future<void> add(Media media) async {
if (_playlist == null) return;

playlist = _playlist!.copyWith(
medias: [..._playlist!.medias, media],
Expand Down Expand Up @@ -289,16 +289,16 @@ class MkPlayerWithState extends Player {

/// Doesn't work when active media is the one to be removed
@override
FutureOr<void> remove(int index) async {
Future<void> remove(int index) async {
if (_playlist == null ||
index < 0 ||
index > _playlist!.medias.length - 1 ||
_playlist!.index == index) {
return null;
return;
}

final targetItem = _playlist!.medias.elementAtOrNull(index);
if (targetItem == null) return null;
if (targetItem == null) return;

if (shuffled && _tempMedias != null) {
_tempMedias!.remove(targetItem);
Expand Down
Loading

0 comments on commit cf7b849

Please sign in to comment.