Skip to content

Commit

Permalink
feat: Wrap dataManager in provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustl22 committed Jan 17, 2024
1 parent f3361d4 commit bbc3e97
Show file tree
Hide file tree
Showing 38 changed files with 300 additions and 208 deletions.
11 changes: 8 additions & 3 deletions wrestling_scoreboard_client/lib/mocks/mock_data_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:math';

import 'package:wrestling_scoreboard_client/mocks/mocks.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 MockDataProvider extends DataProvider {
Expand Down Expand Up @@ -106,11 +107,11 @@ class MockDataProvider extends DataProvider {
List<List<Participation>> teamParticipations;
List<WeightClass> weightClasses;
if (wrestlingEvent is TeamMatch) {
final homeParticipations = await dataProvider.readMany<Participation, Lineup>(filterObject: wrestlingEvent.home);
final homeParticipations = await readMany<Participation, Lineup>(filterObject: wrestlingEvent.home);
final guestParticipations =
await dataProvider.readMany<Participation, Lineup>(filterObject: wrestlingEvent.guest);
await readMany<Participation, Lineup>(filterObject: wrestlingEvent.guest);
teamParticipations = [homeParticipations, guestParticipations];
weightClasses = await dataProvider.readMany<WeightClass, League>(filterObject: wrestlingEvent.league);
weightClasses = await readMany<WeightClass, League>(filterObject: wrestlingEvent.league);
} else if (wrestlingEvent is Competition) {
// TODO get all participations
teamParticipations = [];
Expand Down Expand Up @@ -391,4 +392,8 @@ class MockDataProvider extends DataProvider {
throw DataUnimplementedError(crud, obj.runtimeType);
}
}

@override
// TODO: implement webSocketManager
WebSocketManager get webSocketManager => throw UnimplementedError();
}
48 changes: 24 additions & 24 deletions wrestling_scoreboard_client/lib/provider/data_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:wrestling_scoreboard_client/provider/network_provider.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';

