SHARE
TWEET

Untitled

a guest Sep 19th, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Text;
  3. using System.Threading.Tasks;
  4. using LogifyRWA.Models;
  5. using LogifyRWA.Services;
  6. using Xamarin.Forms;
  7.  
  8. namespace LogifyRWA.ViewModels {
  9.     public class ReportViewModel : NotificationObject {
  10.         public Report Report { get; private set; }
  11.         ReportDataProvider dataProvider;
  12.  
  13.         public ReportViewModel(Report report) {
  14.             Report = report;
  15.             this.dataProvider = new ReportDataProvider();
  16.         }
  17.  
  18.         public int AffectedUsersCount => Report.AffectedUsersCount;
  19.         public bool HasAffectedUsers => Report.AffectedUsersCount > 0;
  20.  
  21.         public int Counter => Report.Counter;
  22.         public bool HasCounter => Report.Counter > 0;
  23.  
  24.         public string ApplicationName => Report.ApplicationName;
  25.         public string Version => Report.Version;
  26.         public string ReportsListInfo => Report.ReportsListInfo;
  27.  
  28.         public Color StatusColor => (Color)Resources.Values[$"ReportStatus{Report.Status.ToString()}"];
  29.  
  30.         public bool HasDateTimeLastReport => !string.IsNullOrEmpty(DateTimeLastReport);
  31.         string dateTimeLastReport = null;
  32.         public string DateTimeLastReport {
  33.             get {
  34.                 if (dateTimeLastReport == null) {
  35.                     var sb = new StringBuilder();
  36.                     var nowDateTime = DateTime.UtcNow;
  37.                     if (Report.DateTime > DateTime.MinValue) {
  38.                         sb.Append(ElapsedString(Report.DateTime, nowDateTime));
  39.                     }
  40.                     if (Report.LastReport > DateTime.MinValue) {
  41.                         if (sb.Length > 0) {
  42.                             sb.Append(" / ");
  43.                         }
  44.                         sb.Append(ElapsedString(Report.LastReport, nowDateTime));
  45.                     }
  46.                     dateTimeLastReport = sb.ToString();
  47.                 }
  48.                 return dateTimeLastReport;
  49.             }
  50.         }
  51.  
  52.         public void UpdateStatus(ReportStatus status) {
  53.             if(status != Report.Status) {
  54.                 Task.Run(() => dataProvider.UpdateStatus(Report, status));
  55.                 Report.Status = status;
  56.                 OnPropertyChanged("StatusColor");
  57.             }
  58.         }
  59.  
  60.         string ElapsedString(DateTime fromDateTime, DateTime toDateTime) {
  61.             var span = toDateTime - fromDateTime;
  62.             if (span.Days > 365) {
  63.                 return span.Days / 365 + "Y";
  64.             }
  65.             var spanMonths = (12 * span.Days) / 365;
  66.             if (spanMonths > 0) {
  67.                 return spanMonths + "M";
  68.             }
  69.             if (span.Days > 0) {
  70.                 return span.Days + "d";
  71.             }
  72.             if (span.Hours > 0) {
  73.                 return span.Hours + "h";
  74.             }
  75.             if (span.Minutes > 0) {
  76.                 return span.Minutes + "m";
  77.             }
  78.             return "a few seconds";
  79.         }
  80.     }
  81. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top