| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows.Controls;
- using Comal.Classes;
- using Comal.Stores;
- using InABox.Clients;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.WPF;
- namespace PRSDesktop;
- internal class GPSTrackerLocationGrid : DynamicOneToManyGrid<GPSTracker, GPSTrackerLocation>
- {
- private static List<GPSTrackerLocation> _cache;
- protected override void Init()
- {
- base.Init();
- Criteria.Add(
- new Filter<GPSTrackerLocation>(x => x.Location.Timestamp).IsGreaterThanOrEqualTo(DateTime.Now.AddDays(-1))
- );
- HiddenColumns.Add(x => x.Location.Longitude);
- HiddenColumns.Add(x => x.Location.Latitude);
- HiddenColumns.Add(x => x.Location.Address);
- ActionColumns.Add(new DynamicMapColumn<GPSTrackerLocation>(this, x => x.Location));
- AddButton("Get Addresses", null, GetAddressClick);
- }
- protected override void DoReconfigure(FluentList<DynamicGridOption> options)
- {
- base.DoReconfigure(options);
- options.AddRange(
- DynamicGridOption.SelectColumns,
- DynamicGridOption.RecordCount,
- DynamicGridOption.FilterRows
- );
- }
- public static string ReverseGeocode(double latitude, double longitude)
- {
- _cache ??= Client.Query(
- new Filter<GPSTrackerLocation>(x => x.Location.Address).IsNotEqualTo(""),
- new Columns<GPSTrackerLocation>(
- x => x.Location.Address,
- x => x.Location.Longitude,
- x => x.Location.Latitude))
- .ToList<GPSTrackerLocation>();
- var tuple = _cache.FirstOrDefault(x => Equals(x.Location.Latitude, latitude) && Equals(x.Location.Longitude, longitude));
- if (tuple == null)
- {
- var address = StoreUtils.ReverseGeocode(latitude, longitude);
- if (!string.IsNullOrWhiteSpace(address))
- {
- tuple = new GPSTrackerLocation();
- tuple.Location.Latitude = latitude;
- tuple.Location.Longitude = longitude;
- tuple.Location.Address = address;
- _cache.Add(tuple);
- }
- }
- return tuple != null ? tuple.Location.Address : "";
- }
- private bool GetAddressClick(Button arg1, CoreRow[] arg2)
- {
- var result = false;
- var rows = Data.Rows.Where(r =>
- string.IsNullOrWhiteSpace(r.Get<GPSTrackerLocation, string>(c => c.Location.Address))
- && !Equals(r.Get<GPSTrackerLocation, double>(c => c.Location.Latitude), 0.0F)
- && !Equals(r.Get<GPSTrackerLocation, double>(c => c.Location.Longitude), 0.0F)
- );
- Progress.ShowModal("Updating Addresses", progress =>
- {
- foreach (var row in rows)
- {
- var item = LoadItem(row);
- var address = ReverseGeocode(
- row.Get<GPSTrackerLocation, double>(x => x.Location.Latitude),
- row.Get<GPSTrackerLocation, double>(x => x.Location.Longitude)
- );
- if (!string.IsNullOrWhiteSpace(address))
- {
- item.Location.Address = address;
- UpdateRow<GPSTrackerLocation, string>(row, x => x.Location.Address, address, false);
- result = true;
- }
- }
- });
- return result;
- }
- }
|