using LeanCloud.Storage.Internal;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LeanCloud.Realtime.Internal;
namespace LeanCloud.Realtime
{
///
/// AVIMM essage pager.
///
public class AVIMMessagePager
{
///
/// Gets the query.
///
/// The query.
public AVIMMessageQuery Query { get; private set; }
///
/// Gets the size of the page.
///
/// The size of the page.
public int PageSize
{
get
{
return Query.Limit;
}
private set
{
Query.Limit = value;
}
}
///
/// Gets the current message identifier flag.
///
/// The current message identifier flag.
public string CurrentMessageIdFlag
{
get
{
return Query.StartMessageId;
}
private set
{
Query.StartMessageId = value;
}
}
///
/// Gets the current date time flag.
///
/// The current date time flag.
public DateTime CurrentDateTimeFlag
{
get
{
return Query.From;
}
private set
{
Query.From = value;
}
}
internal AVIMMessagePager()
{
}
///
/// Initializes a new instance of the class.
///
/// Conversation.
public AVIMMessagePager(AVIMConversation conversation)
: this()
{
Query = conversation.GetMessageQuery();
PageSize = 20;
CurrentDateTimeFlag = DateTime.Now;
}
///
/// Sets the size of the page.
///
/// The page size.
/// Page size.
public AVIMMessagePager SetPageSize(int pageSize)
{
PageSize = pageSize;
return this;
}
///
/// Previouses the async.
///
/// The async.
public Task> PreviousAsync()
{
return Query.FindAsync().OnSuccess(t =>
{
var headerMessage = t.Result.FirstOrDefault();
if (headerMessage != null)
{
CurrentMessageIdFlag = headerMessage.Id;
CurrentDateTimeFlag = headerMessage.ServerTimestamp.ToDateTime();
}
return t.Result;
});
}
///
/// from previous to lastest.
///
///
public Task> NextAsync()
{
return Query.ReverseFindAsync().OnSuccess(t =>
{
var tailMessage = t.Result.LastOrDefault();
if (tailMessage != null)
{
CurrentMessageIdFlag = tailMessage.Id;
CurrentDateTimeFlag = tailMessage.ServerTimestamp.ToDateTime();
}
return t.Result;
});
}
}
///
/// history message interator.
///
public class AVIMMessageQuery
{
///
/// Gets or sets the convsersation.
///
/// The convsersation.
public AVIMConversation Convsersation { get; set; }
///
/// Gets or sets the limit.
///
/// The limit.
public int Limit { get; set; }
///
/// Gets or sets from.
///
/// From.
public DateTime From { get; set; }
///
/// Gets or sets to.
///
/// To.
public DateTime To { get; set; }
///
/// Gets or sets the end message identifier.
///
/// The end message identifier.
public string EndMessageId { get; set; }
///
/// Gets or sets the start message identifier.
///
/// The start message identifier.
public string StartMessageId { get; set; }
internal AVIMMessageQuery()
{
Limit = 20;
From = DateTime.Now;
}
///
/// Initializes a new instance of the class.
///
/// Conversation.
public AVIMMessageQuery(AVIMConversation conversation)
: this()
{
Convsersation = conversation;
}
///
/// Sets the limit.
///
/// The limit.
/// Limit.
public AVIMMessageQuery SetLimit(int limit)
{
Limit = limit;
return this;
}
///
/// from lastest to previous.
///
///
public Task> FindAsync()
{
return FindAsync();
}
///
/// from lastest to previous.
///
///
public Task> ReverseFindAsync()
{
return ReverseFindAsync();
}
///
/// Finds the async.
///
/// The async.
/// set direction to reverse,it means query direct is from old to new.
/// The 1st type parameter.
public Task> FindAsync(bool reverse = false)
where T : IAVIMMessage
{
return Convsersation.QueryMessageAsync(
beforeTimeStampPoint: From,
afterTimeStampPoint: To,
limit: Limit,
afterMessageId: EndMessageId,
beforeMessageId: StartMessageId,
direction: reverse ? 0 : 1);
}
///
/// from previous to lastest.
///
///
public Task> ReverseFindAsync()
where T : IAVIMMessage
{
return FindAsync(true);
}
}
}