This repository has been archived by the owner on Apr 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement message loading logic in explorer
- Loading branch information
Showing
19 changed files
with
380 additions
and
226 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...essenger/Explorer/IMessageModelFactory.cs → ...xplorer/Factories/IMessageModelFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
98 changes: 98 additions & 0 deletions
98
src/Tel.Egram.Model/Messenger/Explorer/Loaders/InitMessageLoader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/Tel.Egram.Model/Messenger/Explorer/Loaders/MessageLoaderConductor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
Oops, something went wrong.