using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Windows.Media; using MahTweets.Mobile.Core.Extensions; using MahTweets.Mobile.Core.Interfaces; using MahTweets.Mobile.Core.Repositories; namespace MahTweets.Mobile.Core { public abstract class AbstractStatusUpdate : MahViewModelBase, IStatusUpdate { private Brush _background; private string _colorargb; private bool _fav; private ObservableCollection _parents; private string _text; protected AbstractStatusUpdate() { Parents = new ObservableCollection(); Types = new List(); } public Brush Background { get { if (_background == null) { UpdateType x = Types.OrderByDescending(t => t.Order).Take(1).FirstOrDefault(); if (x != null && !String.IsNullOrEmpty(x.ColorARGB)) { _background = new SolidColorBrush(x.ColorARGB.ToColor()); } } return _background; } set { _background = value; } } public string OriginalText { get; set; } public Boolean Favourite { get { return _fav; } set { _fav = value; RaisePropertyChanged(() => Favourite); } } private bool _isSelected; public bool IsSelected { get { return _isSelected; } set { _isSelected = value; RaisePropertyChanged(() => IsSelected); } } public string Source { get; set; } public string SourceUri { get; set; } public Contact InReplyTo { get; set; } public string InReplyToID { get; set; } public bool HasLinks { get; set; } #region IStatusUpdate Members public GeoLocation Location { get; set; } public String ID { get; set; } public Contact Contact { get; set; } public string ColorARGB { get { return _colorargb; } set { _colorargb = value; RaisePropertyChanged(() => ColorARGB); } } public string Text { get { return _text; } set { _text = value; if (string.IsNullOrEmpty(OriginalText)) { OriginalText = value; } HasLinks = _text.HasLinks(); RaisePropertyChanged(() => Text); RaisePropertyChanged(() => HasLinks); } } private DateTime _time; public DateTime Time { get { return _time; } set { _time = value; RaisePropertyChanged(() => Time); RaisePropertyChanged(() => FriendlyTime); } } public IList Types { get; set; } public ObservableCollection Parents { get { return _parents; } set { _parents = value; RaisePropertyChanged(() => Parents); } } #endregion public void AddParent(IMicroblog parent) { if (!Parents.Any(microblog => !string.IsNullOrEmpty(microblog.OwnerName) && microblog.OwnerName.Matches(parent.OwnerName))) Parents.Add(parent); } public String FriendlyDistance { get { return Location != null ? Location.FriendlyDistance() : ""; } } public String FriendlyTime { get { var timeSince = (DateTime.Now - Time.ToLocalTime()); if (timeSince < new TimeSpan(0, 0, 15)) return "just now"; if (timeSince < new TimeSpan(0, 1, 0)) return "< 1 min ago"; if (timeSince < new TimeSpan(1, 0, 0)) { if (timeSince > new TimeSpan(0, 1, 59)) return timeSince.Minutes + " mins ago"; return timeSince.Minutes + " min ago"; } if (timeSince < new TimeSpan(23, 0, 0)) { if (timeSince > new TimeSpan(1, 59, 59)) return timeSince.Hours + " hrs ago"; return timeSince.Hours + " hr ago"; } return string.Format("{0} {1} {2} {3}", Time.ToShortTimeString(), Time.DayOfWeek.ToString().Substring(0, 3), "", FriendlyDistance); } } public void Pulse() { RaisePropertyChanged(() => FriendlyTime); } public abstract string FileVersion { get; } public abstract void Load(string version, System.IO.BinaryReader reader, ContactRepository contactRepository, ThreadSafeObservableCollection accounts); public abstract void Save(System.IO.BinaryWriter writer); } }