Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.Threading.Tasks;
- using LogifyRWA.Models;
- using LogifyRWA.Services;
- using Xamarin.Forms;
- namespace LogifyRWA.ViewModels {
- public class ReportViewModel : NotificationObject {
- public Report Report { get; private set; }
- ReportDataProvider dataProvider;
- public ReportViewModel(Report report) {
- Report = report;
- this.dataProvider = new ReportDataProvider();
- }
- public int AffectedUsersCount => Report.AffectedUsersCount;
- public bool HasAffectedUsers => Report.AffectedUsersCount > 0;
- public int Counter => Report.Counter;
- public bool HasCounter => Report.Counter > 0;
- public string ApplicationName => Report.ApplicationName;
- public string Version => Report.Version;
- public string ReportsListInfo => Report.ReportsListInfo;
- public Color StatusColor => (Color)Resources.Values[$"ReportStatus{Report.Status.ToString()}"];
- public bool HasDateTimeLastReport => !string.IsNullOrEmpty(DateTimeLastReport);
- string dateTimeLastReport = null;
- public string DateTimeLastReport {
- get {
- if (dateTimeLastReport == null) {
- var sb = new StringBuilder();
- var nowDateTime = DateTime.UtcNow;
- if (Report.DateTime > DateTime.MinValue) {
- sb.Append(ElapsedString(Report.DateTime, nowDateTime));
- }
- if (Report.LastReport > DateTime.MinValue) {
- if (sb.Length > 0) {
- sb.Append(" / ");
- }
- sb.Append(ElapsedString(Report.LastReport, nowDateTime));
- }
- dateTimeLastReport = sb.ToString();
- }
- return dateTimeLastReport;
- }
- }
- public void UpdateStatus(ReportStatus status) {
- if(status != Report.Status) {
- Task.Run(() => dataProvider.UpdateStatus(Report, status));
- Report.Status = status;
- OnPropertyChanged("StatusColor");
- }
- }
- string ElapsedString(DateTime fromDateTime, DateTime toDateTime) {
- var span = toDateTime - fromDateTime;
- if (span.Days > 365) {
- return span.Days / 365 + "Y";
- }
- var spanMonths = (12 * span.Days) / 365;
- if (spanMonths > 0) {
- return spanMonths + "M";
- }
- if (span.Days > 0) {
- return span.Days + "d";
- }
- if (span.Hours > 0) {
- return span.Hours + "h";
- }
- if (span.Minutes > 0) {
- return span.Minutes + "m";
- }
- return "a few seconds";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement