Commit f09b1b5f authored by 潘栩锋's avatar 潘栩锋 🚴

完成,BufferPage,但没测试

parent 7f890140
......@@ -16,6 +16,8 @@ namespace FLY.OBJComponents.Client
/// </summary>
public class BufferPage<T> : INotifyPropertyChanged, IDisposable
{
const int MARKNO_UPDATE_GCURRPAGE = 1;
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
......@@ -33,16 +35,17 @@ namespace FLY.OBJComponents.Client
/// 但不直接使用Buffer.NewestID,因为 Buffer.NewestID可能与接收的数据滞后,或超前
/// </summary>
public int RecordLastID { get; private set; } = -1;
/// <summary>
/// 保持最新,改变WindowID 无效,它是输入
/// </summary>
public bool IsKeepNewest { get; private set; }
/// <summary>
/// 窗口最后一行的目标ID,它是输入
/// 当前全局页码,GCurrPage*Size 就是页码第1行的ID
/// </summary>
public int WindowID { get; private set; }
public int GCurrPage { get; set; }
private int size = 30;
/// <summary>
......@@ -64,12 +67,11 @@ namespace FLY.OBJComponents.Client
/// <summary>
/// buffer 的总页数
/// </summary>
public int TotalPages { get; private set; }
public int TotalPages => GPageLast - GPage1st + 1;
/// <summary>
/// 当前页码, 页码从1-TotalPages
/// </summary>
public int CurrentPage { get; private set; }
public int CurrentPage => GCurrPage - GPage1st + 1;
/// <summary>
/// 最后一页
......@@ -79,6 +81,9 @@ namespace FLY.OBJComponents.Client
/// 最前一页
/// </summary>
public bool IsFirstPage => (CurrentPage <= 1);
public int GPage1st { get; private set; }
public int GPageLast { get; private set; }
#endregion
......@@ -89,8 +94,8 @@ namespace FLY.OBJComponents.Client
Buffer = buffer;
IsKeepNewest = true;
WindowID = Buffer.NewestID;//显示最新数据!!!
updatePageInfo();
updateGCurrPage();
GetWindow();
Buffer.BufferChanged += Buffer_BufferChanged;
......@@ -103,54 +108,40 @@ namespace FLY.OBJComponents.Client
if (e.PropertyName == "Size")
{
updatePageInfo();
updateGCurrPage();
GetWindow();
}
else if (e.PropertyName == "WindowID")
{
updatePageInfo();
}
else if (e.PropertyName == "IsKeepNewest")
{
}
}
/// <summary>
/// GCurrPage 肯定在合理位置
/// Buffer.Count 肯定大于0
/// </summary>
void GetWindow()
{
if (IsKeepNewest)
{
int firstId = GCurrPage * Size;
int lastId = firstId + Size - 1;
Buffer.GetRecord(Size, (asyncContext, retData) =>
{
GetRecordReponse<T> getRecordReponse = retData as GetRecordReponse<T>;
if (getRecordReponse.Items != null)
{
WindowID = getRecordReponse.LastID;
push(getRecordReponse.LastID, getRecordReponse.Items);
}
int Buffer1stId = Buffer.NewestID - Buffer.Count + 1;
if (firstId < Buffer1stId)
firstId = Buffer1stId;
if (lastId > Buffer.NewestID)
lastId = Buffer.NewestID;
}, null);
}
else
int size = lastId - firstId + 1;
Buffer.GetRecord(lastId, size, (asyncContext, retData) =>
{
int firstId = (CurrentPage - 1) * Size;
int lastId = firstId + Size - 1;
if (lastId > Buffer.NewestID)
lastId = Buffer.NewestID;
if (lastId >= firstId)
{
int size = lastId - firstId + 1;
Buffer.GetRecord(lastId, size, (asyncContext, retData) =>
{
GetRecordReponse<T> getRecordReponse = retData as GetRecordReponse<T>;
if (getRecordReponse.Items != null)
push(getRecordReponse.LastID, getRecordReponse.Items);
GetRecordReponse<T> getRecordReponse = retData as GetRecordReponse<T>;
if (getRecordReponse.Items != null)
push(getRecordReponse.LastID, getRecordReponse.Items);
}, null);
}, null);
}
}
}
private void Buffer_PropertyChanged(object sender, PropertyChangedEventArgs e)
......@@ -159,6 +150,17 @@ namespace FLY.OBJComponents.Client
(e.PropertyName == "NewestID"))
{
updatePageInfo();
FObjBase.PollModule.Current.Poll_JustOnce(() =>
{
if (updateGCurrPage())
{
GetWindow();
}
}, this, MARKNO_UPDATE_GCURRPAGE);
}
}
......@@ -167,41 +169,28 @@ namespace FLY.OBJComponents.Client
switch (e.Action)
{
case NotifyBufferChangedAction.Add:
case NotifyBufferChangedAction.Replace:
{
//判断添加的数据是否在当前窗口内
int newEndingID = e.EndingID;
int nFirstID = e.EndingID;
int nLastID = newEndingID;
if (IsKeepNewest)//保持数据最新
{
WindowID = newEndingID;
}
int wFirstID = GCurrPage * Size;
int wLastID = wFirstID + Size - 1;
int wLastID = WindowID;
if (wFirstID<nLastID)
return;//什么都不用干
if(wLastID<nFirstID)
return;//什么都不用干
//以 nFirstID 为0 偏移
int offset = -nFirstID;
wLastID += offset;
nLastID += offset;
if (wLastID < -1)
{
//新数据不用添加到Record
}
else
{
push(newEndingID, (IList<T>)e.Items);
}
}
break;
case NotifyBufferChangedAction.Replace:
{
push(e.EndingID, (IList<T>)e.Items);
//有交集把数据放入
push(newEndingID, (IList<T>)e.Items);
}
break;
case NotifyBufferChangedAction.Remove:
{
remove(e.EndingID, 1);
remove(e.EndingID, e.Count);
}
break;
case NotifyBufferChangedAction.Reset:
......@@ -209,7 +198,6 @@ namespace FLY.OBJComponents.Client
//数据清空
Record.Clear();
RecordLastID = 0;
WindowID = 0;
}
break;
case NotifyBufferChangedAction.IsConnected:
......@@ -304,28 +292,7 @@ namespace FLY.OBJComponents.Client
//找Record 中的数据,是否与新推送过来的数据有交集
int firstID = items_lastID - cnt + 1;
int lastID = items_lastID;
int rFirstID = RecordLastID - Record.Count() + 1;
int rLastID = RecordLastID;
//转换坐标,以rFirstID=0,全部平移
int offset = -rFirstID;
firstID += offset;
lastID += offset;
rLastID += offset;// = Record.Count() - 1;
rFirstID = 0;
if (lastID <= -1)//被删除数据在当前数据前面,什么都不用做
{
return;
}
else //被删除数据与当前数据有交集
{
//重新问buffer获取当前数据块
Record.Clear();
RecordLastID = -1;
GetWindow();
}
FitRecord(firstID, lastID);
}
}
......@@ -334,58 +301,50 @@ namespace FLY.OBJComponents.Client
/// </summary>
void FitWindow()
{
//找Record 中的数据,是否与新推送过来的数据有交集
int wFirstID = WindowID - Size + 1;
int wLastID = WindowID;
int wFirstID = GCurrPage * Size;
int wLastID = wFirstID + Size - 1;
FitRecord(wFirstID, wLastID);
}
void FitRecord(int firstID, int lastID)
{
int rFirstID = RecordLastID - Record.Count() + 1;
int rLastID = RecordLastID;
//转换坐标,以rFirstID=0,全部平移
int offset = -rFirstID;
wFirstID += offset;
wLastID += offset;
rLastID += offset;
rFirstID += offset;
//删除交集以外的数据
if (wLastID < 0)//WINDOW在旧数据前面,不能合并
if (rFirstID < firstID)
{
Record.Clear();
RecordLastID = 0;
}
else if (wFirstID > Record.Count() - 1)//WINDOW在旧数据后面,不能合并
{
Record.Clear();
RecordLastID = 0;
int remove_cnt = firstID - rFirstID;
if (remove_cnt >= Record.Count())
{
//全部删除
Record.Clear();
RecordLastID = -1;
return;
}
else
{
for (int i = 0; i < remove_cnt; i++)
Record.RemoveAt(0);
}
}
else//有数据剩下
if (rLastID > lastID)
{
if ((wLastID >= rLastID) && (wFirstID <= rFirstID))//在显示范围内,不用删除
int remove_cnt = rLastID - lastID;
if (remove_cnt >= Record.Count())
{
//全部删除
Record.Clear();
RecordLastID = -1;
return;
}
else
{
//删除后面
int cnt = rLastID - wLastID;
for (int i = 0; i < cnt; i++)
{
for (int i = 0; i < remove_cnt; i++)
Record.RemoveAt(Record.Count() - 1);
}
if (cnt >= 0)
{
RecordLastID -= cnt;
}
cnt = wFirstID - rFirstID;
//删除前面
for (int i = 0; i < cnt; i++)
{
Record.RemoveAt(0);
}
RecordLastID = lastID;
}
}
}
......@@ -394,48 +353,44 @@ namespace FLY.OBJComponents.Client
if (Buffer.Count == 0)
{
//没有数据
CurrentPage = 0;
TotalPages = 0;
GPage1st = 0;
GPageLast = -1;
return;
}
//buffer 第1个数据ID
int firstID = Buffer.NewestID - (Buffer.Count - 1);
//buffer 最后一个数据ID
int lastID = Buffer.NewestID;
//当前页面最后一个数据ID
int wLastID = WindowID;
//buffer 第1个数据所在的全局页码
int gFirstPage = firstID / Size;
//buffer 最后一个数据所在的全局页码
int gLastPage = lastID / Size;
//当前全局页码
int gWdPage = wLastID / Size;
//总页数
int totalpages = gLastPage - gFirstPage + 1;
//当前页码
int page = gWdPage - gFirstPage + 1;
//限制当前页码
if (page < 1)
page = 1;
else if (page > totalpages)
page = totalpages;
CurrentPage = page;
TotalPages = totalpages;
GPage1st = (Buffer.NewestID - (Buffer.Count - 1)) / Size;
GPageLast = Buffer.NewestID / Size;
}
bool updateGCurrPage()
{
int gCurrPage = GCurrPage;
if (IsKeepNewest)
{
gCurrPage = GPageLast;
}
else
{
if (GCurrPage < GPage1st)
gCurrPage = GPage1st;
else if (GCurrPage > GPageLast)
gCurrPage = GPageLast;
}
if (gCurrPage != GCurrPage)
{
GCurrPage = gCurrPage;
return true;
}
else
{
return false;
}
}
#region 窗口移动
/// <summary>
/// 下一页, 让窗口向后移,TailerID+=Capacity
/// </summary>
public void MoveNextPage()
{
IsKeepNewest = false;
MovePage(CurrentPage + 1);
}
......@@ -444,7 +399,6 @@ namespace FLY.OBJComponents.Client
/// </summary>
public void MovePrePage()
{
IsKeepNewest = false;
MovePage(CurrentPage - 1);
}
/// <summary>
......@@ -453,7 +407,8 @@ namespace FLY.OBJComponents.Client
public void MoveNewest()
{
IsKeepNewest = true;
GetWindow();
if(updateGCurrPage())
GetWindow();
}
/// <summary>
/// 移动到某页
......@@ -465,22 +420,13 @@ namespace FLY.OBJComponents.Client
{
if ((page <= TotalPages) && (page > 0))
{
WindowID += Size * (page - CurrentPage);
GCurrPage = page + GPage1st - 1;
IsKeepNewest = (GCurrPage == GPageLast);
GetWindow();
}
}
}
/// <summary>
/// 通过 最后数据的ID,移动
/// </summary>
/// <param name="lastID"></param>
public void Move(int lastID)
{
IsKeepNewest = false;
WindowID = lastID;
GetWindow();
}
#endregion
/// <summary>
/// 释放窗口,数据不再更新
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment