<commit_before>﻿using MaterialDesignThemes.Wpf;
using Mattermost.Models;
using Mattermost.Utils;
using Mattermost.Views.Dialogs;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;

namespace Mattermost.ViewModels.Models
{
    class ChannelViewModel : BaseViewModel
    {
        public string ID
        {
            get { return channel.ID; }
        }
        public string Name
        {
            get { return channel.Name; }
        }
        public string DisplayName
        {
            get { return channel.DisplayName; }
        }
        public string Header
        {
            get { return channel.Header; }
        }
        public bool IsSelected
        {
            get { return GetValue<bool>(); }
            set { SetValue(value); }
        }
        public ICommand SendMessageCommand
        {
            get
            {
                if (sendMessage == null)
                    sendMessage = new RelayCommand(SendMessage, () =>
                    {
                        return !string.IsNullOrWhiteSpace(Message);
                    });

                return sendMessage;
            }
        }
        public double ScrollableHeight
        {
            get { return GetValue<double>(); }
            set
            {
                double oldVal = GetValue<double>();

                if (SetValue(value))
                {
                    if (oldVal == ScrollPosition)
                        ScrollPosition = value;
                }
            }
        }
        public double ScrollPosition
        {
            get { return GetValue<double>(); }
            set
            {
                if (SetValue(value) && value < 50 && !loadingMore)
                {
                    loadingMore = true;
                }
            }
        }

        public string Message
        {
            get { return GetValue<string>(); }
            set { SetValue(value); }
        }
        public ObservableCollection<PostViewModel> Posts
        {
            get { return GetValue<ObservableCollection<PostViewModel>>(); }
            private set { SetValue(value); }
        }

        Channel channel;
        RelayCommand sendMessage;
        bool loadingMore;

        public ChannelViewModel(Channel channel)
        {
            this.channel = channel;

            GetPosts();

            ScrollPosition = 10000000;
        }

        public void WebSocketMessage(WebSocketMessage message)
        {
            switch (message.Action)
            {
                case MessageAction.Posted:
                    Post post = JsonConvert.DeserializeObject<Post>(message.Properties.post.ToString());
                    PostViewModel lastPost = Posts.Last();
                    bool datesMatch = lastPost.Timestamp.Date == post.Timestamp.Date;

                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        if (!datesMatch)
                            Posts.Add(new PostViewModel(post.Timestamp));

                        Posts.Add(new PostViewModel(post, lastPost, !datesMatch, this));
                    });
                    break;
            }
        }

        async void GetPosts()
        {
            APIResponse<List<Post>> response = await MattermostAPI.GetPosts(ID, 0, 30);

            if (response.Success)
            {
                List<PostViewModel> posts = new List<PostViewModel>();
                Post lastPost = null;
                DateTime lastDate = DateTime.MinValue;
                bool newDay = false;

                foreach (Post post in response.Value)
                {
                    newDay = false;

                    if (post.Timestamp.Date != lastDate)
                    {
                        posts.Add(new PostViewModel(post.Timestamp));
                        lastDate = post.Timestamp.Date;
                        newDay = true;
                    }

                    posts.Add(new PostViewModel(post, lastPost, newDay, this));

                    lastPost = post;
                }

                Posts = new ObservableCollection<PostViewModel>(posts);
            }
        }

        async void GetEarlierPosts()
        {

        }

        async void SendMessage()
        {
            APIResponse response = await MattermostAPI.PostMessage(channel, Message);

            SimpleMessageDialog view = new SimpleMessageDialog()
            {
                DataContext = "Testing"
            };

            await DialogHost.Show(view, "RootDialog", (object sender, DialogClosingEventArgs args) => { });
        }
    }
}<commit_msg>Removed the random message box after sending a message<commit_after>﻿using MaterialDesignThemes.Wpf;
using Mattermost.Models;
using Mattermost.Utils;
using Mattermost.Views.Dialogs;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;

namespace Mattermost.ViewModels.Models
{
    class ChannelViewModel : BaseViewModel
    {
        public string ID
        {
            get { return channel.ID; }
        }
        public string Name
        {
            get { return channel.Name; }
        }
        public string DisplayName
        {
            get { return channel.DisplayName; }
        }
        public string Header
        {
            get { return channel.Header; }
        }
        public bool IsSelected
        {
            get { return GetValue<bool>(); }
            set { SetValue(value); }
        }
        public ICommand SendMessageCommand
        {
            get
            {
                if (sendMessage == null)
                    sendMessage = new RelayCommand(SendMessage, () =>
                    {
                        return !string.IsNullOrWhiteSpace(Message);
                    });

                return sendMessage;
            }
        }
        public double ScrollableHeight
        {
            get { return GetValue<double>(); }
            set
            {
                double oldVal = GetValue<double>();

                if (SetValue(value))
                {
                    if (oldVal == ScrollPosition)
                        ScrollPosition = value;
                }
            }
        }
        public double ScrollPosition
        {
            get { return GetValue<double>(); }
            set
            {
                if (SetValue(value) && value < 50 && !loadingMore)
                {
                    loadingMore = true;
                }
            }
        }

        public string Message
        {
            get { return GetValue<string>(); }
            set { SetValue(value); }
        }
        public ObservableCollection<PostViewModel> Posts
        {
            get { return GetValue<ObservableCollection<PostViewModel>>(); }
            private set { SetValue(value); }
        }

        Channel channel;
        RelayCommand sendMessage;
        bool loadingMore;

        public ChannelViewModel(Channel channel)
        {
            this.channel = channel;

            GetPosts();

            ScrollPosition = 10000000;
        }

        public void WebSocketMessage(WebSocketMessage message)
        {
            switch (message.Action)
            {
                case MessageAction.Posted:
                    Post post = JsonConvert.DeserializeObject<Post>(message.Properties.post.ToString());
                    PostViewModel lastPost = Posts.Last();
                    bool datesMatch = lastPost.Timestamp.Date == post.Timestamp.Date;

                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        if (!datesMatch)
                            Posts.Add(new PostViewModel(post.Timestamp));

                        Posts.Add(new PostViewModel(post, lastPost, !datesMatch, this));
                    });
                    break;
            }
        }

        async void GetPosts()
        {
            APIResponse<List<Post>> response = await MattermostAPI.GetPosts(ID, 0, 30);

            if (response.Success)
            {
                List<PostViewModel> posts = new List<PostViewModel>();
                Post lastPost = null;
                DateTime lastDate = DateTime.MinValue;
                bool newDay = false;

                foreach (Post post in response.Value)
                {
                    newDay = false;

                    if (post.Timestamp.Date != lastDate)
                    {
                        posts.Add(new PostViewModel(post.Timestamp));
                        lastDate = post.Timestamp.Date;
                        newDay = true;
                    }

                    posts.Add(new PostViewModel(post, lastPost, newDay, this));

                    lastPost = post;
                }

                Posts = new ObservableCollection<PostViewModel>(posts);
            }
        }

        async void GetEarlierPosts()
        {

        }

        async void SendMessage()
        {
            APIResponse response = await MattermostAPI.PostMessage(channel, Message);

            /*SimpleMessageDialog view = new SimpleMessageDialog()
            {
                DataContext = "Testing"
            };

            await DialogHost.Show(view, "RootDialog", (object sender, DialogClosingEventArgs args) => { });*/
        }
    }
}
{'fraction_non_alphanumeric': 0.08140629904781835, 'fraction_numerical': 0.0027205189913152663, 'mean_word_length': 1.2505297857311044, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4884810', 'n_tokens_mistral': 2326, 'n_tokens_neox': 2102, 'n_words': 484}