|
| 1 | +using PhotoTagger.Imaging; |
| 2 | +using PhotoTagger.Wpf; |
| 3 | +using System; |
| 4 | +using System.Collections.ObjectModel; |
| 5 | +using System.Collections.Specialized; |
| 6 | +using System.Linq; |
| 7 | +using System.Windows; |
| 8 | +using System.Windows.Controls; |
| 9 | + |
| 10 | +namespace PhotoTagger { |
| 11 | + /// <summary> |
| 12 | + /// Interaction logic for DateTimeRangeEdit.xaml |
| 13 | + /// </summary> |
| 14 | + public partial class DateTimeRangeEdit : UserControl { |
| 15 | + public DateTimeRangeEdit() { |
| 16 | + if (PhotoSet is INotifyCollectionChanged oc) { |
| 17 | + oc.CollectionChanged += setChanged; |
| 18 | + } |
| 19 | + InitializeComponent(); |
| 20 | + } |
| 21 | + |
| 22 | + public ReadOnlyObservableCollection<Photo> PhotoSet { |
| 23 | + get { |
| 24 | + return (ReadOnlyObservableCollection<Photo>)GetValue( |
| 25 | + PhotoSetProperty); |
| 26 | + } |
| 27 | + set { |
| 28 | + SetValue(PhotoSetProperty, value); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + public static readonly DependencyProperty PhotoSetProperty = |
| 33 | + DependencyProperty.Register(nameof(PhotoSet), |
| 34 | + typeof(ReadOnlyObservableCollection<Photo>), |
| 35 | + typeof(DateTimeRangeEdit), |
| 36 | + new PropertyMetadata(setChanged)); |
| 37 | + |
| 38 | + private static void setChanged(DependencyObject d, |
| 39 | + DependencyPropertyChangedEventArgs e) { |
| 40 | + var photos = e.NewValue as ReadOnlyObservableCollection<Photo>; |
| 41 | + DateTimeRangeEdit self = (d as DateTimeRangeEdit); |
| 42 | + self.DateRange = DateTimeRange.FromList( |
| 43 | + photos.Select(p => p.DateTaken)); |
| 44 | + if (e.OldValue is INotifyCollectionChanged oc) { |
| 45 | + oc.CollectionChanged -= self.setChanged; |
| 46 | + } |
| 47 | + if (photos is INotifyCollectionChanged nc) { |
| 48 | + nc.CollectionChanged += self.setChanged; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private void setChanged(object sender, NotifyCollectionChangedEventArgs e) { |
| 53 | + DateRange = DateTimeRange.FromList( |
| 54 | + PhotoSet.Select(p => p.DateTaken)); |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + public DateTimeRange? DateRange { |
| 59 | + get { |
| 60 | + return (DateTimeRange?)GetValue(DateRangeProperty); |
| 61 | + } |
| 62 | + set { |
| 63 | + SetValue(DateRangeProperty, value); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public static readonly DependencyProperty DateRangeProperty = |
| 68 | + DependencyProperty.Register(nameof(DateRange), typeof(DateTimeRange?), |
| 69 | + typeof(DateTimeRangeEdit), |
| 70 | + new PropertyMetadata(dateChanged)); |
| 71 | + |
| 72 | + private static void dateChanged(DependencyObject d, |
| 73 | + DependencyPropertyChangedEventArgs e) { |
| 74 | + var newTime = e.NewValue as DateTimeRange?; |
| 75 | + if (!newTime.HasValue) { |
| 76 | + return; |
| 77 | + } |
| 78 | + if (d is DateTimeRangeEdit self) { |
| 79 | + if (self.PhotoSet.Count == 0) { |
| 80 | + return; |
| 81 | + } |
| 82 | + DateTimeRange? oldRange = DateTimeRange.FromList( |
| 83 | + self.PhotoSet.Select(p => p.DateTaken)); |
| 84 | + if (!oldRange.HasValue || |
| 85 | + !oldRange.Value.IsRange) { |
| 86 | + self.setAll(newTime.Value.Min); |
| 87 | + } else { |
| 88 | + var shiftAmount = newTime.Value.Min - oldRange.Value.Min; |
| 89 | + self.shiftDates(shiftAmount); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private async void shiftDates(TimeSpan shiftAmount) { |
| 95 | + if (shiftAmount == TimeSpan.Zero) { |
| 96 | + return; |
| 97 | + } |
| 98 | + var part = this.minDatePicker.CurrentDateTimePart; |
| 99 | + foreach (Photo p in this.PhotoSet) { |
| 100 | + if (p.DateTaken.HasValue) { |
| 101 | + p.DateTaken = p.DateTaken.Value + shiftAmount; |
| 102 | + } |
| 103 | + } |
| 104 | + this.DateRange = DateTimeRange.FromList( |
| 105 | + this.PhotoSet.Select(p => p.DateTaken)); |
| 106 | + // restore the CurrentDateTimePart, but only after all of the data |
| 107 | + // binding flow-through has had a chance to propagate. |
| 108 | + await this.Dispatcher.InvokeAsync(() => |
| 109 | + this.minDatePicker.CurrentDateTimePart = part); |
| 110 | + } |
| 111 | + |
| 112 | + private void setAllEqual(object sender, RoutedEventArgs e) { |
| 113 | + if (!this.DateRange.HasValue) { |
| 114 | + return; |
| 115 | + } |
| 116 | + var newTime = this.DateRange.Value.Min; |
| 117 | + setAll(newTime); |
| 118 | + } |
| 119 | + |
| 120 | + private async void setAll(DateTime newTime) { |
| 121 | + bool anyChanged = false; |
| 122 | + var part = this.minDatePicker.CurrentDateTimePart; |
| 123 | + foreach (Photo p in this.PhotoSet) { |
| 124 | + if (p.DateTaken.HasValue && |
| 125 | + p.DateTaken.Value != newTime) { |
| 126 | + p.DateTaken = newTime; |
| 127 | + anyChanged = true; |
| 128 | + } |
| 129 | + } |
| 130 | + if (anyChanged) { |
| 131 | + this.DateRange = DateTimeRange.FromList( |
| 132 | + this.PhotoSet.Select(p => p.DateTaken)); |
| 133 | + // restore the CurrentDateTimePart, but only after all of the data |
| 134 | + // binding flow-through has had a chance to propagate. |
| 135 | + await this.Dispatcher.InvokeAsync(() => |
| 136 | + this.minDatePicker.CurrentDateTimePart = part); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments