Files
umbrix/lib/features/profile/data/profile_data_source.dart

134 lines
4.0 KiB
Dart
Raw Normal View History

2023-07-06 17:18:41 +03:30
import 'package:drift/drift.dart';
import 'package:hiddify/data/local/database.dart';
import 'package:hiddify/data/local/tables.dart';
2023-11-26 21:20:58 +03:30
import 'package:hiddify/features/profile/model/profile_sort_enum.dart';
2023-07-06 17:18:41 +03:30
import 'package:hiddify/utils/utils.dart';
2023-11-26 21:20:58 +03:30
part 'profile_data_source.g.dart';
abstract interface class ProfileDataSource {
Future<ProfileEntry?> getById(String id);
Future<ProfileEntry?> getByUrl(String url);
Stream<ProfileEntry?> watchActiveProfile();
Stream<int> watchProfilesCount();
Stream<List<ProfileEntry>> watchAll({
required ProfilesSort sort,
required SortMode sortMode,
});
Future<void> insert(ProfileEntriesCompanion entry);
Future<void> edit(String id, ProfileEntriesCompanion entry);
Future<void> deleteById(String id);
}
2023-07-06 17:18:41 +03:30
2023-07-26 16:42:31 +03:30
Map<SortMode, OrderingMode> orderMap = {
SortMode.ascending: OrderingMode.asc,
2023-09-06 21:03:43 +03:30
SortMode.descending: OrderingMode.desc,
2023-07-26 16:42:31 +03:30
};
2023-07-06 17:18:41 +03:30
@DriftAccessor(tables: [ProfileEntries])
2023-11-26 21:20:58 +03:30
class ProfileDao extends DatabaseAccessor<AppDatabase>
with _$ProfileDaoMixin, InfraLogger
implements ProfileDataSource {
ProfileDao(super.db);
2023-07-06 17:18:41 +03:30
2023-11-26 21:20:58 +03:30
@override
Future<ProfileEntry?> getById(String id) async {
2023-07-06 17:18:41 +03:30
return (profileEntries.select()..where((tbl) => tbl.id.equals(id)))
.getSingleOrNull();
}
2023-11-26 21:20:58 +03:30
@override
Future<ProfileEntry?> getByUrl(String url) async {
return (select(profileEntries)
..where((tbl) => tbl.url.like('%$url%'))
..limit(1))
.getSingleOrNull();
}
2023-11-26 21:20:58 +03:30
@override
Stream<ProfileEntry?> watchActiveProfile() {
2023-09-21 21:47:38 +03:30
return (profileEntries.select()
..where((tbl) => tbl.active.equals(true))
..limit(1))
2023-07-06 17:18:41 +03:30
.watchSingleOrNull();
}
2023-11-26 21:20:58 +03:30
@override
Stream<int> watchProfilesCount() {
2023-07-06 17:18:41 +03:30
final count = profileEntries.id.count();
return (profileEntries.selectOnly()..addColumns([count]))
.map((exp) => exp.read(count)!)
.watchSingle();
}
2023-11-26 21:20:58 +03:30
@override
Stream<List<ProfileEntry>> watchAll({
required ProfilesSort sort,
required SortMode sortMode,
2023-07-26 16:42:31 +03:30
}) {
2023-07-06 17:18:41 +03:30
return (profileEntries.select()
..orderBy(
2023-07-26 16:42:31 +03:30
[
2023-07-26 20:00:17 +03:30
(tbl) {
final trafficRatio = (tbl.download + tbl.upload) / tbl.total;
final isExpired =
tbl.expire.isSmallerOrEqualValue(DateTime.now());
return OrderingTerm(
expression: (trafficRatio.isNull() |
trafficRatio.isSmallerThanValue(1)) &
(isExpired.isNull() | isExpired.equals(false)),
mode: OrderingMode.desc,
);
},
2023-07-26 16:42:31 +03:30
switch (sort) {
ProfilesSort.name => (tbl) => OrderingTerm(
expression: tbl.name,
2023-11-26 21:20:58 +03:30
mode: orderMap[sortMode]!,
2023-07-26 16:42:31 +03:30
),
2023-07-26 20:00:17 +03:30
ProfilesSort.lastUpdate => (tbl) => OrderingTerm(
2023-07-26 16:42:31 +03:30
expression: tbl.lastUpdate,
2023-11-26 21:20:58 +03:30
mode: orderMap[sortMode]!,
2023-07-26 16:42:31 +03:30
),
2023-07-26 20:00:17 +03:30
},
2023-07-26 16:42:31 +03:30
],
2023-07-06 17:18:41 +03:30
))
.watch();
}
2023-11-26 21:20:58 +03:30
@override
Future<void> insert(ProfileEntriesCompanion entry) async {
2023-07-06 17:18:41 +03:30
await transaction(
() async {
2023-11-26 21:20:58 +03:30
if (entry.active.present && entry.active.value) {
2023-09-21 21:47:38 +03:30
await update(profileEntries)
2023-07-06 17:18:41 +03:30
.write(const ProfileEntriesCompanion(active: Value(false)));
}
2023-11-26 21:20:58 +03:30
await into(profileEntries).insert(entry);
2023-07-06 17:18:41 +03:30
},
);
}
2023-11-26 21:20:58 +03:30
@override
Future<void> edit(String id, ProfileEntriesCompanion entry) async {
2023-07-06 17:18:41 +03:30
await transaction(
() async {
2023-11-26 21:20:58 +03:30
if (entry.active.present && entry.active.value) {
2023-09-21 21:47:38 +03:30
await update(profileEntries)
.write(const ProfileEntriesCompanion(active: Value(false)));
}
2023-07-06 17:18:41 +03:30
await (update(profileEntries)..where((tbl) => tbl.id.equals(id)))
2023-11-26 21:20:58 +03:30
.write(entry);
2023-07-06 17:18:41 +03:30
},
);
}
2023-11-26 21:20:58 +03:30
@override
Future<void> deleteById(String id) async {
2023-07-06 17:18:41 +03:30
await transaction(
() async {
await (delete(profileEntries)..where((tbl) => tbl.id.equals(id))).go();
},
);
}
}