forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlFetcherMixin.cs
More file actions
192 lines (166 loc) · 7.36 KB
/
ControlFetcherMixin.cs
File metadata and controls
192 lines (166 loc) · 7.36 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using Android.App;
using Android.Views;
using Java.Interop;
namespace ReactiveUI
{
/// <summary>
/// ControlFetcherMixin helps you automatically wire-up Activities and
/// Fragments via property names, similar to Butter Knife, as well as allows
/// you to fetch controls manually.
/// </summary>
public static class ControlFetcherMixin
{
static readonly Dictionary<string, int> controlIds;
static readonly ConditionalWeakTable<object, Dictionary<string, View>> viewCache =
new ConditionalWeakTable<object, Dictionary<string, View>>();
static readonly MethodInfo getControlActivity;
static readonly MethodInfo getControlView;
static ControlFetcherMixin()
{
// NB: This is some hacky shit, but on Xamarin.Android at the moment,
// this is always the entry assembly.
var assm = AppDomain.CurrentDomain.GetAssemblies()[1];
var resources = assm.GetModules().SelectMany(x => x.GetTypes()).First(x => x.Name == "Resource");
controlIds = resources.GetNestedType("Id").GetFields()
.Where(x => x.FieldType == typeof(int))
.ToDictionary(k => k.Name.ToLowerInvariant(), v => (int)v.GetRawConstantValue());
var type = typeof(ControlFetcherMixin);
getControlActivity = type.GetMethod("GetControl", new[] { typeof(Activity), typeof(string) });
getControlView = type.GetMethod("GetControl", new[] { typeof(View), typeof(string) });
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public static T GetControl<T>(this Activity This, [CallerMemberName]string propertyName = null)
where T : View
{
return (T)getCachedControl(propertyName, This,
() => This.FindViewById(controlIds[propertyName.ToLowerInvariant()]).JavaCast<T>());
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public static T GetControl<T>(this View This, [CallerMemberName]string propertyName = null)
where T : View
{
return (T)getCachedControl(propertyName, This,
() => This.FindViewById(controlIds[propertyName.ToLowerInvariant()]).JavaCast<T>());
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public static T GetControl<T>(this Fragment This, [CallerMemberName]string propertyName = null)
where T : View
{
return GetControl<T>(This.View, propertyName);
}
/// <summary>
///
/// </summary>
public static void WireUpControls(this ILayoutViewHost This)
{
var members = This.GetType().GetRuntimeProperties()
.Where(m => m.PropertyType.IsSubclassOf(typeof(View)));
members.ToList().ForEach(m => {
try {
// Find the android control with the same name
var view = This.View.getControlInternal(m.PropertyType, m.Name);
// Set the activity field's value to the view with that identifier
m.SetValue(This, view);
} catch (Exception ex) {
throw new MissingFieldException("Failed to wire up the Property "
+ m.Name + " to a View in your layout with a corresponding identifier", ex);
}
});
}
/// <summary>
///
/// </summary>
public static void WireUpControls(this View This)
{
var members = This.GetType().GetRuntimeProperties()
.Where(m => m.PropertyType.IsSubclassOf(typeof(View)));
members.ToList().ForEach(m => {
try {
// Find the android control with the same name
var view = This.getControlInternal(m.PropertyType, m.Name);
// Set the activity field's value to the view with that identifier
m.SetValue(This, view);
} catch (Exception ex) {
throw new MissingFieldException("Failed to wire up the Property "
+ m.Name + " to a View in your layout with a corresponding identifier", ex);
}
});
}
/// <summary>
/// This should be called in the Fragement's OnCreateView, with the newly inflated layout
/// </summary>
/// <param name="This"></param>
/// <param name="inflatedView"></param>
public static void WireUpControls(this Fragment This, View inflatedView)
{
var members = This.GetType().GetRuntimeProperties()
.Where(m => m.PropertyType.IsSubclassOf(typeof(View)));
members.ToList().ForEach(m => {
try {
// Find the android control with the same name from the view
var view = inflatedView.getControlInternal(m.PropertyType, m.Name);
// Set the activity field's value to the view with that identifier
m.SetValue(This, view);
} catch (Exception ex) {
throw new MissingFieldException("Failed to wire up the Property "
+ m.Name + " to a View in your layout with a corresponding identifier", ex);
}
});
}
/// <summary>
///
/// </summary>
public static void WireUpControls(this Activity This)
{
var members = This.GetType().GetRuntimeProperties()
.Where(m => m.PropertyType.IsSubclassOf(typeof(View)));
members.ToList().ForEach(m => {
try {
// Find the android control with the same name
var view = This.getControlInternal(m.PropertyType, m.Name);
// Set the activity field's value to the view with that identifier
m.SetValue(This, view);
} catch (Exception ex) {
throw new MissingFieldException("Failed to wire up the Property "
+ m.Name + " to a View in your layout with a corresponding identifier", ex);
}
});
}
static View getControlInternal(this View parent, Type viewType, string name)
{
var mi = getControlView.MakeGenericMethod(new[] { viewType });
return (View)mi.Invoke(null, new object[] { parent, name });
}
static View getControlInternal(this Activity parent, Type viewType, string name)
{
var mi = getControlActivity.MakeGenericMethod(new[] { viewType });
return (View)mi.Invoke(null, new object[] { parent, name });
}
static View getCachedControl(string propertyName, object rootView, Func<View> fetchControlFromView)
{
var ret = default(View);
var ourViewCache = viewCache.GetOrCreateValue(rootView);
if (ourViewCache.TryGetValue(propertyName, out ret)) {
return ret;
}
ret = fetchControlFromView();
ourViewCache.Add(propertyName, ret);
return ret;
}
}
}