nc-photos/app/lib/app_init.dart

234 lines
8.4 KiB
Dart
Raw Normal View History

2022-03-27 19:55:31 +02:00
import 'package:equatable/equatable.dart';
import 'package:event_bus/event_bus.dart';
import 'package:flutter/foundation.dart';
import 'package:kiwi/kiwi.dart';
import 'package:logging/logging.dart';
import 'package:nc_photos/di_container.dart';
import 'package:nc_photos/entity/album.dart';
import 'package:nc_photos/entity/album/data_source.dart';
import 'package:nc_photos/entity/album/data_source2.dart';
import 'package:nc_photos/entity/album/repo2.dart';
2023-07-03 19:23:42 +02:00
import 'package:nc_photos/entity/face_recognition_person/data_source.dart';
import 'package:nc_photos/entity/face_recognition_person/repo.dart';
2022-03-27 19:55:31 +02:00
import 'package:nc_photos/entity/favorite.dart';
import 'package:nc_photos/entity/favorite/data_source.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/entity/file/data_source.dart';
import 'package:nc_photos/entity/file/data_source2.dart';
import 'package:nc_photos/entity/file/repo.dart';
import 'package:nc_photos/entity/image_location/data_source.dart';
import 'package:nc_photos/entity/image_location/repo.dart';
2022-05-05 16:06:47 +02:00
import 'package:nc_photos/entity/local_file.dart';
import 'package:nc_photos/entity/local_file/data_source.dart';
import 'package:nc_photos/entity/nc_album/data_source.dart';
import 'package:nc_photos/entity/nc_album/repo.dart';
2023-07-17 09:35:45 +02:00
import 'package:nc_photos/entity/pref.dart';
2024-05-24 21:35:58 +02:00
import 'package:nc_photos/entity/pref/provider/secure_storage.dart';
2023-07-17 09:35:45 +02:00
import 'package:nc_photos/entity/pref/provider/shared_preferences.dart';
import 'package:nc_photos/entity/pref_util.dart' as pref_util;
2023-07-16 13:30:23 +02:00
import 'package:nc_photos/entity/recognize_face/data_source.dart';
import 'package:nc_photos/entity/recognize_face/repo.dart';
2022-08-06 06:21:11 +02:00
import 'package:nc_photos/entity/search.dart';
import 'package:nc_photos/entity/search/data_source.dart';
2022-03-27 19:55:31 +02:00
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';
import 'package:nc_photos/entity/tag.dart';
import 'package:nc_photos/entity/tag/data_source.dart';
import 'package:nc_photos/entity/tagged_file.dart';
import 'package:nc_photos/entity/tagged_file/data_source.dart';
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/session_storage.dart';
import 'package:nc_photos/touch_manager.dart';
import 'package:np_db/np_db.dart';
2023-09-12 18:29:44 +02:00
import 'package:np_gps_map/np_gps_map.dart';
2024-07-30 19:34:06 +02:00
import 'package:np_http/np_http.dart';
2023-08-17 18:36:51 +02:00
import 'package:np_log/np_log.dart' as np_log;
2023-08-27 12:58:05 +02:00
import 'package:np_platform_util/np_platform_util.dart';
2022-06-12 07:45:51 +02:00
import 'package:visibility_detector/visibility_detector.dart';
2022-03-27 19:55:31 +02:00
enum InitIsolateType {
main,
2022-07-28 11:13:39 +02:00
/// Isolates with Flutter engine, e.g., those spawned by flutter_isolate or
/// flutter_background_service
flutterIsolate,
}
Future<void> init(InitIsolateType isolateType) async {
2022-06-06 10:59:42 +02:00
if (_hasInitedInThisIsolate) {
2022-08-20 12:31:54 +02:00
_log.warning("[init] Already initialized in this isolate");
2022-06-06 10:59:42 +02:00
return;
}
initLog();
await _initDeviceInfo();
2022-03-27 19:55:31 +02:00
_initKiwi();
await _initPref();
await _initAccountPrefs();
_initEquatable();
if (features.isSupportSelfSignedCert) {
await _initSelfSignedCertManager();
2022-03-27 19:55:31 +02:00
}
2024-08-05 19:22:46 +02:00
await initHttp(
appVersion: k.versionStr,
isNewHttpEngine: Pref().isNewHttpEngine() ?? false,
);
await _initDiContainer(isolateType);
2022-06-12 07:45:51 +02:00
_initVisibilityDetector();
initGpsMap();
// init session storage
SessionStorage();
2022-06-06 10:59:42 +02:00
_hasInitedInThisIsolate = true;
2022-03-27 19:55:31 +02:00
}
2022-06-06 10:59:42 +02:00
void initLog() {
if (_hasInitedInThisIsolate) {
return;
}
2023-08-17 18:36:51 +02:00
np_log.initLog(
isDebugMode: np_log.isDevMode,
2023-08-17 18:36:51 +02:00
print: (log) => debugPrint(log, wrapWidth: 1024),
);
2022-03-27 19:55:31 +02:00
}
Future<void> _initPref() async {
final provider = PrefSharedPreferencesProvider();
await provider.init();
final pref = Pref.scoped(provider);
Pref.setGlobalInstance(pref);
if (Pref().getLastVersion() == null) {
if (Pref().getSetupProgress() == null) {
// new install
await Pref().setLastVersion(k.version);
} else {
// v6 is the last version without saving the version number in pref
await Pref().setLastVersion(6);
}
}
}
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 {
2023-08-27 12:58:05 +02:00
if (getRawPlatform() == NpPlatform.android) {
2022-03-27 19:55:31 +02:00
await AndroidInfo.init();
}
}
void _initKiwi() {
final kiwi = KiwiContainer();
kiwi.registerInstance<EventBus>(EventBus());
}
void _initEquatable() {
EquatableConfig.stringify = false;
}
Future<void> _initSelfSignedCertManager() async {
try {
return SelfSignedCertManager().init();
} catch (e, stackTrace) {
_log.shout("[_initSelfSignedCertManager] Failed to load self signed certs",
e, stackTrace);
}
2022-03-27 19:55:31 +02:00
}
Future<void> _initDiContainer(InitIsolateType isolateType) async {
final c = DiContainer.late();
c.pref = Pref();
2024-05-24 21:35:58 +02:00
c.securePref = await _createSecurePref();
c.npDb = await _createDb(isolateType);
c.albumRepo = AlbumRepo(AlbumCachedDataSource(c));
c.albumRepoRemote = AlbumRepo(AlbumRemoteDataSource());
c.albumRepoLocal = AlbumRepo(AlbumSqliteDbDataSource(c));
c.albumRepo2 = CachedAlbumRepo2(
const AlbumRemoteDataSource2(), AlbumSqliteDbDataSource2(c.npDb));
c.albumRepo2Remote = const BasicAlbumRepo2(AlbumRemoteDataSource2());
c.albumRepo2Local = BasicAlbumRepo2(AlbumSqliteDbDataSource2(c.npDb));
c.fileRepo = FileRepo(FileCachedDataSource(c));
c.fileRepoRemote = const FileRepo(FileWebdavDataSource());
c.fileRepoLocal = FileRepo(FileSqliteDbDataSource(c));
c.fileRepo2 =
CachedFileRepo(const FileRemoteDataSource(), FileNpDbDataSource(c.npDb));
c.fileRepo2Remote = const BasicFileRepo(FileRemoteDataSource());
c.fileRepo2Local = BasicFileRepo(FileNpDbDataSource(c.npDb));
c.shareRepo = ShareRepo(ShareRemoteDataSource());
c.shareeRepo = ShareeRepo(ShareeRemoteDataSource());
c.favoriteRepo = const FavoriteRepo(FavoriteRemoteDataSource());
c.tagRepo = const TagRepo(TagRemoteDataSource());
2022-08-05 11:28:28 +02:00
c.tagRepoRemote = const TagRepo(TagRemoteDataSource());
c.tagRepoLocal = TagRepo(TagSqliteDbDataSource(c.npDb));
c.taggedFileRepo = const TaggedFileRepo(TaggedFileRemoteDataSource());
c.searchRepo = SearchRepo(SearchSqliteDbDataSource(c));
c.ncAlbumRepo = CachedNcAlbumRepo(
const NcAlbumRemoteDataSource(), NcAlbumSqliteDbDataSource(c.npDb));
c.ncAlbumRepoRemote = const BasicNcAlbumRepo(NcAlbumRemoteDataSource());
c.ncAlbumRepoLocal = BasicNcAlbumRepo(NcAlbumSqliteDbDataSource(c.npDb));
2023-07-04 19:34:39 +02:00
c.faceRecognitionPersonRepo = const BasicFaceRecognitionPersonRepo(
FaceRecognitionPersonRemoteDataSource());
c.faceRecognitionPersonRepoRemote = const BasicFaceRecognitionPersonRepo(
FaceRecognitionPersonRemoteDataSource());
c.faceRecognitionPersonRepoLocal = BasicFaceRecognitionPersonRepo(
FaceRecognitionPersonSqliteDbDataSource(c.npDb));
2023-07-16 13:30:23 +02:00
c.recognizeFaceRepo =
const BasicRecognizeFaceRepo(RecognizeFaceRemoteDataSource());
c.recognizeFaceRepoRemote =
const BasicRecognizeFaceRepo(RecognizeFaceRemoteDataSource());
c.recognizeFaceRepoLocal =
BasicRecognizeFaceRepo(RecognizeFaceSqliteDbDataSource(c.npDb));
c.imageLocationRepo =
BasicImageLocationRepo(ImageLocationNpDbDataSource(c.npDb));
c.touchManager = TouchManager(c);
2023-08-27 12:58:05 +02:00
if (getRawPlatform() == NpPlatform.android) {
2022-05-05 16:06:47 +02:00
// local file currently only supported on Android
c.localFileRepo = const LocalFileRepo(LocalFileMediaStoreDataSource());
2022-05-05 16:06:47 +02:00
}
KiwiContainer().registerInstance<DiContainer>(c);
2022-03-27 19:55:31 +02:00
}
2022-06-12 07:45:51 +02:00
void _initVisibilityDetector() {
VisibilityDetectorController.instance.updateInterval = Duration.zero;
}
Future<NpDb> _createDb(InitIsolateType isolateType) async {
final npDb = NpDb();
2023-12-11 16:46:37 +01:00
final androidSdk =
getRawPlatform() == NpPlatform.android ? AndroidInfo().sdkInt : null;
if (isolateType == InitIsolateType.main) {
2023-12-11 16:46:37 +01:00
await npDb.initMainIsolate(androidSdk: androidSdk);
} else {
2023-12-11 16:46:37 +01:00
await npDb.initBackgroundIsolate(androidSdk: androidSdk);
}
return npDb;
}
2024-05-24 21:35:58 +02:00
Future<Pref> _createSecurePref() async {
final provider = PrefSecureStorageProvider();
await provider.init();
return Pref.scoped(provider);
}
2022-03-27 19:55:31 +02:00
final _log = Logger("app_init");
2022-06-06 10:59:42 +02:00
var _hasInitedInThisIsolate = false;