Add profiles sort option

This commit is contained in:
problematicconsumer
2023-07-26 16:42:31 +03:30
parent d741b7a427
commit 3dd21213ef
10 changed files with 206 additions and 25 deletions

View File

@@ -2,11 +2,17 @@ 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/enums.dart';
import 'package:hiddify/domain/profiles/profiles.dart';
import 'package:hiddify/utils/utils.dart';
part 'profiles_dao.g.dart';
Map<SortMode, OrderingMode> orderMap = {
SortMode.ascending: OrderingMode.asc,
SortMode.descending: OrderingMode.desc
};
@DriftAccessor(tables: [ProfileEntries])
class ProfilesDao extends DatabaseAccessor<AppDatabase>
with _$ProfilesDaoMixin, InfraLogger {
@@ -31,10 +37,24 @@ class ProfilesDao extends DatabaseAccessor<AppDatabase>
.watchSingle();
}
Stream<List<Profile>> watchAll() {
Stream<List<Profile>> watchAll({
ProfilesSort sort = ProfilesSort.lastUpdate,
SortMode mode = SortMode.ascending,
}) {
return (profileEntries.select()
..orderBy(
[(tbl) => OrderingTerm.desc(tbl.active)],
[
switch (sort) {
ProfilesSort.name => (tbl) => OrderingTerm(
expression: tbl.name,
mode: orderMap[mode]!,
),
_ => (tbl) => OrderingTerm(
expression: tbl.lastUpdate,
mode: orderMap[mode]!,
),
}
],
))
.map(ProfileMapper.fromEntry)
.watch();