Move service logic out of main

This commit is contained in:
Oystein Kristoffer Tveit 2022-01-25 20:15:35 +01:00
parent 76f8cc6c86
commit 241ff1cab6
4 changed files with 39 additions and 33 deletions

View File

@ -1,43 +1,13 @@
import 'dart:convert';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'package:get_it/get_it.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sembast/sembast.dart';
import 'package:sembast/sembast_io.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'bloc/theme/theme_bloc.dart';
import 'routing/router.dart';
import 'services/database.dart';
import 'services/licenses.dart';
import 'services/preferences.dart';
import 'settings.dart';
Future<void> setupDatabase() async {
final Directory appDocDir = await getApplicationDocumentsDirectory();
if (!appDocDir.existsSync()) appDocDir.createSync(recursive: true);
final Database database =
await databaseFactoryIo.openDatabase(join(appDocDir.path, 'sembast.db'));
GetIt.instance.registerSingleton<Database>(database);
}
Future<void> setupSharedPreferences() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
GetIt.instance.registerSingleton<SharedPreferences>(prefs);
}
void registerExtraLicenses() => LicenseRegistry.addLicense(() async* {
final jsonString = await rootBundle.loadString('assets/licenses.json');
final Map<String, dynamic> jsonData = jsonDecode(jsonString);
for (final license in jsonData.entries)
yield LicenseEntryWithLineBreaks(
[license.key],
await rootBundle.loadString(license.value as String),
);
});
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();

View File

@ -0,0 +1,15 @@
import 'dart:io';
import 'package:get_it/get_it.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sembast/sembast.dart';
import 'package:sembast/sembast_io.dart';
Future<void> setupDatabase() async {
final Directory appDocDir = await getApplicationDocumentsDirectory();
if (!appDocDir.existsSync()) appDocDir.createSync(recursive: true);
final Database database =
await databaseFactoryIo.openDatabase(join(appDocDir.path, 'sembast.db'));
GetIt.instance.registerSingleton<Database>(database);
}

View File

@ -0,0 +1,14 @@
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart' show rootBundle;
void registerExtraLicenses() => LicenseRegistry.addLicense(() async* {
final jsonString = await rootBundle.loadString('assets/licenses.json');
final Map<String, dynamic> jsonData = jsonDecode(jsonString);
for (final license in jsonData.entries)
yield LicenseEntryWithLineBreaks(
[license.key],
await rootBundle.loadString(license.value as String),
);
});

View File

@ -0,0 +1,7 @@
import 'package:get_it/get_it.dart';
import 'package:shared_preferences/shared_preferences.dart';
Future<void> setupSharedPreferences() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
GetIt.instance.registerSingleton<SharedPreferences>(prefs);
}