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

Normalize Activity application architecture

parent dcc694e5
Branches
Tags
1 merge request!84Resolve "Normalize Activity application architecture"
Pipeline #6683 passed
Showing
with 558 additions and 44 deletions
{
"app_name": "Word guess",
"page_home": "Home",
"page_game": "Game",
"settings_title": "Settings",
"settings_label_theme": "Theme mode",
......
{
"app_name": "Trouve le mot",
"page_home": "Accueil",
"page_game": "Jeu",
"settings_title": "Réglages",
"settings_label_theme": "Thème de couleurs",
......
Normalize Activity application architecture.
Harmonisation des applications en Activity.
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:wordguessing/ui/screens/page_about.dart';
import 'package:wordguessing/ui/screens/page_game.dart';
import 'package:wordguessing/ui/screens/page_settings.dart';
import 'package:wordguessing/common/ui/pages/game.dart';
import 'package:wordguessing/common/ui/pages/parameters.dart';
class MenuItem {
class ActivityPageItem {
final String code;
final Icon icon;
final Widget page;
const MenuItem({
const ActivityPageItem({
required this.code,
required this.icon,
required this.page,
});
}
class Menu {
static const indexGame = 0;
static const menuItemGame = MenuItem(
icon: Icon(UniconsLine.home),
page: PageGame(),
);
class ActivityPage {
static const bool displayBottomNavBar = false;
static const indexSettings = 1;
static const menuItemSettings = MenuItem(
icon: Icon(UniconsLine.setting),
page: PageSettings(),
static const indexHome = 0;
static const pageHome = ActivityPageItem(
code: 'page_home',
icon: Icon(UniconsLine.home),
page: PageParameters(),
);
static const indexAbout = 2;
static const menuItemAbout = MenuItem(
icon: Icon(UniconsLine.info_circle),
page: PageAbout(),
static const indexGame = 1;
static const pageGame = ActivityPageItem(
code: 'page_game',
icon: Icon(UniconsLine.star),
page: PageGame(),
);
static Map<int, MenuItem> items = {
indexGame: menuItemGame,
indexSettings: menuItemSettings,
indexAbout: menuItemAbout,
static const Map<int, ActivityPageItem> items = {
indexHome: pageHome,
indexGame: pageGame,
};
static int defaultPageIndex = indexHome;
static bool isIndexAllowed(int pageIndex) {
return items.keys.contains(pageIndex);
}
static Widget getPageWidget(int pageIndex) {
return items[pageIndex]?.page ?? menuItemGame.page;
static Widget getWidget(int pageIndex) {
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:wordguessing/common/ui/screens/about.dart';
import 'package:wordguessing/common/ui/screens/activity.dart';
import 'package:wordguessing/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:wordguessing/config/menu.dart';
import 'package:wordguessing/common/config/activity_page.dart';
class NavCubit extends HydratedCubit<int> {
NavCubit() : super(0);
class NavCubitPage extends HydratedCubit<int> {
NavCubitPage() : super(0);
void updateIndex(int index) {
if (Menu.isIndexAllowed(index)) {
if (ActivityPage.isIndexAllowed(index)) {
emit(index);
} else {
goToGamePage();
emit(ActivityPage.indexHome);
}
}
void goToGamePage() {
emit(Menu.indexGame);
void goToPageHome() {
updateIndex(ActivityPage.indexHome);
}
void goToSettingsPage() {
emit(Menu.indexSettings);
}
void goToAboutPage() {
emit(Menu.indexAbout);
void goToPageGame() {
updateIndex(ActivityPage.indexGame);
}
@override
int fromJson(Map<String, dynamic> json) {
return Menu.indexGame;
return ActivityPage.indexHome;
}
@override
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:wordguessing/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:wordguessing/common/config/activity_page.dart';
import 'package:wordguessing/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_custom_toolbox/flutter_toolbox.dart';
import 'package:wordguessing/config/menu.dart';
import 'package:wordguessing/cubit/game_cubit.dart';
import 'package:wordguessing/cubit/nav_cubit.dart';
import 'package:wordguessing/models/game/game.dart';
import 'package:wordguessing/common/config/screen.dart';
import 'package:wordguessing/common/cubit/nav/nav_cubit_pages.dart';
import 'package:wordguessing/common/cubit/nav/nav_cubit_screens.dart';
import 'package:wordguessing/cubit/activity/activity_cubit.dart';
import 'package:wordguessing/models/activity/activity.dart';
class GlobalAppBar extends StatelessWidget implements PreferredSizeWidget {
const GlobalAppBar({super.key});
@override
Widget build(BuildContext context) {
return BlocBuilder<GameCubit, GameState>(
builder: (BuildContext context, GameState gameState) {
return BlocBuilder<NavCubit, int>(
return BlocBuilder<ActivityCubit, ActivityState>(
builder: (BuildContext context, ActivityState activityState) {
return BlocBuilder<NavCubitScreen, int>(
builder: (BuildContext context, int pageIndex) {
final Game currentGame = gameState.currentGame;
final Activity currentActivity = activityState.currentActivity;
final List<Widget> menuActions = [];
if (currentGame.isRunning && !currentGame.isFinished) {
if (currentActivity.isRunning && !currentActivity.isFinished) {
menuActions.add(StyledButton(
color: Colors.red,
onPressed: () {},
onLongPress: () {
BlocProvider.of<GameCubit>(context).quitGame();
BlocProvider.of<ActivityCubit>(context).quitActivity();
BlocProvider.of<NavCubitPage>(context).goToPageHome();
},
child: const Image(
image: AssetImage('assets/ui/button_back.png'),
......@@ -32,38 +35,38 @@ class GlobalAppBar extends StatelessWidget implements PreferredSizeWidget {
),
));
} else {
if (pageIndex == Menu.indexGame) {
if (pageIndex == Screen.indexActivity) {
// go to Settings page
menuActions.add(ElevatedButton(
onPressed: () {
BlocProvider.of<NavCubit>(context).goToSettingsPage();
BlocProvider.of<NavCubitScreen>(context).goToScreenSettings();
},
style: ElevatedButton.styleFrom(
shape: const CircleBorder(),
),
child: Menu.menuItemSettings.icon,
child: Screen.screenSettings.icon,
));
// go to About page
menuActions.add(ElevatedButton(
onPressed: () {
BlocProvider.of<NavCubit>(context).goToAboutPage();
BlocProvider.of<NavCubitScreen>(context).goToScreenAbout();
},
style: ElevatedButton.styleFrom(
shape: const CircleBorder(),
),
child: Menu.menuItemAbout.icon,
child: Screen.screenAbout.icon,
));
} else {
// back to Home page
menuActions.add(ElevatedButton(
onPressed: () {
BlocProvider.of<NavCubit>(context).goToGamePage();
BlocProvider.of<NavCubitScreen>(context).goToScreenActivity();
},
style: ElevatedButton.styleFrom(
shape: const CircleBorder(),
),
child: Menu.menuItemGame.icon,
child: Screen.screenActivity.icon,
));
}
}
......
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:wordguessing/cubit/game_cubit.dart';
import 'package:wordguessing/models/game/game.dart';
import 'package:wordguessing/cubit/activity/activity_cubit.dart';
import 'package:wordguessing/models/activity/activity.dart';
import 'package:wordguessing/ui/game/game_end.dart';
import 'package:wordguessing/ui/game/game_top.dart';
import 'package:wordguessing/ui/widgets/game/game_board.dart';
class GameLayout extends StatelessWidget {
const GameLayout({super.key});
class PageGame extends StatelessWidget {
const PageGame({super.key});
@override
Widget build(BuildContext context) {
return BlocBuilder<GameCubit, GameState>(
builder: (BuildContext context, GameState gameState) {
final Game currentGame = gameState.currentGame;
return BlocBuilder<ActivityCubit, ActivityState>(
builder: (BuildContext context, ActivityState activityState) {
final Activity currentActivity = activityState.currentActivity;
return Container(
alignment: AlignmentDirectional.topCenter,
......@@ -27,7 +27,7 @@ class GameLayout extends StatelessWidget {
const SizedBox(height: 8),
const GameBoardWidget(),
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_custom_toolbox/flutter_toolbox.dart';
import 'package:wordguessing/config/default_game_settings.dart';
import 'package:wordguessing/common/ui/parameters/parameter_widget.dart';
import 'package:wordguessing/config/default_activity_settings.dart';
import 'package:wordguessing/config/default_global_settings.dart';
import 'package:wordguessing/cubit/settings_game_cubit.dart';
import 'package:wordguessing/cubit/settings_global_cubit.dart';
import 'package:wordguessing/ui/parameters/parameter_widget.dart';
import 'package:wordguessing/cubit/activity/activity_cubit.dart';
import 'package:wordguessing/cubit/settings/settings_activity_cubit.dart';
import 'package:wordguessing/cubit/settings/settings_global_cubit.dart';
import 'package:wordguessing/models/activity/activity.dart';
import 'package:wordguessing/ui/widgets/actions/button_delete_saved_game.dart';
import 'package:wordguessing/ui/widgets/actions/button_game_start_new.dart';
import 'package:wordguessing/ui/widgets/actions/button_resume_saved_game.dart';
class ParametersLayout extends StatelessWidget {
const ParametersLayout({super.key, required this.canResume});
final bool canResume;
class PageParameters extends StatelessWidget {
const PageParameters({super.key});
final double separatorHeight = 8.0;
@override
Widget build(BuildContext context) {
return BlocBuilder<ActivityCubit, ActivityState>(
builder: (BuildContext context, ActivityState activityState) {
final Activity currentActivity = activityState.currentActivity;
final List<Widget> lines = [];
// Game settings
for (String code in DefaultGameSettings.availableParameters) {
for (String code in DefaultActivitySettings.availableParameters) {
lines.add(Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: buildParametersLine(
......@@ -38,7 +43,7 @@ class ParametersLayout extends StatelessWidget {
child: SizedBox(height: separatorHeight),
));
if (canResume == false) {
if (currentActivity.canBeResumed == false) {
// Start new game
lines.add(
const AspectRatio(
......@@ -77,6 +82,8 @@ class ParametersLayout extends StatelessWidget {
return Column(
children: lines,
);
},
);
}
List<Widget> buildParametersLine({
......@@ -87,25 +94,25 @@ class ParametersLayout extends StatelessWidget {
final List<String> availableValues = isGlobal
? DefaultGlobalSettings.getAvailableValues(code)
: DefaultGameSettings.getAvailableValues(code);
: DefaultActivitySettings.getAvailableValues(code);
if (availableValues.length <= 1) {
return [];
}
for (String value in availableValues) {
final Widget parameterButton = BlocBuilder<GameSettingsCubit, GameSettingsState>(
builder: (BuildContext context, GameSettingsState gameSettingsState) {
final Widget parameterButton = BlocBuilder<ActivitySettingsCubit, ActivitySettingsState>(
builder: (BuildContext context, ActivitySettingsState activitySettingsState) {
return BlocBuilder<GlobalSettingsCubit, GlobalSettingsState>(
builder: (BuildContext context, GlobalSettingsState globalSettingsState) {
final GameSettingsCubit gameSettingsCubit =
BlocProvider.of<GameSettingsCubit>(context);
final ActivitySettingsCubit activitySettingsCubit =
BlocProvider.of<ActivitySettingsCubit>(context);
final GlobalSettingsCubit globalSettingsCubit =
BlocProvider.of<GlobalSettingsCubit>(context);
final String currentValue = isGlobal
? globalSettingsCubit.getParameterValue(code)
: gameSettingsCubit.getParameterValue(code);
: activitySettingsCubit.getParameterValue(code);
final bool isSelected = (value == currentValue);
......@@ -119,12 +126,12 @@ class ParametersLayout extends StatelessWidget {
value: value,
isSelected: isSelected,
size: itemWidth,
gameSettings: gameSettingsState.settings,
activitySettings: activitySettingsState.settings,
globalSettings: globalSettingsState.settings,
onPressed: () {
isGlobal
? globalSettingsCubit.setParameterValue(code, value)
: gameSettingsCubit.setParameterValue(code, value);
: activitySettingsCubit.setParameterValue(code, value);
},
),
);
......
......@@ -3,20 +3,20 @@ import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:wordguessing/models/settings/settings_game.dart';
import 'package:wordguessing/models/settings/settings_activity.dart';
import 'package:wordguessing/models/settings/settings_global.dart';
class ParameterPainter extends CustomPainter {
const ParameterPainter({
required this.code,
required this.value,
required this.gameSettings,
required this.activitySettings,
required this.globalSettings,
});
final String code;
final String value;
final GameSettings gameSettings;
final ActivitySettings activitySettings;
final GlobalSettings globalSettings;
@override
......
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:wordguessing/config/default_game_settings.dart';
import 'package:wordguessing/models/settings/settings_game.dart';
import 'package:wordguessing/config/default_activity_settings.dart';
import 'package:wordguessing/models/settings/settings_activity.dart';
import 'package:wordguessing/models/settings/settings_global.dart';
class ParameterWidget extends StatelessWidget {
......@@ -12,7 +12,7 @@ class ParameterWidget extends StatelessWidget {
required this.value,
required this.isSelected,
required this.size,
required this.gameSettings,
required this.activitySettings,
required this.globalSettings,
required this.onPressed,
});
......@@ -21,7 +21,7 @@ class ParameterWidget extends StatelessWidget {
final String value;
final bool isSelected;
final double size;
final GameSettings gameSettings;
final ActivitySettings activitySettings;
final GlobalSettings globalSettings;
final VoidCallback onPressed;
......@@ -35,10 +35,10 @@ class ParameterWidget extends StatelessWidget {
Widget content = const SizedBox.shrink();
switch (code) {
case DefaultGameSettings.parameterCodeGameType:
case DefaultActivitySettings.parameterCodeGameType:
content = getGameTypeParameterItem();
break;
case DefaultGameSettings.parameterCodeLangValue:
case DefaultActivitySettings.parameterCodeLangValue:
content = getLangValueParameterItem();
break;
default:
......@@ -77,7 +77,8 @@ class ParameterWidget extends StatelessWidget {
color: backgroundColor,
onPressed: onPressed,
child: Image(
image: AssetImage('assets/ui/${DefaultGameSettings.parameterCodeGameType}_$value.png'),
image: AssetImage(
'assets/ui/${DefaultActivitySettings.parameterCodeGameType}_$value.png'),
fit: BoxFit.fill,
),
);
......@@ -89,10 +90,10 @@ class ParameterWidget extends StatelessWidget {
String text = '';
switch (value) {
case DefaultGameSettings.langValueFr:
case DefaultActivitySettings.langValueFr:
text = '🇫🇷';
break;
case DefaultGameSettings.langValueEn:
case DefaultActivitySettings.langValueEn:
text = '🇬🇧';
break;
default:
......
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
class PageAbout extends StatelessWidget {
const PageAbout({super.key});
class ScreenAbout extends StatelessWidget {
const ScreenAbout({super.key});
@override
Widget build(BuildContext context) {
......
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:wordguessing/common/config/activity_page.dart';
import 'package:wordguessing/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_custom_toolbox/flutter_toolbox.dart';
class PageSettings extends StatelessWidget {
const PageSettings({super.key});
class ScreenSettings extends StatelessWidget {
const ScreenSettings({super.key});
@override
Widget build(BuildContext context) {
......
class ApplicationConfig {
static const String appTitle = 'Words Game';
}
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
class DefaultGameSettings {
class DefaultActivitySettings {
// available game parameters codes
static const String parameterCodeGameType = 'type';
static const String parameterCodeLangValue = 'lang';
......@@ -33,9 +33,9 @@ class DefaultGameSettings {
static List<String> getAvailableValues(String parameterCode) {
switch (parameterCode) {
case parameterCodeGameType:
return DefaultGameSettings.allowedGameTypeValues;
return DefaultActivitySettings.allowedGameTypeValues;
case parameterCodeLangValue:
return DefaultGameSettings.allowedLangValues;
return DefaultActivitySettings.allowedLangValues;
}
printlog('Did not find any available value for game parameter "$parameterCode".');
......
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:wordguessing/models/activity/activity.dart';
import 'package:wordguessing/models/settings/settings_activity.dart';
import 'package:wordguessing/models/settings/settings_global.dart';
part 'activity_state.dart';
class ActivityCubit extends HydratedCubit<ActivityState> {
ActivityCubit()
: super(ActivityState(
currentActivity: Activity.createEmpty(),
));
void updateState(Activity activity) {
emit(ActivityState(
currentActivity: activity,
));
}
void refresh() {
final Activity activity = Activity(
// Settings
activitySettings: state.currentActivity.activitySettings,
globalSettings: state.currentActivity.globalSettings,
// State
isRunning: state.currentActivity.isRunning,
isStarted: state.currentActivity.isStarted,
isFinished: state.currentActivity.isFinished,
animationInProgress: state.currentActivity.animationInProgress,
// Base data
word: state.currentActivity.word,
otherWords: state.currentActivity.otherWords,
images: state.currentActivity.images,
// Game data
recentWordsKeys: state.currentActivity.recentWordsKeys,
questionsCount: state.currentActivity.questionsCount,
goodAnswers: state.currentActivity.goodAnswers,
wrongAnswers: state.currentActivity.wrongAnswers,
);
// game.dump();
updateState(activity);
}
void startNewActivity({
required ActivitySettings activitySettings,
required GlobalSettings globalSettings,
}) {
final Activity newActivity = Activity.createNew(
// Settings
activitySettings: activitySettings,
globalSettings: globalSettings,
);
updateState(newActivity);
nextWord();
newActivity.dump();
updateState(newActivity);
refresh();
}
void quitActivity() {
state.currentActivity.isRunning = false;
refresh();
}
void resumeSavedActivity() {
state.currentActivity.isRunning = true;
refresh();
}
void deleteSavedActivity() {
state.currentActivity.isRunning = false;
state.currentActivity.isFinished = true;
refresh();
}
void nextWord() {
state.currentActivity.pickNewWord();
refresh();
}
void checkWord(word) {
if (state.currentActivity.word.key == word.key) {
state.currentActivity.goodAnswers++;
nextWord();
} else {
state.currentActivity.wrongAnswers++;
}
refresh();
}
@override
ActivityState? fromJson(Map<String, dynamic> json) {
final Activity currentActivity = json['currentActivity'] as Activity;
return ActivityState(
currentActivity: currentActivity,
);
}
@override
Map<String, dynamic>? toJson(ActivityState state) {
return <String, dynamic>{
'currentActivity': state.currentActivity.toJson(),
};
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment