/
MainWindow.xaml.cs
153 lines (137 loc) · 5.13 KB
/
MainWindow.xaml.cs
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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.TextFormatting;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CoreTweet;
using CoreTweet.Streaming;
using MouseKeyboardActivityMonitor;
using MouseKeyboardActivityMonitor.WinApi;
using KeyEventArgs = System.Windows.Forms.KeyEventArgs;
namespace SCNTwitter
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
private KeyboardHookListener keyboardHookListener = new KeyboardHookListener(new GlobalHooker());
private ObservableCollection<TweetModel> messages = new ObservableCollection<TweetModel>();
private Twitter twitter;
public MainWindow()
{
InitializeComponent();
BindingOperations.EnableCollectionSynchronization(messages, new object());
DataContext = messages;
twitter = Twitter.CreateFromConfig("Resources\\account.config");
twitter.GetTokens().Streaming.UserAsObservable()
.Where((StreamingMessage m) => m.Type == MessageType.Create)
.Cast<StatusMessage>()
.Subscribe(m => messages.Insert(0, new TweetModel()
{
UserName = m.Status.User.Name,
UserId = "@" + m.Status.User.ScreenName,
Text = m.Status.Text,
UserIconUrl = m.Status.User.ProfileImageUrlHttps,
Content = m.Status
}));
keyboardHookListener.Enabled = true;
keyboardHookListener.KeyDown += Window_Active;
}
public void Window_Active(object sender, KeyEventArgs args)
{
if (!args.Control || !args.Alt) return;
if (args.KeyValue == 'P')
{
this.Topmost = true;
this.Topmost = false;
this.Activate();
listBox.Focus();
listBox.SelectedIndex = 0;
}
if (args.KeyValue == 'L' || args.KeyValue == 'R')
{
if (!this.IsActive)
{
listBox.SelectedIndex = 0;
}
TweetModel tweetModel = (TweetModel)listBox.SelectedValue;
if (args.KeyValue == 'L')
{
twitter.Send_Like(tweetModel.Content.Id);
}
else
{
twitter.Send_Retweet(tweetModel.Content.Id);
}
}
}
public void Button_Click(object sender, RoutedEventArgs routedEventArgs)
{
twitter.Tweet(textBox.Text);
}
public void Like_Click(object sender, RoutedEventArgs routedEventArgs)
{
TweetModel tweetModel = (TweetModel) (sender as Button).DataContext;
twitter.Send_Like(tweetModel.Content.Id);
}
public void Retweet_Click(object sender, RoutedEventArgs routedEventArgs)
{
TweetModel tweetModel = (TweetModel)(sender as Button).DataContext;
twitter.Send_Retweet(tweetModel.Content.Id);
}
}
public struct TweetModel
{
public string UserName { get; set; }
public string UserId { get; set; }
public string Text { get; set; }
public string UserIconUrl { get; set; }
public Status Content { get; set; }
}
public class Twitter
{
private Tokens tokens;
public Twitter(string ck, string cs, string at, string ats)
{
tokens = CoreTweet.Tokens.Create(ck, cs, at, ats);
}
public void Tweet(string text)
{
tokens.Statuses.Update(new { status = text });
}
public void Send_Like(long id)
{
tokens.Favorites.Create(new {id = id});
}
public void Send_Retweet(long id)
{
tokens.Statuses.Retweet(new {id = id});
}
public Tokens GetTokens()
{
return tokens;
}
public static Twitter CreateFromConfig(string filename)
{
var configFileMap = new ExeConfigurationFileMap { ExeConfigFilename = @filename };
var openMappedExeConfiguration = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
var config = openMappedExeConfiguration.AppSettings.Settings;
return new Twitter(config["ConsumerKey"].Value, config["ConsumerSecret"].Value,
config["AccessToken"].Value, config["AccessTokenSecret"].Value);
}
}
}