Add recommended geo assets

This commit is contained in:
problematicconsumer
2023-11-17 23:44:32 +03:30
parent 436fc8133c
commit a728280249
12 changed files with 96 additions and 6 deletions

View File

@@ -12,6 +12,10 @@ class GeoAssetsDao extends DatabaseAccessor<AppDatabase>
with _$GeoAssetsDaoMixin, InfraLogger {
GeoAssetsDao(super.db);
Future<void> add(GeoAsset geoAsset) async {
await into(geoAssetEntries).insert(geoAsset.toCompanion());
}
Future<GeoAsset?> getActive(GeoAssetType type) async {
return (geoAssetEntries.select()
..where((tbl) => tbl.active.equals(true))

View File

@@ -146,4 +146,23 @@ class GeoAssetsRepositoryImpl
GeoAssetFailure.unexpected,
);
}
@override
TaskEither<GeoAssetFailure, Unit> addRecommended() {
return exceptionHandler(
() async {
final persistedIds = await geoAssetsDao
.watchAll()
.first
.then((value) => value.map((e) => e.id));
final missing =
recommendedGeoAssets.where((e) => !persistedIds.contains(e.id));
for (final geoAsset in missing) {
await geoAssetsDao.add(geoAsset);
}
return right(unit);
},
GeoAssetFailure.unexpected,
);
}
}

View File

@@ -49,3 +49,21 @@ const defaultGeosite = GeoAsset(
);
const defaultGeoAssets = [defaultGeoip, defaultGeosite];
const recommendedGeoAssets = [
...defaultGeoAssets,
GeoAsset(
id: "chocolate4U-geoip",
name: "geoip.db",
type: GeoAssetType.geoip,
active: false,
providerName: "Chocolate4U/Iran-sing-box-rules",
),
GeoAsset(
id: "chocolate4U-geosite",
name: "geosite.db",
type: GeoAssetType.geosite,
active: false,
providerName: "Chocolate4U/Iran-sing-box-rules",
),
];

View File

@@ -11,4 +11,6 @@ abstract interface class GeoAssetsRepository {
TaskEither<GeoAssetFailure, Unit> update(GeoAsset geoAsset);
TaskEither<GeoAssetFailure, Unit> markAsActive(GeoAsset geoAsset);
TaskEither<GeoAssetFailure, Unit> addRecommended();
}

View File

@@ -89,6 +89,11 @@ class GeoAssetTile extends HookConsumerWidget {
],
),
selected: geoAsset.active,
onTap: () async {
await ref
.read(geoAssetsNotifierProvider.notifier)
.markAsActive(geoAsset);
},
trailing: PopupMenuButton(
itemBuilder: (context) {
return [

View File

@@ -20,7 +20,28 @@ class GeoAssetsNotifier extends _$GeoAssetsNotifier with AppLogger {
Future<void> updateGeoAsset(GeoAsset geoAsset) async {
await ref.read(geoAssetsRepositoryProvider).update(geoAsset).getOrElse(
(f) {
loggy.warning("error updating profile", f);
loggy.warning("error updating geo asset", f);
throw f;
},
).run();
}
Future<void> markAsActive(GeoAsset geoAsset) async {
await ref
.read(geoAssetsRepositoryProvider)
.markAsActive(geoAsset)
.getOrElse(
(f) {
loggy.warning("error marking geo asset as active", f);
throw f;
},
).run();
}
Future<void> addRecommended() async {
await ref.read(geoAssetsRepositoryProvider).addRecommended().getOrElse(
(f) {
loggy.warning("error adding recommended geo assets", f);
throw f;
},
).run();

View File

@@ -17,6 +17,22 @@ class GeoAssetsPage extends HookConsumerWidget {
slivers: [
SliverAppBar(
title: Text(t.settings.geoAssets.pageTitle),
actions: [
PopupMenuButton(
itemBuilder: (context) {
return [
PopupMenuItem(
child: Text(t.settings.geoAssets.addRecommended),
onTap: () {
ref
.read(geoAssetsNotifierProvider.notifier)
.addRecommended();
},
),
];
},
),
],
),
switch (state) {
AsyncData(value: final geoAssets) => SliverList.builder(