part 'data_provider.g.dart';
Expand All @@ -24,19 +24,19 @@ class SingleProviderData<T extends DataObject> {

@riverpod
Stream<T> singleDataStream<T extends DataObject>(
SingleDataStreamRef ref,
SingleDataStreamRef ref,
SingleProviderData<T> pData,
) async* {
// Reload, whenever the stream connects or disconnects
// TODO: integrate in updating websocket URL from stream provider
ref.watch(webSocketStateStreamProvider);

// ref.onDispose(webSocketConnectionStream.close);
// TODO: e.g. may be triggered twice
if(pData.initialData != null) {
yield pData.initialData!;
// Reload, whenever the stream is connected
final wsConnectionState = await ref.watch(webSocketStateStreamProvider.future);
if (wsConnectionState == WebSocketConnectionState.connected) {
// TODO: e.g. may be triggered twice
if (pData.initialData != null) {
yield pData.initialData!;
}
final dataManager = ref.watch(dataManagerProvider);
yield* dataManager.streamSingle<T>(pData.id, init: pData.initialData == null);
}
yield* dataProvider.streamSingle<T>(pData.id, init: pData.initialData == null);
}

/// Class to wrap equal many data for providers.
Expand All @@ -59,19 +59,19 @@ class ManyProviderData<T extends DataObject, S extends DataObject?> {

@riverpod
Stream<List<T>> manyDataStream<T extends DataObject, S extends DataObject?>(
ManyDataStreamRef ref,
ManyProviderData<T,S> pData,
ManyDataStreamRef ref,
ManyProviderData<T, S> pData,
) async* {
// Reload, whenever the stream connects or disconnects
// TODO: integrate in updating websocket URL from stream provider
ref.watch(webSocketStateStreamProvider);

// ref.onDispose(webSocketConnectionStream.close);
// TODO: e.g. bout action event triggered twice
if(pData.initialData != null) {
yield pData.initialData!;
// Reload, whenever the stream is connected
final wsConnectionState = await ref.watch(webSocketStateStreamProvider.future);
if (wsConnectionState == WebSocketConnectionState.connected) {
// TODO: e.g. bout action event triggered twice
if (pData.initialData != null) {
yield pData.initialData!;
}
final dataManager = ref.watch(dataManagerProvider);
yield* dataManager
.streamMany<T, S>(filterObject: pData.filterObject, init: pData.initialData == null)
.map((event) => event.data);
}
yield* dataProvider
.streamMany<T, S>(filterObject: pData.filterObject, init: pData.initialData == null)
.map((event) => event.data);
}
6 changes: 3 additions & 3 deletions wrestling_scoreboard_client/lib/provider/data_provider.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 20 additions & 4 deletions wrestling_scoreboard_client/lib/provider/network_provider.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
import 'package:riverpod_annotation/riverpod_annotation.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_client/mocks/mock_data_provider.dart';
import 'package:wrestling_scoreboard_client/util/environment.dart';
import 'package:wrestling_scoreboard_client/util/network/remote/rest.dart';

part 'network_provider.g.dart';

final _isMock = Env.appEnvironment.fromString() == 'mock';

@Riverpod(keepAlive: true)
Raw<Stream<WebSocketConnectionState>> webSocketStateStream(WebSocketStateStreamRef ref) {
final webSocketConnectionStream = WebSocketManager.onWebSocketConnection.stream
class DataManager extends _$DataManager {
@override
Raw<DataProvider> build() {
// TODO: override with mock via rivperpod overrides.
final dataProvider = _isMock ? MockDataProvider() : RestDataProvider();
return dataProvider;
}
}

@Riverpod(keepAlive: true)
Stream<WebSocketConnectionState> webSocketStateStream(WebSocketStateStreamRef ref) {
final dataManager = ref.watch(dataManagerProvider);
// TODO: integrate in updating websocket URL from stream provider
final webSocketConnectionStream = dataManager.webSocketManager.onWebSocketConnection.stream
.distinct()
.where((event) => event == WebSocketConnectionState.disconnected || event == WebSocketConnectionState.connected);

// ref.onDispose(webSocketConnectionStream.close);
return webSocketConnectionStream;
}

Expand Down
22 changes: 18 additions & 4 deletions wrestling_scoreboard_client/lib/provider/network_provider.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 18 additions & 5 deletions wrestling_scoreboard_client/lib/ui/components/consumer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:wrestling_scoreboard_client/provider/data_provider.dart';
import 'package:wrestling_scoreboard_client/provider/network_provider.dart';
import 'package:wrestling_scoreboard_client/ui/components/exception.dart';
import 'package:wrestling_scoreboard_client/ui/components/loading_builder.dart';
import 'package:wrestling_scoreboard_client/util/network/remote/web_socket.dart';
Expand Down Expand Up @@ -30,7 +31,12 @@ class NullableSingleConsumer<T extends DataObject> extends ConsumerWidget {
builder: builder,
future: stream,
initialData: null, // Handle initial data via the stream
onRetry: () => WebSocketManager.onWebSocketConnection.sink.add(WebSocketConnectionState.connecting),
onRetry: () => ref
.read(dataManagerProvider)
.webSocketManager
.onWebSocketConnection
.sink
.add(WebSocketConnectionState.connecting),
);
}
}
Expand Down Expand Up @@ -58,7 +64,9 @@ class SingleConsumer<T extends DataObject> extends StatelessWidget {
return ExceptionWidget(AppLocalizations.of(context)!.notFoundException);
}
return builder(context, data);
}, id: id, initialData: initialData);
},
id: id,
initialData: initialData);
}
}

