Sort places by count instead of time

This commit is contained in:
Ming Ming 2022-08-29 01:43:32 +08:00
parent ac6b3586d3
commit 075d208fff
3 changed files with 24 additions and 22 deletions

View file

@ -8,19 +8,19 @@ import 'package:nc_photos/location_util.dart' as location_util;
class LocationGroup {
const LocationGroup(
this.place, this.countryCode, this.latest, this.latestFileId);
this.place, this.countryCode, this.count, this.latestFileId);
@override
toString() => "$runtimeType {"
"place: $place, "
"countryCode: $countryCode, "
"latest: $latest, "
"count: $count, "
"latestFileId: $latestFileId, "
"}";
final String place;
final String countryCode;
final DateTime latest;
final int count;
final int latestFileId;
}
@ -60,14 +60,15 @@ class ListLocationGroup {
final countryCodeResult = <LocationGroup>[];
for (final r in account.roots) {
final latest = db.accountFiles.bestDateTime.max();
final nameQ =
_buildQuery(db, dbAccount, r, latest, db.imageLocations.name);
final count = db.imageLocations.rowId.count();
final nameQ = _buildQuery(
db, dbAccount, r, latest, count, db.imageLocations.name);
try {
nameResult.addAll(await nameQ
.map((r) => LocationGroup(
r.read(db.imageLocations.name)!,
r.read(db.imageLocations.countryCode)!,
r.read(latest),
r.read(count),
r.read(db.files.fileId)!,
))
.get());
@ -75,14 +76,14 @@ class ListLocationGroup {
_log.shout("[call] Failed while query name group", e, stackTrace);
}
final admin1Q =
_buildQuery(db, dbAccount, r, latest, db.imageLocations.admin1);
final admin1Q = _buildQuery(
db, dbAccount, r, latest, count, db.imageLocations.admin1);
try {
admin1Result.addAll(await admin1Q
.map((r) => LocationGroup(
r.read(db.imageLocations.admin1)!,
r.read(db.imageLocations.countryCode)!,
r.read(latest),
r.read(count),
r.read(db.files.fileId)!,
))
.get());
@ -90,14 +91,14 @@ class ListLocationGroup {
_log.shout("[call] Failed while query admin1 group", e, stackTrace);
}
final admin2Q =
_buildQuery(db, dbAccount, r, latest, db.imageLocations.admin2);
final admin2Q = _buildQuery(
db, dbAccount, r, latest, count, db.imageLocations.admin2);
try {
admin2Result.addAll(await admin2Q
.map((r) => LocationGroup(
r.read(db.imageLocations.admin2)!,
r.read(db.imageLocations.countryCode)!,
r.read(latest),
r.read(count),
r.read(db.files.fileId)!,
))
.get());
@ -106,12 +107,12 @@ class ListLocationGroup {
}
final countryCodeQ = _buildQuery(
db, dbAccount, r, latest, db.imageLocations.countryCode);
db, dbAccount, r, latest, count, db.imageLocations.countryCode);
try {
countryCodeResult.addAll(await countryCodeQ.map((r) {
final cc = r.read(db.imageLocations.countryCode)!;
return LocationGroup(location_util.alpha2CodeToName(cc) ?? cc, cc,
r.read(latest), r.read(db.files.fileId)!);
r.read(count), r.read(db.files.fileId)!);
}).get());
} catch (e, stackTrace) {
_log.shout(
@ -131,6 +132,7 @@ class ListLocationGroup {
sql.Account dbAccount,
String dir,
sql.Expression<DateTime> latest,
sql.Expression<int> count,
sql.GeneratedColumn<String?> groupColumn,
) {
final query = db.selectOnly(db.imageLocations).join([
@ -142,7 +144,7 @@ class ListLocationGroup {
]);
if (identical(groupColumn, db.imageLocations.countryCode)) {
query
..addColumns([db.imageLocations.countryCode, latest, db.files.fileId])
..addColumns([db.imageLocations.countryCode, count, db.files.fileId])
..groupBy([db.imageLocations.countryCode],
having: db.accountFiles.bestDateTime.equalsExp(latest));
} else {
@ -150,7 +152,7 @@ class ListLocationGroup {
..addColumns([
groupColumn,
db.imageLocations.countryCode,
latest,
count,
db.files.fileId
])
..groupBy([groupColumn, db.imageLocations.countryCode],

View file

@ -191,11 +191,11 @@ class _PlacesBrowserState extends State<PlacesBrowser> {
}
int sorter(LocationGroup a, LocationGroup b) {
final latestCompare = b.latest.compareTo(a.latest);
if (latestCompare == 0) {
final compare = b.count.compareTo(a.count);
if (compare == 0) {
return a.place.compareTo(b.place);
} else {
return latestCompare;
return compare;
}
}

View file

@ -269,11 +269,11 @@ class _SearchLandingState extends State<SearchLanding> {
void _transformLocations(LocationGroupResult locations) {
_locationItems = locations.name
.sorted((a, b) {
final latestCompare = b.latest.compareTo(a.latest);
if (latestCompare == 0) {
final compare = b.count.compareTo(a.count);
if (compare == 0) {
return a.place.compareTo(b.place);
} else {
return latestCompare;
return compare;
}
})
.map((e) => _LandingLocationItem(