Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/TodoTxt.Sharp.UI/ViewModels/TaskFileViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace TodoTxt.Sharp.UI.ViewModels
public class TaskFileViewModel : Screen
{
private TaskFile _file;
private bool _isInEdit;
private Task _selectedTodo;

public TaskFileViewModel(string filePath, bool shouldFileBeWiped = false) {
if(string.IsNullOrWhiteSpace(filePath))
Expand All @@ -25,6 +27,26 @@ public TaskFileViewModel(string filePath, bool shouldFileBeWiped = false) {
File = new TaskFile(filePath);
}

public bool IsInEdit {
get { return _isInEdit; }
set {
if (value.Equals(_isInEdit))
return;
_isInEdit = value;
NotifyOfPropertyChange(() => IsInEdit);
}
}

public Task SelectedTodo {
get { return _selectedTodo; }
set {
if (Equals(value, _selectedTodo))
return;
_selectedTodo = value;
NotifyOfPropertyChange(() => SelectedTodo);
}
}

public TaskFile File {
get { return _file; }
private set {
Expand Down
28 changes: 27 additions & 1 deletion src/TodoTxt.Sharp.UI/Views/TaskFileView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,33 @@
xmlns:viewModels="clr-namespace:TodoTxt.Sharp.UI.ViewModels"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" d:DataContext="{d:DesignInstance viewModels:TaskFileViewModel}">
<UserControl.Resources>
<DataTemplate x:Key="ReadyOnlyTemplate">
<TextBlock Text="{Binding Path=Raw}" />
</DataTemplate>
<DataTemplate x:Key="EditTemplate">
<TextBox Text="{Binding Path=Raw}" />
</DataTemplate>

<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}" x:Key="EditReadOnlyStyle">
<Setter Property="ContentTemplate" Value="{StaticResource ReadyOnlyTemplate}" />
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=DataContext.IsInEdit, RelativeSource={RelativeSource AncestorType=UserControl}}" Value="True" />
<Condition Binding="{Binding RelativeSource={RelativeSource Self},Path=IsSelected}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Property="ContentTemplate" Value="{StaticResource EditTemplate}" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Grid>
<ListView ItemsSource="{Binding File.Tasks}"></ListView>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<CheckBox IsChecked="{Binding IsInEdit}"></CheckBox>
<ListBox Grid.Row="1" ItemsSource="{Binding File.Tasks}" ItemContainerStyle="{StaticResource EditReadOnlyStyle}" SelectedItem="{Binding SelectedTodo}" SelectionMode="Single"></ListBox>
</Grid>
</UserControl>
9 changes: 5 additions & 4 deletions src/TodoTxt.Sharp/TaskFile.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using TodoTxt.Sharp.Comparers;
Expand All @@ -22,7 +23,7 @@ public string Path {
get { return _path; }
}

public List<Task> Tasks { get; private set; }
public ObservableCollection<Task> Tasks { get; private set; }

/// <summary>
/// Loads the tasks from a file.
Expand All @@ -32,7 +33,7 @@ public void LoadFromFile(bool ignoreLastWriteTime = false) {
if (!ignoreLastWriteTime && File.GetLastWriteTimeUtc(_path) <= _fileLastWriteTime)
return;

Tasks = new List<Task>();
Tasks = new ObservableCollection<Task>();

if (!File.Exists(_path)) {
using (File.Create(_path)) {}
Expand Down Expand Up @@ -116,11 +117,11 @@ private void UpdateFilePositions(Task task, int filePosition) {
}
}
task.FilePosition = filePosition;
Tasks.Sort(new TaskFilePositionComparer());
//Tasks. Sort(new TaskFilePositionComparer());
}

private void WriteAllTasksToFile() {
File.WriteAllText(_path, string.Join(_newLineDelimter, Tasks.Select(t => t.Raw)));
File.WriteAllText(_path, string.Join(_newLineDelimter, Tasks.OrderBy(x => x.FilePosition).Select(t => t.Raw)));
}

/// <summary>
Expand Down