wxr
2024-09-30 dc9a1b15bb69227e19afc070adf58156a362d2a2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using Android.Content;
using Com.Widget.Jlcountrycode;
using Com.Widget.Jlcountrycode.Sortselect;
using System.Collections.Generic;
using Java.Util;
using Newtonsoft.Json.Linq;
using System.Threading.Tasks;
using System.Net.Http;
 
namespace JLCountrycode
{
    public class CountryCodeView
    {
        /// <summary>
        /// 接口类的返回信息
        /// </summary>
        private static CountryCodeView m_Current = null;
        /// <summary>
        /// 接口类的返回信息
        /// </summary>
        public static CountryCodeView Current
        {
            get
            {
                if (m_Current == null)
                {
                    m_Current = new CountryCodeView();
                }
                return m_Current;
            }
        }
 
 
        /// <summary>
        /// 根据手机当前的IP获取国家信息
        /// </summary>
        /// <returns></returns>
        public async Task<string> GetCountryByIP()
        {
            string country = "Unknown";
 
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    // 使用 ipinfo.io 获取位置信息
                    HttpResponseMessage response = await client.GetAsync("https://ipinfo.io/json");
 
                    if (response.IsSuccessStatusCode)
                    {
                        string json = await response.Content.ReadAsStringAsync();
                        JObject jsonObject = JObject.Parse(json);
 
                        // 从返回的 JSON 中提取国家信息
                        country = jsonObject["country"].ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error fetching IP info: {ex.Message}");
            }
 
            return country;
        }
 
 
        /// <summary>
        /// 国家区号选择
        /// </summary>
        /// <param name="action"></param>
        public void Show(Action<string, string> action)
        {
            JLCountryCodeActivity.OnCountryCodeCallback = new OnCountryCodeCallback(action);
            Shared.Application.Activity.StartActivity(new Intent(Shared.Application.Activity, typeof(JLCountryCodeActivity)));
        }
 
        /// <summary>
        /// 自定义传入字典数据 选择
        /// </summary>
        /// <param name="titleStr"></param>
        /// <param name="dic"></param>
        /// <param name="action"></param>
        public void ShowSortSelection(string titleStr, Dictionary<string, List<string>> dic, Action<string> action)
        {
            try
            {
                SortSelectionUtils.Instance.OnSortSelectCallback = new OnSSCallback(action);
                SortSelectionUtils.Instance.SortSelectTitle = titleStr;
                SortSelectionUtils.Instance.MSortSelectList = ConvertToHashMap(dic);
                Shared.Application.Activity.StartActivity(new Intent(Shared.Application.Activity, typeof(JLSortSelectionActivity)));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
 
        }
 
        /// <summary>
        /// Dictionary 转 Android HashMap
        /// </summary>
        /// <param name="dict"></param>
        /// <returns></returns>
        Dictionary<string, ArrayList> ConvertToHashMap(Dictionary<string, List<string>> dict)
        {
            Dictionary<string, ArrayList> newDictionary = new Dictionary<string, ArrayList>();
            try
            {
                foreach (string key in dict.Keys)
                {
                    var value = dict[key];
                    var arrayList = new ArrayList();
                    arrayList.AddAll(value.ToArray());
                    newDictionary.Add(key, arrayList);
                }
            }
            catch (Exception Ex)
            {
                //Logger.LogException(Ex);
            }
            return newDictionary;
        }
    }
 
    /// <summary>
    /// OnCountryCodeCallback
    /// </summary>
    public class OnCountryCodeCallback : Java.Lang.Object, Com.Widget.Jlcountrycode.Contact.IOnCountryCodeCallback
    {
 
        Action<string, string> mAction;
        public OnCountryCodeCallback(Action<string, string> action)
        {
            mAction = action;
        }
 
        public void OnSelectCountryCallback(string countryName, string code)
        {
            mAction?.Invoke(countryName, code);
        }
    }
 
 
 
    /// <summary>
    /// OnSortSelectCallback
    /// </summary>
    public class OnSSCallback : Java.Lang.Object, IOnSortSelectCallback
    {
 
        Action<string> mAction;
        public OnSSCallback(Action<string> action)
        {
            mAction = action;
        }
 
        public void OnSortSelectCallback(string countryName)
        {
            mAction?.Invoke(countryName);
        }
 
    }
 
 
 
}