Skip to content

Commit

Permalink
feat: Basic support for API providers (#47)
Browse files Browse the repository at this point in the history
Contributes to #39
  • Loading branch information
Gustl22 authored Mar 24, 2024
1 parent a38c48e commit fea89a8
Show file tree
Hide file tree
Showing 80 changed files with 3,214 additions and 621 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,12 @@ class MockDataManager extends DataManager {
// TODO: implement restoreDatabase
throw UnimplementedError();
}

@override
Future<void> organizationImport(int id) {
// TODO: implement organizationImport
throw UnimplementedError();
}
}

class MockWebSocketManager implements WebSocketManager {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ import 'dart:async';

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:wrestling_scoreboard_common/common.dart';

class Preferences {
static const keyLocale = 'locale';
static const keyThemeMode = 'theme-mode';
static const keyApiUrl = 'api-url';
static const keyWsUrl = 'ws-url';
static const keyApiProvider = 'api-provider';
static const keyReportProvider = 'report-provider';

/// Network timeout in milliseconds.
static const keyNetworkTimeout = 'network-timeout';
Expand All @@ -22,8 +19,6 @@ class Preferences {
static final StreamController<ThemeMode> onChangeThemeMode = StreamController.broadcast();
static final StreamController<String> onChangeApiUrl = StreamController.broadcast();
static final StreamController<String> onChangeWsUrlWebSocket = StreamController.broadcast();
static final StreamController<WrestlingApiProvider?> onChangeApiProvider = StreamController.broadcast();
static final StreamController<WrestlingReportProvider?> onChangeReportProvider = StreamController.broadcast();
static final StreamController<Duration> onChangeNetworkTimeout = StreamController.broadcast();
static final StreamController<String> onChangeBellSound = StreamController.broadcast();
static final StreamController<String?> onChangeFontFamily = StreamController.broadcast();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:wrestling_scoreboard_client/provider/local_preferences.dart';
import 'package:wrestling_scoreboard_client/utils/environment.dart';
import 'package:wrestling_scoreboard_common/common.dart';

part 'local_preferences_provider.g.dart';

Expand Down Expand Up @@ -96,42 +95,6 @@ class ApiUrlNotifier extends _$ApiUrlNotifier {
}
}

@riverpod
class ApiProviderNotifier extends _$ApiProviderNotifier {
@override
Raw<Future<WrestlingApiProvider?>> build() async {
Preferences.onChangeApiProvider.stream.distinct().listen((event) {
state = Future.value(event);
});
final apiProviderStr = await Preferences.getString(Preferences.keyApiProvider);
final WrestlingApiProvider? apiProvider;
if (apiProviderStr != null) {
apiProvider = WrestlingApiProvider.values.byName(apiProviderStr);
} else {
apiProvider = null;
}
return apiProvider;
}
}

@riverpod
class ReportProviderNotifier extends _$ReportProviderNotifier {
@override
Raw<Future<WrestlingReportProvider?>> build() async {
Preferences.onChangeReportProvider.stream.distinct().listen((event) {
state = Future.value(event);
});
final apiProviderStr = await Preferences.getString(Preferences.keyReportProvider);
final WrestlingReportProvider? apiProvider;
if (apiProviderStr != null) {
apiProvider = WrestlingReportProvider.values.byName(apiProviderStr);
} else {
apiProvider = null;
}
return apiProvider;
}
}

@Riverpod(keepAlive: true)
class BellSoundNotifier extends _$BellSoundNotifier {
@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
Expand Up @@ -79,6 +79,8 @@ abstract class DataManager {

Future<void> resetDatabase();

Future<void> organizationImport(int id);

final Map<Type, StreamController<DataObject>> _singleStreamControllers = {};
final Map<Type, Map<Type, StreamController<ManyDataObject<dynamic>>>> _manyStreamControllers = {};
final Map<Type, StreamController<Map<String, dynamic>>> _singleRawStreamControllers = {};
Expand Down
10 changes: 10 additions & 0 deletions wrestling_scoreboard_client/lib/services/network/remote/rest.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ class RestDataManager extends DataManager {
set webSocketManager(WebSocketManager manager) {
_webSocketManager = manager;
}

@override
Future<void> organizationImport(int id) async {
final uri = Uri.parse('$_apiUrl/organization/$id/api/import');
final response = await http.post(uri);
if (response.statusCode != 200) {
throw Exception(
'Failed to import from organization $id: ${response.reasonPhrase ?? response.statusCode.toString()}');
}
}
}

class RestException implements Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ class _OrganizationEditState extends ConsumerState<OrganizationEdit> {
String? _name;
String? _abbreviation;
Organization? _parent;
WrestlingApiProvider? _apiProvider;
WrestlingReportProvider? _reportProvider;

@override
void initState() {
_parent = widget.organization?.parent ?? widget.initialParent;
_apiProvider = widget.organization?.apiProvider;
_reportProvider = widget.organization?.reportProvider;
super.initState();
}

Expand Down Expand Up @@ -73,7 +77,7 @@ class _OrganizationEditState extends ConsumerState<OrganizationEdit> {
onSaved: (Organization? value) => setState(() {
_parent = value;
}),
allowEmpty: false,
allowEmpty: true,
itemAsString: (u) => u.name,
onFind: (String? filter) async {
_availableOrganizations ??=
Expand All @@ -85,6 +89,44 @@ class _OrganizationEditState extends ConsumerState<OrganizationEdit> {
},
),
),
ListTile(
leading: const Icon(Icons.api),
title: ButtonTheme(
alignedDropdown: true,
child: SimpleDropdown<WrestlingApiProvider>(
isNullable: true,
hint: localizations.apiProvider,
isExpanded: true,
options: WrestlingApiProvider.values.map((value) => MapEntry(
value,
Text(value.name),
)),
selected: _apiProvider,
onChange: (newValue) => setState(() {
_apiProvider = newValue;
}),
),
),
),
ListTile(
leading: const Icon(Icons.description),
title: ButtonTheme(
alignedDropdown: true,
child: SimpleDropdown<WrestlingReportProvider>(
isNullable: true,
hint: localizations.apiProvider,
isExpanded: true,
options: WrestlingReportProvider.values.map((value) => MapEntry(
value,
Text(value.name),
)),
selected: _reportProvider,
onChange: (newValue) => setState(() {
_reportProvider = newValue;
}),
),
),
),
];

return Form(
Expand All @@ -106,6 +148,8 @@ class _OrganizationEditState extends ConsumerState<OrganizationEdit> {
name: _name!,
abbreviation: _abbreviation,
parent: _parent,
reportProvider: _reportProvider,
apiProvider: _apiProvider,
));
navigator.pop();
}
Expand Down
Loading

0 comments on commit fea89a8

Please sign in to comment.