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

完成,BufferPage,但没测试

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