Skip to content

Commit

Permalink
Improved code
Browse files Browse the repository at this point in the history
  • Loading branch information
gokadzev committed Nov 18, 2023
1 parent 8c72955 commit 1933654
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 117 deletions.
98 changes: 90 additions & 8 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:async';
import 'package:audio_service/audio_service.dart';
import 'package:background_downloader/background_downloader.dart';
import 'package:dynamic_color/dynamic_color.dart';
import 'package:fluentui_system_icons/fluentui_system_icons.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Expand All @@ -12,13 +13,16 @@ import 'package:google_fonts/google_fonts.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:musify/API/musify.dart';
import 'package:musify/extensions/audio_quality.dart';
import 'package:musify/screens/root_page.dart';
import 'package:musify/extensions/l10n.dart';
import 'package:musify/services/audio_service.dart';
import 'package:musify/services/data_manager.dart';
import 'package:musify/services/logger_service.dart';
import 'package:musify/services/router_service.dart';
import 'package:musify/services/settings_manager.dart';
import 'package:musify/services/update_manager.dart';
import 'package:musify/style/app_themes.dart';
import 'package:musify/utilities/formatter.dart';
import 'package:musify/widgets/mini_player.dart';
import 'package:receive_sharing_intent/receive_sharing_intent.dart';

late MusifyAudioHandler audioHandler;
Expand All @@ -27,6 +31,9 @@ final logger = Logger();
Locale locale = const Locale('en', '');
var isFdroidBuild = false;

final _navigatorKey = GlobalKey<NavigatorState>();
int _selectedIndex = 0;

final appLanguages = <String, String>{
'English': 'en',
'Arabic': 'ar',
Expand All @@ -46,8 +53,8 @@ final appSupportedLocales = appLanguages.values
.map((languageCode) => Locale.fromSubtags(languageCode: languageCode))
.toList();

class MyApp extends StatefulWidget {
const MyApp({super.key});
class Musify extends StatefulWidget {
const Musify({super.key});

static Future<void> updateAppState(
BuildContext context, {
Expand All @@ -56,7 +63,7 @@ class MyApp extends StatefulWidget {
Color? newAccentColor,
bool? useSystemColor,
}) async {
final state = context.findAncestorStateOfType<_MyAppState>()!;
final state = context.findAncestorStateOfType<_MusifyState>()!;
if (newThemeMode != null) {
state.changeTheme(newThemeMode);
}
Expand All @@ -69,10 +76,10 @@ class MyApp extends StatefulWidget {
}

@override
_MyAppState createState() => _MyAppState();
_MusifyState createState() => _MusifyState();
}

class _MyAppState extends State<MyApp> {
class _MusifyState extends State<Musify> {
void changeTheme(ThemeMode newThemeMode) {
setState(() {
themeMode = newThemeMode;
Expand Down Expand Up @@ -164,6 +171,12 @@ class _MyAppState extends State<MyApp> {
} catch (e) {
logger.log('License Registration Error: $e');
}

if (!isFdroidBuild) {
unawaited(checkAppUpdates(context));
}

unawaited(checkNecessaryPermissions(context));
}

@override
Expand Down Expand Up @@ -199,7 +212,76 @@ class _MyAppState extends State<MyApp> {
],
supportedLocales: appSupportedLocales,
locale: locale,
home: Musify(),
home: Scaffold(
body: PopScope(
canPop: _navigatorKey.currentState?.canPop() == true,
child: Navigator(
key: _navigatorKey,
initialRoute: RoutePaths.home,
onGenerateRoute: RouterService.generateRoute,
),
),
bottomNavigationBar: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
StreamBuilder<MediaItem?>(
stream: audioHandler.mediaItem,
builder: (context, snapshot) {
final metadata = snapshot.data;
if (metadata == null) {
return const SizedBox();
} else {
return MiniPlayer(metadata: metadata);
}
},
),
NavigationBar(
selectedIndex: _selectedIndex,
labelBehavior: locale == const Locale('en', '')
? NavigationDestinationLabelBehavior.onlyShowSelected
: NavigationDestinationLabelBehavior.alwaysHide,
onDestinationSelected: (int index) {
if (_selectedIndex == index) {
if (_navigatorKey.currentState?.canPop() == true) {
_navigatorKey.currentState?.pop();
}
} else {
setState(() {
_selectedIndex = index;
});
_navigatorKey.currentState?.pushNamedAndRemoveUntil(
destinations[index],
ModalRoute.withName(destinations[index]),
);
}
},
destinations: [
NavigationDestination(
icon: const Icon(FluentIcons.home_24_regular),
selectedIcon: const Icon(FluentIcons.home_24_filled),
label: context.l10n?.home ?? 'Home',
),
NavigationDestination(
icon: const Icon(FluentIcons.search_24_regular),
selectedIcon: const Icon(FluentIcons.search_24_filled),
label: context.l10n?.search ?? 'Search',
),
NavigationDestination(
icon: const Icon(FluentIcons.book_24_regular),
selectedIcon: const Icon(FluentIcons.book_24_filled),
label: context.l10n?.userPlaylists ?? 'User Playlists',
),
NavigationDestination(
icon: const Icon(FluentIcons.more_horizontal_24_regular),
selectedIcon:
const Icon(FluentIcons.more_horizontal_24_filled),
label: context.l10n?.more ?? 'More',
),
],
),
],
),
),
);
},
);
Expand All @@ -209,7 +291,7 @@ class _MyAppState extends State<MyApp> {
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await initialisation();
runApp(const MyApp());
runApp(const Musify());
}

Future<void> initialisation() async {
Expand Down
8 changes: 4 additions & 4 deletions lib/screens/more_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class MorePage extends StatelessWidget {
'accentColor',
availableColors[index].value,
);
MyApp.updateAppState(
Musify.updateAppState(
context,
newAccentColor:
availableColors[index],
Expand Down Expand Up @@ -221,7 +221,7 @@ class MorePage extends StatelessWidget {
'themeMode',
availableModes[index].name,
);
MyApp.updateAppState(
Musify.updateAppState(
context,
newThemeMode: availableModes[index],
);
Expand Down Expand Up @@ -277,7 +277,7 @@ class MorePage extends StatelessWidget {
'language',
availableLanguages[index],
);
MyApp.updateAppState(
Musify.updateAppState(
context,
newLocale: Locale(
appLanguages[
Expand Down Expand Up @@ -313,7 +313,7 @@ class MorePage extends StatelessWidget {
value,
);
useSystemColor.value = value;
MyApp.updateAppState(
Musify.updateAppState(
context,
newAccentColor: primaryColor,
useSystemColor: value,
Expand Down
104 changes: 0 additions & 104 deletions lib/screens/root_page.dart

This file was deleted.

2 changes: 1 addition & 1 deletion test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'package:musify/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());
await tester.pumpWidget(const Musify());

// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
Expand Down

0 comments on commit 1933654

Please sign in to comment.