using System;
using System.Collections.Generic;
using System.Text;
namespace Shared.Phone.UserCenter
{
public class AreaCodeSelectForm : DialogCommonForm
{
#region ■ 变量声明___________________________
///
/// 完成选择的事件(地区码)
///
public Action FinishSelectEvent = null;
///
/// 列表控件
///
private VerticalListControl listView = null;
///
/// 前回选择的控件
///
private FrameRowControl oldFrameRowControl = null;
///
/// 锁
///
private object objLock = new object();
///
/// 这个东西是个静态(它是个特殊的)
///
private static List listAreaCode = null;
#endregion
#region ■ 初始化_____________________________
///
/// 画面显示(底层会固定调用此方法,借以完成画面创建)
///
public void ShowForm()
{
//初始化中部信息
this.InitMiddleFrame();
}
///
/// 初始化中部信息
///
private void InitMiddleFrame()
{
var frameBack = new FrameLayout();
frameBack.Width = Application.GetRealWidth(850);
frameBack.Height = Application.GetRealHeight(1342);
frameBack.Radius = (uint)Application.GetRealHeight(17);
frameBack.BackgroundColor = UserCenterColor.Current.White;
frameBack.Gravity = Gravity.CenterHorizontal;
frameBack.Y = Application.GetRealHeight(328);
bodyFrameLayout.AddChidren(frameBack);
this.listView = new VerticalListControl(3);
listView.Y = Application.GetRealHeight(164 - 11);
listView.Height = frameBack.Height - Application.GetRealHeight(164 - 11);
listView.Radius = (uint)Application.GetRealHeight(17);
frameBack.AddChidren(listView);
//请选择区号
var btnTitle = new NormalViewControl(270, 60, true);
btnTitle.X = ControlCommonResourse.XXLeft;
btnTitle.Y = Application.GetRealHeight(69);
btnTitle.TextID = R.MyInternationalizationString.uPleaseSelectAreaCode;
btnTitle.TextSize = 15;
btnTitle.TextColor = UserCenterColor.Current.TextColor2;
frameBack.AddChidren(btnTitle);
//搜索
var btnSearch = new MySearchControl(Language.StringByID(R.MyInternationalizationString.uSearch));
btnSearch.X = Application.GetRealWidth(334);
btnSearch.Y = Application.GetRealHeight(46);
frameBack.AddChidren(btnSearch);
btnSearch.BindEvent(this.SearchEvent);
//让这个按钮这个区域不能点击菜单
var frameTemp = new FrameLayout();
frameTemp.Y = Application.GetRealHeight(1146);
frameTemp.Height = frameBack.Height - Application.GetRealHeight(1146);
frameBack.AddChidren(frameTemp);
var btnOk = new BottomClickButton(688);
btnOk.Y = Application.GetRealHeight(1146);
btnOk.TextID = R.MyInternationalizationString.uConfirm1;
frameBack.AddChidren(btnOk);
btnOk.ButtonClickEvent += (sender, e) =>
{
if (oldFrameRowControl != null)
{
this.FinishSelectEvent?.Invoke(oldFrameRowControl.MainKeys);
}
this.CloseForm();
};
HdlThreadLogic.Current.RunThread(() =>
{
//初始化地区列表
this.InitAreaList();
});
}
#endregion
#region ■ 初始化地区列表_____________________
///
/// 初始化地区列表
///
private void InitAreaList()
{
if (listAreaCode == null)
{
//第一次获取
//开启进度条
ProgressBar.Show();
//获取全部的地区代码列表
var Pra = new { Common.CommonPage.RequestVersion };
string result = UserCenterLogic.GetResponseDataByRequestHttps("ZigbeeUsers/GetAreaCode", false, Pra);
//关闭进度条
ProgressBar.Close();
if (string.IsNullOrEmpty(result) == true)
{
return;
}
listAreaCode = Newtonsoft.Json.JsonConvert.DeserializeObject>(result);
for (int i = 0; i < listAreaCode.Count; i++)
{
//获取首字母
listAreaCode[i].First = this.GetCharSpellCode(listAreaCode[i].Name.Substring(0, 1));
}
//排序
listAreaCode.Sort((obj1, obj2) =>
{
return obj1.First.CompareTo(obj2.First);
});
}
Application.RunOnMainThread(() =>
{
//添加全部明细行
this.AddAllDetailRow(listAreaCode);
});
}
///
/// 添加全部明细行
///
///
private void AddAllDetailRow(List listData)
{
listView.RemoveAll();
string oldFirst = string.Empty;
int realHeight = 0;
for (int i = 0; i < listData.Count; i++)
{
if (oldFirst != listData[i].First)
{
oldFirst = listData[i].First;
//添加字母标题
var frame1 = new FrameRowControl(listView.rowSpace / 2);
frame1.UseClickStatu = false;
//列表控件只帮扩大3,而它的间距需要11
frame1.Height = Application.GetRealHeight(58 + 8);
frame1.Width = Application.GetRealWidth(850);
listView.AddChidren(frame1);
realHeight += frame1.Height;
var btnView1 = frame1.AddLeftCaption(listData[i].First, 300);
btnView1.TextColor = UserCenterColor.Current.TextGrayColor2;
if (listData[i].First == "ZZ")
{
//其他
btnView1.TextID = R.MyInternationalizationString.uOther;
}
}
//添加明细行
var frameDetail = new FrameRowControl(listView.rowSpace / 2);
frameDetail.MainKeys = listData[i].Code;
frameDetail.UseClickStatu = false;
frameDetail.Width = Application.GetRealWidth(850);
frameDetail.Height = Application.GetRealHeight(118);
listView.AddChidren(frameDetail);
realHeight += frameDetail.Height;
//地区名
var btnName = frameDetail.AddLeftCaption(listData[i].Name, 600);
btnName.TextColor = UserCenterColor.Current.TextGrayColor3;
//地区码
var btnCode = frameDetail.AddMostRightView("+" + listData[i].Code, 300);
btnCode.TextColor = UserCenterColor.Current.TextGrayColor3;
frameDetail.ButtonClickEvent += (sender, e) =>
{
if (oldFrameRowControl != null)
{
if (oldFrameRowControl.MainKeys == frameDetail.MainKeys)
{
//同一个东西
return;
}
//取消上一次的选择
oldFrameRowControl.BackgroundColor = UserCenterColor.Current.Transparent;
((NormalViewControl)oldFrameRowControl.GetChildren(0)).TextColor = UserCenterColor.Current.TextGrayColor3;
((NormalViewControl)oldFrameRowControl.GetChildren(1)).TextColor = UserCenterColor.Current.TextGrayColor3;
}
//选中状态
frameDetail.BackgroundColor = 0x0ffc744b;
btnName.TextColor = UserCenterColor.Current.TextColor1;
btnCode.TextColor = UserCenterColor.Current.TextColor1;
oldFrameRowControl = frameDetail;
};
}
if (realHeight > listView.Height - Application.GetRealHeight(196))
{
//促使被挡住的菜单能够向上滑动
var frameTemp = new FrameLayout();
frameTemp.Height = Application.GetRealHeight(196 + 34);//间距34
listView.AddChidren(frameTemp);
}
}
#endregion
#region ■ 搜索事件___________________________
///
/// 搜索事件
///
///
private void SearchEvent(string changdValue)
{
lock (objLock)
{
//如果只输入一位的话,可以判断首字母
string first = changdValue.Length == 1 ? changdValue.ToUpper() : string.Empty;
var listData = new List();
for (int i = 0; i < listAreaCode.Count; i++)
{
//如果只输入一位的话,可以判断首字母
if (listAreaCode[i].First == first)
{
listData.Add(listAreaCode[i]);
}
//判断名字和地区码
else if (listAreaCode[i].Code.Contains(changdValue) == true || listAreaCode[i].Name.Contains(changdValue) == true)
{
listData.Add(listAreaCode[i]);
}
}
Application.RunOnMainThread(() =>
{
//添加全部明细行
this.AddAllDetailRow(listData);
});
}
}
#endregion
#region ■ 搜索控件___________________________
///
/// 制作一个根据键值进行搜索的控件
/// 添加控件后,请调用LoadEvent()函数进行回调函数的绑定
///
public class MySearchControl : FrameLayout
{
#region ■ 变量声明__________________________
///
/// 搜索控件
///
private TextInputControl txtSearch = null;
///
/// 回调函数
///
private Action actionMethod = null;
///
/// 没有输入键值的显示文本
///
private string tipText = string.Empty;
#endregion
#region ■ 初始化____________________________
///
/// 制作一个根据键值进行搜索的控件
/// 添加控件到父控件后,请调用BindEvent()函数进行回调函数的绑定
///
/// 没有输入键值的显示文本
public MySearchControl(string i_tipText)
{
this.tipText = i_tipText;
//桌布控件
this.Width = Application.GetRealWidth(458);
this.Height = Application.GetRealHeight(104);
this.Radius = (uint)Application.GetRealHeight(17);
this.BorderColor = 0xffbfc1c2;
this.BorderWidth = 1;
}
///
/// 控件初始化
///
private void InitControl()
{
//输入文本
int textWidth = this.Width - Application.GetRealWidth(35 * 2);
txtSearch = new TextInputControl(textWidth, Application.GetRealHeight(58), false);
txtSearch.X = Application.GetRealWidth(35);
txtSearch.PlaceholderText = this.tipText;
txtSearch.Gravity = Gravity.CenterVertical;
this.AddChidren(txtSearch);
string oldText = string.Empty;
//值改变事件
txtSearch.TextChangeEventHandler += (sender, e) =>
{
this.nextSearchKeys = txtSearch.Text.Trim();
if (oldText == this.nextSearchKeys)
{
//输入的值一样
return;
}
oldText = this.nextSearchKeys;
//允许执行回调函数
this.isCanSearch = true;
};
}
#endregion
#region ■ 绑定事件__________________________
///
/// 绑定事件
///
/// 回调函数:值是输入的关键字
public void BindEvent(Action action)
{
this.actionMethod = action;
//控件初始化
this.InitControl();
//打开搜索键值的线程
this.StartSearchKeysThead();
}
#endregion
#region ■ 开启线程__________________________
///
/// 能否执行搜索
///
private bool isCanSearch = false;
///
/// 下一个搜索键值
///
private string nextSearchKeys = string.Empty;
///
/// 打开搜索键值的线程
///
private void StartSearchKeysThead()
{
HdlThreadLogic.Current.RunThread(() =>
{
while (this.Parent != null)
{
//根据逻辑,不能够搜索的话,等待
if (this.isCanSearch == false)
{
System.Threading.Thread.Sleep(1000);
continue;
}
//获取当前瞬间的键值
string nowSearchKeys = nextSearchKeys;
//根据搜索键值,调用回调函数
this.actionMethod(nowSearchKeys);
//处理完毕后,如果当前的检索键值与下一个检索键值一样的话
//说明用户已经不再输入键值了,这个时候,停止搜索
if (nowSearchKeys == nextSearchKeys)
{
this.isCanSearch = false;
}
//如果当前的检索键值与下一个检索键值不一样的话
//那就说明,用户再次输入了键值,这个时候,让线程继续跑下去
}
});
}
#endregion
#region ■ 控件摧毁__________________________
///
/// 控件摧毁
///
public override void RemoveFromParent()
{
actionMethod = null;
base.RemoveFromParent();
}
#endregion
}
#endregion
#region ■ 一般方法___________________________
///
/// 获取汉字首字母
///
///
///
private string GetCharSpellCode(string textValue)
{
long iCnChar;
byte[] ZW = Encoding.GetEncoding("gb2312").GetBytes(textValue);
//如果是字母,则直接返回
if (ZW.Length == 1)
{
return textValue.ToUpper();
}
else
{
// get the array of byte from the single char
int i1 = (short)(ZW[0]);
int i2 = (short)(ZW[1]);
iCnChar = i1 * 256 + i2;
}
//expresstion
//table of the constant list
// 'A'; //45217..45252
// 'B'; //45253..45760
// 'C'; //45761..46317
// 'D'; //46318..46825
// 'E'; //46826..47009
// 'F'; //47010..47296
// 'G'; //47297..47613
// 'H'; //47614..48118
// 'J'; //48119..49061
// 'K'; //49062..49323
// 'L'; //49324..49895
// 'M'; //49896..50370
// 'N'; //50371..50613
// 'O'; //50614..50621
// 'P'; //50622..50905
// 'Q'; //50906..51386
// 'R'; //51387..51445
// 'S'; //51446..52217
// 'T'; //52218..52697
//没有U,V
// 'W'; //52698..52979
// 'X'; //52980..53640
// 'Y'; //53689..54480
// 'Z'; //54481..55289
// iCnChar match the constant
if ((iCnChar >= 45217) && (iCnChar <= 45252))
{
return "A";
}
else if ((iCnChar >= 45253) && (iCnChar <= 45760))
{
return "B";
}
else if ((iCnChar >= 45761) && (iCnChar <= 46317))
{
return "C";
}
else if ((iCnChar >= 46318) && (iCnChar <= 46825))
{
return "D";
}
else if ((iCnChar >= 46826) && (iCnChar <= 47009))
{
return "E";
}
else if ((iCnChar >= 47010) && (iCnChar <= 47296))
{
return "F";
}
else if ((iCnChar >= 47297) && (iCnChar <= 47613))
{
return "G";
}
else if ((iCnChar >= 47614) && (iCnChar <= 48118))
{
return "H";
}
else if ((iCnChar >= 48119) && (iCnChar <= 49061))
{
return "J";
}
else if ((iCnChar >= 49062) && (iCnChar <= 49323))
{
return "K";
}
else if ((iCnChar >= 49324) && (iCnChar <= 49895))
{
return "L";
}
else if ((iCnChar >= 49896) && (iCnChar <= 50370))
{
return "M";
}
else if ((iCnChar >= 50371) && (iCnChar <= 50613))
{
return "N";
}
else if ((iCnChar >= 50614) && (iCnChar <= 50621))
{
return "O";
}
else if ((iCnChar >= 50622) && (iCnChar <= 50905))
{
return "P";
}
else if ((iCnChar >= 50906) && (iCnChar <= 51386))
{
return "Q";
}
else if ((iCnChar >= 51387) && (iCnChar <= 51445))
{
return "R";
}
else if ((iCnChar >= 51446) && (iCnChar <= 52217))
{
return "S";
}
else if ((iCnChar >= 52218) && (iCnChar <= 52697))
{
return "T";
}
else if ((iCnChar >= 52698) && (iCnChar <= 52979))
{
return "W";
}
else if ((iCnChar >= 52980) && (iCnChar <= 53640))
{
return "X";
}
else if ((iCnChar >= 53689) && (iCnChar <= 54480))
{
return "Y";
}
else if ((iCnChar >= 54481) && (iCnChar <= 55289))
{
return "Z";
}
else return ("ZZ");
}
#endregion
#region ■ 结构体_____________________________
///
/// 地区码
///
private class AreaCodeInfo
{
///
/// 首字母
///
public string First = string.Empty;
///
/// 名字
///
public string Name = string.Empty;
///
/// 地区码
///
public string Code = string.Empty;
///
/// Id
///
public string Id = string.Empty;
}
#endregion
}
}