Skip to content

Commit

Permalink
feat: Make FightDisplay available via route
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustl22 committed Dec 2, 2023
1 parent 044674d commit 62cebf3
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 29 deletions.
57 changes: 53 additions & 4 deletions wrestling_scoreboard_client/lib/ui/fight/fight_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:go_router/go_router.dart';
import 'package:printing/printing.dart';
import 'package:provider/provider.dart';
import 'package:wrestling_scoreboard_client/data/fight_role.dart';
import 'package:wrestling_scoreboard_client/data/wrestling_style.dart';
import 'package:wrestling_scoreboard_client/ui/components/consumer.dart';
import 'package:wrestling_scoreboard_client/ui/components/exception.dart';
import 'package:wrestling_scoreboard_client/ui/fight/fight_action_controls.dart';
import 'package:wrestling_scoreboard_client/ui/fight/fight_shortcuts.dart';
import 'package:wrestling_scoreboard_client/ui/fight/technical_points.dart';
import 'package:wrestling_scoreboard_client/ui/fight/time_display.dart';
import 'package:wrestling_scoreboard_client/ui/models/participant_state_model.dart';
import 'package:wrestling_scoreboard_client/ui/overview/team_match_overview.dart';
import 'package:wrestling_scoreboard_client/util/audio/audio.dart';
import 'package:wrestling_scoreboard_client/util/colors.dart';
import 'package:wrestling_scoreboard_client/util/network/data_provider.dart';
Expand All @@ -24,9 +27,55 @@ import '../match/common_elements.dart';
import 'fight_actions.dart';
import 'fight_main_controls.dart';

void navigateToFightScreen(NavigatorState navigator, TeamMatch match, List<Fight> fights, int index) async {
final actions = await dataProvider.readMany<FightAction, Fight>(filterObject: fights[index]);
navigator.push(MaterialPageRoute(builder: (context) => FightScreen(match, fights, actions, index)));
void navigateToFightScreen(BuildContext context, TeamMatch match, Fight fight) async {
context.go('/${TeamMatchOverview.route}/${match.id}/${FightDisplay.route}/${fight.id}');
}

/// Class to load a single fight, while also consider the previous and the next fight.
/// So must load the whole list of fights to keep track of what comes next.
/// TODO: This may can be done server side with its own request in the future.
class FightDisplay extends StatelessWidget {
static const route = 'fight';
final int matchId;
final int fightId;
final TeamMatch? initialMatch;

const FightDisplay({
required this.matchId,
required this.fightId,
this.initialMatch,
super.key,
});

@override
Widget build(BuildContext context) {
final localizations = AppLocalizations.of(context)!;
return SingleConsumer<TeamMatch>(
id: matchId,
initialData: initialMatch,
builder: (context, match) {
if (match == null) return ExceptionWidget(localizations.notFoundException);
return ManyConsumer<Fight, TeamMatch>(
filterObject: match,
builder: (context, fights) {
if (fights.isEmpty) {
return Center(
child: Text(
localizations.noItems,
style: Theme.of(context).textTheme.bodySmall,
),
);
}
final currentFight = fights.singleWhere((element) => element.id == fightId);
final currentFightIndex = fights.indexOf(currentFight);
return ManyConsumer<FightAction, Fight>(
filterObject: currentFight,
builder: (context, actions) {
return FightScreen(match, fights, actions, currentFightIndex);
});
});
});
}
}

/// Initialize with default values, but do not synchronize with live data, as during a fight the connection could be interrupted. So the client always sends data, but never should receive any.
Expand Down Expand Up @@ -161,7 +210,7 @@ class FightState extends State<FightScreen> {
handleAction = (FightScreenActionIntent intent) {
FightActionHandler.handleIntentStatic(
intent, stopwatch, match, fights, getActions, setActions, fightIndex, doAction,
navigator: Navigator.of(context));
context: context);
};
}

Expand Down
24 changes: 12 additions & 12 deletions wrestling_scoreboard_client/lib/ui/fight/fight_shortcuts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:go_router/go_router.dart';
import 'package:wrestling_scoreboard_client/util/audio/audio.dart';
import 'package:wrestling_scoreboard_client/util/network/data_provider.dart';
import 'package:wrestling_scoreboard_common/common.dart';
Expand Down Expand Up @@ -125,9 +126,9 @@ class FightActionHandler extends StatelessWidget {
Key? key})
: super(key: key);

Future<void> handleIntent(FightScreenActionIntent intent, {NavigatorState? navigator}) async {
Future<void> handleIntent(FightScreenActionIntent intent, {BuildContext? context}) async {
await handleIntentStatic(intent, stopwatch, match, fights, getActions, setActions, fightIndex, doAction,
navigator: navigator);
context: context);
}

