optimizations
This commit is contained in:
@@ -6,7 +6,6 @@ import 'package:immich_mobile/infrastructure/entities/exif.entity.drift.dart';
|
||||
import 'package:immich_mobile/infrastructure/entities/remote_asset.entity.drift.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/db.repository.dart';
|
||||
import 'package:maplibre_gl/maplibre_gl.dart';
|
||||
import 'package:stream_transform/stream_transform.dart';
|
||||
|
||||
class DriftMapRepository extends DriftDatabaseRepository {
|
||||
final Drift _db;
|
||||
@@ -14,88 +13,59 @@ class DriftMapRepository extends DriftDatabaseRepository {
|
||||
const DriftMapRepository(super._db) : _db = _db;
|
||||
|
||||
MapQuery remote(String ownerId) => _mapQueryBuilder(
|
||||
assetFilter: (row) =>
|
||||
row.deletedAt.isNull() &
|
||||
row.visibility.equalsValue(AssetVisibility.timeline) &
|
||||
row.ownerId.equals(ownerId),
|
||||
);
|
||||
assetFilter: (row) =>
|
||||
row.deletedAt.isNull() & row.visibility.equalsValue(AssetVisibility.timeline) & row.ownerId.equals(ownerId),
|
||||
);
|
||||
|
||||
MapQuery _mapQueryBuilder({
|
||||
Expression<bool> Function($RemoteAssetEntityTable row)? assetFilter,
|
||||
Expression<bool> Function($RemoteExifEntityTable row)? exifFilter,
|
||||
}) {
|
||||
return (
|
||||
markerSource: (bounds) => _watchMapMarker(
|
||||
assetFilter: assetFilter,
|
||||
exifFilter: exifFilter,
|
||||
bounds: bounds,
|
||||
)
|
||||
);
|
||||
MapQuery _mapQueryBuilder({Expression<bool> Function($RemoteAssetEntityTable row)? assetFilter}) {
|
||||
return (markerSource: (bounds) => _watchMapMarker(assetFilter: assetFilter, bounds: bounds));
|
||||
}
|
||||
|
||||
Stream<List<Marker>> _watchMapMarker({
|
||||
Future<List<Marker>> _watchMapMarker({
|
||||
Expression<bool> Function($RemoteAssetEntityTable row)? assetFilter,
|
||||
Expression<bool> Function($RemoteExifEntityTable row)? exifFilter,
|
||||
LatLngBounds? bounds,
|
||||
}) {
|
||||
final query = _db.remoteExifEntity.select().join([
|
||||
innerJoin(
|
||||
_db.remoteAssetEntity,
|
||||
_db.remoteAssetEntity.id.equalsExp(_db.remoteExifEntity.assetId),
|
||||
useColumns: false,
|
||||
),
|
||||
])
|
||||
..where(
|
||||
_db.remoteExifEntity.latitude.isNotNull() &
|
||||
_db.remoteExifEntity.longitude.isNotNull(),
|
||||
);
|
||||
final query = _db.remoteExifEntity.selectOnly()
|
||||
..addColumns([_db.remoteExifEntity.assetId, _db.remoteExifEntity.latitude, _db.remoteExifEntity.longitude])
|
||||
..join([
|
||||
innerJoin(
|
||||
_db.remoteAssetEntity,
|
||||
_db.remoteAssetEntity.id.equalsExp(_db.remoteExifEntity.assetId),
|
||||
useColumns: false,
|
||||
),
|
||||
])
|
||||
..limit(10000);
|
||||
|
||||
if (assetFilter != null) {
|
||||
query.where(assetFilter(_db.remoteAssetEntity));
|
||||
}
|
||||
|
||||
if (exifFilter != null) {
|
||||
query.where(exifFilter(_db.remoteExifEntity));
|
||||
}
|
||||
|
||||
if (bounds != null) {
|
||||
query.where(_db.remoteExifEntity.inBounds(bounds));
|
||||
} else {
|
||||
query.where(_db.remoteExifEntity.latitude.isNotNull() & _db.remoteExifEntity.longitude.isNotNull());
|
||||
}
|
||||
|
||||
return query
|
||||
.map((row) => row.readTable(_db.remoteExifEntity).toMarker())
|
||||
.watch()
|
||||
.throttle(const Duration(seconds: 3));
|
||||
return query.map((row) {
|
||||
return Marker(
|
||||
assetId: row.read(_db.remoteExifEntity.assetId)!,
|
||||
location: LatLng(row.read(_db.remoteExifEntity.latitude)!, row.read(_db.remoteExifEntity.longitude)!),
|
||||
);
|
||||
}).get();
|
||||
}
|
||||
}
|
||||
|
||||
extension MapBounds on $RemoteExifEntityTable {
|
||||
Expression<bool> inBounds(LatLngBounds bounds) {
|
||||
final isLatitudeInBounds =
|
||||
latitude.isBiggerOrEqualValue(bounds.southwest.latitude) &
|
||||
latitude.isSmallerOrEqualValue(bounds.northeast.latitude);
|
||||
final southwest = bounds.southwest;
|
||||
final northeast = bounds.northeast;
|
||||
|
||||
final Expression<bool> isLongitudeInBounds;
|
||||
|
||||
if (bounds.southwest.longitude <= bounds.northeast.longitude) {
|
||||
isLongitudeInBounds =
|
||||
longitude.isBiggerOrEqualValue(bounds.southwest.longitude) &
|
||||
longitude.isSmallerOrEqualValue(bounds.northeast.longitude);
|
||||
if (southwest.longitude <= northeast.longitude) {
|
||||
return latitude.isBetweenValues(southwest.latitude, northeast.latitude) &
|
||||
longitude.isBetweenValues(southwest.longitude, northeast.longitude);
|
||||
} else {
|
||||
isLongitudeInBounds =
|
||||
longitude.isBiggerOrEqualValue(bounds.southwest.longitude) |
|
||||
longitude.isSmallerOrEqualValue(bounds.northeast.longitude);
|
||||
return latitude.isBetweenValues(southwest.latitude, northeast.latitude) &
|
||||
(longitude.isBiggerOrEqualValue(southwest.longitude) | longitude.isSmallerOrEqualValue(northeast.longitude));
|
||||
}
|
||||
|
||||
return isLatitudeInBounds & isLongitudeInBounds;
|
||||
}
|
||||
}
|
||||
|
||||
extension on RemoteExifEntityData {
|
||||
Marker toMarker() {
|
||||
return Marker(
|
||||
assetId: assetId,
|
||||
location: LatLng(latitude!, longitude!),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user