Skip to content
Snippets Groups Projects
Commit cc57819e authored by Benoît Harrault's avatar Benoît Harrault
Browse files

Normalize Activity application architecture

parent 32455dab
Branches
Tags
1 merge request!43Resolve "Normalize Activity application architecture"
Pipeline #6669 passed
Showing
with 652 additions and 63 deletions
{ {
"app_name": "Jeweled", "app_name": "Jeweled",
"page_home": "Home",
"page_game": "Game",
"settings_title": "Settings", "settings_title": "Settings",
"settings_label_theme": "Theme mode", "settings_label_theme": "Theme mode",
......
{ {
"app_name": "Jeweled", "app_name": "Jeweled",
"page_home": "Accueil",
"page_game": "Jeu",
"settings_title": "Réglages", "settings_title": "Réglages",
"settings_label_theme": "Thème de couleurs", "settings_label_theme": "Thème de couleurs",
......
Normalize Activity application architecture.
Harmonisation des applications en Activity.
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart'; import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:jeweled/ui/screens/page_game.dart'; import 'package:jeweled/common/ui/pages/game.dart';
import 'package:jeweled/ui/screens/page_settings.dart'; import 'package:jeweled/common/ui/pages/parameters.dart';
import 'package:jeweled/ui/screens/page_about.dart';
class MenuItem { class ActivityPageItem {
final String code;
final Icon icon; final Icon icon;
final Widget page; final Widget page;
const MenuItem({ const ActivityPageItem({
required this.code,
required this.icon, required this.icon,
required this.page, required this.page,
}); });
} }
class Menu { class ActivityPage {
static const indexGame = 0; static const bool displayBottomNavBar = false;
static const menuItemGame = MenuItem(
icon: Icon(UniconsLine.home),
page: PageGame(),
);
static const indexSettings = 1; static const indexHome = 0;
static const menuItemSettings = MenuItem( static const pageHome = ActivityPageItem(
icon: Icon(UniconsLine.setting), code: 'page_home',
page: PageSettings(), icon: Icon(UniconsLine.home),
page: PageParameters(),
); );
static const indexAbout = 2; static const indexGame = 1;
static const menuItemAbout = MenuItem( static const pageGame = ActivityPageItem(
icon: Icon(UniconsLine.info_circle), code: 'page_game',
page: PageAbout(), icon: Icon(UniconsLine.star),
page: PageGame(),
); );
static Map<int, MenuItem> items = { static const Map<int, ActivityPageItem> items = {
indexGame: menuItemGame, indexHome: pageHome,
indexSettings: menuItemSettings, indexGame: pageGame,
indexAbout: menuItemAbout,
}; };
static int defaultPageIndex = indexHome;
static bool isIndexAllowed(int pageIndex) { static bool isIndexAllowed(int pageIndex) {
return items.keys.contains(pageIndex); return items.keys.contains(pageIndex);
} }
static Widget getPageWidget(int pageIndex) { static Widget getWidget(int pageIndex) {
return items[pageIndex]?.page ?? menuItemGame.page; return items[pageIndex]?.page ?? pageHome.page;
} }
static int itemsCount = Menu.items.length;
} }
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:jeweled/common/ui/screens/about.dart';
import 'package:jeweled/common/ui/screens/activity.dart';
import 'package:jeweled/common/ui/screens/settings.dart';
class ScreenItem {
final String code;
final Icon icon;
final Widget screen;
const ScreenItem({
required this.code,
required this.icon,
required this.screen,
});
}
class Screen {
static const indexActivity = 0;
static const screenActivity = ScreenItem(
code: 'screen_activity',
icon: Icon(UniconsLine.home),
screen: ScreenActivity(),
);
static const indexSettings = 1;
static const screenSettings = ScreenItem(
code: 'screen_settings',
icon: Icon(UniconsLine.setting),
screen: ScreenSettings(),
);
static const indexAbout = 2;
static const screenAbout = ScreenItem(
code: 'screen_about',
icon: Icon(UniconsLine.info_circle),
screen: ScreenAbout(),
);
static Map<int, ScreenItem> items = {
indexActivity: screenActivity,
indexSettings: screenSettings,
indexAbout: screenAbout,
};
static bool isIndexAllowed(int screenIndex) {
return items.keys.contains(screenIndex);
}
static Widget getWidget(int screenIndex) {
return items[screenIndex]?.screen ?? screenActivity.screen;
}
}
import 'package:flutter_custom_toolbox/flutter_toolbox.dart'; import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:jeweled/config/menu.dart'; import 'package:jeweled/common/config/activity_page.dart';
class NavCubit extends HydratedCubit<int> { class NavCubitPage extends HydratedCubit<int> {
NavCubit() : super(0); NavCubitPage() : super(0);
void updateIndex(int index) { void updateIndex(int index) {
if (Menu.isIndexAllowed(index)) { if (ActivityPage.isIndexAllowed(index)) {
emit(index); emit(index);
} else { } else {
goToGamePage(); emit(ActivityPage.indexHome);
} }
} }
void goToGamePage() { void goToPageHome() {
emit(Menu.indexGame); updateIndex(ActivityPage.indexHome);
} }
void goToSettingsPage() { void goToPageGame() {
emit(Menu.indexSettings); updateIndex(ActivityPage.indexGame);
}
void goToAboutPage() {
emit(Menu.indexAbout);
} }
@override @override
int fromJson(Map<String, dynamic> json) { int fromJson(Map<String, dynamic> json) {
return Menu.indexGame; return ActivityPage.indexHome;
} }
@override @override
Map<String, dynamic>? toJson(int state) { Map<String, dynamic>? toJson(int state) {
return <String, int>{'pageIndex': state}; return <String, int>{'index': state};
} }
} }
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:jeweled/common/config/screen.dart';
class NavCubitScreen extends HydratedCubit<int> {
NavCubitScreen() : super(0);
void updateIndex(int index) {
if (Screen.isIndexAllowed(index)) {
emit(index);
} else {
goToScreenActivity();
}
}
void goToScreenActivity() {
emit(Screen.indexActivity);
}
void goToScreenSettings() {
emit(Screen.indexSettings);
}
void goToScreenAbout() {
emit(Screen.indexAbout);
}
@override
int fromJson(Map<String, dynamic> json) {
return Screen.indexActivity;
}
@override
Map<String, dynamic>? toJson(int state) {
return <String, int>{'index': state};
}
}
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:jeweled/common/config/activity_page.dart';
import 'package:jeweled/common/cubit/nav/nav_cubit_pages.dart';
class BottomNavBar extends StatelessWidget {
const BottomNavBar({super.key});
@override
Widget build(BuildContext context) {
return Card(
margin: const EdgeInsets.only(top: 1, right: 4, left: 4),
elevation: 4,
shadowColor: Theme.of(context).colorScheme.shadow,
color: Theme.of(context).colorScheme.surfaceContainerHighest,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(16),
topRight: Radius.circular(16),
),
),
child: BlocBuilder<NavCubitPage, int>(builder: (BuildContext context, int state) {
final List<BottomNavigationBarItem> items = [];
ActivityPage.items.forEach((int pageIndex, ActivityPageItem item) {
items.add(BottomNavigationBarItem(
icon: item.icon,
label: tr(item.code),
));
});
return BottomNavigationBar(
currentIndex: state,
onTap: (int index) => BlocProvider.of<NavCubitPage>(context).updateIndex(index),
type: BottomNavigationBarType.fixed,
elevation: 0,
backgroundColor: Colors.transparent,
selectedItemColor: Theme.of(context).colorScheme.primary,
unselectedItemColor: Theme.of(context).textTheme.bodySmall!.color,
items: items,
);
}),
);
}
}
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart'; import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:jeweled/config/menu.dart'; import 'package:jeweled/common/config/screen.dart';
import 'package:jeweled/cubit/game_cubit.dart'; import 'package:jeweled/common/cubit/nav/nav_cubit_pages.dart';
import 'package:jeweled/cubit/nav_cubit.dart'; import 'package:jeweled/common/cubit/nav/nav_cubit_screens.dart';
import 'package:jeweled/models/game/game.dart';
import 'package:jeweled/cubit/activity/activity_cubit.dart';
import 'package:jeweled/models/activity/activity.dart';
class GlobalAppBar extends StatelessWidget implements PreferredSizeWidget { class GlobalAppBar extends StatelessWidget implements PreferredSizeWidget {
const GlobalAppBar({super.key}); const GlobalAppBar({super.key});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocBuilder<GameCubit, GameState>( return BlocBuilder<ActivityCubit, ActivityState>(
builder: (BuildContext context, GameState gameState) { builder: (BuildContext context, ActivityState activityState) {
return BlocBuilder<NavCubit, int>( return BlocBuilder<NavCubitScreen, int>(
builder: (BuildContext context, int pageIndex) { builder: (BuildContext context, int pageIndex) {
final Game currentGame = gameState.currentGame; final Activity currentActivity = activityState.currentActivity;
final List<Widget> menuActions = []; final List<Widget> menuActions = [];
if (currentGame.isRunning && !currentGame.isFinished) { if (currentActivity.isRunning && !currentActivity.isFinished) {
menuActions.add(StyledButton( menuActions.add(StyledButton(
color: Colors.red, color: Colors.red,
onPressed: () {}, onPressed: () {},
onLongPress: () { onLongPress: () {
BlocProvider.of<GameCubit>(context).quitGame(); BlocProvider.of<ActivityCubit>(context).quitActivity();
BlocProvider.of<NavCubitPage>(context).goToPageHome();
}, },
child: const Image( child: const Image(
image: AssetImage('assets/ui/button_back.png'), image: AssetImage('assets/ui/button_back.png'),
...@@ -32,44 +35,44 @@ class GlobalAppBar extends StatelessWidget implements PreferredSizeWidget { ...@@ -32,44 +35,44 @@ class GlobalAppBar extends StatelessWidget implements PreferredSizeWidget {
), ),
)); ));
} else { } else {
if (pageIndex == Menu.indexGame) { if (pageIndex == Screen.indexActivity) {
// go to Settings page // go to Settings page
menuActions.add(ElevatedButton( menuActions.add(ElevatedButton(
onPressed: () { onPressed: () {
BlocProvider.of<NavCubit>(context).goToSettingsPage(); BlocProvider.of<NavCubitScreen>(context).goToScreenSettings();
}, },
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
shape: const CircleBorder(), shape: const CircleBorder(),
), ),
child: Menu.menuItemSettings.icon, child: Screen.screenSettings.icon,
)); ));
// go to About page // go to About page
menuActions.add(ElevatedButton( menuActions.add(ElevatedButton(
onPressed: () { onPressed: () {
BlocProvider.of<NavCubit>(context).goToAboutPage(); BlocProvider.of<NavCubitScreen>(context).goToScreenAbout();
}, },
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
shape: const CircleBorder(), shape: const CircleBorder(),
), ),
child: Menu.menuItemAbout.icon, child: Screen.screenAbout.icon,
)); ));
} else { } else {
// back to Home page // back to Home page
menuActions.add(ElevatedButton( menuActions.add(ElevatedButton(
onPressed: () { onPressed: () {
BlocProvider.of<NavCubit>(context).goToGamePage(); BlocProvider.of<NavCubitScreen>(context).goToScreenActivity();
}, },
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
shape: const CircleBorder(), shape: const CircleBorder(),
), ),
child: Menu.menuItemGame.icon, child: Screen.screenActivity.icon,
)); ));
} }
} }
return AppBar( return AppBar(
title: const AppTitle(text: 'app_name'), title: const AppHeader(text: 'app_name'),
actions: menuActions, actions: menuActions,
); );
}, },
......
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart'; import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:jeweled/cubit/game_cubit.dart'; import 'package:jeweled/cubit/activity/activity_cubit.dart';
import 'package:jeweled/models/game/game.dart'; import 'package:jeweled/models/activity/activity.dart';
import 'package:jeweled/ui/game/game_end.dart'; import 'package:jeweled/ui/game/game_end.dart';
import 'package:jeweled/ui/game/game_top.dart'; import 'package:jeweled/ui/game/game_top.dart';
import 'package:jeweled/ui/widgets/game/board.dart'; import 'package:jeweled/ui/widgets/game/board.dart';
class GameLayout extends StatelessWidget { class PageGame extends StatelessWidget {
const GameLayout({super.key}); const PageGame({super.key});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocBuilder<GameCubit, GameState>( return BlocBuilder<ActivityCubit, ActivityState>(
builder: (BuildContext context, GameState gameState) { builder: (BuildContext context, ActivityState activityState) {
final Game currentGame = gameState.currentGame; final Activity currentActivity = activityState.currentActivity;
return Container( return Container(
alignment: AlignmentDirectional.topCenter, alignment: AlignmentDirectional.topCenter,
...@@ -28,7 +28,7 @@ class GameLayout extends StatelessWidget { ...@@ -28,7 +28,7 @@ class GameLayout extends StatelessWidget {
const BoardWidget(), const BoardWidget(),
const SizedBox(height: 8), const SizedBox(height: 8),
const Expanded(child: SizedBox.shrink()), const Expanded(child: SizedBox.shrink()),
currentGame.isFinished ? const GameEndWidget() : const SizedBox.shrink(), currentActivity.isFinished ? const GameEndWidget() : const SizedBox.shrink(),
], ],
), ),
); );
......
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart'; import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:jeweled/config/default_game_settings.dart'; import 'package:jeweled/common/ui/parameters/parameter_widget.dart';
import 'package:jeweled/config/default_activity_settings.dart';
import 'package:jeweled/config/default_global_settings.dart'; import 'package:jeweled/config/default_global_settings.dart';
import 'package:jeweled/cubit/settings_game_cubit.dart'; import 'package:jeweled/cubit/activity/activity_cubit.dart';
import 'package:jeweled/cubit/settings_global_cubit.dart'; import 'package:jeweled/cubit/settings/settings_activity_cubit.dart';
import 'package:jeweled/ui/parameters/parameter_widget.dart'; import 'package:jeweled/cubit/settings/settings_global_cubit.dart';
import 'package:jeweled/models/activity/activity.dart';
import 'package:jeweled/ui/widgets/actions/button_delete_saved_game.dart'; import 'package:jeweled/ui/widgets/actions/button_delete_saved_game.dart';
import 'package:jeweled/ui/widgets/actions/button_game_start_new.dart'; import 'package:jeweled/ui/widgets/actions/button_game_start_new.dart';
import 'package:jeweled/ui/widgets/actions/button_resume_saved_game.dart'; import 'package:jeweled/ui/widgets/actions/button_resume_saved_game.dart';
class ParametersLayout extends StatelessWidget { class PageParameters extends StatelessWidget {
const ParametersLayout({super.key, required this.canResume}); const PageParameters({super.key});
final bool canResume;
final double separatorHeight = 8.0; final double separatorHeight = 8.0;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocBuilder<ActivityCubit, ActivityState>(
builder: (BuildContext context, ActivityState activityState) {
final Activity currentActivity = activityState.currentActivity;
final List<Widget> lines = []; final List<Widget> lines = [];
// Game settings // Game settings
for (String code in DefaultGameSettings.availableParameters) { for (String code in DefaultActivitySettings.availableParameters) {
lines.add(Row( lines.add(Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween, mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: buildParametersLine( children: buildParametersLine(
...@@ -38,7 +43,7 @@ class ParametersLayout extends StatelessWidget { ...@@ -38,7 +43,7 @@ class ParametersLayout extends StatelessWidget {
child: SizedBox(height: separatorHeight), child: SizedBox(height: separatorHeight),
)); ));
if (canResume == false) { if (currentActivity.canBeResumed == false) {
// Start new game // Start new game
lines.add( lines.add(
const AspectRatio( const AspectRatio(
...@@ -77,6 +82,8 @@ class ParametersLayout extends StatelessWidget { ...@@ -77,6 +82,8 @@ class ParametersLayout extends StatelessWidget {
return Column( return Column(
children: lines, children: lines,
); );
},
);
} }
List<Widget> buildParametersLine({ List<Widget> buildParametersLine({
...@@ -87,25 +94,25 @@ class ParametersLayout extends StatelessWidget { ...@@ -87,25 +94,25 @@ class ParametersLayout extends StatelessWidget {
final List<String> availableValues = isGlobal final List<String> availableValues = isGlobal
? DefaultGlobalSettings.getAvailableValues(code) ? DefaultGlobalSettings.getAvailableValues(code)
: DefaultGameSettings.getAvailableValues(code); : DefaultActivitySettings.getAvailableValues(code);
if (availableValues.length <= 1) { if (availableValues.length <= 1) {
return []; return [];
} }
for (String value in availableValues) { for (String value in availableValues) {
final Widget parameterButton = BlocBuilder<GameSettingsCubit, GameSettingsState>( final Widget parameterButton = BlocBuilder<ActivitySettingsCubit, ActivitySettingsState>(
builder: (BuildContext context, GameSettingsState gameSettingsState) { builder: (BuildContext context, ActivitySettingsState activitySettingsState) {
return BlocBuilder<GlobalSettingsCubit, GlobalSettingsState>( return BlocBuilder<GlobalSettingsCubit, GlobalSettingsState>(
builder: (BuildContext context, GlobalSettingsState globalSettingsState) { builder: (BuildContext context, GlobalSettingsState globalSettingsState) {
final GameSettingsCubit gameSettingsCubit = final ActivitySettingsCubit activitySettingsCubit =
BlocProvider.of<GameSettingsCubit>(context); BlocProvider.of<ActivitySettingsCubit>(context);
final GlobalSettingsCubit globalSettingsCubit = final GlobalSettingsCubit globalSettingsCubit =
BlocProvider.of<GlobalSettingsCubit>(context); BlocProvider.of<GlobalSettingsCubit>(context);
final String currentValue = isGlobal final String currentValue = isGlobal
? globalSettingsCubit.getParameterValue(code) ? globalSettingsCubit.getParameterValue(code)
: gameSettingsCubit.getParameterValue(code); : activitySettingsCubit.getParameterValue(code);
final bool isSelected = (value == currentValue); final bool isSelected = (value == currentValue);
...@@ -119,12 +126,12 @@ class ParametersLayout extends StatelessWidget { ...@@ -119,12 +126,12 @@ class ParametersLayout extends StatelessWidget {
value: value, value: value,
isSelected: isSelected, isSelected: isSelected,
size: itemWidth, size: itemWidth,
gameSettings: gameSettingsState.settings, activitySettings: activitySettingsState.settings,
globalSettings: globalSettingsState.settings, globalSettings: globalSettingsState.settings,
onPressed: () { onPressed: () {
isGlobal isGlobal
? globalSettingsCubit.setParameterValue(code, value) ? globalSettingsCubit.setParameterValue(code, value)
: gameSettingsCubit.setParameterValue(code, value); : activitySettingsCubit.setParameterValue(code, value);
}, },
), ),
); );
......
...@@ -5,22 +5,22 @@ import 'package:flutter/material.dart'; ...@@ -5,22 +5,22 @@ import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart'; import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:jeweled/config/color_theme.dart'; import 'package:jeweled/config/color_theme.dart';
import 'package:jeweled/config/default_game_settings.dart'; import 'package:jeweled/config/default_activity_settings.dart';
import 'package:jeweled/config/default_global_settings.dart'; import 'package:jeweled/config/default_global_settings.dart';
import 'package:jeweled/models/settings/settings_game.dart'; import 'package:jeweled/models/settings/settings_activity.dart';
import 'package:jeweled/models/settings/settings_global.dart'; import 'package:jeweled/models/settings/settings_global.dart';
class ParameterPainter extends CustomPainter { class ParameterPainter extends CustomPainter {
const ParameterPainter({ const ParameterPainter({
required this.code, required this.code,
required this.value, required this.value,
required this.gameSettings, required this.activitySettings,
required this.globalSettings, required this.globalSettings,
}); });
final String code; final String code;
final String value; final String value;
final GameSettings gameSettings; final ActivitySettings activitySettings;
final GlobalSettings globalSettings; final GlobalSettings globalSettings;
@override @override
...@@ -30,10 +30,10 @@ class ParameterPainter extends CustomPainter { ...@@ -30,10 +30,10 @@ class ParameterPainter extends CustomPainter {
// content // content
switch (code) { switch (code) {
case DefaultGameSettings.parameterCodeColorsCount: case DefaultActivitySettings.parameterCodeColorsCount:
paintColorsCountParameterItem(canvas, canvasSize); paintColorsCountParameterItem(canvas, canvasSize);
break; break;
case DefaultGameSettings.parameterCodeBoardSize: case DefaultActivitySettings.parameterCodeBoardSize:
paintBoardSizeParameterItem(canvas, canvasSize); paintBoardSizeParameterItem(canvas, canvasSize);
break; break;
case DefaultGlobalSettings.parameterCodeColorsTheme: case DefaultGlobalSettings.parameterCodeColorsTheme:
...@@ -92,16 +92,16 @@ class ParameterPainter extends CustomPainter { ...@@ -92,16 +92,16 @@ class ParameterPainter extends CustomPainter {
int gridWidth = 1; int gridWidth = 1;
switch (value) { switch (value) {
case DefaultGameSettings.boardSizeValueSmall: case DefaultActivitySettings.boardSizeValueSmall:
gridWidth = 2; gridWidth = 2;
break; break;
case DefaultGameSettings.boardSizeValueMedium: case DefaultActivitySettings.boardSizeValueMedium:
gridWidth = 3; gridWidth = 3;
break; break;
case DefaultGameSettings.boardSizeValueLarge: case DefaultActivitySettings.boardSizeValueLarge:
gridWidth = 4; gridWidth = 4;
break; break;
case DefaultGameSettings.boardSizeValueExtraLarge: case DefaultActivitySettings.boardSizeValueExtraLarge:
gridWidth = 5; gridWidth = 5;
break; break;
default: default:
......
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart'; import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:jeweled/config/default_game_settings.dart'; import 'package:jeweled/common/ui/parameters/parameter_painter.dart';
import 'package:jeweled/config/default_activity_settings.dart';
import 'package:jeweled/config/default_global_settings.dart'; import 'package:jeweled/config/default_global_settings.dart';
import 'package:jeweled/models/settings/settings_game.dart'; import 'package:jeweled/models/settings/settings_activity.dart';
import 'package:jeweled/models/settings/settings_global.dart'; import 'package:jeweled/models/settings/settings_global.dart';
import 'package:jeweled/ui/parameters/parameter_painter.dart';
class ParameterWidget extends StatelessWidget { class ParameterWidget extends StatelessWidget {
const ParameterWidget({ const ParameterWidget({
...@@ -15,7 +15,7 @@ class ParameterWidget extends StatelessWidget { ...@@ -15,7 +15,7 @@ class ParameterWidget extends StatelessWidget {
required this.value, required this.value,
required this.isSelected, required this.isSelected,
required this.size, required this.size,
required this.gameSettings, required this.activitySettings,
required this.globalSettings, required this.globalSettings,
required this.onPressed, required this.onPressed,
}); });
...@@ -24,7 +24,7 @@ class ParameterWidget extends StatelessWidget { ...@@ -24,7 +24,7 @@ class ParameterWidget extends StatelessWidget {
final String value; final String value;
final bool isSelected; final bool isSelected;
final double size; final double size;
final GameSettings gameSettings; final ActivitySettings activitySettings;
final GlobalSettings globalSettings; final GlobalSettings globalSettings;
final VoidCallback onPressed; final VoidCallback onPressed;
...@@ -38,10 +38,10 @@ class ParameterWidget extends StatelessWidget { ...@@ -38,10 +38,10 @@ class ParameterWidget extends StatelessWidget {
Widget content = const SizedBox.shrink(); Widget content = const SizedBox.shrink();
switch (code) { switch (code) {
case DefaultGameSettings.parameterCodeColorsCount: case DefaultActivitySettings.parameterCodeColorsCount:
content = getColorsCountParameterItem(); content = getColorsCountParameterItem();
break; break;
case DefaultGameSettings.parameterCodeBoardSize: case DefaultActivitySettings.parameterCodeBoardSize:
content = getBoardSizeParameterItem(); content = getBoardSizeParameterItem();
break; break;
case DefaultGlobalSettings.parameterCodeColorsTheme: case DefaultGlobalSettings.parameterCodeColorsTheme:
...@@ -83,16 +83,16 @@ class ParameterWidget extends StatelessWidget { ...@@ -83,16 +83,16 @@ class ParameterWidget extends StatelessWidget {
Color backgroundColor = Colors.grey; Color backgroundColor = Colors.grey;
switch (value) { switch (value) {
case DefaultGameSettings.colorsCountValueLow: case DefaultActivitySettings.colorsCountValueLow:
backgroundColor = Colors.green; backgroundColor = Colors.green;
break; break;
case DefaultGameSettings.colorsCountValueMedium: case DefaultActivitySettings.colorsCountValueMedium:
backgroundColor = Colors.orange; backgroundColor = Colors.orange;
break; break;
case DefaultGameSettings.colorsCountValueHigh: case DefaultActivitySettings.colorsCountValueHigh:
backgroundColor = Colors.red; backgroundColor = Colors.red;
break; break;
case DefaultGameSettings.colorsCountValueVeryHigh: case DefaultActivitySettings.colorsCountValueVeryHigh:
backgroundColor = Colors.purple; backgroundColor = Colors.purple;
break; break;
default: default:
...@@ -108,7 +108,7 @@ class ParameterWidget extends StatelessWidget { ...@@ -108,7 +108,7 @@ class ParameterWidget extends StatelessWidget {
painter: ParameterPainter( painter: ParameterPainter(
code: code, code: code,
value: value, value: value,
gameSettings: gameSettings, activitySettings: activitySettings,
globalSettings: globalSettings, globalSettings: globalSettings,
), ),
isComplex: true, isComplex: true,
...@@ -120,16 +120,16 @@ class ParameterWidget extends StatelessWidget { ...@@ -120,16 +120,16 @@ class ParameterWidget extends StatelessWidget {
Color backgroundColor = Colors.grey; Color backgroundColor = Colors.grey;
switch (value) { switch (value) {
case DefaultGameSettings.boardSizeValueSmall: case DefaultActivitySettings.boardSizeValueSmall:
backgroundColor = Colors.green; backgroundColor = Colors.green;
break; break;
case DefaultGameSettings.boardSizeValueMedium: case DefaultActivitySettings.boardSizeValueMedium:
backgroundColor = Colors.orange; backgroundColor = Colors.orange;
break; break;
case DefaultGameSettings.boardSizeValueLarge: case DefaultActivitySettings.boardSizeValueLarge:
backgroundColor = Colors.red; backgroundColor = Colors.red;
break; break;
case DefaultGameSettings.boardSizeValueExtraLarge: case DefaultActivitySettings.boardSizeValueExtraLarge:
backgroundColor = Colors.purple; backgroundColor = Colors.purple;
break; break;
default: default:
...@@ -145,7 +145,7 @@ class ParameterWidget extends StatelessWidget { ...@@ -145,7 +145,7 @@ class ParameterWidget extends StatelessWidget {
painter: ParameterPainter( painter: ParameterPainter(
code: code, code: code,
value: value, value: value,
gameSettings: gameSettings, activitySettings: activitySettings,
globalSettings: globalSettings, globalSettings: globalSettings,
), ),
isComplex: true, isComplex: true,
...@@ -165,7 +165,7 @@ class ParameterWidget extends StatelessWidget { ...@@ -165,7 +165,7 @@ class ParameterWidget extends StatelessWidget {
painter: ParameterPainter( painter: ParameterPainter(
code: code, code: code,
value: value, value: value,
gameSettings: gameSettings, activitySettings: activitySettings,
globalSettings: globalSettings, globalSettings: globalSettings,
), ),
isComplex: true, isComplex: true,
...@@ -185,7 +185,7 @@ class ParameterWidget extends StatelessWidget { ...@@ -185,7 +185,7 @@ class ParameterWidget extends StatelessWidget {
painter: ParameterPainter( painter: ParameterPainter(
code: code, code: code,
value: value, value: value,
gameSettings: gameSettings, activitySettings: activitySettings,
globalSettings: globalSettings, globalSettings: globalSettings,
), ),
isComplex: true, isComplex: true,
......
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart'; import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
class PageAbout extends StatelessWidget { class ScreenAbout extends StatelessWidget {
const PageAbout({super.key}); const ScreenAbout({super.key});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
......
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:jeweled/common/config/activity_page.dart';
import 'package:jeweled/common/cubit/nav/nav_cubit_pages.dart';
class ScreenActivity extends StatelessWidget {
const ScreenActivity({super.key});
@override
Widget build(BuildContext context) {
return BlocBuilder<NavCubitPage, int>(
builder: (BuildContext context, int pageIndex) {
return ActivityPage.getWidget(pageIndex);
},
);
}
}
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart'; import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
class PageSettings extends StatelessWidget { class ScreenSettings extends StatelessWidget {
const PageSettings({super.key}); const ScreenSettings({super.key});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
......
class ApplicationConfig {
static const String appTitle = 'Jeweled';
}
import 'package:flutter_custom_toolbox/flutter_toolbox.dart'; import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
class DefaultGameSettings { class DefaultActivitySettings {
// available game parameters codes // available game parameters codes
static const String parameterCodeBoardSize = 'boardSize'; static const String parameterCodeBoardSize = 'boardSize';
static const String parameterCodeColorsCount = 'colorsCount'; static const String parameterCodeColorsCount = 'colorsCount';
...@@ -41,9 +41,9 @@ class DefaultGameSettings { ...@@ -41,9 +41,9 @@ class DefaultGameSettings {
static List<String> getAvailableValues(String parameterCode) { static List<String> getAvailableValues(String parameterCode) {
switch (parameterCode) { switch (parameterCode) {
case parameterCodeBoardSize: case parameterCodeBoardSize:
return DefaultGameSettings.allowedBoardSizeValues; return DefaultActivitySettings.allowedBoardSizeValues;
case parameterCodeColorsCount: case parameterCodeColorsCount:
return DefaultGameSettings.allowedColorsCountValues; return DefaultActivitySettings.allowedColorsCountValues;
} }
printlog('Did not find any available value for game parameter "$parameterCode".'); printlog('Did not find any available value for game parameter "$parameterCode".');
......
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart'; import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:jeweled/config/default_game_settings.dart'; import 'package:jeweled/config/default_activity_settings.dart';
import 'package:jeweled/models/game/cell_location.dart'; import 'package:jeweled/models/activity/cell_location.dart';
import 'package:jeweled/models/game/game.dart'; import 'package:jeweled/models/activity/activity.dart';
import 'package:jeweled/models/settings/settings_game.dart'; import 'package:jeweled/models/settings/settings_activity.dart';
import 'package:jeweled/models/settings/settings_global.dart'; import 'package:jeweled/models/settings/settings_global.dart';
part 'game_state.dart'; part 'activity_state.dart';
class GameCubit extends HydratedCubit<GameState> { class ActivityCubit extends HydratedCubit<ActivityState> {
GameCubit() ActivityCubit()
: super(GameState( : super(ActivityState(
currentGame: Game.createEmpty(), currentActivity: Activity.createEmpty(),
)); ));
void updateState(Game game) { void updateState(Activity activity) {
emit(GameState( emit(ActivityState(
currentGame: game, currentActivity: activity,
)); ));
} }
void refresh() { void refresh() {
final Game game = Game( final Activity activity = Activity(
// Settings // Settings
gameSettings: state.currentGame.gameSettings, activitySettings: state.currentActivity.activitySettings,
globalSettings: state.currentGame.globalSettings, globalSettings: state.currentActivity.globalSettings,
// State // State
isRunning: state.currentGame.isRunning, isRunning: state.currentActivity.isRunning,
isStarted: state.currentGame.isStarted, isStarted: state.currentActivity.isStarted,
isFinished: state.currentGame.isFinished, isFinished: state.currentActivity.isFinished,
animationInProgress: state.currentGame.animationInProgress, animationInProgress: state.currentActivity.animationInProgress,
// Base data // Base data
board: state.currentGame.board, board: state.currentActivity.board,
// Game data // Game data
shuffledColors: state.currentGame.shuffledColors, shuffledColors: state.currentActivity.shuffledColors,
availableBlocksCount: state.currentGame.availableBlocksCount, availableBlocksCount: state.currentActivity.availableBlocksCount,
score: state.currentGame.score, score: state.currentActivity.score,
movesCount: state.currentGame.movesCount, movesCount: state.currentActivity.movesCount,
); );
// game.dump(); // game.dump();
updateState(game); updateState(activity);
} }
void startNewGame({ void startNewActivity({
required GameSettings gameSettings, required ActivitySettings activitySettings,
required GlobalSettings globalSettings, required GlobalSettings globalSettings,
}) { }) {
final Game newGame = Game.createNew( final Activity newActivity = Activity.createNew(
gameSettings: gameSettings, activitySettings: activitySettings,
globalSettings: globalSettings, globalSettings: globalSettings,
); );
newGame.dump(); newActivity.dump();
updateState(newGame); updateState(newActivity);
postAnimate(); postAnimate();
} }
void quitGame() { void quitActivity() {
state.currentGame.isRunning = false; state.currentActivity.isRunning = false;
refresh(); refresh();
} }
void resumeSavedGame() { void resumeSavedActivity() {
state.currentGame.isRunning = true; state.currentActivity.isRunning = true;
refresh(); refresh();
} }
void deleteSavedGame() { void deleteSavedActivity() {
state.currentGame.isRunning = false; state.currentActivity.isRunning = false;
state.currentGame.isFinished = true; state.currentActivity.isFinished = true;
refresh(); refresh();
} }
void updateCellValue(CellLocation locationToUpdate, int? value) { void updateCellValue(CellLocation locationToUpdate, int? value) {
state.currentGame.updateCellValue(locationToUpdate, value); state.currentActivity.updateCellValue(locationToUpdate, value);
refresh(); refresh();
} }
void increaseMovesCount() { void increaseMovesCount() {
state.currentGame.isStarted = true; state.currentActivity.isStarted = true;
state.currentGame.increaseMovesCount(); state.currentActivity.increaseMovesCount();
refresh(); refresh();
} }
void increaseScore(int increment) { void increaseScore(int increment) {
state.currentGame.increaseScore(increment); state.currentActivity.increaseScore(increment);
refresh(); refresh();
} }
void updateAvailableBlocksCount() { void updateAvailableBlocksCount() {
state.currentGame.updateAvailableBlocksCount(); state.currentActivity.updateAvailableBlocksCount();
refresh(); refresh();
} }
void updateGameIsFinished(bool gameIsFinished) { void updateGameIsFinished(bool gameIsFinished) {
state.currentGame.isFinished = gameIsFinished; state.currentActivity.isFinished = gameIsFinished;
refresh(); refresh();
} }
void shuffleColors(final String colorsTheme) { void shuffleColors(final String colorsTheme) {
state.currentGame.shuffleColorsAgain(colorsTheme); state.currentActivity.shuffleColorsAgain(colorsTheme);
} }
moveCellsDown() { moveCellsDown() {
final Game currentGame = state.currentGame; final Activity currentActivity = state.currentActivity;
final int boardSizeHorizontal = currentGame.gameSettings.boardSizeValue; final int boardSizeHorizontal = currentActivity.activitySettings.boardSizeValue;
final int boardSizeVertical = currentGame.gameSettings.boardSizeValue; final int boardSizeVertical = currentActivity.activitySettings.boardSizeValue;
for (int row = 0; row < boardSizeVertical; row++) { for (int row = 0; row < boardSizeVertical; row++) {
for (int col = 0; col < boardSizeHorizontal; col++) { for (int col = 0; col < boardSizeHorizontal; col++) {
// empty cell? // empty cell?
if (currentGame.getCellValue(CellLocation.go(row, col)) == null) { if (currentActivity.getCellValue(CellLocation.go(row, col)) == null) {
// move cells down // move cells down
for (int r = row; r > 0; r--) { for (int r = row; r > 0; r--) {
updateCellValue(CellLocation.go(r, col), updateCellValue(CellLocation.go(r, col),
currentGame.getCellValue(CellLocation.go(r - 1, col))); currentActivity.getCellValue(CellLocation.go(r - 1, col)));
} }
// fill top empty cell // fill top empty cell
updateCellValue( updateCellValue(CellLocation.go(0, col),
CellLocation.go(0, col), currentGame.getFillValue(CellLocation.go(row, col))); currentActivity.getFillValue(CellLocation.go(row, col)));
} }
} }
} }
...@@ -142,13 +142,13 @@ class GameCubit extends HydratedCubit<GameState> { ...@@ -142,13 +142,13 @@ class GameCubit extends HydratedCubit<GameState> {
} }
List<CellLocation> tapOnCell(CellLocation tappedCellLocation) { List<CellLocation> tapOnCell(CellLocation tappedCellLocation) {
final Game currentGame = state.currentGame; final Activity currentActivity = state.currentActivity;
final int? cellValue = currentGame.getCellValue(tappedCellLocation); final int? cellValue = currentActivity.getCellValue(tappedCellLocation);
if (cellValue != null) { if (cellValue != null) {
List<CellLocation> blockCells = currentGame.getSiblingCells(tappedCellLocation, []); List<CellLocation> blockCells = currentActivity.getSiblingCells(tappedCellLocation, []);
if (blockCells.length >= DefaultGameSettings.blockMinimumCellsCount) { if (blockCells.length >= DefaultActivitySettings.blockMinimumCellsCount) {
deleteBlock(blockCells); deleteBlock(blockCells);
increaseMovesCount(); increaseMovesCount();
increaseScore(getScoreFromBlock(blockCells.length)); increaseScore(getScoreFromBlock(blockCells.length));
...@@ -164,25 +164,25 @@ class GameCubit extends HydratedCubit<GameState> { ...@@ -164,25 +164,25 @@ class GameCubit extends HydratedCubit<GameState> {
updateAvailableBlocksCount(); updateAvailableBlocksCount();
refresh(); refresh();
if (!state.currentGame.hasAtLeastOneAvailableBlock()) { if (!state.currentActivity.hasAtLeastOneAvailableBlock()) {
printlog('no more block found. finish game.'); printlog('no more block found. finish game.');
updateGameIsFinished(true); updateGameIsFinished(true);
} }
} }
@override @override
GameState? fromJson(Map<String, dynamic> json) { ActivityState? fromJson(Map<String, dynamic> json) {
final Game currentGame = json['currentGame'] as Game; final Activity currentActivity = json['currentActivity'] as Activity;
return GameState( return ActivityState(
currentGame: currentGame, currentActivity: currentActivity,
); );
} }
@override @override
Map<String, dynamic>? toJson(GameState state) { Map<String, dynamic>? toJson(ActivityState state) {
return <String, dynamic>{ return <String, dynamic>{
'currentGame': state.currentGame.toJson(), 'currentActivity': state.currentActivity.toJson(),
}; };
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment