using CommunityToolkit.Mvvm.ComponentModel;
using FieldLogger.Models;
using FieldLogger.Services;
using FieldLogger.Services.Data;
namespace FieldLogger.ViewModels;
[QueryProperty(nameof(JobId), "jobId")]
public sealed partial class MapViewModel : ObservableObject
{
private readonly AppDatabase _database;
private readonly SettingsService _settings;
/// Job to display; 0 means "use the active job".
[ObservableProperty]
private int _jobId;
[ObservableProperty]
private string _jobName = "Active job map";
[ObservableProperty]
private string _statusText = "Loading locally saved points…";
public SettingsService Settings => _settings;
public MapViewModel(AppDatabase database, SettingsService settings)
{
_database = database;
_settings = settings;
}
/// Loads the points to plot (only points with a valid GNSS position).
public async Task> LoadPointsAsync()
{
var jobId = JobId > 0 ? JobId : _settings.ActiveJobId;
if (jobId is null)
{
JobName = "No active job";
StatusText = "No job selected. Create or activate a job to see its points.";
return new List();
}
var job = await _database.GetJobAsync(jobId.Value);
JobName = job?.Name ?? "";
var all = await _database.GetPointsAsync(jobId.Value);
var plottable = all.Where(p => p.GpsValid).ToList();
StatusText = plottable.Count == all.Count
? $"{JobName}: {all.Count} points"
: $"{JobName}: {plottable.Count} of {all.Count} points have GPS positions";
return plottable;
}
}