Example #1
0
        private void CommandInitialize()
        {
            AddAccountCommand = new RelayCommand <TwitterAccount>(account =>
            {
                account.OnTweetBegin  += (status) => MessengerInstance.Send <PostStatusBase>(status, "OnTweetBegin");
                account.OnTweetFailed += async(status) =>
                {
                    await NotificationAsync(new NotificationMessage {
                        TweetMessage = Tweet.ZeroTweet,
                        Message      = "ツイートの送信に失敗しました"
                    });
                    MessengerInstance.Send <PostStatusBase>(status, "OnTweetFailed");
                };
                account.OnTweetCompleted += (status) => MessengerInstance.Send <PostStatusBase>(status, "OnTweetCompleted");
                account.OnHttpGetError   += async(e) =>
                {
                    await NotificationAsync(new NotificationMessage
                    {
                        TweetMessage = Tweet.ZeroTweet,
                        Message      = e.Message
                    });

                    MessengerInstance.Send <HttpRequestException>(e, "OnHttpGetError");
                };
                account.OnHttpPostError += async(e) =>
                {
                    await NotificationAsync(new NotificationMessage
                    {
                        TweetMessage = Tweet.ZeroTweet,
                        Message      = e.Message
                    });
                    MessengerInstance.Send <HttpRequestException>(e, "OnHttpPostError");
                };
                account.OnUserStreamHttpError += async(e) =>
                {
                    await NotificationAsync(new NotificationMessage
                    {
                        TweetMessage = Tweet.ZeroTweet,
                        Message      = e.Message
                    });
                    MessengerInstance.Send <HttpRequestException>(e, "OnUserStreamHttpError");
                };
                account.OnFollowStreamHttpError += async(e) =>
                {
                    await NotificationAsync(new NotificationMessage
                    {
                        TweetMessage = Tweet.ZeroTweet,
                        Message      = e.Message
                    });
                    MessengerInstance.Send <HttpRequestException>(e, "OnFollowStreamHttpError");
                };
                account.ChangeUserStreamEvent += async(state) =>
                {
                    switch (state)
                    {
                    case StreamState.Connect:
                        // await NotificationAsync("Stream Connected");
                        await SharedDispatcher.RunAsync(() =>
                        {
                            ConnectionStatusStr = "Stream Connected";
                        });
                        break;

                    case StreamState.TryConnect:
                        // await NotificationAsync("Stream TryConnected");
                        await SharedDispatcher.RunAsync(() =>
                        {
                            ConnectionStatusStr = "Stream TryConnected";
                        });
                        break;

                    case StreamState.DisConnect:
                        // await NotificationAsync("Stream DisConnect");
                        await SharedDispatcher.RunAsync(() =>
                        {
                            ConnectionStatusStr = "Stream DisConnected";
                        });
                        break;
                    }
                };

                this.AccountList.Add(account);
            });

            DeleteAccountCommand = new RelayCommand <TwitterAccount>(account =>
            {
                this.AccountList.Remove(account);
            });

            PostStatusCommand = new RelayCommand(async() =>
            {
                if (!AccountList.Where(q => q.IsActive == true).Select(q => q).Any())
                {
                    Tweet tweet = Tweet.ZeroTweet;
                    await NotificationAsync(new NotificationMessage {
                        TweetMessage = Tweet.ZeroTweet,
                        Message      = "アカウントが選択されていません"
                    });
                }
                else
                {
                    PostStatusBase status = TwitterUIComponent.GetPostStatus();
                    status.Status        += " " + Setting.Footer;
                    DeletePostImageCommand.Execute(null);
                    TwitterUIComponent.ResetPostText();

                    await ActionSelectedAccountAsync(async(client) =>
                    {
                        if (status is PostStatus)
                        {
                            await client.UpdateStatusAsync(status as PostStatus);
                        }
                        else if (status is PostStatusWithReply)
                        {
                            await client.UpdateStatusAsync(status as PostStatusWithReply);
                        }
                        else if (status is PostStatusMedia)
                        {
                            await client.UpdateStatusWithMediaAsync(status as PostStatusMedia);
                        }
                        else if (status is PostStatusMediaWithReply)
                        {
                            await client.UpdateStatusWithMediaAsync(status as PostStatusMediaWithReply);
                        }
                    });
                }
            });


            AddTimelineTabCommand = new RelayCommand <TimelineTab>(tab =>
            {
                AddTimelineTab(tab);
            });

            DeleteTimelineTabCommand = new RelayCommand <TimelineTab>(tab =>
            {
                foreach (TimelineBase timeline in tab.TimelineList)
                {
                    timeline.DeleteTimeLine();
                }
                TimelineListTab.Remove(tab);
                if (TimelineListTab.Count > 0)
                {
                    var t = TimelineListTab.Take(1).Single();
                    ChangeTabAsync(t);
                }
                MessengerInstance.Send <TimelineTab>(tab, "DeleteTimelineTab");
            });

            AddTimelineCommand = new RelayCommand <TimelineBase>(timeline =>
            {
                GetNowTab().TimelineList.Add(timeline);
                timeline.AddTimeLine();
                ResetTimeline();
                MessengerInstance.Send <TimelineBase>(timeline, "AddTimeline");
            });

            DeleteTimelineCommand = new RelayCommand <TimelineBase>(timeline =>
            {
                GetNowTab().TimelineList.Remove(timeline);
                timeline.DeleteTimeLine();
                ResetTimeline();
                MessengerInstance.Send <TimelineBase>(timeline, "DeleteTimeline");
            });

            ChangeTabCommand = new RelayCommand <TimelineTab>(tab =>
            {
                ChangeTabAsync(tab);
                MessengerInstance.Send <TimelineTab>(tab, "ChangeTab");
            });

            EditTimelineTabCommand = new RelayCommand <TimelineTab>(tab =>
            {
                this.nowEditTimelineTab = tab;
                MessengerInstance.Send <TimelineTab>(tab, "EditTimelineTab");
            });

            EditTimelineCommand = new RelayCommand <TimelineBase>(timeline =>
            {
                this.nowEditTimeline = timeline;
                MessengerInstance.Send <TimelineBase>(timeline, "EditTimeline");
            });

            FavoriteCommand = new RelayCommand <Tweet>(async tweet =>
            {
                await ActionSelectedAccountWithUiAsync(async client =>
                {
                    if (tweet.favorited == false)
                    {
                        tweet.favorited = true;
                        await client.CreateFavoriteAsync(tweet);
                    }
                    else
                    {
                        tweet.favorited = false;
                        await client.DestroyFavoriteAsync(tweet);
                    }
                });
            });

            RetweetCommand = new RelayCommand <Tweet>(async tweet =>
            {
                await ActionSelectedAccountWithUiAsync(async client =>
                {
                    if (tweet.retweeted == false)
                    {
                        tweet.retweeted = true;
                        await client.CreateRetweetAsync(tweet);
                    }
                    else
                    {
                        tweet.retweeted = false;
                        //await client.DestroyRetweetAsync(tweet);
                    }
                });
            });


            QuoteCommand = new RelayCommand <Tweet>(tweet => this.TwitterUIComponent.SetPostText(" RT @" + tweet.user.screen_name + " " + tweet.text));

            TweetDetailCommand = new RelayCommand <TweetDetailParameter>(async tweet =>
            {
                this.MessengerInstance.Send <TweetDetail>(TweetDetail, "ShowTweetDetail");
                this.TweetDetail.OwnerAccount = GetAccount(tweet.OwnerScreenName);
                this.TweetDetail.Set(new TimelineRow(tweet.tweet, "", Setting, CallRowAction),
                                     (await GetAccount(tweet.OwnerScreenName).TwitterClient.GetConversationAsync(tweet.tweet)).Select(q => new TimelineRow(q, tweet.OwnerScreenName, Setting, CallRowAction)).ToList());
            });

            ReplyCommand = new RelayCommand <Tweet>(tweet =>
            {
                this.TwitterUIComponent.AddPostText("@" + tweet.user.screen_name + " ", tweet);
                Messenger.Default.Send <int>(TwitterUIComponent.PostText.Length, "SetPostTextCursor");
            });

            DescriptionDommand = new RelayCommand <Tweet>(tweet =>
            {
                this.TwitterUIComponent.SetPostText("(@" + tweet.user.screen_name + ")");
                Messenger.Default.Send <int>(0, "SetPostTextCursor");
            });

            UserDetailCommand = new RelayCommand <UserDetailParameter>(async screen_name =>
            {
                this.MessengerInstance.Send <UserDetail>(UserDetail, "ShowUserDetail");
                this.UserDetail.OwnerAccount = GetAccount(screen_name.OwnerScreenName);
                this.UserDetail.Set(await GetAccount(screen_name.OwnerScreenName).TwitterClient.GetAccountInformationAsync(screen_name.ScreenName), (await GetAccount(screen_name.OwnerScreenName).TwitterClient.GetUserTimeLineAsync(screen_name.ScreenName, 100)).Select(q => new TimelineRow(q, screen_name.OwnerScreenName, Setting, CallRowAction)).ToList());
            });

            SearchCommand = new RelayCommand <SearchDetailParameter>(async searchWord =>
            {
                this.MessengerInstance.Send <SearchDetail>(SearchDetail, "ShowSearchDetail");
                this.SearchDetail.OwnerAccount = GetAccount(searchWord.OwnerScreenName);
                this.SearchDetail.Set(searchWord.SearchWord, (await GetAccountFirst().TwitterClient.GetSearchAsync(searchWord.SearchWord, 100)).statuses.Select(q => new TimelineRow(q, searchWord.OwnerScreenName, Setting, CallRowAction)).ToList());
            });

            BrowseCommand = new RelayCommand <string>(url => this.MessengerInstance.Send <string>(url, "BrowsUrl"));

            BeginAuthCommand = new RelayCommand(async() =>
            {
                string requestUrl = await Authorizer.BeginAuthorizedAsync(this.ConsumerData);
                this.MessengerInstance.Send <string>(requestUrl, "GetAuthorizedUrl");
            });
            PinAuthCommand = new RelayCommand <string>(async pin =>
            {
                TokenResponse <AccessToken> res = null;
                try
                {
                    res = await Authorizer.PinAuthorizedAsync(pin);

                    var name    = res.ExtraData["screen_name"].ElementAt(0);
                    var account = new TwitterAccount(ConsumerData, new AccessTokenData(res.Token.Key, res.Token.Secret), name);
                    await account.InitializeAsync();
                    account.IsActive = true;
                    AddAccountCommand.Execute(account);
                    var tab = new TimelineTab(name + " - MainTab", CallTabAction, CallTimelineAction, CallRowAction);
                    AddTimelineTab(tab);

                    AddTimelineCommand.Execute(new HomeTimeline(account, "Home", tab.TabTitle, Setting, CallTimelineAction, CallRowAction));

                    AddTimelineCommand.Execute(new MentionTimeline(account, "Mention", tab.TabTitle, Setting, CallTimelineAction, CallRowAction)
                    {
                        IsNewNotification = true
                    });


                    var tab2 = new TimelineTab(name + " - SubTab", CallTabAction, CallTimelineAction, CallRowAction);
                    AddTimelineTab(tab2);

                    AddTimelineCommand.Execute(new NotificationTimeline(account, "Notification", tab2.TabTitle, Setting, CallTimelineAction, CallRowAction)
                    {
                        IsNewNotification = true
                    });
                    AddTimelineCommand.Execute(new UserTimeline(account, "@" + name, tab2.TabTitle, name, Setting, CallTimelineAction, CallRowAction));

                    ChangeTabAsync(tab);

                    this.MessengerInstance.Send <bool>(true, "AuthorizedCompleted");
                }
                catch (Exception e)
                {
                    this.MessengerInstance.Send <bool>(false, "AuthorizedCompleted");
                }
            });

            SetPostImageCommand = new RelayCommand <PostMedia>(media =>
            {
                TwitterUIComponent.SetPostMedia(media);
            });

            DeletePostImageCommand = new RelayCommand(() =>
            {
                TwitterUIComponent.ResetPostMedia();
                MessengerInstance.Send <string>("", "DeletePostImage");
            });



            SelectSuggestCommand = new RelayCommand <string>(item =>
            {
                if (item.StartsWith("@") && TwitterUIComponent.PostText.Contains("@"))
                {
                    var ss    = TwitterUIComponent.PostText;
                    int index = ss.LastIndexOf("@", System.StringComparison.Ordinal);
                    string s  = ss.Substring(0, index);
                    TwitterUIComponent.PostText = s + item + " ";
                }
                else if (item.StartsWith("#") && TwitterUIComponent.PostText.Contains("#"))
                {
                    var ss    = TwitterUIComponent.PostText;
                    int index = ss.LastIndexOf("#", System.StringComparison.Ordinal);
                    string s  = ss.Substring(0, index);
                    TwitterUIComponent.PostText = s + item + " ";
                }
            });

            ToggleAccountActivityCommand = new RelayCommand <TwitterAccount>(account => account.ToggleActivity());

            DirectMessageDetailCommand = new RelayCommand <DirectMessageDetailParameter>(async dm =>
            {
                MessengerInstance.Send <DirectMessage>(dm.Message, "ShowDirectMessageDetail");
                this.directMesssageDetail.OwnerAccount = GetAccount(dm.OwnerScreenName);
                this.directMesssageDetail.DMessage     = dm.Message;
                this.directMesssageDetail.Conversations.Clear();
                var dms  = (await GetAccount(dm.OwnerScreenName).TwitterClient.GetDirectMessages());
                var conv = dms.Where(q => (q.sender_screen_name == dm.Message.sender_screen_name &&
                                           q.recipient_screen_name == dm.Message.recipient_screen_name) ||
                                     (q.sender_screen_name == dm.Message.recipient_screen_name &&
                                      q.recipient_screen_name == dm.Message.sender_screen_name)
                                     ).Select(q => q).ToList();
                foreach (var c in conv)
                {
                    this.directMesssageDetail.Conversations.Add(new DirectMessageRow(c, dm.OwnerScreenName, Setting, CallRowAction));
                }
            });

            SendDirectMessageCommand = new RelayCommand <SendDirectMessage>(async message =>
            {
                await GetAccount(message.SenderScreenName).TwitterClient.PostDirectMessageNew(message.RecipientScreenName, message.Message);
            });

            ChangeTimelineWidthCommand = new RelayCommand <double>(size =>
            {
                this.TimelineWidth = size;
                foreach (var timeline in NowTimelineList)
                {
                    timeline.TimelineWidth = size;
                }
            });

            ExitCommand = new RelayCommand(async() =>
            {
                await SaveSettingDataAsync();
                await SaveTwitterDataAsync();

                Application.Current.Exit();
            });

            AddSuggestPostText = new RelayCommand <string>(str =>
            {
                TwitterUIComponent.SetPostText(TwitterUIComponent.PostText.Substring(0, TwitterUIComponent.PostText.Count() - 1));
                TwitterUIComponent.AddPostText(str + " ");
            });

            ChangeUIBrushImageCommand = new RelayCommand <string>(str =>
            {
                MessengerInstance.Send <string>(str, "ChangeUIBrushImage");
            });

            ResetThemeSettingCommand = new RelayCommand(() =>
            {
                Setting.SettingInitialize();
            });

            PurchaseApplicationThemeCommand = new RelayCommand(async() =>
            {
                if (!LicenseInfo.ProductLicenses["ApplicationTheme"].IsActive)
                {
                    var result = await CurrentApp.RequestProductPurchaseAsync("ApplicationTheme");

                    if (result.Status == ProductPurchaseStatus.Succeeded)
                    {
                        MessageDialog dialog = new MessageDialog("Thank you for purchase Neuronia CusomAppTheme! Enjoy Neuronia and Twitter Life!", "Thank You!");
                        await dialog.ShowAsync();
                        IsPurchase = LicenseInfo.ProductLicenses["ApplicationTheme"].IsActive;
                    }
                }
            });

            AddMuteAccountCommand = new RelayCommand <string>(async(screenName) =>
            {
                Setting.MuteAccountList.Add(screenName);
                await NotificationAsync(new NotificationMessage {
                    TweetMessage = Tweet.ZeroTweet,
                    Message      = "Mute Complate @" + screenName
                });
            });

            CopyClipBoardCommand = new RelayCommand <string>(async str =>
            {
                var package = new DataPackage();
                package.SetText(str);
                Clipboard.SetContent(package);
                await NotificationAsync(new NotificationMessage {
                    TweetMessage = Tweet.ZeroTweet,
                    Message      = "Copy Completed! " + str
                });
            });

            NextTabCommand = new RelayCommand(() =>
            {
                var num = TimelineListTab.TakeWhile(q => q.IsNowTab).Select(q => q).Count() - 1;
                if (num != TimelineListTab.Count - 1)
                {
                    ChangeTabAsync(TimelineListTab.ElementAt(++num));
                }
            });

            PrevTabCommand = new RelayCommand(() =>
            {
                var num = TimelineListTab.TakeWhile(q => q.IsNowTab).Select(q => q).Count() + 1;
                if (num != 0)
                {
                    ChangeTabAsync(TimelineListTab.ElementAt(--num));
                }
            });
        }