Skip to content

Commit

Permalink
fix(client): Make generic requests work
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustl22 committed Nov 27, 2023
1 parent 1ee137c commit b596805
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 67 deletions.
16 changes: 9 additions & 7 deletions wrestling_scoreboard_client/lib/mocks/mock_data_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ class MockDataProvider extends DataProvider {

@override
Future<T> readSingle<T extends DataObject>(int id) async {
final Iterable<T> many = await readMany<T>();
final Iterable<T> many = await readMany<T, DataObject>();
return many.singleWhere((element) => element.id == id);
}

@override
Future<List<T>> readMany<T extends DataObject>({DataObject? filterObject}) async {
Future<List<T>> readMany<T extends DataObject, S extends DataObject>({S? filterObject}) async {
await Future.delayed(latency);
return Future.value(getManyMocksFromClass<T>(filterObject: filterObject));
}
Expand All @@ -29,7 +29,8 @@ class MockDataProvider extends DataProvider {
}

@override
Future<Iterable<Map<String, dynamic>>> readManyJson<T extends DataObject>({DataObject? filterObject}) async {
Future<Iterable<Map<String, dynamic>>> readManyJson<T extends DataObject, S extends DataObject>(
{S? filterObject}) async {
throw UnimplementedError('Raw types are not supported in Mock mode');
// return Future.value(getManyMocksFromClass<T>(filterObject: filterObject).map((e) => e.toJson()));
}
Expand Down Expand Up @@ -76,7 +77,7 @@ class MockDataProvider extends DataProvider {
}

@override
Future<void> generateFights(WrestlingEvent wrestlingEvent, [bool isReset = false]) async {
Future<void> generateFights<T extends WrestlingEvent>(WrestlingEvent wrestlingEvent, [bool isReset = false]) async {
List<Fight> oldFights; // TODO really needs old fights or just use the exising ones from teammatch
if (wrestlingEvent is TeamMatch) {
oldFights = getFightsOfTeamMatch(wrestlingEvent);
Expand Down Expand Up @@ -106,10 +107,11 @@ class MockDataProvider extends DataProvider {
List<List<Participation>> teamParticipations;
List<WeightClass> weightClasses;
if (wrestlingEvent is TeamMatch) {
final homeParticipations = await dataProvider.readMany<Participation>(filterObject: wrestlingEvent.home);
final guestParticipations = await dataProvider.readMany<Participation>(filterObject: wrestlingEvent.guest);
final homeParticipations = await dataProvider.readMany<Participation, Lineup>(filterObject: wrestlingEvent.home);
final guestParticipations =
await dataProvider.readMany<Participation, Lineup>(filterObject: wrestlingEvent.guest);
teamParticipations = [homeParticipations, guestParticipations];
weightClasses = await dataProvider.readMany<WeightClass>(filterObject: wrestlingEvent.league);
weightClasses = await dataProvider.readMany<WeightClass, League>(filterObject: wrestlingEvent.league);
} else if (wrestlingEvent is Tournament) {
// TODO get all participations
teamParticipations = [];
Expand Down
12 changes: 6 additions & 6 deletions wrestling_scoreboard_client/lib/ui/components/consumer.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:wrestling_scoreboard_common/common.dart';
import 'package:flutter/material.dart';
import 'package:wrestling_scoreboard_client/ui/components/exception.dart';
import 'package:wrestling_scoreboard_client/util/network/data_provider.dart';
import 'package:wrestling_scoreboard_client/util/network/remote/web_socket.dart';
import 'package:wrestling_scoreboard_common/common.dart';

class SingleConsumer<T extends DataObject> extends StatefulWidget {
final int? id;
Expand Down Expand Up @@ -48,19 +48,19 @@ class SingleConsumerState<T extends DataObject> extends State<SingleConsumer<T>>
}
}

class ManyConsumer<T extends DataObject> extends StatefulWidget {
class ManyConsumer<T extends DataObject, S extends DataObject> extends StatefulWidget {
final List<T>? initialData;
final DataObject? filterObject;
final S? filterObject;
final Widget Function(BuildContext context, List<T> data) builder;

const ManyConsumer({this.initialData, required this.builder, this.filterObject, Key? key}) : super(key: key);

@override
State<StatefulWidget> createState() => ManyConsumerState<T>();
State<StatefulWidget> createState() => ManyConsumerState<T, S>();
}

// TODO add const CircularProgressIndicator() on load
class ManyConsumerState<T extends DataObject> extends State<ManyConsumer<T>> {
class ManyConsumerState<T extends DataObject, S extends DataObject> extends State<ManyConsumer<T, S>> {
late Stream<WebSocketConnectionState> webSocketConnectionStream;

@override
Expand All @@ -75,7 +75,7 @@ class ManyConsumerState<T extends DataObject> extends State<ManyConsumer<T>> {
return StreamBuilder(
stream: webSocketConnectionStream,
builder: (context, snapshot) => StreamBuilder(
stream: dataProvider.streamMany<T>(filterObject: widget.filterObject, init: widget.initialData == null),
stream: dataProvider.streamMany<T, S>(filterObject: widget.filterObject, init: widget.initialData == null),
initialData: ManyDataObject<T>(data: widget.initialData ?? []),
builder: (BuildContext context, AsyncSnapshot<ManyDataObject<T>> snap) {
if (snap.hasError) {
Expand Down
35 changes: 18 additions & 17 deletions wrestling_scoreboard_client/lib/ui/edit/lineup_edit.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:collection';

import 'package:wrestling_scoreboard_common/common.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
Expand All @@ -9,6 +8,7 @@ import 'package:wrestling_scoreboard_client/ui/components/dropdown.dart';
import 'package:wrestling_scoreboard_client/ui/components/edit.dart';
import 'package:wrestling_scoreboard_client/ui/components/font.dart';
import 'package:wrestling_scoreboard_client/util/network/data_provider.dart';
import 'package:wrestling_scoreboard_common/common.dart';

class LineupEdit extends StatefulWidget {
final Lineup lineup;
Expand All @@ -17,13 +17,13 @@ class LineupEdit extends StatefulWidget {

final Function()? onSubmit;

const LineupEdit(
{Key? key,
required this.lineup,
required this.weightClasses,
required this.participations,
this.onSubmit})
: super(key: key);
const LineupEdit({
Key? key,
required this.lineup,
required this.weightClasses,
required this.participations,
this.onSubmit,
}) : super(key: key);

@override
State<StatefulWidget> createState() => LineupEditState();
Expand All @@ -41,18 +41,16 @@ class LineupEditState extends State<LineupEdit> {
final HashSet<Participation> _createOrUpdateParticipations = HashSet();

Future<List<Membership>> filterMemberships(String? filter) async {
memberships ??= await dataProvider.readMany<Membership>(filterObject: widget.lineup.team.club);
return (filter == null
? memberships!
: memberships!.where((element) => element.person.fullName.contains(filter)))
memberships ??= await dataProvider.readMany<Membership, Club>(filterObject: widget.lineup.team.club);
return (filter == null ? memberships! : memberships!.where((element) => element.person.fullName.contains(filter)))
.toList();
}

Future<void> handleSubmit(NavigatorState navigator) async {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
await dataProvider.createOrUpdateSingle(
Lineup(id: widget.lineup.id, team: widget.lineup.team, leader: _leader, coach: _coach));
await dataProvider
.createOrUpdateSingle(Lineup(id: widget.lineup.id, team: widget.lineup.team, leader: _leader, coach: _coach));
await Future.forEach(_deleteParticipations, (Participation element) => dataProvider.deleteSingle(element));
await Future.forEach(
_createOrUpdateParticipations, (Participation element) => dataProvider.createOrUpdateSingle(element));
Expand All @@ -77,7 +75,7 @@ class LineupEditState extends State<LineupEdit> {
Widget build(BuildContext context) {
final localizations = AppLocalizations.of(context)!;
final navigator = Navigator.of(context);

return Form(
key: _formKey,
child: EditWidget(
Expand Down Expand Up @@ -122,7 +120,8 @@ class LineupEditState extends State<LineupEdit> {
padding: const EdgeInsets.only(right: 8, top: 8, bottom: 8),
child: getDropdown<Membership>(
selectedItem: participation?.membership,
label: '${localizations.weightClass} ${weightClass.name} ${styleToAbbr(weightClass.style, context)}',
label:
'${localizations.weightClass} ${weightClass.name} ${styleToAbbr(weightClass.style, context)}',
context: context,
onSaved: (Membership? newMembership) {
final oldParticipation = _participations[weightClass];
Expand Down Expand Up @@ -158,7 +157,9 @@ class LineupEditState extends State<LineupEdit> {
decoration: InputDecoration(
contentPadding: const EdgeInsets.symmetric(vertical: 20),
labelText: localizations.weight),
inputFormatters: <TextInputFormatter>[FilteringTextInputFormatter.allow(RegExp(r'^\d{1,3}(\.\d{0,2})?'))],
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp(r'^\d{1,3}(\.\d{0,2})?'))
],
onSaved: (String? value) {
var participation = _participations[weightClass];
if (participation != null) {
Expand Down
2 changes: 1 addition & 1 deletion wrestling_scoreboard_client/lib/ui/edit/team_edit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class TeamEditState extends State<TeamEdit> {
}),
itemAsString: (u) => u.name,
onFind: (String? filter) async {
availableClubs ??= await dataProvider.readMany<Club>();
availableClubs ??= await dataProvider.readMany<Club, DataObject>();
return (filter == null ? availableClubs! : availableClubs!.where((element) => element.name.contains(filter))).toList();
},
),
Expand Down
12 changes: 6 additions & 6 deletions wrestling_scoreboard_client/lib/ui/edit/team_match_edit.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'package:wrestling_scoreboard_common/common.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:wrestling_scoreboard_client/ui/components/dropdown.dart';
import 'package:wrestling_scoreboard_client/ui/components/edit.dart';
import 'package:wrestling_scoreboard_client/util/network/data_provider.dart';
import 'package:wrestling_scoreboard_client/util/date_time.dart';
import 'package:wrestling_scoreboard_client/util/network/data_provider.dart';
import 'package:wrestling_scoreboard_common/common.dart';

class TeamMatchEdit extends StatefulWidget {
final TeamMatch? teamMatch;
Expand Down Expand Up @@ -104,8 +104,8 @@ class TeamMatchEditState extends State<TeamMatchEdit> {
}),
itemAsString: (u) => u.name,
onFind: (String? filter) async {
teams ??= await dataProvider
.readMany<Team>(); // TODO filter by teams of same league, but may add an option to search all teams
teams ??= await dataProvider.readMany<Team,
DataObject>(); // TODO: filter by teams of same league, but may add an option to search all teams
return (filter == null ? teams! : teams!.where((element) => element.name.contains(filter))).toList();
},
),
Expand All @@ -121,8 +121,8 @@ class TeamMatchEditState extends State<TeamMatchEdit> {
}),
itemAsString: (u) => u.name,
onFind: (String? filter) async {
teams ??= await dataProvider
.readMany<Team>(); // TODO filter by teams of same league, but may add an option to search all teams
teams ??= await dataProvider.readMany<Team,
DataObject>(); // TODO: filter by teams of same league, but may add an option to search all teams
return (filter == null ? teams! : teams!.where((element) => element.name.contains(filter))).toList();
},
),
Expand Down
2 changes: 1 addition & 1 deletion wrestling_scoreboard_client/lib/ui/fight/fight_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ 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>(filterObject: fights[index]);
final actions = await dataProvider.readMany<FightAction, Fight>(filterObject: fights[index]);
navigator.push(MaterialPageRoute(builder: (context) => FightScreen(match, fights, actions, index)));
}

Expand Down
6 changes: 3 additions & 3 deletions wrestling_scoreboard_client/lib/ui/home/home.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:wrestling_scoreboard_common/common.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:wrestling_scoreboard_client/ui/components/consumer.dart';
Expand All @@ -8,6 +7,7 @@ import 'package:wrestling_scoreboard_client/ui/edit/league_edit.dart';
import 'package:wrestling_scoreboard_client/ui/overview/club_overview.dart';
import 'package:wrestling_scoreboard_client/ui/overview/league_overview.dart';
import 'package:wrestling_scoreboard_client/util/network/remote/web_socket.dart';
import 'package:wrestling_scoreboard_common/common.dart';

class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
Expand Down Expand Up @@ -83,7 +83,7 @@ class HomeState extends State<Home> {
),
body: TabBarView(
children: [
ManyConsumer<Club>(
ManyConsumer<Club, DataObject>(
builder: (BuildContext context, List<Club> clubs) {
return ListGroup(
header: HeadingItem(
Expand All @@ -108,7 +108,7 @@ class HomeState extends State<Home> {
);
},
),
ManyConsumer<League>(
ManyConsumer<League, DataObject>(
builder: (BuildContext context, List<League> leagues) {
return ListGroup(
header: HeadingItem(
Expand Down
4 changes: 2 additions & 2 deletions wrestling_scoreboard_client/lib/ui/match/match_sequence.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class MatchSequence extends StatelessWidget {
),
]),
),
body: ManyConsumer<Fight>(
body: ManyConsumer<Fight, TeamMatch>(
filterObject: match,
builder: (context, fights) {
final matchInfos = [
Expand Down Expand Up @@ -135,7 +135,7 @@ class FightListItem extends StatelessWidget {
listItemCallback(match, fights, index);
},
child: IntrinsicHeight(
child: ManyConsumer<FightAction>(
child: ManyConsumer<FightAction, Fight>(
filterObject: fight,
builder: (context, actions) => Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ClubOverview extends StatelessWidget {
),
body: GroupedList(items: [
description,
ManyConsumer<Team>(
ManyConsumer<Team, Club>(
filterObject: data,
builder: (BuildContext context, List<Team> teams) {
return ListGroup(
Expand All @@ -70,7 +70,7 @@ class ClubOverview extends StatelessWidget {
);
},
),
ManyConsumer<Membership>(
ManyConsumer<Membership, Club>(
filterObject: data,
builder: (BuildContext context, List<Membership> memberships) {
return ListGroup(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class LeagueOverview extends StatelessWidget {
),
body: GroupedList(items: [
description,
ManyConsumer<Team>(
ManyConsumer<Team, League>(
filterObject: data,
builder: (BuildContext context, List<Team> team) {
return ListGroup(
Expand Down Expand Up @@ -85,7 +85,7 @@ class LeagueOverview extends StatelessWidget {
);
},
),
ManyConsumer<LeagueWeightClass>(
ManyConsumer<LeagueWeightClass, League>(
filterObject: data,
builder: (BuildContext context, List<LeagueWeightClass> leagueWeightClasses) {
return ListGroup(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ class TeamMatchOverview extends StatelessWidget {
}

handleSelectedLineup(Lineup lineup, League league, NavigatorState navigator) async {
final participations = await dataProvider.readMany<Participation>(filterObject: lineup);
final weightClasses = await dataProvider.readMany<WeightClass>(filterObject: league);
final participations = await dataProvider.readMany<Participation, Lineup>(filterObject: lineup);
final weightClasses = await dataProvider.readMany<WeightClass, League>(filterObject: league);
navigator.push(
MaterialPageRoute(
builder: (context) {
Expand All @@ -116,7 +116,7 @@ class TeamMatchOverview extends StatelessWidget {
participations: participations,
lineup: lineup,
onSubmit: () {
dataProvider.generateFights(match, false);
dataProvider.generateFights<TeamMatch>(match, false);
},
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class TeamOverview<T extends DataObject> extends StatelessWidget {
),
body: GroupedList(items: [
description,
ManyConsumer<TeamMatch>(
ManyConsumer<TeamMatch, Team>(
filterObject: data,
builder: (BuildContext context, List<TeamMatch> matches) {
return ListGroup(
Expand Down
Loading

0 comments on commit b596805

Please sign in to comment.