Expand All @@ -78,7 +86,12 @@ class ManyConsumer<T extends DataObject, S extends DataObject?> extends Consumer
builder: builder,
future: stream,
initialData: null, // Handle initial data via the stream
onRetry: () => WebSocketManager.onWebSocketConnection.sink.add(WebSocketConnectionState.connecting),
onRetry: () => ref
.read(dataManagerProvider)
.webSocketManager
.onWebSocketConnection
.sink
.add(WebSocketConnectionState.connecting),
);
}
}
Expand Down Expand Up @@ -119,7 +132,7 @@ class SingleConsumerState<T extends DataObject> extends State<SingleConsumer<T>>
? widget.builder(context, widget.initialData)
: StreamBuilder<T>(
stream:
widget.id == null ? null : dataProvider.streamSingle<T>(widget.id!, init: widget.initialData == null),
widget.id == null ? null : ref.read(dataManagerProvider).streamSingle<T>(widget.id!, init: widget.initialData == null),
initialData: widget.initialData,
builder: (BuildContext context, AsyncSnapshot<T> snap) {
if (snap.hasError) {
Expand Down Expand Up @@ -164,7 +177,7 @@ class ManyConsumerState<T extends DataObject, S extends DataObject?> extends Sta
stream: webSocketConnectionStream,
builder: (context, snapshot) {
final stream =
dataProvider.streamMany<T, S>(filterObject: widget.filterObject, init: widget.initialData == null);
ref.read(dataManagerProvider).streamMany<T, S>(filterObject: widget.filterObject, init: widget.initialData == null);
final initialData = widget.initialData == null ? null : ManyDataObject<T>(data: widget.initialData!);
return LoadingStreamBuilder(
builder: (context, data) => widget.builder(context, data.data),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:wrestling_scoreboard_client/data/bout_utils.dart';
import 'package:wrestling_scoreboard_client/data/wrestling_style.dart';
import 'package:wrestling_scoreboard_client/provider/app_state_provider.dart';
import 'package:wrestling_scoreboard_client/provider/data_provider.dart';
import 'package:wrestling_scoreboard_client/provider/network_provider.dart';
import 'package:wrestling_scoreboard_client/ui/components/consumer.dart';
import 'package:wrestling_scoreboard_client/ui/components/loading_builder.dart';
import 'package:wrestling_scoreboard_client/ui/components/scaled_text.dart';
Expand All @@ -22,7 +23,6 @@ import 'package:wrestling_scoreboard_client/ui/models/participant_state_model.da
import 'package:wrestling_scoreboard_client/ui/overview/team_match/team_match_bout_overview.dart';
import 'package:wrestling_scoreboard_client/ui/overview/team_match/team_match_overview.dart';
import 'package:wrestling_scoreboard_client/util/audio/audio.dart';
import 'package:wrestling_scoreboard_client/util/network/data_provider.dart';
import 'package:wrestling_scoreboard_client/util/print/pdf/score_sheet.dart';
import 'package:wrestling_scoreboard_client/util/units.dart';
import 'package:wrestling_scoreboard_common/common.dart';
Expand Down Expand Up @@ -174,7 +174,7 @@ class BoutState extends ConsumerState<BoutScreen> {
_b.activityStopwatch?.stop();

// Save time to database on each stop
dataProvider.createOrUpdateSingle(bout);
ref.read(dataManagerProvider).createOrUpdateSingle(bout);
});
_boutStopwatch.onAdd.stream.listen((event) {
_r.activityStopwatch?.addDuration(event);
Expand Down Expand Up @@ -228,6 +228,7 @@ class BoutState extends ConsumerState<BoutScreen> {

void handleAction(BoutScreenActionIntent intent) {
intent.handle(
ref.read(dataManagerProvider),
stopwatch,
widget.bouts,
getActions,
Expand Down Expand Up @@ -509,6 +510,6 @@ class BoutState extends ConsumerState<BoutScreen> {
_breakStopwatch.dispose();

// Save time to database when dispose
await dataProvider.createOrUpdateSingle(bout);
await ref.read(dataManagerProvider).createOrUpdateSingle(bout);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:wrestling_scoreboard_client/data/bout_result.dart';
import 'package:wrestling_scoreboard_client/data/bout_utils.dart';
import 'package:wrestling_scoreboard_client/provider/network_provider.dart';
import 'package:wrestling_scoreboard_client/ui/components/consumer.dart';
import 'package:wrestling_scoreboard_client/ui/components/dropdown.dart';
import 'package:wrestling_scoreboard_client/ui/components/themed.dart';
Expand All @@ -9,17 +11,17 @@ import 'package:wrestling_scoreboard_client/ui/display/bout/bout_shortcuts.dart'
import 'package:wrestling_scoreboard_client/util/network/data_provider.dart';
import 'package:wrestling_scoreboard_common/common.dart';

class BoutMainControls extends StatefulWidget {
class BoutMainControls extends ConsumerStatefulWidget {
final Function(BoutScreenActionIntent) callback;
final BoutState boutState;

const BoutMainControls(this.callback, this.boutState, {super.key});

@override
State<StatefulWidget> createState() => BoutMainControlsState();
ConsumerState<ConsumerStatefulWidget> createState() => BoutMainControlsState();
}

class BoutMainControlsState extends State<BoutMainControls> {
class BoutMainControlsState extends ConsumerState<BoutMainControls> {
IconData _pausePlayButton = Icons.play_arrow;

@override
Expand Down Expand Up @@ -129,9 +131,10 @@ class BoutMainControlsState extends State<BoutMainControls> {
result: val,
);
bout = bout.updateClassificationPoints(actions);
dataProvider.createOrUpdateSingle(bout);
if (bout.r != null) dataProvider.createOrUpdateSingle(bout.r!);
if (bout.b != null) dataProvider.createOrUpdateSingle(bout.b!);
final dataManager = ref.read(dataManagerProvider);
dataManager.createOrUpdateSingle(bout);
if (bout.r != null) dataManager.createOrUpdateSingle(bout.r!);
if (bout.b != null) dataManager.createOrUpdateSingle(bout.b!);
widget.boutState.bout = bout;
});
},
Expand Down
Loading

0 comments on commit bbc3e97

Please sign in to comment.