Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Commit

Permalink
Merge branch 'main' into mo/bloc
Browse files Browse the repository at this point in the history
  • Loading branch information
bisgardo committed Nov 15, 2023
2 parents ab00906 + 8e47cec commit 0d8e6af
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
11 changes: 7 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ void main() {
runApp(const App());
}

// In the future, this will be loaded from a proper source rather than being hardcoded.
final config = Config.ofNetworks([
const Network(
name: NetworkName.testnet,
Expand All @@ -23,7 +24,6 @@ final config = Config.ofNetworks([
),
),
]);
const httpService = HttpService();

class App extends StatelessWidget {
const App({super.key});
Expand All @@ -41,12 +41,15 @@ class App extends StatelessWidget {
// (including the blocs created in the child provider).
return RepositoryProvider(
create: (_) {
final testnet = config.availableNetworks[NetworkName.testnet]!;
final prefsSvc = SharedPreferencesService(prefs);
const httpService = HttpService();
// TODO 'enableNetwork' is async so should be initialized in a 'FutureBuilder'.
// Currently it actually is sync though, so this change can wait a bit.
return ServiceRepository(
networkServices: {testnet: NetworkServices.forNetwork(testnet, httpService: httpService)},
config: config,
httpService: httpService,
sharedPreferences: prefsSvc,
);
)..enableNetwork(NetworkName.testnet);
},
child: MultiBlocProvider(
providers: [
Expand Down
33 changes: 31 additions & 2 deletions lib/state/services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,39 @@ class NetworkServices {
/// Collection of all services available to the app.
class ServiceRepository {
/// Service collections for all "enabled" networks (as defined in [Config.availableNetworks]).
final Map<Network, NetworkServices> networkServices;
final Map<Network, NetworkServices> networkServices = {};

/// Global configuration used when starting services.
final Config config;

/// Global service for performing HTTP calls.
final HttpService httpService;

/// Global service for interacting with shared preferences.
final SharedPreferencesService sharedPreferences;

const ServiceRepository({required this.networkServices, required this.sharedPreferences});
ServiceRepository({required this.config, required this.httpService, required this.sharedPreferences});

/// Enable the network with the provided name.
///
/// The services for interacting with the network are initialized using the global configuration.
/// Once the future completes, the services may be looked up by the network name in [networkServices].
Future<void> enableNetwork(NetworkName name) async {
final n = config.availableNetworks[name];
if (n == null) {
throw Exception('unknown network');
}
return _enableNetwork(n);
}

void _enableNetwork(Network n) {
if (networkServices.containsKey(n)) {
throw Exception('network is already enabled');
}
_setNetwork(n, NetworkServices.forNetwork(n, httpService: httpService));
}

void _setNetwork(Network n, NetworkServices services) {
networkServices[n] = services;
}
}

0 comments on commit 0d8e6af

Please sign in to comment.