Skip to content

Commit 6d406c3

Browse files
committed
Add a button to sort photos by date.
Most of the time they're already in order, but if photos were added in batches, or from different cameras, they might not be.
1 parent 212d9a6 commit 6d406c3

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

PhotoTagger.Wpf/PhotoList.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public ObservableCollection<Photo> Photos {
3838
}
3939

4040
public static readonly DependencyProperty PhotosProperty =
41-
DependencyProperty.Register("Photos",
41+
DependencyProperty.Register(nameof(Photos),
4242
typeof(ObservableCollection<Photo>), typeof(PhotoList));
4343

4444

PhotoTagger.Wpf/ValueConverters.cs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,57 @@
1010
namespace PhotoTagger.Wpf {
1111
[ValueConversion(typeof(IReadOnlyList<Photo>), typeof(Visibility))]
1212
[ValueConversion(typeof(int), typeof(Visibility))]
13+
[ValueConversion(typeof(IReadOnlyList<Photo>), typeof(bool))]
14+
[ValueConversion(typeof(int), typeof(bool))]
1315
public class ElementCountToVisibilityValueConverter : IValueConverter {
1416
public object Convert(object value, Type targetType, object parameter,
1517
CultureInfo culture) {
1618
if (value is IReadOnlyList<Photo> list) {
1719
if (parameter is int desiredCount) {
1820
if (list.Count == desiredCount) {
19-
return Visibility.Visible;
21+
if (targetType == typeof(Visibility)) {
22+
return Visibility.Visible;
23+
} else {
24+
return true;
25+
}
2026
}
2127
} else if (list.Count > 1) {
22-
return Visibility.Visible;
28+
if (targetType == typeof(Visibility)) {
29+
return Visibility.Visible;
30+
} else {
31+
return true;
32+
}
2333
}
2434
} else if (value is int count) {
2535
if (parameter is int desiredCount) {
2636
if (count == desiredCount) {
27-
return Visibility.Visible;
37+
if (targetType == typeof(Visibility)) {
38+
return Visibility.Visible;
39+
} else {
40+
return true;
41+
}
2842
}
2943
} else if (parameter is IConvertible para) {
3044
if (para.ToInt32(CultureInfo.InvariantCulture) == count) {
31-
return Visibility.Visible;
45+
if (targetType == typeof(Visibility)) {
46+
return Visibility.Visible;
47+
} else {
48+
return true;
49+
}
3250
}
3351
} else if (count > 1) {
34-
return Visibility.Visible;
52+
if (targetType == typeof(Visibility)) {
53+
return Visibility.Visible;
54+
} else {
55+
return true;
56+
}
3557
}
3658
}
37-
return Visibility.Collapsed;
59+
if (targetType == typeof(Visibility)) {
60+
return Visibility.Collapsed;
61+
} else {
62+
return false;
63+
}
3864
}
3965

4066
public object ConvertBack(object value, Type targetType,

PhotoTagger/TaggerWindow.xaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@
5151
<StackPanel Grid.Row="1">
5252
<Button Content="Add images..."
5353
Click="addImagesEvent" />
54+
<Button Click="sortImagesEvent">
55+
<Button.Content>
56+
Sort by date
57+
</Button.Content>
58+
<Button.IsEnabled>
59+
<Binding Path="Photos.Count"
60+
ElementName="window"
61+
Converter="{StaticResource ElementCountToVisibilityValueConverter}"
62+
Mode="OneWay">
63+
</Binding>
64+
</Button.IsEnabled>
65+
</Button>
5466
<Button Content="Commit changes" Name="commitButton"
5567
IsEnabled="False" Click="commitEvent" />
5668
<Button Content="Close" Click="closeSelectedEvent"

PhotoTagger/TaggerWindow.xaml.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public TaggerWindow() {
2222
}
2323

2424
void photoCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
25+
if (e.Action == NotifyCollectionChangedAction.Move) {
26+
return;
27+
}
2528
if (e.OldItems != null) {
2629
foreach (var item in e.OldItems) {
2730
if (item is Photo photo) {
@@ -166,5 +169,12 @@ private void onFilesDrop(object sender, DragEventArgs e) {
166169
}
167170
addImages(files);
168171
}
172+
173+
private void sortImagesEvent(object sender, RoutedEventArgs e) {
174+
Photos.CollectionChanged -= photoCollectionChanged;
175+
Photos = new ObservableCollection<Photo>(Photos.OrderBy(
176+
p => p.DateTaken ?? DateTime.MaxValue));
177+
Photos.CollectionChanged += photoCollectionChanged;
178+
}
169179
}
170180
}

0 commit comments

Comments
 (0)