static Future<void> handleIntentStatic(
Expand All @@ -139,7 +140,7 @@ class FightActionHandler extends StatelessWidget {
void Function(List<FightAction> actions) setActions,
int fightIndex,
Function(FightScreenActions action) doAction,
{NavigatorState? navigator}) async {
{BuildContext? context}) async {
final fight = fights[fightIndex];
switch (intent.type) {
case FightScreenActions.startStop:
Expand All @@ -159,25 +160,25 @@ class FightActionHandler extends StatelessWidget {
stopwatch.reset();
break;
case FightScreenActions.nextFight:
if (navigator != null) {
if (context != null) {
int index = fightIndex + 1;
if (index < fights.length) {
navigator.pop();
navigateToFightScreen(navigator, match, fights, index);
context.pop();
navigateToFightScreen(context, match, fights[index]);
}
}
break;
case FightScreenActions.previousFight:
if (navigator != null) {
if (context != null) {
int index = fightIndex - 1;
if (index >= 0) {
navigator.pop();
navigateToFightScreen(navigator, match, fights, index);
context.pop();
navigateToFightScreen(context, match, fights[index]);
}
}
break;
case FightScreenActions.quit:
if (navigator != null) navigator.pop();
if (context != null) context.pop();
break;
case FightScreenActions.redOne:
var action = FightAction(
Expand Down Expand Up @@ -346,7 +347,6 @@ class FightActionHandler extends StatelessWidget {

@override
Widget build(BuildContext context) {
final navigator = Navigator.of(context);
const redOneIntent = FightScreenActionIntent.redOne();
const redTwoIntent = FightScreenActionIntent.redTwo();
const redThreeIntent = FightScreenActionIntent.redThree();
Expand Down Expand Up @@ -377,7 +377,7 @@ class FightActionHandler extends StatelessWidget {
child: Actions(
actions: <Type, Action<Intent>>{
FightScreenActionIntent: CallbackAction<FightScreenActionIntent>(
onInvoke: (FightScreenActionIntent intent) => handleIntent(intent, navigator: navigator),
onInvoke: (FightScreenActionIntent intent) => handleIntent(intent, context: context),
)
},
child: RawKeyboardListener(
Expand Down
15 changes: 6 additions & 9 deletions wrestling_scoreboard_client/lib/ui/match/match_sequence.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class MatchSequence extends StatelessWidget {
@override
Widget build(BuildContext context) {
final localizations = AppLocalizations.of(context)!;
final navigator = Navigator.of(context);

double width = MediaQuery.of(context).size.width;
double padding = width / 100;
Expand Down Expand Up @@ -84,8 +83,8 @@ class MatchSequence extends StatelessWidget {
child: ListView.builder(
itemCount: fights.length,
itemBuilder: (context, index) {
return FightListItem(match, fights, index,
(match, fights, index) => navigateToFightScreen(navigator, match, fights, index),
return FightListItem(
match, fights[index], (match, fight) => navigateToFightScreen(context, match, fight),
flexWidthWeight: flexWidthWeight, flexWidthStyle: flexWidthStyle);
},
),
Expand All @@ -106,13 +105,12 @@ class MatchSequence extends StatelessWidget {

class FightListItem extends StatelessWidget {
final TeamMatch match;
final List<Fight> fights;
final int index;
final Function(TeamMatch match, List<Fight> fights, int index) listItemCallback;
final Fight fight;
final Function(TeamMatch match, Fight fight) listItemCallback;
final int flexWidthWeight;
final int flexWidthStyle;

const FightListItem(this.match, this.fights, this.index, this.listItemCallback,
const FightListItem(this.match, this.fight, this.listItemCallback,
{this.flexWidthWeight = 12, this.flexWidthStyle = 5, Key? key})
: super(key: key);

Expand All @@ -137,12 +135,11 @@ class FightListItem extends StatelessWidget {
double fontSizeDefault = width / 60;
TextStyle fontStyleDefault = TextStyle(fontSize: fontSizeDefault);

final fight = fights[index];
return Column(
children: [
InkWell(
onTap: () {
listItemCallback(match, fights, index);
listItemCallback(match, fight);
},
child: IntrinsicHeight(
child: ManyConsumer<FightAction, Fight>(
Expand Down
17 changes: 13 additions & 4 deletions wrestling_scoreboard_client/lib/ui/router.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:go_router/go_router.dart';
import 'package:wrestling_scoreboard_client/ui/app_navigation.dart';
import 'package:wrestling_scoreboard_client/ui/fight/fight_screen.dart';
import 'package:wrestling_scoreboard_client/ui/match/match_sequence.dart';
import 'package:wrestling_scoreboard_client/ui/overview/club_overview.dart';
import 'package:wrestling_scoreboard_client/ui/overview/league_overview.dart';
Expand All @@ -23,14 +24,22 @@ final router = GoRouter(
builder: (context, state) => LeagueOverview(id: int.parse(state.pathParameters['id']!)),
),
GoRoute(
path: '${TeamMatchOverview.route}/:id',
builder: (context, state) => TeamMatchOverview(id: int.parse(state.pathParameters['id']!)),
path: '${TeamMatchOverview.route}/:match_id',
builder: (context, state) => TeamMatchOverview(id: int.parse(state.pathParameters['match_id']!)),
routes: [
GoRoute(
path: MatchSequence.route,
builder: (context, state) => MatchSequence(id: int.parse(state.pathParameters['id']!)),
builder: (context, state) => MatchSequence(id: int.parse(state.pathParameters['match_id']!)),
),
]
GoRoute(
path: '${FightDisplay.route}/:fight_id',
builder: (context, state) {
final matchId = int.parse(state.pathParameters['match_id']!);
final fightId = int.parse(state.pathParameters['fight_id']!);
return FightDisplay(matchId: matchId, fightId: fightId);
},
),
],
),
GoRoute(
path: '${MembershipOverview.route}/:id',
Expand Down

0 comments on commit 62cebf3

Please sign in to comment.