refactored init & added content blocker init
This commit is contained in:
@@ -1,6 +1,10 @@
|
|||||||
import 'package:bang_navigator/features/about/data/repositories/package_info_repository.dart';
|
import 'package:bang_navigator/features/about/data/repositories/package_info_repository.dart';
|
||||||
|
import 'package:bang_navigator/features/bangs/data/models/bang.dart';
|
||||||
import 'package:bang_navigator/features/bangs/domain/repositories/sync.dart';
|
import 'package:bang_navigator/features/bangs/domain/repositories/sync.dart';
|
||||||
|
import 'package:bang_navigator/features/content_block/data/models/host.dart';
|
||||||
|
import 'package:bang_navigator/features/content_block/domain/repositories/sync.dart';
|
||||||
import 'package:bang_navigator/features/search_browser/domain/services/session.dart';
|
import 'package:bang_navigator/features/search_browser/domain/services/session.dart';
|
||||||
|
import 'package:bang_navigator/features/settings/data/models/settings.dart';
|
||||||
import 'package:bang_navigator/features/settings/data/repositories/settings_repository.dart';
|
import 'package:bang_navigator/features/settings/data/repositories/settings_repository.dart';
|
||||||
import 'package:exceptions/exceptions.dart';
|
import 'package:exceptions/exceptions.dart';
|
||||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||||
@@ -10,58 +14,95 @@ part 'app_initialization.g.dart';
|
|||||||
@Riverpod(keepAlive: true)
|
@Riverpod(keepAlive: true)
|
||||||
class AppInitializationService extends _$AppInitializationService {
|
class AppInitializationService extends _$AppInitializationService {
|
||||||
/// Will de facto restart the app
|
/// Will de facto restart the app
|
||||||
Future<void> reinitialize() async {
|
Future<void> reinitialize() {
|
||||||
ref.invalidateSelf();
|
ref.invalidateSelf();
|
||||||
return initialize();
|
return initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _initPackageInfo() {
|
||||||
|
//Ensure Package info is loaded
|
||||||
|
state = Result.success(
|
||||||
|
(
|
||||||
|
initialized: false,
|
||||||
|
stage: 'Loading Package Info...',
|
||||||
|
errors: List.empty(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
return ref.read(packageInfoProvider.future);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<Map<BangGroup, Result<void>>> _initBangs() {
|
||||||
|
state = Result.success(
|
||||||
|
(
|
||||||
|
initialized: false,
|
||||||
|
stage: 'Synchronizing Bangs...',
|
||||||
|
errors: List.empty(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
return ref
|
||||||
|
.read(bangSyncRepositoryProvider.notifier)
|
||||||
|
.syncBangGroups(syncInterval: const Duration(days: 7));
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<Map<HostSource, Result<void>>> _initHosts(Settings settings) async {
|
||||||
|
if (settings.enableContentBlocking) {
|
||||||
|
state = Result.success(
|
||||||
|
(
|
||||||
|
initialized: false,
|
||||||
|
stage: 'Synchronizing Ad & Content Blocking Lists...',
|
||||||
|
errors: List.empty(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
return ref.read(hostSyncRepositoryProvider.notifier).syncHostSources(
|
||||||
|
sources: settings.enableHostList,
|
||||||
|
syncInterval: const Duration(days: 7),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _initIncognito(Settings settings) async {
|
||||||
|
state = Result.success(
|
||||||
|
(
|
||||||
|
initialized: false,
|
||||||
|
stage: 'Enforcing Privacy...',
|
||||||
|
errors: List.empty(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (settings.incognitoMode) {
|
||||||
|
await ref.read(sessionServiceProvider.notifier).clearAllData();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (settings.kagiSession case final String session) {
|
||||||
|
if (session.isNotEmpty) {
|
||||||
|
await ref.read(sessionServiceProvider.notifier).setKagiSession(session);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> initialize() async {
|
Future<void> initialize() async {
|
||||||
state = await Result.fromAsync(() async {
|
state = await Result.fromAsync(() async {
|
||||||
|
final settings = await ref.read(settingsRepositoryProvider.future);
|
||||||
final errors = <ErrorMessage>[];
|
final errors = <ErrorMessage>[];
|
||||||
|
|
||||||
//Ensure Package info is loaded
|
await _initPackageInfo();
|
||||||
state = Result.success(
|
|
||||||
(
|
|
||||||
initialized: false,
|
|
||||||
stage: 'Loading Package Info...',
|
|
||||||
errors: List.empty()
|
|
||||||
),
|
|
||||||
);
|
|
||||||
await ref.read(packageInfoProvider.future);
|
|
||||||
|
|
||||||
state = Result.success(
|
final bangSyncResults = await _initBangs();
|
||||||
(
|
for (final MapEntry(value: result) in bangSyncResults.entries) {
|
||||||
initialized: false,
|
|
||||||
stage: 'Synchronizing Bangs...',
|
|
||||||
errors: List.empty()
|
|
||||||
),
|
|
||||||
);
|
|
||||||
final syncResults = await ref
|
|
||||||
.read(bangSyncRepositoryProvider.notifier)
|
|
||||||
.syncAllBangGroups(syncInterval: const Duration(days: 7));
|
|
||||||
for (final MapEntry(value: result) in syncResults.entries) {
|
|
||||||
result.onFailure(errors.add);
|
result.onFailure(errors.add);
|
||||||
}
|
}
|
||||||
|
|
||||||
state = Result.success(
|
final hostSyncResults = await _initHosts(settings);
|
||||||
(
|
for (final MapEntry(value: result) in hostSyncResults.entries) {
|
||||||
initialized: false,
|
result.onFailure(errors.add);
|
||||||
stage: 'Enforcing Privacy...',
|
|
||||||
errors: List.empty()
|
|
||||||
),
|
|
||||||
);
|
|
||||||
final settings = await ref.read(settingsRepositoryProvider.future);
|
|
||||||
if (settings.incognitoMode) {
|
|
||||||
await ref.read(sessionServiceProvider.notifier).clearAllData();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (settings.kagiSession case final String session) {
|
await _initIncognito(settings);
|
||||||
if (session.isNotEmpty) {
|
|
||||||
await ref
|
|
||||||
.read(sessionServiceProvider.notifier)
|
|
||||||
.setKagiSession(session);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return (initialized: true, stage: null, errors: errors);
|
return (initialized: true, stage: null, errors: errors);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ part of 'app_initialization.dart';
|
|||||||
// **************************************************************************
|
// **************************************************************************
|
||||||
|
|
||||||
String _$appInitializationServiceHash() =>
|
String _$appInitializationServiceHash() =>
|
||||||
r'9d970ee0c347a1fc89bea6546956a51ba8d2d711';
|
r'0588d175aae7eb1734e9d6eb17d40a05ba88757e';
|
||||||
|
|
||||||
/// See also [AppInitializationService].
|
/// See also [AppInitializationService].
|
||||||
@ProviderFor(AppInitializationService)
|
@ProviderFor(AppInitializationService)
|
||||||
|
|||||||
Reference in New Issue
Block a user