Skip to content

Commit

Permalink
[CastIt.Android] Clean the house part V
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Jun 28, 2021
1 parent 039517c commit 4be80f0
Show file tree
Hide file tree
Showing 34 changed files with 385 additions and 405 deletions.
73 changes: 23 additions & 50 deletions CastIt.Android/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,48 +1,28 @@
import 'package:castit/domain/services/device_info_service.dart';
import 'package:castit/domain/services/locale_service.dart';
import 'package:castit/domain/services/logging_service.dart';
import 'package:castit/domain/services/settings_service.dart';
import 'package:castit/generated/l10n.dart';
import 'package:castit/injection.dart';
import 'package:castit/presentation/shared/extensions/app_theme_type_extensions.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

import 'bloc/intro/intro_bloc.dart';
import 'bloc/main/main_bloc.dart';
import 'bloc/play/play_bloc.dart';
import 'bloc/played_file_options/played_file_options_bloc.dart';
import 'bloc/playlist/playlist_bloc.dart';
import 'bloc/playlist_rename/playlist_rename_bloc.dart';
import 'bloc/playlists/playlists_bloc.dart';
import 'bloc/server_ws/server_ws_bloc.dart';
import 'bloc/settings/settings_bloc.dart';
import 'generated/i18n.dart';
import 'injection.dart';
import 'services/logging_service.dart';
import 'services/settings_service.dart';
import 'telemetry.dart';
import 'ui/pages/intro_page.dart';
import 'ui/pages/main_page.dart';
import 'application/bloc.dart';
import 'presentation/intro/intro_page.dart';
import 'presentation/main/main_page.dart';

Future main() async {
WidgetsFlutterBinding.ensureInitialized();
initInjection();
await initTelemetry();
await initInjection();
await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
runApp(MyApp());
}

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
final GeneratedLocalizationsDelegate i18n = I18n.delegate;

