using System;
using Shared;
using HDL_ON.UI.CSS;
using System.Collections.Generic;
using System.Threading;
using HDL_ON.DAL.Server;
namespace HDL_ON.UI
{
///
/// 信息中心页面
///
public class MessageCenterPage : FrameLayout
{
FrameLayout bodyView;
///
/// 当前
///
VerticalScrolViewLayout bodyScrolView;
///
/// 内容为空提示View
///
FrameLayout emptyTipView;
///
/// menuView
///
HorizontalScrolViewLayout menuView;//2021-08-27 增加多了一个菜单按钮所以改为滚动控件
MenuButton allMenuButton;
MenuButton shareMenuButton;
MenuButton alarmMenuButton;
MenuButton systemMenuButton;
MenuButton propertyMenuButton;//增加物业通知按钮
FrameLayout messageView;
///
/// 显示的消息类型
/// 0全部 1分享与功能 2报警类 3系统信息 4物业公告
///
int showMesType = 0;
///
/// 推送消息列表
///
List PushMessageInfoList = new List();
///
/// 页面关闭时间
///
Action backAction;
public override void RemoveFromParent()
{
UnregisterGetPushMessageAction();
base.RemoveFromParent();
backAction?.Invoke();
}
///
///
///
public MessageCenterPage()
{
bodyView = this;
BackgroundColor = CSS_Color.BackgroundColor;
}
///
///
///
///
public void LoadPage(Action backAction)
{
this.backAction = backAction;
var topView = new TopViewDiv(bodyView, Language.StringByID(StringId.MessageCenter));
topView.maginY = 10;
topView.LoadTopView();
AddTopMenuView();
AddMessageView();
MenuButtonSelect(showMesType);
RegisterGetPushMessageAction();
}
///
/// 顶部菜单选项
///
void AddTopMenuView()
{
menuView = new HorizontalScrolViewLayout()
{
Y = Application.GetRealHeight(64 + 10),
Height = Application.GetRealWidth(117),
BackgroundColor = CSS_Color.MainBackgroundColor,
HorizontalScrollBarEnabled = false//隐藏滚动条
};
bodyView.AddChidren(menuView);
//左padding View
menuView.AddChidren(new Button()
{
Width = Application.GetRealWidth(15),
BackgroundColor = CSS_Color.viewTranslucence
});
#region 全部按钮
allMenuButton = new MenuButton()
{
X = Application.GetRealWidth(15),
Y = Application.GetRealWidth(16),
};
menuView.AddChidren(allMenuButton);
allMenuButton.ImageButton.SelectedImagePath = "Collection/MesCenter/AllOn.png";
allMenuButton.ImageButton.UnSelectedImagePath = "Collection/MesCenter/All.png";
allMenuButton.TextButton.TextID = StringId.All;
Action allAction = () =>
{
MenuButtonSelect(0);
};
allMenuButton.SelectAction = allAction;
//测试提前当前推送ID
allMenuButton.ImageButton.MouseLongEventHandler = (sender, e) =>
{
//#if DEBUG
// CopyToClipboard("推送ID:" + OnAppConfig.Instance.PushId);
//#else
//检测是否获取推送ID成功,提示推送注册是否正常
if (string.IsNullOrEmpty(OnAppConfig.Instance.PushId))
{
Utlis.ShowTip(Language.StringByID(StringId.PushException));
}
else
{
Utlis.ShowTip(Language.StringByID(StringId.PushNormal));
}
//#endif
};
#endregion
#region 分享与功能
shareMenuButton = new MenuButton()
{
X = allMenuButton.Right,
Y = Application.GetRealWidth(16),
};
menuView.AddChidren(shareMenuButton);
shareMenuButton.ImageButton.SelectedImagePath = "Collection/MesCenter/ShareOn.png";
shareMenuButton.ImageButton.UnSelectedImagePath = "Collection/MesCenter/Share.png";
shareMenuButton.TextButton.TextID = StringId.SharingAndFunctionality;
shareMenuButton.TextButton.IsMoreLines = true;
Action shateAction = () =>
{
MenuButtonSelect(1);
};
shareMenuButton.SelectAction = shateAction;
#endregion
#region 报警类
alarmMenuButton = new MenuButton()
{
X = shareMenuButton.Right,
Y = Application.GetRealWidth(16),
};
menuView.AddChidren(alarmMenuButton);
alarmMenuButton.ImageButton.SelectedImagePath = "Collection/MesCenter/AlarmOn.png";
alarmMenuButton.ImageButton.UnSelectedImagePath = "Collection/MesCenter/Alarm.png";
alarmMenuButton.TextButton.TextID = StringId.AlarmType;
Action alarmAction = () =>
{
MenuButtonSelect(2);
};
alarmMenuButton.SelectAction = alarmAction;
#endregion
#region 系统信息
systemMenuButton = new MenuButton()
{
X = alarmMenuButton.Right,
Y = Application.GetRealWidth(16),
};
menuView.AddChidren(systemMenuButton);
systemMenuButton.ImageButton.SelectedImagePath = "Collection/MesCenter/SystemOn.png";
systemMenuButton.ImageButton.UnSelectedImagePath = "Collection/MesCenter/System.png";
systemMenuButton.TextButton.TextID = StringId.SystemMessage;
Action systemAction = () =>
{
MenuButtonSelect(3);
};
systemMenuButton.SelectAction = systemAction;
#endregion
#region 物业通知 2021-09-15增加
propertyMenuButton = new MenuButton()
{
X = systemMenuButton.Right,
Y = Application.GetRealWidth(16),
};
menuView.AddChidren(propertyMenuButton);
propertyMenuButton.ImageButton.SelectedImagePath = "Collection/MesCenter/PropertyOn.png";
propertyMenuButton.ImageButton.UnSelectedImagePath = "Collection/MesCenter/Property.png";
propertyMenuButton.TextButton.TextID = StringId.Propertynotice;
propertyMenuButton.TextButton.IsMoreLines = true;
Action propertyAction = () =>
{
MenuButtonSelect(4);
};
propertyMenuButton.SelectAction = propertyAction;
#endregion
//右paddingView
menuView.AddChidren(new Button()
{
Width = Application.GetRealWidth(15),
BackgroundColor = CSS_Color.viewTranslucence
});
}
///
/// 中间信息内容
///
void AddMessageView()
{
int messageViewY = menuView.Bottom + Application.GetRealWidth(8);
int messageViewHeight = bodyView.Height - messageViewY;
messageView = new FrameLayout()
{
Y = messageViewY,
Height = messageViewHeight,
BackgroundColor = CSS_Color.MainBackgroundColor,
};
bodyView.AddChidren(messageView);
bodyScrolView = new VerticalScrolViewLayout()
{
Height = messageViewHeight,
};
messageView.AddChidren(bodyScrolView);
}
///
///
///
/// 0全部 1分享与功能 2报警类 3系统信息 4物业通知
void MenuButtonSelect(int selectId)
{
showMesType = selectId;
if (selectId == 0)
{
allMenuButton.IsSelected = true;
shareMenuButton.IsSelected = true;
alarmMenuButton.IsSelected = true;
systemMenuButton.IsSelected = true;
propertyMenuButton.IsSelected = true;
}
else if (selectId == 1)
{
allMenuButton.IsSelected = false;
shareMenuButton.IsSelected = true;
alarmMenuButton.IsSelected = false;
systemMenuButton.IsSelected = false;
propertyMenuButton.IsSelected = false;
}
else if (selectId == 2)
{
allMenuButton.IsSelected = false;
shareMenuButton.IsSelected = false;
alarmMenuButton.IsSelected = true;
systemMenuButton.IsSelected = false;
propertyMenuButton.IsSelected = false;
}
else if (selectId == 3)
{
allMenuButton.IsSelected = false;
shareMenuButton.IsSelected = false;
alarmMenuButton.IsSelected = false;
systemMenuButton.IsSelected = true;
propertyMenuButton.IsSelected = false;
}
else if (selectId == 4)
{
allMenuButton.IsSelected = false;
shareMenuButton.IsSelected = false;
alarmMenuButton.IsSelected = false;
systemMenuButton.IsSelected = false;
propertyMenuButton.IsSelected = true;
}
menuView.ScrollToX(selectId * Application.GetRealWidth(40));
GetPushMessageList(showMesType);
}
///
/// 添加内容为空提示页面
///
void AddEmptyTipView()
{
emptyTipView = new FrameLayout()
{
Height = bodyScrolView.Height,
Width = bodyScrolView.Width,
};
bodyScrolView.AddChidren(emptyTipView);
var tipView = new EmptyTipView()
{
Gravity = Gravity.Center
};
emptyTipView.AddChidren(tipView);
}
///
/// 年份标记
///
string YearMark = "";
///
/// 加载报警信息RowView
///
///
///
void AddRowView(VerticalScrolViewLayout VerticalScrolViewMiddle, PushMessageInfo pushMessageInfo)
{
var rowView = new RowLayout()
{
Height = Application.GetRealWidth(76),
LineColor = CSS_Color.MainBackgroundColor,
};
VerticalScrolViewMiddle.AddChidren(rowView);
//标题
var btnTilte = new TextButton()
{
X = Application.GetRealWidth(16),
Width = Application.GetRealWidth(258),
Height = Application.GetRealWidth(20 + 23),
TextAlignment = TextAlignment.CenterLeft,
TextColor = CSS_Color.FirstLevelTitleColor,
TextSize = CSS_FontSize.TextFontSize,
Text = pushMessageInfo.messageContent,
IsMoreLines = true,
};
rowView.AddChidren(btnTilte);
//var needHeight = btnTilte.GetMoreLineNeedHeight();
//btnTilte.IsMoreLines = true;
//btnTilte.Height = needHeight;
//rowView.Height = Application.GetRealWidth(45) + needHeight;
//副标题
var btnSubtitle = new TextButton()
{
X = Application.GetRealWidth(16),
Y = btnTilte.Bottom + Application.GetRealWidth(4),
Width = Application.GetRealWidth(258),
Height = Application.GetRealWidth(17),
TextAlignment = TextAlignment.CenterLeft,
TextColor = CSS_Color.PromptingColor1,
TextSize = CSS_FontSize.PromptFontSize_FirstLevel,
Text = pushMessageInfo.messageTitle
};
rowView.AddChidren(btnSubtitle);
//日期
var btnDatetime = new Button()
{
X = Application.GetRealWidth(249),
Y = btnTilte.Bottom + Application.GetRealHeight(4),
Width = Application.GetRealWidth(110),
Height = Application.GetRealHeight(17),
TextAlignment = TextAlignment.CenterRight,
TextColor = CSS_Color.PromptingColor1,
TextSize = CSS_FontSize.PromptFontSize_FirstLevel,
};
rowView.AddChidren(btnDatetime);
var lineView = new LineView(rowView.Height - Application.GetRealHeight(1));
rowView.AddChidren(lineView);
//删除按钮
var deleteBtn = new Button()
{
TextID = StringId.Del,
BackgroundColor = CSS_Color.AuxiliaryColor2,
TextColor = CSS_Color.MainBackgroundColor,
TextSize = CSS_FontSize.TextFontSize,
};
rowView.AddRightView(deleteBtn);
deleteBtn.MouseUpEventHandler = (sender, e) =>
{
DeleteThePush(pushMessageInfo, rowView);
};
btnDatetime.Text = GetUnixToDateTime2(pushMessageInfo.createTime);
EventHandler eventHandler = (sender, e) =>
{
OnClickPushMessageInfo(pushMessageInfo);
//new Alert(pushMessageInfo.messageTitle, pushMessageInfo.messageContent, Language.StringByID(StringId.Close)).Show();
};
rowView.MouseUpEventHandler = eventHandler;
btnTilte.MouseUpEventHandler = eventHandler;
btnSubtitle.MouseUpEventHandler = eventHandler;
if (!string.IsNullOrEmpty(pushMessageInfo.messageExpandInfo.ExpantContentInfo.picUrl) &&
pushMessageInfo.messageExpandInfo.ExpantContentInfo.picUrl != "null")
{
ImageView imageView = new ImageView()
{
X = Application.GetRealWidth(291),
Y = Application.GetRealWidth(8),
Width = Application.GetRealWidth(52),
Height = Application.GetRealWidth(32),
Radius = (uint)Application.GetRealWidth(4),
BorderColor = 0x00FFFFFF,
BorderWidth = 1,
ImagePath = "Collection/MesCenter/default_image.png"
};
rowView.AddChidren(imageView);
imageView.MouseUpEventHandler = (sender, e) => {
Dialog dialog = new Dialog();
var dialogView = new FrameLayout();
dialog.AddChidren(dialogView);
dialogView.MouseUpEventHandler = (sender, e) => {
dialog.Close();
};
var dialogContentView = new ImageView()
{
Width = Application.GetRealWidth(375),
Height = Application.GetRealWidth(211),
ImageBytes = imageView.ImageBytes,
Gravity = Gravity.Center,
};
dialogView.AddChidren(dialogContentView);
dialog.Show();
};
new Thread(() => {
var headImageBytes = HttpUtil.HttpsDownload(pushMessageInfo.messageExpandInfo.ExpantContentInfo.picUrl);
if (headImageBytes != null && headImageBytes.Length > 0)
{
Application.RunOnMainThread(() =>
{
imageView.ImageBytes = headImageBytes;
});
}
})
{ IsBackground = true }.Start();
}
}
///
/// 时间格式转换处理
///
/// 毫秒时间戳
///
string GetUnixToDateTime(long time)
{
try
{
return Utlis.UnixToDateTimeWithFormatMS(time, "MM/dd HH:mm");
}
catch (Exception ex)
{
return "";
}
}
///
/// 时间格式转换处理
///
/// 毫秒时间戳
///
string GetUnixToDateTime2(long time)
{
try
{
return Utlis.UnixToDateTimeWithFormatMS(time, "yyyy.MM.dd HH:mm");
}
catch (Exception ex)
{
return "";
}
}
///
/// 删除推送消息
///
///
void DeleteThePush(PushMessageInfo pushMessageInfo, RowLayout rowView)
{
var waitPage = new Loading();
bodyView.AddChidren(waitPage);
waitPage.Start(Language.StringByID(StringId.PleaseWait));
new Thread(() =>
{
try
{
var delPush = new PushMsgIdObj()
{
msgId = pushMessageInfo.id,
pushType = pushMessageInfo.pushType
};
var result = new HttpServerRequest().PushSerivceDeleteMessage(delPush);
if (result)
{
PushMessageInfoList.Remove(pushMessageInfo);
//删除成功
Application.RunOnMainThread(() =>
{
rowView.RemoveFromParent();
});
}
}
catch (Exception ex)
{
}
finally
{
Application.RunOnMainThread(() =>
{
if (waitPage != null)
{
waitPage.RemoveFromParent();
waitPage = null;
}
});
}
})
{ IsBackground = true }.Start();
}
///
/// 加载年份标题
///
///
///
void AddYearRowView(VerticalScrolViewLayout VerticalScrolViewMiddle, string yearStr)
{
var bgView = new FrameLayout()
{
Height = Application.GetRealHeight(57),
};
VerticalScrolViewMiddle.AddChidren(bgView);
var yearBtn = new TextButton()
{
X = Application.GetRealWidth(16),
Y = Application.GetRealHeight(16),
Width = Application.GetRealWidth(320),
Height = Application.GetRealHeight(33),
TextAlignment = TextAlignment.CenterLeft,
TextColor = CSS_Color.FirstLevelTitleColor,
TextSize = CSS_FontSize.EmphasisFontSize_FirstLevel,
IsBold = true,
};
bgView.AddChidren(yearBtn);
yearBtn.Text = yearStr + Language.StringByID(StringId.Years);
}
///
/// 查询推送记录
///
///
void GetPushMessageList(int queryType = 0)
{
bodyScrolView.RemoveAll();
if (PushMessageInfoList == null)
{
PushMessageInfoList = new List();
}
else
{
PushMessageInfoList.Clear();
}
YearMark = "";//标记清空
if (string.IsNullOrEmpty(OnAppConfig.Instance.PushId))
{
AddEmptyTipView();
return;
}
var waitPage = new Loading();
bodyView.AddChidren(waitPage);
waitPage.Start(Language.StringByID(StringId.PleaseWait));
new Thread(() =>
{
try
{
var result = new HttpServerRequest().PushSerivceGetPushmessagelist(queryType);
if (result.Code == StateCode.SUCCESS)
{
//PushMessageInfoList = Newtonsoft.Json.JsonConvert.DeserializeObject>(result.Data.ToString());
//2021-08-28 改为分页接口
MainPage.Log(result.Data.ToString());
var mPushMesList = Newtonsoft.Json.JsonConvert.DeserializeObject(result.Data.ToString());
if (mPushMesList != null)
{
PushMessageInfoList = mPushMesList.list;
}
Application.RunOnMainThread(() =>
{
if (PushMessageInfoList != null && PushMessageInfoList.Count > 0)
{
ParseAndDisplayPushList();
PushSerivceMarkAllMessageRead();
}
else
{
AddEmptyTipView();
}
});
}
else
{
Application.RunOnMainThread(() =>
{
AddEmptyTipView();
});
IMessageCommon.Current.ShowErrorInfoAlter(result.Code);
}
}
catch (Exception ex)
{
MainPage.Log("Error", $"获取推送消息列表异常:{ex.StackTrace}");
}
finally
{
Application.RunOnMainThread(() =>
{
if (waitPage != null)
{
waitPage.RemoveFromParent();
waitPage = null;
}
});
}
})
{ IsBackground = true }.Start();
}
///
/// 解析和显示推送消息
///
void ParseAndDisplayPushList()
{
bodyScrolView.RemoveAll();
foreach (var info in PushMessageInfoList)
{
//年份解析 判断是否需要添加年份标题
var mesYear = Utlis.UnixToDateTimeYearMS(info.createTime);
if (string.IsNullOrEmpty(YearMark))
{
//刚开始YearMark标记为空,添加第一个年份标题
YearMark = mesYear;
AddYearRowView(bodyScrolView, YearMark);
}
else if (YearMark != mesYear)
{
//当前信息年份和之前的不一样,重新添加一个年份标题
YearMark = mesYear;
AddYearRowView(bodyScrolView, YearMark);
}
AddRowView(bodyScrolView, info);
}
}
///
/// 获取物业通知详情
///
///
void GetPropertyNoticeDetails(string noticeId)
{
var waitPage = new Loading();
bodyView.AddChidren(waitPage);
waitPage.Start(Language.StringByID(StringId.PleaseWait));
new Thread(() =>
{
try
{
var result = new HttpServerRequest().GetPropertyNoticeDetails(noticeId);
if (result.Code == StateCode.SUCCESS)
{
var mPushNoticeInfo = Newtonsoft.Json.JsonConvert.DeserializeObject(result.Data.ToString());
if (mPushNoticeInfo != null)
{
Application.RunOnMainThread(() =>
{
new WebViewDialog().LoadPageWithHtmlStr(mPushNoticeInfo.title, mPushNoticeInfo.content);
});
}
}
}
catch (Exception ex)
{
}
finally
{
Application.RunOnMainThread(() =>
{
if (waitPage != null)
{
waitPage.RemoveFromParent();
waitPage = null;
}
});
}
})
{ IsBackground = true }.Start();
}
///
/// 通知信息点击事件
///
///
void OnClickPushMessageInfo(PushMessageInfo pushMessageInfo)
{
if (pushMessageInfo.pushType == PushType.Notice.ToString())
{
try
{
//Utlis.WriteLine("物业通知");
if (!string.IsNullOrEmpty(pushMessageInfo.messageExpand))
{
GetPropertyNoticeDetails(pushMessageInfo.messageExpandInfo.targetId);
}
else
{
Utlis.WriteLine("noticeId null");
}
}
catch (Exception ex)
{
MainPage.Log("Error", $"通知信息点击事件:{ex.StackTrace}");
}
}
else
{
Utlis.WriteLine("其它通知类型");
}
}
///
/// 标记所有推送消息已读
///
void PushSerivceMarkAllMessageRead()
{
new Thread(() =>
{
var result = new HttpServerRequest().PushSerivceMarkAllMessageRead();
if (result)
{
Utlis.WriteLine("标记成功");
}
else
{
Utlis.WriteLine("标记失败");
}
})
{ IsBackground = true }.Start();
}
///
/// 注册收到推送监听
///
void RegisterGetPushMessageAction()
{
HDLCommon.GetPushMessageAction = () =>
{
if (bodyView != null)
{
//Utlis.WriteLine("GetPushMessageAction M收到推送");
GetPushMessageList(showMesType);
}
};
}
///
/// 取消收到推送监听
///
void UnregisterGetPushMessageAction()
{
HDLCommon.GetPushMessageAction = null;
}
}
///
/// 推送消息类型详情
///
[System.Serializable]
public class PushMessageInfo
{
///
/// 记录id
///
public string id;
///
/// 推送Token记录Id
///
public string pushId;
///
/// 云端定义 推送类型;0:默认;1:报警;2:提示;3:新闻;4:推荐;
///
/// Default:分享与功能;Alarm:报警类;Prompt:系统消息
///
public string pushType;
///
/// 消息主题
///
public string messageTitle;
///
/// 消息内容
///
public string messageContent;
///
/// 扩展数据
///
public string messageExpand;
MessageExpand _messageExpandInfo;
public MessageExpand messageExpandInfo
{
get
{
if (_messageExpandInfo == null)
{
if (!string.IsNullOrEmpty(messageExpand))
{
_messageExpandInfo = Newtonsoft.Json.JsonConvert.DeserializeObject(messageExpand);
}
else
{
_messageExpandInfo = new MessageExpand();
}
}
return _messageExpandInfo;
}
}
/////
///// 消息类型;0:提示;1:报警;2:丰林可是对讲呼叫;3:强制下线推送
/////
//public string messageType;
///
/// 是否已读
///
public bool isRead;
///
/// 推送时间
///
public long createTime;
}
[System.Serializable]
public class MessageExpand
{
///
/// 扩展数据内容
///
public string expantContent;
private ExpantContent _expantContent;
public ExpantContent ExpantContentInfo
{
get
{
if (_expantContent == null)
{
if (!string.IsNullOrEmpty(expantContent))
{
_expantContent = Newtonsoft.Json.JsonConvert.DeserializeObject(expantContent);
}
else
{
_expantContent = new ExpantContent();
}
}
return _expantContent;
}
}
public string homeId;
public int messageLevel;
public string messageType;
public string targetId;
}
[System.Serializable]
public class ExpantContent
{
public string currentTime;
public string picUrl;
public string devSerial;
public string interphoneTypeEnum;
public string subToken;
public string deviceSid;
public string msgId;
public string type;
public string pushTime;
public string extDevId;
public string deviceId;
public string spk;
}
///
// 推送消息分页对象
///
public class PushMessageInfoList : BasePagingObj
{
public List list = new List();
}
///
/// 物业公告详情
///
[System.Serializable]
public class PushNoticeInfo
{
///
/// 消息ID
///
public string noticeId;
///
/// 消息标题
///
public string title;
///
/// 富文本内容
///
public string content;
}
///
/// 推送扩展信息
///
[System.Serializable]
public class MessageExpandInfo
{
public string expantContent;
}
}