<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using Akavache;
using Gitter.Models;
using ReactiveUI;
using Splat;

namespace Gitter.ViewModels
{
    public class MessagesViewModel : ReactiveObject, IRoutableViewModel
    {
        private string messageText;

        public MessagesViewModel(Room room, IGitterApi api = null, IScreen hostScreen = null)
        {
            this.HostScreen = hostScreen ?? Locator.Current.GetService<IScreen>();

            this.Messages = new ReactiveList<MessageViewModel>();
            this.UrlPathSegment = room.name;

            IObservable<string> accessToken = GitterApi.GetAccessToken().PublishLast().RefCount();

            this.LoadMessages = ReactiveCommand.CreateAsyncObservable(_ => accessToken.SelectMany(token => (api ?? GitterApi.UserInitiated).GetMessages(room.id, token)));

            this.SendMessage = ReactiveCommand.CreateAsyncTask(async _ =>
            {
                await (api ?? GitterApi.UserInitiated).SendMessage(room.id, new SendMessage(this.MessageText), await accessToken);
                this.MessageText = String.Empty;
            });

            this.LoadMessages.FirstAsync()
                .Concat(this.SendMessage.StartWith(Unit.Default).SelectMany(_ => Observable.Interval(TimeSpan.FromSeconds(10), RxApp.TaskpoolScheduler).Select(__ => Unit.Default)).Merge(this.SendMessage).SelectMany(__ => this.LoadMessages.ExecuteAsync()))
                .Select(x => x.Select(y => new MessageViewModel(y)))
                .Subscribe(x =>
                {
                    using (this.Messages.SuppressChangeNotifications())
                    {
                        this.Messages.Clear();
                        this.Messages.AddRange(x);
                    }
                });
        }

        public IScreen HostScreen { get; private set; }

        public ReactiveCommand<IReadOnlyList<Message>> LoadMessages { get; private set; }

        public IReactiveList<MessageViewModel> Messages { get; private set; }

        public string MessageText
        {
            get { return this.messageText; }
            set { this.RaiseAndSetIfChanged(ref this.messageText, value); }
        }

        public ReactiveCommand<Unit> SendMessage { get; private set; }

        public string UrlPathSegment { get; private set; }

        private IObservable<Message> StreamMessages(string roomId, string accessToken)
        {
            var streamApi = new GitterStreamingApi();

            return BlobCache.Secure.GetLoginAsync("Gitter")
                .SelectMany(x => streamApi.ObserveMessages(roomId, accessToken));
        }
    }
}<commit_msg>Explain what we're doing<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using Akavache;
using Gitter.Models;
using ReactiveUI;
using Splat;

namespace Gitter.ViewModels
{
    public class MessagesViewModel : ReactiveObject, IRoutableViewModel
    {
        private string messageText;

        public MessagesViewModel(Room room, IGitterApi api = null, IScreen hostScreen = null)
        {
            this.HostScreen = hostScreen ?? Locator.Current.GetService<IScreen>();

            this.Messages = new ReactiveList<MessageViewModel>();
            this.UrlPathSegment = room.name;

            IObservable<string> accessToken = GitterApi.GetAccessToken().PublishLast().RefCount();

            this.LoadMessages = ReactiveCommand.CreateAsyncObservable(_ => accessToken.SelectMany(token => (api ?? GitterApi.UserInitiated).GetMessages(room.id, token)));

            this.SendMessage = ReactiveCommand.CreateAsyncTask(async _ =>
            {
                await (api ?? GitterApi.UserInitiated).SendMessage(room.id, new SendMessage(this.MessageText), await accessToken);
                this.MessageText = String.Empty;
            });

            this.LoadMessages.FirstAsync()
                // Fetch the messages every 10 seconds or when we've sent a message
                //
                // This is a workaround till the message streaming works
                .Concat(this.SendMessage.StartWith(Unit.Default).SelectMany(_ => Observable.Interval(TimeSpan.FromSeconds(10), RxApp.TaskpoolScheduler).Select(__ => Unit.Default)).Merge(this.SendMessage)
                    .SelectMany(__ => this.LoadMessages.ExecuteAsync()))
                .Select(x => x.Select(y => new MessageViewModel(y)))
                .Subscribe(x =>
                {
                    using (this.Messages.SuppressChangeNotifications())
                    {
                        this.Messages.Clear();
                        this.Messages.AddRange(x);
                    }
                });
        }

        public IScreen HostScreen { get; private set; }

        public ReactiveCommand<IReadOnlyList<Message>> LoadMessages { get; private set; }

        public IReactiveList<MessageViewModel> Messages { get; private set; }

        public string MessageText
        {
            get { return this.messageText; }
            set { this.RaiseAndSetIfChanged(ref this.messageText, value); }
        }

        public ReactiveCommand<Unit> SendMessage { get; private set; }

        public string UrlPathSegment { get; private set; }

        private IObservable<Message> StreamMessages(string roomId, string accessToken)
        {
            var streamApi = new GitterStreamingApi();

            return BlobCache.Secure.GetLoginAsync("Gitter")
                .SelectMany(x => streamApi.ObserveMessages(roomId, accessToken));
        }
    }
}
{'fraction_non_alphanumeric': 0.09823321554770319, 'fraction_numerical': 0.0010600706713780918, 'mean_word_length': 2.3556609365737997, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '4213241', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1417, 'n_words': 295}