@override
void initState() {
super.initState();
I18n.onLocaleChanged = _onLocaleChange;
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
Expand All @@ -51,7 +31,9 @@ class _MyAppState extends State<MyApp> {
create: (ctx) {
final logger = getIt<LoggingService>();
final settings = getIt<SettingsService>();
return MainBloc(logger, settings)..add(MainEvent.init());
final deviceInfo = getIt<DeviceInfoService>();
final localeService = getIt<LocaleService>();
return MainBloc(logger, settings, deviceInfo, localeService)..add(MainEvent.init());
},
),
BlocProvider(
Expand Down Expand Up @@ -83,7 +65,7 @@ class _MyAppState extends State<MyApp> {
create: (ctx) {
final settings = getIt<SettingsService>();
final serverWsBloc = ctx.read<ServerWsBloc>();
return SettingsBloc(settings, serverWsBloc);
return SettingsBloc(settings, serverWsBloc, ctx.read<MainBloc>());
},
),
BlocProvider(create: (ctx) {
Expand All @@ -105,37 +87,28 @@ class _MyAppState extends State<MyApp> {

Widget _buildApp(MainState state) {
final delegates = <LocalizationsDelegate>[
// A class which loads the translations from JSON files
i18n,
// Built-in localization of basic text for Material widgets
S.delegate,
GlobalMaterialLocalizations.delegate,
// Built-in localization for text direction LTR/RTL
GlobalWidgetsLocalizations.delegate,
// Built-in localization of basic text for Cupertino widgets
GlobalCupertinoLocalizations.delegate,
];
return state.map<Widget>(
loading: (_) {
return const CircularProgressIndicator();
},
loaded: (s) {
final locale = Locale(s.language.code, s.language.countryCode);
final themeData = s.accentColor.getThemeData(s.theme);
return MaterialApp(
title: s.appTitle,
theme: s.theme,
theme: themeData,
home: s.firstInstall ? IntroPage() : MainPage(),
//Without this, the lang won't be reloaded
locale: locale,
localizationsDelegates: delegates,
supportedLocales: i18n.supportedLocales,
localeResolutionCallback: i18n.resolution(
fallback: i18n.supportedLocales.first,
),
supportedLocales: S.delegate.supportedLocales,
);
},
);
}

void _onLocaleChange(Locale locale) {
setState(() {
I18n.locale = locale;
});
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import 'package:castit/application/bloc.dart';
import 'package:castit/domain/enums/enums.dart';
import 'package:castit/generated/l10n.dart';
import 'package:castit/presentation/shared/extensions/i18n_extensions.dart';
import 'package:castit/presentation/shared/extensions/styles.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../bloc/intro/intro_bloc.dart';
import '../../bloc/main/main_bloc.dart';
import '../../bloc/settings/settings_bloc.dart';
import '../../common/enums/app_language_type.dart';
import '../../common/extensions/i18n_extensions.dart';
import '../../common/styles.dart';
import '../../generated/i18n.dart';
import '../widgets/items/intro_page_item.dart';
import '../widgets/modals/change_connection_bottom_sheet_dialog.dart';
import '../widgets/modals/skip_intro_bottom_sheet.dart';
import '../shared/change_connection_bottom_sheet_dialog.dart';
import 'widgets/intro_page_item.dart';
import 'widgets/skip_intro_bottom_sheet.dart';

class IntroPage extends StatefulWidget {
@override
Expand Down Expand Up @@ -60,15 +58,15 @@ class _IntroPageState extends State<IntroPage> {
}

Widget _buildPage(IntroState state) {
final i18n = I18n.of(context);
final i18n = S.of(context);
return state.map(
loading: (s) => PageView(),
loading: (_) => PageView(),
loaded: (s) => PageView(
controller: _pageController,
onPageChanged: (index) => context.read<IntroBloc>().add(IntroEvent.changePage(newPage: index)),
children: [
IntroPageItem(
mainTitle: i18n!.welcome(i18n.appName),
mainTitle: i18n.welcome(i18n.appName),
subTitle: i18n.aboutSummary,
content: i18n.welcomeSummary,
extraContent: _buildLanguageSettings(s.currentLang),
Expand All @@ -93,7 +91,7 @@ class _IntroPageState extends State<IntroPage> {
loading: (_) => null,
loaded: (s) {
final theme = Theme.of(context);
final i18n = I18n.of(context);
final i18n = S.of(context);
return s.page != 2
? Container(
margin: const EdgeInsets.symmetric(vertical: 5),
Expand All @@ -105,7 +103,7 @@ class _IntroPageState extends State<IntroPage> {
await _showSkipDialog();
},
child: Text(
i18n!.skip.toUpperCase(),
i18n.skip.toUpperCase(),
style: TextStyle(color: theme.accentColor, fontWeight: FontWeight.w600),
),
),
Expand All @@ -131,7 +129,7 @@ class _IntroPageState extends State<IntroPage> {
color: theme.accentColor,
alignment: Alignment.center,
child: Text(
i18n!.start.toUpperCase(),
i18n.start.toUpperCase(),
style: const TextStyle(color: Colors.white, fontWeight: FontWeight.w600),
),
),
Expand All @@ -153,10 +151,8 @@ class _IntroPageState extends State<IntroPage> {
);
}

Widget _buildLanguageSettings(
AppLanguageType currentLang,
) {
final i18n = I18n.of(context)!;
Widget _buildLanguageSettings(AppLanguageType currentLang) {
final i18n = S.of(context);
final dropdown = [AppLanguageType.english, AppLanguageType.spanish]
.map<DropdownMenuItem<AppLanguageType>>(
(lang) => DropdownMenuItem<AppLanguageType>(
Expand Down Expand Up @@ -198,7 +194,7 @@ class _IntroPageState extends State<IntroPage> {
}

void _showUrlModal(String castItUrl) {
final i18n = I18n.of(context);
final i18n = S.of(context);
showModalBottomSheet(
context: context,
shape: Styles.modalBottomSheetShape,
Expand All @@ -207,7 +203,7 @@ class _IntroPageState extends State<IntroPage> {
isScrollControlled: true,
builder: (_) => ChangeConnectionBottomSheetDialog(
icon: Icons.info_outline,
title: i18n!.webServerUrl,
title: i18n.webServerUrl,
currentUrl: castItUrl,
showRefreshButton: false,
showOkButton: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';

import '../../../common/styles.dart';
import '../../shared/extensions/styles.dart';

class IntroPageItem extends StatelessWidget {
final String mainTitle;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import 'package:castit/generated/l10n.dart';
import 'package:castit/presentation/shared/confirm_bottom_sheet.dart';
import 'package:flutter/material.dart';

import '../../../generated/i18n.dart';
import 'confirm_bottom_sheet.dart';

class SkipIntroBottomSheet extends StatelessWidget {
@override
Widget build(BuildContext context) {
final i18n = I18n.of(context)!;
final i18n = S.of(context);
return ConfirmBottomSheet(
title: i18n.confirm,
icon: Icons.skip_next,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import 'package:castit/bloc/playlist/playlist_bloc.dart';
import 'package:castit/bloc/playlists/playlists_bloc.dart';
import 'package:castit/common/enums/app_message_type.dart';
import 'package:castit/application/bloc.dart';
import 'package:castit/domain/enums/enums.dart';
import 'package:castit/generated/l10n.dart';
import 'package:castit/presentation/play/play_page.dart';
import 'package:castit/presentation/playlists/playlists_page.dart';
import 'package:castit/presentation/settings/settings_page.dart';
import 'package:castit/presentation/shared/change_connection_bottom_sheet_dialog.dart';
import 'package:castit/presentation/shared/extensions/i18n_extensions.dart';
import 'package:castit/presentation/shared/extensions/styles.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../bloc/main/main_bloc.dart';
import '../../bloc/server_ws/server_ws_bloc.dart';
import '../../bloc/settings/settings_bloc.dart';
import '../../common/extensions/i18n_extensions.dart';
import '../../common/styles.dart';
import '../../generated/i18n.dart';
import '../widgets/modals/change_connection_bottom_sheet_dialog.dart';
import 'play_page.dart';
import 'playlists_page.dart';
import 'settings_page.dart';

class MainPage extends StatefulWidget {
@override
_MainPageState createState() => _MainPageState();
Expand Down Expand Up @@ -71,9 +66,10 @@ class _MainPageState extends State<MainPage> with SingleTickerProviderStateMixin
body: SafeArea(
child: BlocConsumer<MainBloc, MainState>(
listener: (ctx, state) async {
if (state is MainLoadedState) {
_changeCurrentTab(state.currentSelectedTab);
}
state.maybeMap(
loaded: (s) => _changeCurrentTab(s.currentSelectedTab),
orElse: () {},
);
},
builder: (ctx, state) => BlocConsumer<ServerWsBloc, ServerWsState>(
listener: (ctx2, state2) async {
Expand Down Expand Up @@ -116,7 +112,7 @@ class _MainPageState extends State<MainPage> with SingleTickerProviderStateMixin
}

List<BottomNavigationBarItem> _buildBottomNavBars() {
final i18n = I18n.of(context)!;
final i18n = S.of(context);
return [
BottomNavigationBarItem(
label: i18n.playing,
Expand Down Expand Up @@ -149,9 +145,7 @@ class _MainPageState extends State<MainPage> with SingleTickerProviderStateMixin
shape: Styles.modalBottomSheetShape,
isDismissible: true,
isScrollControlled: true,
builder: (_) => ChangeConnectionBottomSheetDialog(
currentUrl: currentCastIt,
),
builder: (_) => ChangeConnectionBottomSheetDialog(currentUrl: currentCastIt),
);
_isShowingConnectionModal = false;
} else if (isConnected && _isShowingConnectionModal) {
Expand All @@ -163,7 +157,7 @@ class _MainPageState extends State<MainPage> with SingleTickerProviderStateMixin
void _showServerMsg(BuildContext ctx, AppMessageType msg) {
final theme = Theme.of(ctx);
final color = theme.accentColor.withOpacity(0.8);
final s = I18n.of(context);
final s = S.of(context);

final snackBar = SnackBar(
behavior: SnackBarBehavior.floating,
Expand All @@ -174,7 +168,7 @@ class _MainPageState extends State<MainPage> with SingleTickerProviderStateMixin
Container(
margin: const EdgeInsets.only(left: 10),
child: Text(
s!.translateAppMsgType(msg),
s.translateAppMsgType(msg),
style: const TextStyle(color: Colors.white),
overflow: TextOverflow.ellipsis,
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'package:castit/application/bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../bloc/play/play_bloc.dart';
import '../widgets/play/play_buttons.dart';
import '../widgets/play/play_cover_img.dart';
import '../widgets/play/play_progress_bar.dart';
import '../widgets/play/play_progress_text.dart';
import 'widgets/play_buttons.dart';
import 'widgets/play_cover_img.dart';
import 'widgets/play_progress_bar.dart';
import 'widgets/play_progress_text.dart';

class PlayPage extends StatefulWidget {
@override
Expand Down Expand Up @@ -33,6 +33,7 @@ class _PlayPageState extends State<PlayPage> with AutomaticKeepAliveClientMixin<
}

List<Widget> _buildPage(PlayState state) {
final isPlaying = state.maybeMap(playing: (_) => true, orElse: () => false);
final widgets = [
Flexible(
fit: FlexFit.tight,
Expand All @@ -41,7 +42,7 @@ class _PlayPageState extends State<PlayPage> with AutomaticKeepAliveClientMixin<
),
const Flexible(flex: 8, fit: FlexFit.tight, child: PlayProgressBar()),
const Flexible(flex: 3, fit: FlexFit.tight, child: PlayProgressText()),
Flexible(flex: 19, fit: FlexFit.tight, child: PlayButtons(areDisabled: state is! PlayingState)),
Flexible(flex: 19, fit: FlexFit.tight, child: PlayButtons(areDisabled: !isPlaying)),
];
return widgets;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import 'package:castit/application/bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../bloc/play/play_bloc.dart';
import '../../../bloc/server_ws/server_ws_bloc.dart';

class PlayButtons extends StatelessWidget {
static const double _iconSize = 50;

Expand Down Expand Up @@ -42,12 +40,10 @@ class PlayButtons extends StatelessWidget {
borderRadius: BorderRadius.circular(50.0),
),
child: BlocBuilder<PlayBloc, PlayState>(
builder: (ctx, state) {
if (state is PlayingState) {
return _buildPlayBackButton(context, state.isPaused!);
}
return _buildPlayBackButton(context, false);
},
builder: (ctx, state) => state.maybeMap(
playing: (state) => _buildPlayBackButton(context, state.isPaused!),
orElse: () => _buildPlayBackButton(context, false),
),
),
),
IconButton(
Expand Down
Loading

0 comments on commit 4be80f0

Please sign in to comment.