Skip to content

Commit

Permalink
feat: Show network timeout (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustl22 committed Mar 5, 2024
1 parent 7214e09 commit bdbda2a
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ class Preferences {
static const keyApiUrl = 'api-url';
static const keyWsUrl = 'ws-url';
static const keyBellSound = 'bell-sound';
static const keyNetworkTimeout = 'network-timeout';

static final StreamController<Locale?> onChangeLocale = StreamController.broadcast();
static final StreamController<ThemeMode> onChangeThemeMode = StreamController.broadcast();
static final StreamController<String> onChangeApiUrl = StreamController.broadcast();
static final StreamController<String> onChangeWsUrlWebSocket = StreamController.broadcast();
static final StreamController<String> onChangeBellSound = StreamController.broadcast();
static final StreamController<Duration> onChangeNetworkTimeout = StreamController.broadcast();

static final supportedLanguages = {
'en_US': const Locale('en', 'US'),
Expand All @@ -31,4 +33,6 @@ class Preferences {
}

static Future<String?> getString(String key) => SharedPreferences.getInstance().then((value) => value.getString(key));

static Future<int?> getInt(String key) => SharedPreferences.getInstance().then((value) => value.getInt(key));
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ class WebSocketUrlNotifier extends _$WebSocketUrlNotifier {
}
}

@Riverpod(keepAlive: true)
class NetworkTimeoutNotifier extends _$NetworkTimeoutNotifier {
@override
Raw<Future<Duration>> build() async {
var networkTimeout = await Preferences.getInt(Preferences.keyNetworkTimeout);
Preferences.onChangeNetworkTimeout.stream.distinct().listen((event) {
state = Future.value(event);
});
return Duration(milliseconds: networkTimeout ?? 10000);
}
}

@Riverpod(keepAlive: true)
class ApiUrlNotifier extends _$ApiUrlNotifier {
@override
Expand Down

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

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

Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:wrestling_scoreboard_client/provider/local_preferences_provider.dart';
import 'package:wrestling_scoreboard_client/view/widgets/exception.dart';

class LoadingBuilder<T> extends StatelessWidget {
class LoadingBuilder<T> extends ConsumerWidget {
final Future<T> future;
final T? initialData;
final void Function()? onRetry;
Expand All @@ -16,9 +18,9 @@ class LoadingBuilder<T> extends StatelessWidget {
});

@override
Widget build(BuildContext context) {
Widget build(BuildContext context, WidgetRef ref) {
return FutureBuilder<T>(
future: future,
future: ref.read(networkTimeoutNotifierProvider).then((timeout) => future.timeout(timeout)),
builder: (BuildContext context, AsyncSnapshot<T> snapshot) {
if (snapshot.hasError) {
return ExceptionWidget(snapshot.error!, onRetry: onRetry);
Expand Down

0 comments on commit bdbda2a

Please sign in to comment.