From 79e53ef0e85e0d2f4bd52eaf8380120434d7cd87 Mon Sep 17 00:00:00 2001 From: Asval Date: Sun, 7 Jul 2019 17:12:25 +0200 Subject: [PATCH] added tweet without image + fixed freeze when tweeting --- FModel/Forms/TweetForm.Designer.cs | 5 ++- FModel/Forms/TweetForm.cs | 50 ++++++++++++++++++++++++------ 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/FModel/Forms/TweetForm.Designer.cs b/FModel/Forms/TweetForm.Designer.cs index 666532b7..2e8e1ec1 100644 --- a/FModel/Forms/TweetForm.Designer.cs +++ b/FModel/Forms/TweetForm.Designer.cs @@ -181,13 +181,12 @@ this.label5.AutoSize = true; this.label5.Location = new System.Drawing.Point(12, 279); this.label5.Name = "label5"; - this.label5.Size = new System.Drawing.Size(37, 13); + this.label5.Size = new System.Drawing.Size(40, 13); this.label5.TabIndex = 5; - this.label5.Text = "Status"; + this.label5.Text = "Status:"; // // backgroundWorker1 // - this.backgroundWorker1.WorkerReportsProgress = true; this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.BackgroundWorker1_DoWork); this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.BackgroundWorker1_RunWorkerCompleted); // diff --git a/FModel/Forms/TweetForm.cs b/FModel/Forms/TweetForm.cs index 0eb1bfe0..0f32b093 100644 --- a/FModel/Forms/TweetForm.cs +++ b/FModel/Forms/TweetForm.cs @@ -11,6 +11,7 @@ namespace FModel.Forms public partial class TweetForm : Form { private static string ImagePath { get; set; } + private static TwitterService service { get; set; } public TweetForm() { @@ -49,18 +50,25 @@ namespace FModel.Forms private void BackgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { + label5.ForeColor = Color.FromArgb(255, 0, 0, 0); + string tweetText = ""; + Invoke(new Action(() => + { + tweetText = richTextBox1.Text; + })); + Properties.Settings.Default.tConsKey = textBox1.Text; Properties.Settings.Default.tConsSecret = textBox2.Text; Properties.Settings.Default.tToken = textBox4.Text; Properties.Settings.Default.tTokenSecret = textBox3.Text; Properties.Settings.Default.Save(); - TwitterService service = new TwitterService(Properties.Settings.Default.tConsKey, Properties.Settings.Default.tConsSecret); - service.AuthenticateWith(Properties.Settings.Default.tToken, Properties.Settings.Default.tTokenSecret); - Invoke(new Action(() => + if (service == null) { - label5.Text = "Status: Authentication to Twitter"; - })); + service = new TwitterService(Properties.Settings.Default.tConsKey, Properties.Settings.Default.tConsSecret); + service.AuthenticateWith(Properties.Settings.Default.tToken, Properties.Settings.Default.tTokenSecret); + UpdateStatus("Authentication to Twitter"); + } Dictionary myDict = new Dictionary(); if (pictureBox1.Image != null) @@ -68,28 +76,50 @@ namespace FModel.Forms myDict.Add(Path.GetFileNameWithoutExtension(ImagePath), new FileStream(ImagePath, FileMode.Open, FileAccess.Read, FileShare.Read)); } - Invoke(new Action(() => +#pragma warning disable CS0618 + if (pictureBox1.Image != null) { - #pragma warning disable CS0618 + UpdateStatus("Tweeting with " + Path.GetFileNameWithoutExtension(ImagePath)); TwitterStatus response = service.SendTweetWithMedia(new SendTweetWithMediaOptions { - Status = richTextBox1.Text, + Status = tweetText, Images = myDict }); - #pragma warning restore CS0618 - })); + } + else + { + UpdateStatus("Tweeting without image"); + TwitterStatus response = service.SendTweet(new SendTweetOptions + { + Status = tweetText + }); + } +#pragma warning restore CS0618 } private void BackgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Error != null) { + label5.ForeColor = Color.FromArgb(255, 244, 66, 66); label5.Text = "Status: " + e.Error.Message; } else { + label5.ForeColor = Color.FromArgb(255, 43, 135, 28); label5.Text = "Status: Tweeted"; } } + + private void UpdateStatus(string text) + { + if (label5.InvokeRequired) + { + BeginInvoke(new Action(UpdateStatus), new object[] { text }); + return; + } + + label5.Text = "Status: " + text; + } } }