123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using Comal.Classes;
- using InABox.Core;
- using Xamarin.Forms;
- namespace comal.timesheets
- {
- public class EquipmentListDataModel : ListDataModel<Equipment, EquipmentListDataModelItem>
- {
- public override Columns<Equipment> Columns => new Columns<Equipment>(x => x.ID)
- .Add(x => x.Code)
- .Add(x => x.Description)
- .Add(x => x.GroupLink.ID)
- .Add(x => x.GroupLink.Description)
- .Add(x=>x.TrackerLink.BatteryLevel)
- .Add(x => x.GroupLink.Thumbnail.ID)
- .Add(x=>x.TrackerLink.Location.Timestamp);
- public EquipmentListDataModel() : base()
- {
- Images = new Dictionary<Guid, ImageSource>() { { Guid.Empty, null } };
- }
-
- public override void BeforeLoad(MultiQuery query, Filter<Equipment> filter)
- {
- base.BeforeLoad(query, filter);
- query.Add<Document>(
- //new Filter<Document>(x => x.ID).InQuery<Equipment>(filter, x => x.GroupLink.Thumbnail.ID),
- new Filter<Document>(x => x.ID).InQuery<Equipment>(filter, x => x.GroupLink.ID),
- new Columns<Document>(x => x.ID, x => x.Data)
- );
- }
- public String[] GroupNames => Items.Select(x => x.GroupName).Distinct().OrderBy(x => x).Prepend("All").ToArray();
-
- public Dictionary<Guid, ImageSource> Images { get; private set; }
-
- public override void AfterLoad(MultiQuery query, Filter<Equipment> filter)
- {
- base.AfterLoad(query, filter);
-
- Images.Clear();
- CoreTable docs = query.Get<Document>();
- foreach (var row in docs.Rows)
- {
- Guid imgid = row.Get<Document, Guid>(c => c.ID);
- byte[] imgdata = row.Get<Document, byte[]>(c => c.Data);
- ImageSource imgsource = ImageSource.FromStream(() => new MemoryStream(imgdata));
- Images[imgid] = imgsource;
- }
- }
- }
-
- public class EquipmentListDataModelItem : CoreDataModelItem
- {
- public Guid ID => Row.Get<Equipment, Guid>(x => x.ID);
- public string Code => Row.Get<Equipment, String>(x => x.Code);
- public string Description => Row.Get<Equipment, String>(x => x.Description);
- public Guid GroupID => Row.Get<Equipment, Guid>(x => x.GroupLink.ID);
- public string GroupName => Row.Get<Equipment, String>(x => x.GroupLink.Description);
- public double BatteryDouble => Row.Get<Equipment, double>(x => x.TrackerLink.BatteryLevel);
- public DateTime LastUpdate => Row.Get<Equipment, DateTime>(x => x.TrackerLink.Location.Timestamp);
-
- public ImageSource ImageSource
- {
- get
- {
- (Parent as EquipmentListDataModel).Images.TryGetValue(GroupID, out ImageSource img);
- return img;
- }
- }
- public double ColumnWidth => (Parent as EquipmentListDataModel).Images.Any() ? 55 : 0;
-
- public string Battery => $"Battery {BatteryDouble:F2}";
-
- public string BatteryLastUpdate => $"Last Update: {LastUpdate:dd MMM yy}";
- public Color LastUpdateColour => LastUpdate < DateTime.Today.AddMonths(-1)
- ? Color.FromHex("#f08080")
- : LastUpdate < DateTime.Today.AddDays(-14)
- ? Color.FromHex("#ffef00")
- : Color.LightGreen;
-
- public double BatteryRowHeight => BatteryDouble > 0 ? 25 : 0;
-
- public Color BatteryColour => BatteryDouble <= 40D
- ? Color.FromHex("#f08080")
- : BatteryDouble > 40D && BatteryDouble <= 70D
- ? Color.FromHex("#ffef00")
- : Color.LightGreen;
-
-
- }
-
- }
|