Skip to content
This repository has been archived by the owner on Apr 15, 2021. It is now read-only.

Commit

Permalink
Implement message loading logic in explorer
Browse files Browse the repository at this point in the history
  • Loading branch information
x2bool committed Jan 7, 2019
1 parent 4652e06 commit 5686ae1
Show file tree
Hide file tree
Showing 19 changed files with 380 additions and 226 deletions.
47 changes: 0 additions & 47 deletions src/Tel.Egram.Model/Messenger/Explorer/ExplorerLogic.cs

This file was deleted.

53 changes: 30 additions & 23 deletions src/Tel.Egram.Model/Messenger/Explorer/ExplorerModel.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
using System;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using DynamicData;
using DynamicData.Binding;
using PropertyChanged;
using ReactiveUI;
using Tel.Egram.Model.Messenger.Explorer.Items;
using Tel.Egram.Model.Messenger.Explorer.Loaders;
using Tel.Egram.Services.Messaging.Chats;
using Tel.Egram.Services.Utils;
using Tel.Egram.Services.Utils.Reactive;

namespace Tel.Egram.Model.Messenger.Explorer
{
Expand All @@ -14,44 +19,46 @@ public class ExplorerModel : ISupportsActivation
{
public bool IsVisible { get; set; } = true;

public Range VisibleRange { get; set; }

public ObservableCollectionExtended<ItemModel> Items { get; set; }
= new ObservableCollectionExtended<ItemModel>();

public SourceList<ItemModel> SourceItems { get; set; }
= new SourceList<ItemModel>();

public Range VisibleRange { get; set; }

public ExplorerModel(Aggregate aggregate)
{
// this.WhenActivated(disposables =>
// {
// this.BindSource()
// .DisposeWith(disposables);
//
// this.BindVisibleRangeChanges(aggregate)
// .DisposeWith(disposables);
//
// this.InitMessageLoading(aggregate)
// .DisposeWith(disposables);
// });
}

public ExplorerModel(Chat chat)
{
this.WhenActivated(disposables =>
{
this.BindSource()
BindSource()
.DisposeWith(disposables);
// this.BindVisibleRangeChanges(chat)
// .DisposeWith(disposables);
this.InitMessageLoading(chat)
var conductor = new MessageLoaderConductor();
new InitMessageLoader(conductor)
.Bind(this, chat)
.DisposeWith(disposables);
new NextMessageLoader(conductor)
.Bind(this, chat)
.DisposeWith(disposables);
new PrevMessageLoader(conductor)
.Bind(this, chat)
.DisposeWith(disposables);
});
}

private IDisposable BindSource()
{
return SourceItems.Connect()
.SubscribeOn(RxApp.TaskpoolScheduler)
.ObserveOn(RxApp.MainThreadScheduler)
.Bind(Items)
.Accept();
}

private ExplorerModel()
{
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Tel.Egram.Model.Messenger.Explorer.Messages;
using Tel.Egram.Services.Messaging.Messages;

namespace Tel.Egram.Model.Messenger.Explorer
namespace Tel.Egram.Model.Messenger.Explorer.Factories
{
public interface IMessageModelFactory
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Tel.Egram.Model.Messenger.Explorer.Messages;
using Tel.Egram.Services.Messaging.Messages;

namespace Tel.Egram.Model.Messenger.Explorer
namespace Tel.Egram.Model.Messenger.Explorer.Factories
{
public partial class MessageModelFactory : IMessageModelFactory
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Tel.Egram.Model.Messenger.Explorer.Messages.Basic;
using Tel.Egram.Services.Messaging.Messages;

namespace Tel.Egram.Model.Messenger.Explorer
namespace Tel.Egram.Model.Messenger.Explorer.Factories
{
public partial class MessageModelFactory : IMessageModelFactory
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Tel.Egram.Model.Messenger.Explorer.Messages;
using Tel.Egram.Services.Messaging.Messages;

namespace Tel.Egram.Model.Messenger.Explorer
namespace Tel.Egram.Model.Messenger.Explorer.Factories
{
public partial class MessageModelFactory : IMessageModelFactory
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Tel.Egram.Model.Messenger.Explorer.Messages;
using Tel.Egram.Services.Messaging.Messages;

namespace Tel.Egram.Model.Messenger.Explorer
namespace Tel.Egram.Model.Messenger.Explorer.Factories
{
public partial class MessageModelFactory : IMessageModelFactory
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Tel.Egram.Model.Messenger.Explorer.Messages.Visual;
using Tel.Egram.Services.Messaging.Messages;

namespace Tel.Egram.Model.Messenger.Explorer
namespace Tel.Egram.Model.Messenger.Explorer.Factories
{
public partial class MessageModelFactory : IMessageModelFactory
{
Expand Down
17 changes: 0 additions & 17 deletions src/Tel.Egram.Model/Messenger/Explorer/IMessageManager.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Threading;
using DynamicData;
using ReactiveUI;
using Splat;
using Tel.Egram.Model.Messenger.Explorer.Factories;
using Tel.Egram.Model.Messenger.Explorer.Messages;
using Tel.Egram.Services.Messaging.Chats;
using Tel.Egram.Services.Messaging.Messages;
using Tel.Egram.Services.Utils.Reactive;

namespace Tel.Egram.Model.Messenger.Explorer.Loaders
{
public class InitMessageLoader
{
private readonly MessageLoaderConductor _conductor;

private readonly IChatLoader _chatLoader;
private readonly IMessageLoader _messageLoader;
private readonly IMessageModelFactory _messageModelFactory;

public InitMessageLoader(
MessageLoaderConductor conductor)
: this(
Locator.Current.GetService<IChatLoader>(),
Locator.Current.GetService<IMessageLoader>(),
Locator.Current.GetService<IMessageModelFactory>())
{
_conductor = conductor;
}

public InitMessageLoader(
IChatLoader chatLoader,
IMessageLoader messageLoader,
IMessageModelFactory messageModelFactory)
{
_chatLoader = chatLoader;
_messageLoader = messageLoader;
_messageModelFactory = messageModelFactory;
}

public IDisposable Bind(
ExplorerModel model,
Chat chat)
{
return model.WhenAnyValue(m => m.VisibleRange)
.Where(r => model.SourceItems.Count == 0) // only initial
.Where(r => !_conductor.IsBusy) // ignore if other load are already in progress
.Synchronize(_conductor.Locker)
.SelectSeq(r => StartLoading(chat))
.ObserveOn(RxApp.MainThreadScheduler)
.Accept(list => HandleLoading(model, list));
}

private IObservable<IList<MessageModel>> StartLoading(
Chat chat)
{
Console.WriteLine("Start init: {0}", Thread.CurrentThread.ManagedThreadId);
_conductor.IsBusy = true;

return LoadInitMessages(chat)
.ObserveOn(RxApp.TaskpoolScheduler)
.SubscribeOn(RxApp.MainThreadScheduler)
.Finally(() =>
{
_conductor.IsBusy = false;
});
}

private void HandleLoading(
ExplorerModel model,
IList<MessageModel> messageModels)
{
Console.WriteLine("End init");
model.SourceItems.AddRange(messageModels);
}

private IObservable<IList<MessageModel>> LoadInitMessages(
Chat chat)
{
return _chatLoader.LoadChat(chat.ChatData.Id)
.SelectSeq(c => GetInitMessages(c)
.Select(_messageModelFactory.CreateMessage)
.ToList())
.Select(list => list.Reverse().ToList());
}

private IObservable<Message> GetInitMessages(
Chat chat)
{
var fromMessageId = chat.ChatData.LastReadInboxMessageId;
return _messageLoader.LoadInitMessages(chat, fromMessageId, 32);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Tel.Egram.Model.Messenger.Explorer.Loaders
{
public class MessageLoaderConductor
{
public object Locker { get; } = new object();

public bool IsBusy { get; set; }
}
}
Loading

0 comments on commit 5686ae1

Please sign in to comment.