nc-photos/lib/main.dart

173 lines
5 KiB
Dart
Raw Normal View History

2021-04-10 06:28:12 +02:00
import 'package:equatable/equatable.dart';
import 'package:event_bus/event_bus.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
2022-01-17 11:59:40 +01:00
import 'package:flutter/services.dart';
2021-04-10 06:28:12 +02:00
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:kiwi/kiwi.dart';
import 'package:logging/logging.dart';
import 'package:nc_photos/app_db.dart';
import 'package:nc_photos/debug_util.dart';
import 'package:nc_photos/di_container.dart';
import 'package:nc_photos/entity/album.dart';
import 'package:nc_photos/entity/face.dart';
import 'package:nc_photos/entity/face/data_source.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/entity/file/data_source.dart';
import 'package:nc_photos/entity/person.dart';
import 'package:nc_photos/entity/person/data_source.dart';
import 'package:nc_photos/entity/share.dart';
import 'package:nc_photos/entity/share/data_source.dart';
import 'package:nc_photos/entity/sharee.dart';
import 'package:nc_photos/entity/sharee/data_source.dart';
2021-04-16 10:45:17 +02:00
import 'package:nc_photos/k.dart' as k;
import 'package:nc_photos/mobile/android/android_info.dart';
import 'package:nc_photos/mobile/self_signed_cert_manager.dart';
import 'package:nc_photos/platform/features.dart' as features;
import 'package:nc_photos/platform/k.dart' as platform_k;
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/pref.dart';
2021-12-05 13:02:22 +01:00
import 'package:nc_photos/pref_util.dart' as pref_util;
2021-04-17 10:42:57 +02:00
import 'package:nc_photos/widget/my_app.dart';
2021-04-10 06:28:12 +02:00
void main() async {
WidgetsFlutterBinding.ensureInitialized();
_initLog();
2021-10-02 14:49:30 +02:00
_initKiwi();
2021-04-10 06:28:12 +02:00
await _initPref();
2021-12-05 13:02:22 +01:00
await _initAccountPrefs();
await _initDeviceInfo();
2021-04-10 06:28:12 +02:00
_initBloc();
_initEquatable();
if (features.isSupportSelfSignedCert) {
_initSelfSignedCertManager();
}
_initDiContainer();
2021-04-10 06:28:12 +02:00
2022-01-17 11:59:40 +01:00
if (platform_k.isMobile) {
// reset orientation override just in case, see #59
SystemChrome.setPreferredOrientations([]);
}
2021-09-15 08:58:06 +02:00
runApp(const MyApp());
2021-04-10 06:28:12 +02:00
}
void _initLog() {
if (kDebugMode) {
debugPrintGestureArenaDiagnostics = true;
}
2021-04-20 12:53:41 +02:00
Logger.root.level = kReleaseMode ? Level.WARNING : Level.ALL;
2021-04-10 06:28:12 +02:00
Logger.root.onRecord.listen((record) {
// dev.log(
// "${record.level.name} ${record.time}: ${record.message}",
// time: record.time,
// sequenceNumber: record.sequenceNumber,
// level: record.level.value,
// name: record.loggerName,
// );
String msg =
"[${record.loggerName}] ${record.level.name} ${record.time}: ${record.message}";
if (record.error != null) {
msg += " (throw: ${record.error.runtimeType} { ${record.error} })";
}
if (record.stackTrace != null) {
msg += "\nStack Trace:\n${record.stackTrace}";
}
2021-06-14 21:05:59 +02:00
if (kDebugMode) {
// show me colors!
int color;
if (record.level >= Level.SEVERE) {
color = 91;
} else if (record.level >= Level.WARNING) {
color = 33;
} else if (record.level >= Level.INFO) {
color = 34;
2021-08-15 19:06:43 +02:00
} else if (record.level >= Level.FINER) {
2021-06-14 21:05:59 +02:00
color = 32;
} else {
color = 90;
}
msg = "\x1B[${color}m$msg\x1B[0m";
}
2021-04-10 06:28:12 +02:00
debugPrint(msg);
LogCapturer().onLog(msg);
2021-04-10 06:28:12 +02:00
});
}
2021-04-16 10:45:17 +02:00
Future<void> _initPref() async {
final provider = PrefSharedPreferencesProvider();
await provider.init();
final pref = Pref.scoped(provider);
Pref.setGlobalInstance(pref);
2021-10-27 22:40:54 +02:00
if (Pref().getLastVersion() == null) {
if (Pref().getSetupProgress() == null) {
2021-04-16 10:45:17 +02:00
// new install
2021-10-27 22:40:54 +02:00
await Pref().setLastVersion(k.version);
2021-04-16 10:45:17 +02:00
} else {
// v6 is the last version without saving the version number in pref
2021-10-27 22:40:54 +02:00
await Pref().setLastVersion(6);
2021-04-16 10:45:17 +02:00
}
}
}
2021-04-10 06:28:12 +02:00
2021-12-05 13:02:22 +01:00
Future<void> _initAccountPrefs() async {
for (final a in Pref().getAccounts3Or([])) {
try {
AccountPref.setGlobalInstance(a, await pref_util.loadAccountPref(a));
} catch (e, stackTrace) {
_log.shout("[_initAccountPrefs] Failed reading pref for account: $a", e,
stackTrace);
}
}
}
Future<void> _initDeviceInfo() async {
if (platform_k.isAndroid) {
await AndroidInfo.init();
}
}
2021-04-10 06:28:12 +02:00
void _initBloc() {
Bloc.observer = _BlocObserver();
}
void _initKiwi() {
final kiwi = KiwiContainer();
kiwi.registerInstance<EventBus>(EventBus());
}
void _initEquatable() {
EquatableConfig.stringify = false;
}
void _initSelfSignedCertManager() {
SelfSignedCertManager().init();
}
void _initDiContainer() {
KiwiContainer().registerInstance<DiContainer>(DiContainer(
albumRepo: AlbumRepo(AlbumCachedDataSource(AppDb())),
faceRepo: const FaceRepo(FaceRemoteDataSource()),
fileRepo: FileRepo(FileCachedDataSource(AppDb())),
personRepo: const PersonRepo(PersonRemoteDataSource()),
shareRepo: ShareRepo(ShareRemoteDataSource()),
shareeRepo: ShareeRepo(ShareeRemoteDataSource()),
appDb: AppDb(),
pref: Pref(),
));
}
2021-04-10 06:28:12 +02:00
class _BlocObserver extends BlocObserver {
@override
onChange(BlocBase bloc, Change change) {
super.onChange(bloc, change);
_log.finer("${bloc.runtimeType} $change");
}
2021-12-05 12:01:50 +01:00
static final _log = Logger("main._BlocObserver");
2021-04-10 06:28:12 +02:00
}
2021-12-05 13:02:22 +01:00
final _log = Logger("main");