This repository was archived by the owner on Sep 24, 2024. It is now read-only.
forked from caoyue/Wox.Plugin.Todos
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.cs
More file actions
193 lines (176 loc) · 6.82 KB
/
Copy pathMain.cs
File metadata and controls
193 lines (176 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using Flow.Launcher.Plugin;
using Flow.Launcher.Infrastructure.Storage;
namespace Wox.Plugin.Todos
{
public class Main : IPlugin, ISettingProvider, ISavable
{
private static Todos _todos;
private readonly PluginJsonStorage<Settings> _storage;
private readonly Settings _setting;
public Main()
{
_storage = new PluginJsonStorage<Settings>();
_setting = _storage.Load();
}
#region Query
public List<Result> Query(Query query)
{
_todos.Reload();
_todos.ActionKeyword = query.ActionKeyword;
var help = new Help(_todos.Context, query);
if (query.FirstSearch.Equals("-"))
{
return help.Show;
}
if (!query.FirstSearch.StartsWith("-"))
{
return Search(query.Search, t => !t.Completed);
}
TodoCommand op;
if (!Enum.TryParse(query.FirstSearch.TrimStart('-'), true, out op))
{
return Search(query.Search, t => !t.Completed);
}
switch (op)
{
case TodoCommand.H:
return help.Show;
case TodoCommand.C:
if (query.SecondSearch.Equals("--all", StringComparison.OrdinalIgnoreCase))
{
return new List<Result> {
new Result {
Title = "Mark all todos as done?",
SubTitle = "click to make all todos as done",
IcoPath = _todos.GetFilePath(),
Action = c => {
_todos.CompleteAll();
return true;
}
}
};
}
var cResults = _todos.Find(
t => t.Content.IndexOf(query.SecondToEndSearch, StringComparison.OrdinalIgnoreCase) >= 0 && !t.Completed,
t2 => "click to mark todo as done",
(c, t3) =>
{
_todos.Complete(t3);
//requery to refresh results
_todos.Context.API.ChangeQuery($"{query.ActionKeyword} -l ", true);
return false;
});
return cResults;
case TodoCommand.R:
if (query.SecondSearch.Equals("--all", StringComparison.OrdinalIgnoreCase))
{
return new List<Result> {
new Result {
Title = "Remove all todos?",
SubTitle = "click to remove all todos",
IcoPath = _todos.GetFilePath(),
Action = c => {
_todos.RemoveAll();
return true;
}
}
};
}
else if (query.SecondSearch.Equals("--done", StringComparison.OrdinalIgnoreCase))
{
return new List<Result> {
new Result {
Title = "Remove all completed todos?",
SubTitle = "click to remove all todos",
IcoPath = _todos.GetFilePath(),
Action = c => {
_todos.RemoveAllCompletedTodos();
return true;
}
}
};
}
var results = _todos.Find(
t => t.Content.IndexOf(query.SecondToEndSearch, StringComparison.OrdinalIgnoreCase) >= 0,
t2 => "click to remove todo",
(c, t3) =>
{
_todos.Remove(t3);
return true;
});
return results;
case TodoCommand.A:
return new List<Result> {
AddResult(query.SecondToEndSearch)
};
case TodoCommand.L:
return Search(query.SecondToEndSearch);
case TodoCommand.Rl:
return new List<Result> {
new Result {
Title = "Reload todos from data file?",
SubTitle = "click to reload",
IcoPath = _todos.GetFilePath(),
Action = c => {
_todos.Reload();
_todos.Context.API.ChangeQuery($"{query.ActionKeyword} ", true);
return false;
}
}
};
default:
return Search(query.Search, t => !t.Completed);
}
}
#endregion
public void Init(PluginInitContext context)
{
_todos = new Todos(context, _setting);
}
public Control CreateSettingPanel()
{
return new FilePathSetting(_setting, _storage);
}
#region Utils
private static List<Result> Search(string search, Func<Todo, bool> conditions = null)
{
var s = search;
var results = _todos.Find(t =>
t.Content.IndexOf(s, StringComparison.OrdinalIgnoreCase) >= 0
&& (conditions?.Invoke(t) ?? true));
if (!string.IsNullOrEmpty(s) && !results.Any())
{
results.Insert(0, AddResult(s));
}
return results;
}
private static Result AddResult(string content)
{
return new Result
{
Title = $"add new item \"{content}\"",
SubTitle = "",
IcoPath = _todos.GetFilePath(),
Action = c =>
{
_todos.Add(new Todo
{
Content = content,
Completed = false,
CreatedTime = DateTime.Now
});
return false;
}
};
}
public void Save()
{
_storage.Save();
}
#endregion
}
}