Refactor geo assets
This commit is contained in:
@@ -1,2 +0,0 @@
|
||||
export 'geo_assets_dao.dart';
|
||||
export 'profiles_dao.dart';
|
||||
@@ -1,46 +0,0 @@
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:hiddify/data/local/data_mappers.dart';
|
||||
import 'package:hiddify/data/local/database.dart';
|
||||
import 'package:hiddify/data/local/tables.dart';
|
||||
import 'package:hiddify/domain/rules/geo_asset.dart';
|
||||
import 'package:hiddify/utils/custom_loggers.dart';
|
||||
|
||||
part 'geo_assets_dao.g.dart';
|
||||
|
||||
@DriftAccessor(tables: [GeoAssetEntries])
|
||||
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))
|
||||
..where((tbl) => tbl.type.equalsValue(type))
|
||||
..limit(1))
|
||||
.map(GeoAssetMapper.fromEntry)
|
||||
.getSingleOrNull();
|
||||
}
|
||||
|
||||
Stream<List<GeoAsset>> watchAll() {
|
||||
return geoAssetEntries.select().map(GeoAssetMapper.fromEntry).watch();
|
||||
}
|
||||
|
||||
Future<void> edit(GeoAsset patch) async {
|
||||
await transaction(
|
||||
() async {
|
||||
if (patch.active) {
|
||||
await (update(geoAssetEntries)
|
||||
..where((tbl) => tbl.active.equals(true))
|
||||
..where((tbl) => tbl.type.equalsValue(patch.type)))
|
||||
.write(const GeoAssetEntriesCompanion(active: Value(false)));
|
||||
}
|
||||
await (update(geoAssetEntries)..where((tbl) => tbl.id.equals(patch.id)))
|
||||
.write(patch.toCompanion());
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:hiddify/data/local/database.dart';
|
||||
import 'package:hiddify/domain/profiles/profiles.dart';
|
||||
import 'package:hiddify/domain/rules/geo_asset.dart';
|
||||
|
||||
extension ProfileMapper on Profile {
|
||||
ProfileEntriesCompanion toCompanion() {
|
||||
@@ -72,29 +71,3 @@ extension ProfileMapper on Profile {
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
extension GeoAssetMapper on GeoAsset {
|
||||
GeoAssetEntriesCompanion toCompanion() {
|
||||
return GeoAssetEntriesCompanion.insert(
|
||||
id: id,
|
||||
type: type,
|
||||
active: active,
|
||||
name: name,
|
||||
providerName: providerName,
|
||||
version: Value(version),
|
||||
lastCheck: Value(lastCheck),
|
||||
);
|
||||
}
|
||||
|
||||
static GeoAsset fromEntry(GeoAssetEntry e) {
|
||||
return GeoAsset(
|
||||
id: e.id,
|
||||
name: e.name,
|
||||
type: e.type,
|
||||
active: e.active,
|
||||
providerName: e.providerName,
|
||||
version: e.version,
|
||||
lastCheck: e.lastCheck,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,22 +2,19 @@ import 'dart:io';
|
||||
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:drift/native.dart';
|
||||
import 'package:hiddify/data/local/dao/dao.dart';
|
||||
import 'package:hiddify/data/local/data_mappers.dart';
|
||||
import 'package:hiddify/data/local/schema_versions.dart';
|
||||
import 'package:hiddify/data/local/tables.dart';
|
||||
import 'package:hiddify/data/local/type_converters.dart';
|
||||
import 'package:hiddify/domain/profiles/profiles.dart';
|
||||
import 'package:hiddify/domain/rules/geo_asset.dart';
|
||||
import 'package:hiddify/features/geo_asset/data/geo_asset_data_mapper.dart';
|
||||
import 'package:hiddify/features/geo_asset/model/default_geo_assets.dart';
|
||||
import 'package:hiddify/features/geo_asset/model/geo_asset_entity.dart';
|
||||
import 'package:hiddify/services/files_editor_service.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
|
||||
part 'database.g.dart';
|
||||
|
||||
@DriftDatabase(
|
||||
tables: [ProfileEntries, GeoAssetEntries],
|
||||
daos: [ProfilesDao, GeoAssetsDao],
|
||||
)
|
||||
@DriftDatabase(tables: [ProfileEntries, GeoAssetEntries])
|
||||
class AppDatabase extends _$AppDatabase {
|
||||
AppDatabase({required QueryExecutor connection}) : super(connection);
|
||||
|
||||
@@ -57,7 +54,7 @@ class AppDatabase extends _$AppDatabase {
|
||||
|
||||
Future<void> _prePopulateGeoAssets() async {
|
||||
await transaction(() async {
|
||||
final geoAssets = defaultGeoAssets.map((e) => e.toCompanion());
|
||||
final geoAssets = defaultGeoAssets.map((e) => e.toEntry());
|
||||
for (final geoAsset in geoAssets) {
|
||||
await into(geoAssetEntries).insert(geoAsset);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:hiddify/data/local/type_converters.dart';
|
||||
import 'package:hiddify/domain/profiles/profiles.dart';
|
||||
import 'package:hiddify/domain/rules/geo_asset.dart';
|
||||
import 'package:hiddify/features/geo_asset/model/geo_asset_entity.dart';
|
||||
|
||||
@DataClassName('ProfileEntry')
|
||||
class ProfileEntries extends Table {
|
||||
|
||||
Reference in New Issue
Block a user